2005-10-09 [colin] 1.9.15cvs26
[claws.git] / tools / newscache_clean.pl
1 #!/usr/bin/perl
2
3 #  * Copyright 2001 Paul Mangan <claws@thewildbeast.co.uk>
4 #  *
5 #  * This file is free software; you can redistribute it and/or modify it
6 #  * under the terms of the GNU General Public License as published by
7 #  * the Free Software Foundation; either version 2 of the License, or
8 #  * (at your option) any later version.
9 #  *
10 #  * This program is distributed in the hope that it will be useful, but
11 #  * WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  * General Public License for more details.
14 #  *
15 #  * You should have received a copy of the GNU General Public License
16 #  * along with this program; if not, write to the Free Software
17 #  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #  *
19
20 use File::Path;
21
22 chdir;
23 chdir '.sylpheed' || die("You don't appear to have Sylpheed installed\n");
24
25 open(ACCOUNTRC, "<accountrc") || die("Can't find accountrc\n");
26         @accountrc = <ACCOUNTRC>;
27 close ACCOUNTRC;
28
29 foreach $accountrc (@accountrc) {
30         if ($accountrc =~ m/nntp_server=[A-Za-z0-9]/) {
31                 $accountrc =~ s/nntp_server=//;
32                 chomp $accountrc;
33                 push(@newsserver, "$accountrc");        
34         }
35 }
36
37 %seen = ();
38 foreach $newsserver (@newsserver) {
39         $seen{$newsserver}++;
40 }
41
42 opendir(NEWSCACHE, "newscache") || die("Can't open newscache\n");
43         push(@cached,(readdir(NEWSCACHE)));
44 closedir(NEWSCACHE);
45 splice(@cached, 0, 2);
46 foreach $cached (@cached) { ## remove old newsserver directory tree
47         rmtree("./newscache/$cached") unless $seen{$cached};
48 }
49
50 open(FOLDERLIST, "<folderlist.xml") || die("Can't find folderlist.xml\n");
51         @folderlist = <FOLDERLIST>;
52 close FOLDERLIST;
53 %saw = ();
54 $wegotnews = 0;
55 foreach $folderlist (@folderlist) { ## remove old newsgroups directory trees
56         unless ($wegotnews) {
57                 if ($folderlist =~ m/<folder type="news"/) {
58                         $wegotnews = 1;
59                         $done_grp = 0;
60                         $nntpserver = shift(@newsserver);
61                 }
62         } 
63         if ($wegotnews && $folderlist =~ m/<\/folder>\n/ && !$done_grp) {
64                 opendir(NEWSGCACHE, "newscache/$nntpserver") || die("Can't open newscache/$nntpserver\n");
65                 push(@cachedgrp,(readdir(NEWSGCACHE)));
66                 closedir(NEWSGCACHE);
67                 splice(@cachedgrp, 0, 2);
68                 foreach $cachedgrp (@cachedgrp) { 
69                         rmtree("./newscache/$nntpserver/$cachedgrp") unless ($saw{$cachedgrp}) || ($cachedgrp eq ".newsgroup_list");
70                 }
71                 $done_grp = 1;
72                 $wegotnews = 0;
73                 @cachedgrp = ();
74         }
75         if ($wegotnews && $folderlist !~ m/<\/folder>\n/) {
76                 if ($folderlist =~ m/<folderitem type="normal"/) {
77                         $folderlist =~ s/<folderitem type="normal" name="[A-Z0-9.]+" path="//i;
78                         $folderlist =~ s/" threaded="[0-1]+//;
79                         $folderlist =~ s/" hidereadmsgs="[0-1]+//;
80                         $folderlist =~ s/" mtime="[0-9]+" new="[0-9]+" unread="[0-9]+" total="[0-9]+" \/>//;
81                         $folderlist =~ s/ +//;
82                         chomp $folderlist;
83                         $saw{$folderlist}++;
84                 }        
85         }
86 }
87 print "Finished cleaning Sylpheed's newscache\n";
88 exit;