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
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
14 # Version 0.3 by James Stone (jmstone@dsl.pipex.com)
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.
22 # please submit bug reports and code optimisations as you see fit!
28 #user editable section
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
37 #end of user editable section
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
46 #connect to each mailserver get all the new message UIDLs and delete any
49 for a in range(len(mailserver)):
50 connect=poplib.POP3(mailserver[a])
51 connect.user(login[a])
52 connect.pass_(password[a])
54 number,size=connect.stat()
56 for mesnum in range(number):
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):
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]))
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))
78 #get rid of lines in uidldate file corresponding to the messages that have been
79 #expired (and hopefully been deleted)
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)
87 datefile=open(localuidldate,'w+')
92 datefile=open(localuidldate,'a+')
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.
97 for mesnum in range(totnum):
98 popuidl=popuidllist[mesnum]
99 popuidlsplit=popuidl.split(' ')
101 for localuidl in readfile:
102 if(localuidl.find(popuidlsplit[2])<>-1):
105 for stored in datefile:
106 if (stored.find(popuidlsplit[2])<>-1):
108 if not(foundindatefile):
109 datefile.write(popuidlsplit[2]+' '+str(time.time())+' '
110 +popuidlsplit[3]+'\n')