]> Pileus Git - ~andy/fetchmail/blob - contrib/mold_remover.py
Rediff patch.
[~andy/fetchmail] / contrib / mold_remover.py
1 # Mold Remover
2 #
3 # A short python script to remove old read mail from a pop3 mailserver.
4 # Distributed under the GNU GPL (http://www.gnu.org/copyleft/gpl.html)
5 # Dovetails with fetchmail with keep option.
6 # Run it as a cron job... Remember to stop fetchmail in the cron job
7 # before calling mold_remover, and to restart fetchmail after mold_remover
8 # e.g.: 
9 #
10 #       /etc/init.d/fetchmail stop >> /var/log/messages
11 #       /usr/bin/python /usr/local/bin/mold_remover.py >> /var/log/messages
12 #       /etc/init.d/fetchmail start >> /var/log/messages
13 #
14 # Version 0.3 by James Stone (jmstone@dsl.pipex.com)
15 # 15th March 2004
16
17 # Changes:
18 # 0.1 25th September 2003 Original version
19 # 0.2 6th March 2004 Info added regarding use, and explicit mention of GPL made.
20 # 0.3 15th March 2004 days changed to list, 1 day changed from 24 to 23 hours.
21
22 # please submit bug reports and code optimisations as you see fit!
23
24 import string
25 import poplib
26 import time
27
28 #user editable section
29
30 mailserver=["mail.server1","mail.server2"] #list of mailservers
31 login=["login1","login2"] #list of logins for corresponding mailserver
32 password=["pass1","pass2"] #list of passwords (note: chmod 700 for this script)
33 days=[2,3] #number of days to keep on server.
34 localuidlcache="/var/mail/.fetchmail-UIDL-cache" #fetchmail's UIDL cache
35 localuidldate="/var/mail/.UIDLDate" #mold remover's UIDL datefile
36
37 #end of user editable section
38
39 readfile=open(localuidlcache, 'r')
40 datefile=open(localuidldate, 'a+')
41 tempfile=open("/tmp/uidltmp", 'w+')
42 popuidllist=[] #list of undeleted uidls on all servers
43 totnum=0 #number of undeleted messages on all servers
44 connectedto=-1
45
46 #connect to each mailserver get all the new message UIDLs and delete any
47 #expired messages.
48
49 for a in range(len(mailserver)):
50     connect=poplib.POP3(mailserver[a])
51     connect.user(login[a])
52     connect.pass_(password[a])
53     connectedto=a
54     number,size=connect.stat()
55     totnum+=number
56     for mesnum in range(number):
57         messagedeleted=0
58         datefile.seek(0)
59         for uidldate in datefile:
60             uidldatesplit=uidldate.split(' ')
61             if(connectedto==int(uidldatesplit[2])):
62                 if (time.time()-float(uidldatesplit[1]))>(86400*days[a]-3600):
63                     try:
64                         recheckuidl=connect.uidl(mesnum+1)
65                         recheckuidlsplit=recheckuidl.split(' ')
66                         if (recheckuidlsplit[2]==uidldatesplit[0]):
67                             print('deleting'+recheckuidlsplit[1])
68                             print(connect.dele(recheckuidlsplit[1]))
69                             messagedeleted=1
70                             totnum-=1
71                     except poplib.error_proto:
72                         pass #skip over messages that have already been deleted.
73         if not(messagedeleted):
74             popuidllist.append(connect.uidl(mesnum+1)+' '+str(a))
75     connect.quit()        
76
77
78 #get rid of lines in uidldate file corresponding to the messages that have been
79 #expired (and hopefully been deleted)
80
81 datefile.seek(0)
82 for uidldate in datefile:
83     uidldatesplit=uidldate.split(' ')
84     if not(time.time()-float(uidldatesplit[1]))>(86400*days[int(uidldatesplit[2])]):
85         tempfile.write(uidldate)
86 datefile.close()
87 datefile=open(localuidldate,'w+')
88 tempfile.seek(0)
89 for line in tempfile:
90         datefile.write(line)
91 datefile.close()
92 datefile=open(localuidldate,'a+')
93
94 #add date to uidl for any messages still on the server which have been read 
95 #(check in readfile) and store in local datefile.
96
97 for mesnum in range(totnum):    
98             popuidl=popuidllist[mesnum]
99             popuidlsplit=popuidl.split(' ')
100             readfile.seek(0)
101             for localuidl in readfile:
102                 if(localuidl.find(popuidlsplit[2])<>-1):
103                     foundindatefile=0
104                     datefile.seek(0)
105                     for stored in datefile:
106                         if (stored.find(popuidlsplit[2])<>-1):
107                             foundindatefile=1
108                     if not(foundindatefile):
109                         datefile.write(popuidlsplit[2]+' '+str(time.time())+' '
110                             +popuidlsplit[3]+'\n')