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