xref: /AOO41X/main/scripting/examples/java/Newsgroup/SubscribedNewsgroups.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 import java.io.*;
2 import java.util.Vector;
3 
4 
5 public class SubscribedNewsgroups {
6 
7 
8 	private static NewsGroup[] allSubscribed = null;
9 	private static boolean windows = false;
10 
11 	public static void main( String[] args ) {
12 		// Test the class
13 		SubscribedNewsgroups subscribed = new SubscribedNewsgroups();
14 
15 		NewsGroup allGroups[] = subscribed.getNewsGroups();
16 
17 		if( allGroups == null )
18 		{
19 			System.out.println("Could not find subscribed newsgroups from mozilla/netscape mailrc files");
20 		}
21 		else
22 		{
23 			for( int i=0; i < allGroups.length; i++ )
24 			{
25 				System.out.println( "Hostname is: " + allGroups[i].getHostName() + " Newsgroup is: " + allGroups[i].getNewsgroupName() );
26 			}
27 		}
28 	}
29 
30 
31 
32 	// Only public method of the class
33 	// Returns and array of unique NewsGroup objects
34 	public NewsGroup[] getNewsGroups()
35 	{
36 		windows = false;
37 		if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
38 		{
39 			windows = true;
40 		}
41 
42 		String mozillaHome = "";
43 		if( windows )
44 		{
45 			mozillaHome = System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + "Application Data" + System.getProperty( "file.separator" ) + "Mozilla" + System.getProperty( "file.separator" ) + "Profiles";
46 			//System.out.println( "Windows mozilla path: " + mozillaHome );
47 		}
48 		else
49 		{
50 			mozillaHome = System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + ".mozilla";
51 			//System.out.println( "Unix/Linux mozilla path: " + mozillaHome );
52 		}
53 		if( !new File( mozillaHome ).isDirectory() )
54 		{
55 			//System.out.println("Could not find .mozilla directory");
56 			return null;
57 		}
58 		//System.out.println(".mozilla directory found");
59 
60 		// Get all the profiles belonging to the user
61 		File profiles[] = findProfiles( new File ( mozillaHome ) );
62 		if( profiles.length < 1 )
63 		{
64 			//System.out.println("Could not find Profiles");
65 			return null;
66 		}
67 		//System.out.println("Profiles found");
68 
69 		// Get the News directory for each profile
70 		File allNewsDirs[] = new File[ profiles.length ];
71 		for( int i=0; i < profiles.length; i++ ) {
72 			File newsDir = findNewsDir( profiles[i] );
73 			allNewsDirs[i] = newsDir;
74 			//System.out.println( "News is at: " + newsDir.getPath() );
75 		}
76 		// Check that at least one News directory exists and remove nulls
77 		boolean newsFound = false;
78 		//Vector nonNullNews = new Vector();
79 		for( int i=0; i < allNewsDirs.length; i++ ) {
80 			if( allNewsDirs[i] != null ) {
81 				newsFound = true;
82 				break;
83 			}
84 		}
85 		if( !newsFound )
86 		{
87 			//System.out.println("Could not find News directory");
88 			return null;
89 		}
90 		//System.out.println("News directory found");
91 
92 		// Get all the mailrc files for each News directory
93 		File allMailrcs[] = findMailrcFiles( allNewsDirs );
94 		if( allMailrcs == null )
95 		{
96 			//System.out.println("Could not find mailrc files");
97 			return null;
98 		}
99 		//System.out.println("mailrc files found");
100 
101 		Vector subscribed = new Vector();
102 		// Get the newsgroups in each mailrc file
103 		for( int i=0; i < allMailrcs.length; i++ )
104 		{
105 			File mailrc = (File) allMailrcs[i];
106 			NewsGroup newsgroup[] = findNewsgroups( mailrc );
107 			//if the Newsgroup has not already been added to the list
108 			for( int j=0; j < newsgroup.length; j++ )
109 			{
110 				// if newsgroup is unique then add to the list
111 				if( !listed( newsgroup[j], subscribed ) )
112 				{
113 					subscribed.addElement( newsgroup[j] );
114 				}
115 			}
116 		}
117 
118 		// Copy all unique Newsgroups into the global array
119 		allSubscribed = new NewsGroup[ subscribed.size() ];
120 		subscribed.copyInto( allSubscribed );
121 		// Test that at least one subscribed newsgroup has been found
122 		if( allSubscribed.length < 1 )
123 		{
124 			//System.out.println("Could not find Subscribed newsgroups ");
125 			return null;
126 		}
127 		//System.out.println("Subscribed newsgroups found");
128 
129 		return allSubscribed;
130 	}
131 
132 
133 
134 
135 	// Tests if the NewsGroup object has already been listed by another mailrc file
136 	private static boolean listed( NewsGroup newsgroup, Vector uniqueSubscription )
137 	{
138 		for(int i=0; i < uniqueSubscription.size(); i++)
139 		{
140 			NewsGroup tempGroup = (NewsGroup) uniqueSubscription.elementAt(i);
141 			// Test for duplication
142 			if(newsgroup.getHostName().equalsIgnoreCase( tempGroup.getHostName()) &&
143 			   newsgroup.getNewsgroupName().equalsIgnoreCase( tempGroup.getNewsgroupName() ) )
144 				return true;
145 		}
146 		return false;
147 	}
148 
149 
150 
151 
152 	// Finds all the NewsGroups in an individual mailrc file
153 	private static NewsGroup[] findNewsgroups(File mailrcfile )
154 	{
155 
156 		String hostname = "";
157 		String newsgroup = "";
158 		NewsGroup mailrcNewsGroups[] = null;
159 
160 		//Retrieve name of news host/server from file name
161 		//Sequentially access each of the newsgroups
162 		//If the newsgroup is not already contained in the global NewsGroup[] array then add it
163 
164 		String filename = mailrcfile.getPath();
165 		if( windows )
166 		{
167 			// Windows format "staroffice-news.germany.sun.com.rc"
168 			int hostNameStart = filename.lastIndexOf("\\") + 1;
169 			int hostNameEnd = filename.indexOf(".rc");
170 			hostname = filename.substring( hostNameStart, hostNameEnd );
171 		}
172 		else
173 		{
174 			// Unix/Linux format "newsrc-staroffice-news.germany.sun.com"
175 			int hostNameStart = filename.lastIndexOf("newsrc-") + 7;
176 			hostname = filename.substring( hostNameStart, filename.length() );
177 		}
178 
179 		// Assumes the content format in Window is the same as Unix/Linux (unknown at the moment)
180 		// i.e. a list of newsgroups each ending with a ":"
181 		LineNumberReader in = null;
182 		try {
183 			in = new LineNumberReader( new FileReader( mailrcfile ) );
184 			Vector groups = new Vector();
185 			String inString = "";
186 			int line = 0;
187 			while( inString != null )
188 			{
189 				in.setLineNumber( line );
190 				inString = in.readLine();
191 				line++;
192 				if( inString != null )
193 				{
194 					int newsgroupEnd = inString.indexOf(":");
195 					newsgroup = inString.substring( 0, newsgroupEnd );
196 					NewsGroup group = new NewsGroup( hostname, newsgroup );
197 					groups.addElement( group );
198 				}
199 			}
200 			mailrcNewsGroups = new NewsGroup[ groups.size() ];
201 			groups.copyInto(mailrcNewsGroups);
202 			in.close();
203 		}
204 		catch( IOException ioe ) {
205 			ioe.printStackTrace();
206 		}
207 
208 		return mailrcNewsGroups;
209 	}
210 
211 
212 	// Finds all the mailrc files for all the given News directories
213 	private static File[] findMailrcFiles(File[] newsDirs)
214 	{
215 		Vector allFiles = new Vector();
216 
217 		for( int i=0; i < newsDirs.length; i++ )
218 		{
219 			//System.out.println( "Finding mailrc for: " + newsDirs[i] );
220 			if( newsDirs[i] != null )
221 			{
222 				File mailrcFiles[] = newsDirs[i].listFiles( new VersionFilter() );
223 				if( mailrcFiles != null )
224 				{
225 					//System.out.println( "Number found: " + mailrcFiles.length );
226 					for( int j=0; j < mailrcFiles.length; j++ )
227 					{
228 						//System.out.println( "This mailrc was found: " + mailrcFiles[j] );
229 						allFiles.addElement( mailrcFiles[j] );
230 					}
231 				}
232 			}
233 		}
234 		File allMailrcFiles[] = new File[ allFiles.size() ];
235 		allFiles.copyInto(allMailrcFiles);
236 
237 		//System.out.println( "number of mailrcs in total: " + allMailrcFiles.length );
238 
239 		if( allMailrcFiles.length == 0 ) {
240 			//System.out.println( "Returning null");
241 			return null;
242 		}
243 
244 		//System.out.println( "Returning an File array containing mailrcs");
245 		return allMailrcFiles;
246 	}
247 
248 
249 	// Finds all profiles belonging to one user (can be more than one)
250 	private static File[] findProfiles(File start)
251 	{
252 		// Get all files and directories in .mozilla
253 		File allFiles[] = start.listFiles();
254 		File[] dirs = new File[allFiles.length];
255 		int dirCounter = 0;
256 
257 		// Remove files leaving directories only
258 		for(int i=0; i < allFiles.length; i++ )
259 		{
260 			if(allFiles[i].isDirectory())
261 			{
262 				dirs[dirCounter] = allFiles[i];
263 				dirCounter++;
264 			}
265 		}
266 
267 		// Add each directory to a user profile array
268 		File[] profileDirs = new File[dirCounter];
269 		for( int i=0; i < dirCounter; i++ )
270 		{
271 			profileDirs[i] = dirs[i];
272 		}
273 
274 		// return a File array containing the profile dirs
275 		return profileDirs;
276 	}
277 
278 
279 	// Recursively searches for the News directory for a given profile directory
280         private static File findNewsDir(File start)
281         {
282                 File mailrcFile = null;
283 
284 		// File array containing all matches for the version filter ("News")
285                 File files[] = start.listFiles(new VersionFilter());
286 		// If the array is empty then no matches were found
287                 if (files.length == 0)
288                 {
289 			// File array of all the directories in File start
290                         File dirs[] = start.listFiles(new DirFilter());
291 			// for each of the directories check for a match
292                         for (int i=0; i< dirs.length; i++)
293                         {
294                                 mailrcFile = findNewsDir(dirs[i]);
295                                 if (mailrcFile != null)
296                                 {
297 					// break the for loop
298                                         break;
299                                 }
300                         }
301                 }
302                 else
303 		{
304 			// end recursion
305 			// Check for a News directory inside the News directory (fix for bug)
306 			// Original solution had only "mailrcFile = files[0];"
307 
308 			boolean noChildNews = true;
309 			File checkChildNewsDirs[] = files[0].listFiles(new VersionFilter());
310 			if( checkChildNewsDirs != null )
311 			{
312 				for( int i=0; i < checkChildNewsDirs.length; i++ )
313 				{
314 					if( checkChildNewsDirs[i].getName().equals( "News" ) )
315 					{
316 						noChildNews = false;
317 						break;
318 					}
319 				}
320 			}
321 
322 			if( noChildNews )
323 			{
324        				mailrcFile = files[0];
325 			}
326 			else
327 			{
328 				String childNewsPathName = files[0].getAbsolutePath() + System.getProperty( "file.separator" ) + "News";
329 				mailrcFile = new File( childNewsPathName );
330 			}
331 
332 		}
333 
334 		// return a File representing the News dir in a profile
335                 return mailrcFile;
336 	}
337 }
338 
339 
340 
341 class DirFilter implements FileFilter
342 {
343         public boolean accept(File aFile)
344         {
345                 return aFile.isDirectory();
346         }
347 }
348 
349 
350 class VersionFilter implements FileFilter
351 {
352         public boolean accept(File aFile)
353         {
354 		if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
355 		{
356 			if (aFile.getName().compareToIgnoreCase("News") == 0 ||
357 			aFile.getName().indexOf(".rc") != -1 )
358 			{
359 				return true;
360 			}
361 		}
362 		else
363 		{
364 			if (aFile.getName().compareToIgnoreCase("News") == 0 ||
365 			aFile.getName().indexOf("newsrc") != -1 )
366 			{
367 				return true;
368 			}
369 		}
370 
371                 return false;
372         }
373 }
374