wok-6.x view imapbox/stuff/slitaz.patch @ rev 22176
linld: spare up to 30k more for zImage
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Fri Nov 08 20:55:40 2019 +0100 (2019-11-08) |
parents | |
children |
line source
1 --- imapbox/mailboxresource.py
2 +++ imapbox/mailboxresource.py
3 @@ -9,6 +9,8 @@
4 import hashlib
5 from message import Message
6 import datetime
7 +import time
8 +import rfc822
12 @@ -16,10 +18,22 @@
13 """Operations on a mailbox"""
15 def __init__(self, host, port, username, password, remote_folder):
16 + if port == '143':
17 + self.mailbox = imaplib.IMAP4(host, port)
18 + elif port == '993':
19 self.mailbox = imaplib.IMAP4_SSL(host, port)
20 self.mailbox.login(username, password)
21 self.mailbox.select(remote_folder, readonly=True)
23 + if remote_folder=='INBOX':
24 + self.remote_folder=''
25 + else
26 + r_f=re.sub('INBOX','',remote_folder)
27 + if re.findall('^\.',r_f):
28 + self.remote_folder=re.sub('\.','/',rf)
29 + else:
30 + self.remote_folder='/'+r_f
31 +
32 def copy_emails(self, days, local_folder, wkhtmltopdf):
34 n_saved = 0
35 @@ -50,9 +64,19 @@
38 def getEmailFolder(self, msg, data):
39 - if msg['Message-Id']:
40 - foldername = re.sub('[^a-zA-Z0-9_\-\.()\s]+', '', msg['Message-Id'])
41 + if msg['To'] and re.findall('Sent',self.remote_folder):
42 + foldername = re.findall("<.*>", msg['To'])
43 + if foldername:
44 + foldername = re.sub('[\<\>]+', '', foldername[0])
45 else:
46 + foldername=msg['To']
47 + elif msg['From']:
48 + foldername = re.findall("<.*>", msg['From'])
49 + if foldername:
50 + foldername = re.sub('[\<\>]+', '', foldername[0])
51 + else:
52 + foldername=msg['From']
53 + else:
54 foldername = hashlib.sha224(data).hexdigest()
56 year = 'None'
57 @@ -62,20 +86,33 @@
58 year = match.group(1)
61 - return os.path.join(self.local_folder, year, foldername)
62 + return os.path.join(self.local_folder+self.remote_folder, self.normalizeDate(msg['Date'])+'_'+foldername)
64 + def normalizeDate(self, datestr):
65 + t = email.utils.parsedate_tz(datestr)
66 + timeval = time.mktime(t[:-1])
67 + date = email.utils.formatdate(timeval, True)
68 + utc = time.gmtime(email.utils.mktime_tz(t))
69 +# rfc2822 = '{} {:+03d}00'.format(date[:-6], t[9]//3600)
70 + iso8601 = time.strftime('%Y%m%dT%H%M%SZ', utc)
72 + return (iso8601)
74 +
75 def saveEmail(self, data):
76 for response_part in data:
77 if isinstance(response_part, tuple):
78 - msg = email.message_from_string(response_part[1].decode("utf-8"))
79 + try:
80 + msg = email.message_from_string(re.sub('^>', '', response_part[1]).decode("utf-8")) # supprime '>' dans l'email
81 + except UnicodeError:
82 + msg = email.message_from_string(re.sub('^>', '', response_part[1]).decode('latin1').encode('utf-8')) # supprime '>' dans l'email
83 directory = self.getEmailFolder(msg, data[0][1])
85 if os.path.exists(directory):
86 return False
88 os.makedirs(directory)
89 + os.utime(directory,(time.mktime(rfc822.parsedate(msg['Date'])), time.mktime(rfc822.parsedate(msg['Date']))))
91 try:
92 message = Message(directory, msg)
93 @@ -86,9 +123,12 @@
94 if self.wkhtmltopdf:
95 message.createPdfFile(self.wkhtmltopdf)
97 + os.utime(directory,(time.mktime(rfc822.parsedate(msg['Date'])), time.mktime(rfc822.parsedate(msg['Date']))))
98 +
99 except Exception as e:
100 # ex: Unsupported charset on decode
101 print(directory)
102 + os.utime(directory,(time.mktime(rfc822.parsedate(msg['Date'])), time.mktime(rfc822.parsedate(msg['Date']))))
103 if hasattr(e, 'strerror'):
104 print("MailboxClient.saveEmail() failed:", e.strerror)
105 else:
106 --- imapbox/message.py
107 +++ imapbox/message.py
108 @@ -15,6 +15,8 @@
109 import cgi
110 import time
111 import pkgutil
112 +import rfc822
113 +import sys
115 from six.moves import html_parser
117 @@ -82,14 +84,15 @@
118 except email.Errors.HeaderParseError:
119 # This already append in email.base64mime.decode()
120 # instead return a sanitized ascii string
121 - return header_text.encode('ascii', 'replace').decode('ascii')
122 + return header_text.encode('ascii', 'replace').decode(default)
123 else:
124 for i, (text, charset) in enumerate(headers):
125 headers[i]=text
126 if charset:
127 - headers[i]=str(text, charset)
128 + text = unicode(text,charset)
129 + headers[i]=text
130 else:
131 - headers[i]=str(text)
132 + headers[i]=text.decode('utf-8')
133 return u"".join(headers)
136 @@ -102,21 +105,27 @@
137 # use the same for both and see later
138 name=addr
140 + headers=decode_header(name)
141 try:
142 - # address must be ascii only
143 - addr=addr.encode('ascii')
144 + addr = addr.encode("ascii")
145 except UnicodeError:
146 - addr=''
147 + addr = ''
148 else:
149 # address must match adress regex
150 if not email_address_re.match(addr.decode("utf-8")):
151 addr=''
152 - addrs[i]=(self.getmailheader(name), addr.decode("utf-8"))
153 + addrs[i]=(self.getmailheader(name), "utf-8")
154 return addrs
156 def getSubject(self):
157 - if not hasattr(self, 'subject'):
158 - self.subject = self.getmailheader(self.msg.get('Subject', ''))
159 +# if not hasattr(self, 'subject'):
160 +# self.subject = self.getmailheader(self.msg.get('Subject'),'')
161 + headers=decode_header(self.msg.get('Subject'))
162 + for i, (text, charset) in enumerate(headers):
163 + if charset:
164 + self.subject = unicode(text,charset)
165 + else:
166 + self.subject=text.decode('utf-8')
167 return self.subject
169 def getFrom(self):
170 @@ -172,6 +181,7 @@
171 json_file.write(data)
173 json_file.close()
174 + os.utime('%s/metadata.json' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date']))))
178 @@ -180,6 +190,7 @@
179 f = gzip.open('%s/raw.eml.gz' %(self.directory), 'wb')
180 f.write(data)
181 f.close()
182 + os.utime('%s/raw.eml.gz' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date']))))
185 def getPartCharset(self, part):
186 @@ -192,14 +203,23 @@
187 if not hasattr(self, 'text_content'):
188 self.text_content = ''
189 for part in parts:
190 + raw_content_0 = part.get_payload()
191 raw_content = part.get_payload(decode=True)
192 charset = self.getPartCharset(part)
193 self.text_content += raw_content.decode(charset, "replace")
194 + if charset!='utf-8' and self.text_content:
195 + try:
196 + self.text_content=self.text_content.encode('raw_unicode_escape').decode('utf-8') # Double-decoding unicode
197 + except UnicodeError:
198 + self.text_content=raw_content_0.decode('utf_8')
199 return self.text_content
202 def createTextFile(self, parts):
203 - utf8_content = self.getTextContent(parts)
204 + intro=''
205 + if self.getSubject():
206 + intro='==> ' + self.getSubject() + '\r\n\r\n'
207 + utf8_content = intro + self.getTextContent(parts)
208 with open(os.path.join(self.directory, 'message.txt'), 'wb') as fp:
209 fp.write(bytearray(utf8_content, 'utf-8'))
211 @@ -208,9 +228,15 @@
212 self.html_content = ''
214 for part in parts:
215 + raw_content_0 = part.get_payload()
216 raw_content = part.get_payload(decode=True)
217 charset = self.getPartCharset(part)
218 self.html_content += raw_content.decode(charset, "replace")
219 + if charset!='utf-8' and self.html_content:
220 + try:
221 + self.html_content.encode('raw_unicode_escape').decode('utf-8') # Double-decoding unicode
222 + except UnicodeError:
223 + self.html_content=raw_content_0.decode('utf_8')
225 m = re.search('<body[^>]*>(.+)<\/body>', self.html_content, re.S | re.I)
226 if (m != None):
227 @@ -304,9 +330,11 @@
229 if message_parts['text']:
230 self.createTextFile(message_parts['text'])
231 + os.utime('%s/message.txt' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date']))))
233 if message_parts['html']:
234 self.createHtmlFile(message_parts['html'], message_parts['embed_images'])
235 + os.utime('%s/message.html' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date']))))
237 if message_parts['files']:
238 attdir = os.path.join(self.directory, 'attachments')
239 @@ -317,6 +345,8 @@
240 payload = afile[0].get_payload(decode=True)
241 if payload:
242 fp.write(payload)
243 + os.utime('%s/attachments/%s' %(self.directory,afile[1]),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date']))))
244 + os.utime('%s/attachments' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date']))))
247 def createPdfFile(self, wkhtmltopdf):
248 @@ -325,5 +355,6 @@
249 pdf_path = os.path.join(self.directory, 'message.pdf')
250 config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf)
251 pdfkit.from_file(html_path, pdf_path, configuration=config)
252 + os.utime('%s/message.pdf' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date']))))
253 else:
254 print("Couldn't create PDF message, since \"pdfkit\" module isn't installed.")