aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/mold_remover.py
blob: 1ccba7c3bb8eb92ec2e1a4bc43f86b74d6ad361e (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
# Mold Remover
# A short python script to remove old read mail from a pop3 mailserver.
# Dovetails with fetchmail with keep option.
# Run it as a cron job...
# Version 0.1 by James Stone (stone1@btinternet.com)
# please submit bug reports and code optimisations as you see fit!

import string
import poplib
import time

#user editable section
mailserver=["mail.server1","mail.server2"] #list of mailservers
login=["login1","login2"] #list of logins for corresponding mailserver
password=["pass1","pass2"] #list of passwords (note: chmod 700 for this script)
days=2 #number of days to keep on server.
localuidlcache="/var/mail/.fetchmail-UIDL-cache" #fetchmail's UIDL cache
localuidldate="/var/mail/.UIDLDate" #mold remover's UIDL datefile
#end of user editable section

readfile=open(localuidlcache, 'r')
datefile=open(localuidldate, 'a+')
tempfile=open("/tmp/uidltmp", 'w+')
popuidllist=[] #list of undeleted uidls on all servers
totnum=0 #number of undeleted messages on all servers
connectedto=-1

#connect to each mailserver get all the new message UIDLs and delete any
#expired messages.

for a in range(len(mailserver)):
    connect=poplib.POP3(mailserver[a])
    connect.user(login[a])
    connect.pass_(password[a])
    connectedto=a
    number,size=connect.stat()
    totnum+=number
    for mesnum in range(number):
        messagedeleted=0
        datefile.seek(0)
        for uidldate in datefile:
            uidldatesplit=uidldate.split(' ')
            if(connectedto==int(uidldatesplit[2])):
                if (time.time()-float(uidldatesplit[1]))>(86400*days):
                    try:
                        recheckuidl=connect.uidl(mesnum+1)
                        recheckuidlsplit=recheckuidl.split(' ')
                        if (recheckuidlsplit[2]==uidldatesplit[0]):
                            print('deleting'+recheckuidlsplit[1])
                            print(connect.dele(recheckuidlsplit[1]))
                            messagedeleted=1
                            totnum-=1
                    except poplib.error_proto:
                        pass #skip over messages that have already been deleted.
        if not(messagedeleted):
            popuidllist.append(connect.uidl(mesnum+1)+' '+str(a))
    connect.quit()        


#get rid of lines in uidldate file corresponding to the messages that have been
#expired (and hopefully been deleted)

datefile.seek(0)
for uidldate in datefile:
    uidldatesplit=uidldate.split(' ')
    if not(time.time()-float(uidldatesplit[1]))>(86400*days):
        tempfile.write(uidldate)
datefile.close()
datefile=open(localuidldate,'w+')
tempfile.seek(0)
for line in tempfile:
        datefile.write(line)
datefile.close()
datefile=open(localuidldate,'a+')

#add date to uidl for any messages still on the server which have been read 
#(check in readfile) and store in local datefile.

for mesnum in range(totnum):    
            popuidl=popuidllist[mesnum]
            popuidlsplit=popuidl.split(' ')
            readfile.seek(0)
            for localuidl in readfile:
                if(localuidl.find(popuidlsplit[2])<>-1):
                    foundindatefile=0
                    datefile.seek(0)
                    for stored in datefile:
                        if (stored.find(popuidlsplit[2])<>-1):
                            foundindatefile=1
                    if not(foundindatefile):
                        datefile.write(popuidlsplit[2]+' '+str(time.time())+' '
                            +popuidlsplit[3]+'\n')