-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimapbackup.py
executable file
·136 lines (102 loc) · 2.88 KB
/
imapbackup.py
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
#!/usr/bin/env python3
#Author:Bhasker C V([email protected])
#GNU GPL v2
#Very simple programme to mirror a IMAP server contents here with
# minimal inputs given (just the required ones).
import sys, getopt,os;
import imaplib,mailbox;
import getpass,re;
servername=""
username=""
localdir=""
replacefile=False
exceptions=[] #Folder names you do not want to be synced. TODO: add this to input options ?
mbox=[]
def printhelp():
print("-s <server name/ip> -u <user name> -l <local directory> -r <flag to set replace of a MDIR(default append)")
print("Output:")
print(" ! => Success \n . => Erroe ")
exit()
def printerror(strv):
print("ERROR:",strv);
exit()
if len(sys.argv) < 2:
printhelp()
#print("Args:",sys.argv)
try:
opts,args = getopt.getopt(sys.argv[1:],"rs:u:l:")
except:
printhelp()
#print(len(opts))
for opt, arg in opts:
if opt == "-s":
servername=arg
if opt == "-u":
username=arg
if opt == "-l":
localdir=arg
if not re.search('[/]$',localdir):
localdir=localdir+'/'
if opt == "-r":
replacefile=True
#print("To copy ",username,"@",servername," to ",localdir)
ignfil = open(os.getenv("HOME")+"/.imap-ignore","r")
ignlist = ignfil.read().split('\n')
#Start ...
M = imaplib.IMAP4_SSL(servername)
try:
rv, data = M.login(username,getpass.getpass())
except imaplib.IMAP4.error:
printerror("Error logging into IMAP")
if rv != 'OK':
printerror("Login failed. Wrong password ? ")
rv,mlist= M.list()
for f in mlist:
mbox.append((' '.join((re.search("[)] .*",f.decode('utf-8'))).group(0).split()[2:])).replace('"',''))
for mailfolder in mbox:
mailfoldername=mailfolder
if any(mailfoldername in s for s in ignlist):
continue
print("Processing ",mailfoldername)
rv,tot=M.select('"'+mailfolder+'"')
try:
if int(tot[0]) > 0:
print("Folder:"+mailfoldername+" has "+str(int(tot[0]))+" messages")
else:
continue
except:
print("Error processing mailbox:"+mailfoldername)
continue
# os.chdir(localdir)
fname=mailfoldername
if any(fname in s for s in exceptions):
continue
if not os.path.isdir(os.path.dirname(fname)) and re.search('[/]',fname):
# print("processing:"+fname)
mdirname=os.path.dirname(fname)+'/'
mdirname='.dir/'.join(mdirname.split('/'))
# print("need to create "+mdirname)
fulllocaldirname=localdir+'/'+mdirname
if not os.path.exists(fulllocaldirname):
os.makedirs(fulllocaldirname)
fname=mdirname+"/"+os.path.basename(fname)
fname=localdir+fname
# print("Final Filename to create:"+fname)
else:
fname=localdir+fname
#fp=open(fname,"w+")
if replacefile and os.path.exists(fname):
os.remove(fname)
fp=mailbox.mbox(fname,create=True)
rv,data=M.search(None,"ALL")
msglist=data[0].decode('utf-8').split()
mboxformat="";
for f in msglist:
rv,mesg = M.fetch(f.encode('utf-8'),'(RFC822)')
try:
fp.add(mesg[0][1])
sys.stdout.write("!")
except:
sys.stdout.write(".")
sys.stdout.flush()
print("")