summaryrefslogtreecommitdiffstats
path: root/scripting/examples/java/Newsgroup/SubscribedNewsgroups.java
blob: eb135a3e8f3d91c6a35f6ac662ccf3a92a2f9a27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package org.libreoffice.example.java_scripts;

import java.io.*;
import java.util.Vector;


public class SubscribedNewsgroups {


    private static NewsGroup[] allSubscribed = null;
    private static boolean windows = false;

    public static void main(String[] args) {
        // Test the class
        SubscribedNewsgroups subscribed = new SubscribedNewsgroups();

        NewsGroup allGroups[] = subscribed.getNewsGroups();

        if (allGroups == null) {
            System.out.println("Could not find subscribed newsgroups from mozilla/netscape mailrc files");
        } else {
            for (int i = 0; i < allGroups.length; i++) {
                System.out.println("Hostname is: " + allGroups[i].getHostName() +
                                   " Newsgroup is: " + allGroups[i].getNewsgroupName());
            }
        }
    }



    // Only public method of the class
    // Returns and array of unique NewsGroup objects
    public NewsGroup[] getNewsGroups() {
        windows = false;

        if (System.getProperty("os.name").indexOf("Windows") != -1) {
            windows = true;
        }

        String mozillaHome = "";

        if (windows) {
            mozillaHome = System.getProperty("user.home") +
                          System.getProperty("file.separator") + "Application Data" +
                          System.getProperty("file.separator") + "Mozilla" +
                          System.getProperty("file.separator") + "Profiles";
        } else {
            mozillaHome = System.getProperty("user.home") +
                          System.getProperty("file.separator") + ".mozilla";
        }

        if (!new File(mozillaHome).isDirectory()) {
            return null;
        }

        // Get all the profiles belonging to the user
        File profiles[] = findProfiles(new File(mozillaHome));

        if (profiles.length < 1) {
            return null;
        }

        // Get the News directory for each profile
        File allNewsDirs[] = new File[ profiles.length ];

        for (int i = 0; i < profiles.length; i++) {
            File newsDir = findNewsDir(profiles[i]);
            allNewsDirs[i] = newsDir;
        }

        // Check that at least one News directory exists and remove nulls
        boolean newsFound = false;

        for (int i = 0; i < allNewsDirs.length; i++) {
            if (allNewsDirs[i] != null) {
                newsFound = true;
                break;
            }
        }

        if (!newsFound) {
            return null;
        }

        // Get all the mailrc files for each News directory
        File allMailrcs[] = findMailrcFiles(allNewsDirs);

        if (allMailrcs == null) {
            return null;
        }

        ArrayList<NewsGroup> subscribed = new ArrayList<NewsGroup>();

        // Get the newsgroups in each mailrc file
        for (int i = 0; i < allMailrcs.length; i++) {
            File mailrc = (File) allMailrcs[i];
            NewsGroup newsgroup[] = findNewsgroups(mailrc);

            //if the Newsgroup has not already been added to the list
            for (int j = 0; j < newsgroup.length; j++) {
                // if newsgroup is unique then add to the list
                if (!listed(newsgroup[j], subscribed)) {
                    subscribed.addElement(newsgroup[j]);
                }
            }
        }

        // Copy all unique Newsgroups into the global array
        allSubscribed = new NewsGroup[ subscribed.size() ];
        subscribed.toArray(allSubscribed);

        // Test that at least one subscribed newsgroup has been found
        if (allSubscribed.length < 1) {
            return null;
        }

        return allSubscribed;
    }




    // Tests if the NewsGroup object has already been listed by another mailrc file
    private static boolean listed(NewsGroup newsgroup,
                                  ArrayList<NewsGroup> uniqueSubscription) {
        for (int i = 0; i < uniqueSubscription.size(); i++) {
            NewsGroup tempGroup = uniqueSubscription.elementAt(i);

            // Test for duplication
            if (newsgroup.getHostName().equalsIgnoreCase(tempGroup.getHostName()) &&
                newsgroup.getNewsgroupName().equalsIgnoreCase(tempGroup.getNewsgroupName()))
                return true;
        }

        return false;
    }




    // Finds all the NewsGroups in an individual mailrc file
    private static NewsGroup[] findNewsgroups(File mailrcfile) {

        String hostname = "";
        String newsgroup = "";
        NewsGroup mailrcNewsGroups[] = null;

        //Retrieve name of news host/server from file name
        //Sequentially access each of the newsgroups
        //If the newsgroup is not already contained in the global NewsGroup[] array then add it

        String filename = mailrcfile.getPath();

        if (windows) {
            // Windows format "staroffice-news.germany.sun.com.rc"
            int hostNameStart = filename.lastIndexOf("\\") + 1;
            int hostNameEnd = filename.indexOf(".rc");
            hostname = filename.substring(hostNameStart, hostNameEnd);
        } else {
            // Unix/Linux format "newsrc-staroffice-news.germany.sun.com"
            int hostNameStart = filename.lastIndexOf("newsrc-") + 7;
            hostname = filename.substring(hostNameStart, filename.length());
        }

        // Assumes the content format in Window is the same as Unix/Linux (unknown at the moment)
        // i.e. a list of newsgroups each ending with a ":"
        LineNumberReader in = null;

        try {
            in = new LineNumberReader(new FileReader(mailrcfile));
            ArrayList groups = new ArrayList();
            String inString = "";
            int line = 0;

            while (inString != null) {
                in.setLineNumber(line);
                inString = in.readLine();
                line++;

                if (inString != null) {
                    int newsgroupEnd = inString.indexOf(":");
                    newsgroup = inString.substring(0, newsgroupEnd);
                    NewsGroup group = new NewsGroup(hostname, newsgroup);
                    groups.addElement(group);
                }
            }

            mailrcNewsGroups = new NewsGroup[ groups.size() ];
            groups.copyInto(mailrcNewsGroups);
            in.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return mailrcNewsGroups;
    }


    // Finds all the mailrc files for all the given News directories
    private static File[] findMailrcFiles(File[] newsDirs) {
        ArrayList allFiles = new ArrayList();

        for (int i = 0; i < newsDirs.length; i++) {
            if (newsDirs[i] != null) {
                File mailrcFiles[] = newsDirs[i].listFiles(new VersionFilter());

                if (mailrcFiles != null) {
                    for (int j = 0; j < mailrcFiles.length; j++) {
                        allFiles.addElement(mailrcFiles[j]);
                    }
                }
            }
        }

        File allMailrcFiles[] = new File[ allFiles.size() ];
        allFiles.copyInto(allMailrcFiles);

        if (allMailrcFiles.length == 0) {
            return null;
        }

        return allMailrcFiles;
    }


    // Finds all profiles belonging to one user (can be more than one)
    private static File[] findProfiles(File start) {
        // Get all files and directories in .mozilla
        File allFiles[] = start.listFiles();
        File[] dirs = new File[allFiles.length];
        int dirCounter = 0;

        // Remove files leaving directories only
        for (int i = 0; i < allFiles.length; i++) {
            if (allFiles[i].isDirectory()) {
                dirs[dirCounter] = allFiles[i];
                dirCounter++;
            }
        }

        // Add each directory to a user profile array
        File[] profileDirs = new File[dirCounter];

        for (int i = 0; i < dirCounter; i++) {
            profileDirs[i] = dirs[i];
        }

        // return a File array containing the profile dirs
        return profileDirs;
    }


    // Recursively searches for the News directory for a given profile directory
    private static File findNewsDir(File start) {
        File mailrcFile = null;

        // File array containing all matches for the version filter ("News")
        File files[] = start.listFiles(new VersionFilter());

        // If the array is empty then no matches were found
        if (files.length == 0) {
            // File array of all the directories in File start
            File dirs[] = start.listFiles(new DirFilter());

            // for each of the directories check for a match
            for (int i = 0; i < dirs.length; i++) {
                mailrcFile = findNewsDir(dirs[i]);

                if (mailrcFile != null) {
                    // break the for loop
                    break;
                }
            }
        } else {
            // end recursion
            // Check for a News directory inside the News directory (fix for bug)
            // Original solution had only "mailrcFile = files[0];"

            boolean noChildNews = true;
            File checkChildNewsDirs[] = files[0].listFiles(new VersionFilter());

            if (checkChildNewsDirs != null) {
                for (int i = 0; i < checkChildNewsDirs.length; i++) {
                    if (checkChildNewsDirs[i].getName().equals("News")) {
                        noChildNews = false;
                        break;
                    }
                }
            }

            if (noChildNews) {
                mailrcFile = files[0];
            } else {
                String childNewsPathName = files[0].getAbsolutePath() +
                                           System.getProperty("file.separator") + "News";
                mailrcFile = new File(childNewsPathName);
            }

        }

        // return a File representing the News dir in a profile
        return mailrcFile;
    }
}



class DirFilter implements FileFilter {
    public boolean accept(File aFile) {
        return aFile.isDirectory();
    }
}


class VersionFilter implements FileFilter {
    public boolean accept(File aFile) {
        if (System.getProperty("os.name").indexOf("Windows") != -1) {
            if (aFile.getName().compareToIgnoreCase("News") == 0 ||
                aFile.getName().indexOf(".rc") != -1) {
                return true;
            }
        } else {
            if (aFile.getName().compareToIgnoreCase("News") == 0 ||
                aFile.getName().indexOf("newsrc") != -1) {
                return true;
            }
        }

        return false;
    }
}