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