wok-6.x diff imapbox/stuff/slitaz.patch @ rev 24855
updated libzip (1.6.1 -> 1.8.0)
author | Hans-G?nter Theisgen |
---|---|
date | Mon Mar 28 14:52:34 2022 +0100 (2022-03-28) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/imapbox/stuff/slitaz.patch Mon Mar 28 14:52:34 2022 +0100 1.3 @@ -0,0 +1,254 @@ 1.4 +--- imapbox/mailboxresource.py 1.5 ++++ imapbox/mailboxresource.py 1.6 +@@ -9,6 +9,8 @@ 1.7 + import hashlib 1.8 + from message import Message 1.9 + import datetime 1.10 ++import time 1.11 ++import rfc822 1.12 + 1.13 + 1.14 + 1.15 +@@ -16,10 +18,22 @@ 1.16 + """Operations on a mailbox""" 1.17 + 1.18 + def __init__(self, host, port, username, password, remote_folder): 1.19 ++ if port == '143': 1.20 ++ self.mailbox = imaplib.IMAP4(host, port) 1.21 ++ elif port == '993': 1.22 + self.mailbox = imaplib.IMAP4_SSL(host, port) 1.23 + self.mailbox.login(username, password) 1.24 + self.mailbox.select(remote_folder, readonly=True) 1.25 + 1.26 ++ if remote_folder=='INBOX': 1.27 ++ self.remote_folder='' 1.28 ++ else 1.29 ++ r_f=re.sub('INBOX','',remote_folder) 1.30 ++ if re.findall('^\.',r_f): 1.31 ++ self.remote_folder=re.sub('\.','/',rf) 1.32 ++ else: 1.33 ++ self.remote_folder='/'+r_f 1.34 ++ 1.35 + def copy_emails(self, days, local_folder, wkhtmltopdf): 1.36 + 1.37 + n_saved = 0 1.38 +@@ -50,9 +64,19 @@ 1.39 + 1.40 + 1.41 + def getEmailFolder(self, msg, data): 1.42 +- if msg['Message-Id']: 1.43 +- foldername = re.sub('[^a-zA-Z0-9_\-\.()\s]+', '', msg['Message-Id']) 1.44 ++ if msg['To'] and re.findall('Sent',self.remote_folder): 1.45 ++ foldername = re.findall("<.*>", msg['To']) 1.46 ++ if foldername: 1.47 ++ foldername = re.sub('[\<\>]+', '', foldername[0]) 1.48 + else: 1.49 ++ foldername=msg['To'] 1.50 ++ elif msg['From']: 1.51 ++ foldername = re.findall("<.*>", msg['From']) 1.52 ++ if foldername: 1.53 ++ foldername = re.sub('[\<\>]+', '', foldername[0]) 1.54 ++ else: 1.55 ++ foldername=msg['From'] 1.56 ++ else: 1.57 + foldername = hashlib.sha224(data).hexdigest() 1.58 + 1.59 + year = 'None' 1.60 +@@ -62,20 +86,33 @@ 1.61 + year = match.group(1) 1.62 + 1.63 + 1.64 +- return os.path.join(self.local_folder, year, foldername) 1.65 ++ return os.path.join(self.local_folder+self.remote_folder, self.normalizeDate(msg['Date'])+'_'+foldername) 1.66 + 1.67 ++ def normalizeDate(self, datestr): 1.68 ++ t = email.utils.parsedate_tz(datestr) 1.69 ++ timeval = time.mktime(t[:-1]) 1.70 ++ date = email.utils.formatdate(timeval, True) 1.71 ++ utc = time.gmtime(email.utils.mktime_tz(t)) 1.72 ++# rfc2822 = '{} {:+03d}00'.format(date[:-6], t[9]//3600) 1.73 ++ iso8601 = time.strftime('%Y%m%dT%H%M%SZ', utc) 1.74 + 1.75 ++ return (iso8601) 1.76 + 1.77 ++ 1.78 + def saveEmail(self, data): 1.79 + for response_part in data: 1.80 + if isinstance(response_part, tuple): 1.81 +- msg = email.message_from_string(response_part[1].decode("utf-8")) 1.82 ++ try: 1.83 ++ msg = email.message_from_string(re.sub('^>', '', response_part[1]).decode("utf-8")) # supprime '>' dans l'email 1.84 ++ except UnicodeError: 1.85 ++ msg = email.message_from_string(re.sub('^>', '', response_part[1]).decode('latin1').encode('utf-8')) # supprime '>' dans l'email 1.86 + directory = self.getEmailFolder(msg, data[0][1]) 1.87 + 1.88 + if os.path.exists(directory): 1.89 + return False 1.90 + 1.91 + os.makedirs(directory) 1.92 ++ os.utime(directory,(time.mktime(rfc822.parsedate(msg['Date'])), time.mktime(rfc822.parsedate(msg['Date'])))) 1.93 + 1.94 + try: 1.95 + message = Message(directory, msg) 1.96 +@@ -86,9 +123,12 @@ 1.97 + if self.wkhtmltopdf: 1.98 + message.createPdfFile(self.wkhtmltopdf) 1.99 + 1.100 ++ os.utime(directory,(time.mktime(rfc822.parsedate(msg['Date'])), time.mktime(rfc822.parsedate(msg['Date'])))) 1.101 ++ 1.102 + except Exception as e: 1.103 + # ex: Unsupported charset on decode 1.104 + print(directory) 1.105 ++ os.utime(directory,(time.mktime(rfc822.parsedate(msg['Date'])), time.mktime(rfc822.parsedate(msg['Date'])))) 1.106 + if hasattr(e, 'strerror'): 1.107 + print("MailboxClient.saveEmail() failed:", e.strerror) 1.108 + else: 1.109 +--- imapbox/message.py 1.110 ++++ imapbox/message.py 1.111 +@@ -15,6 +15,8 @@ 1.112 + import cgi 1.113 + import time 1.114 + import pkgutil 1.115 ++import rfc822 1.116 ++import sys 1.117 + 1.118 + from six.moves import html_parser 1.119 + 1.120 +@@ -82,14 +84,15 @@ 1.121 + except email.Errors.HeaderParseError: 1.122 + # This already append in email.base64mime.decode() 1.123 + # instead return a sanitized ascii string 1.124 +- return header_text.encode('ascii', 'replace').decode('ascii') 1.125 ++ return header_text.encode('ascii', 'replace').decode(default) 1.126 + else: 1.127 + for i, (text, charset) in enumerate(headers): 1.128 + headers[i]=text 1.129 + if charset: 1.130 +- headers[i]=str(text, charset) 1.131 ++ text = unicode(text,charset) 1.132 ++ headers[i]=text 1.133 + else: 1.134 +- headers[i]=str(text) 1.135 ++ headers[i]=text.decode('utf-8') 1.136 + return u"".join(headers) 1.137 + 1.138 + 1.139 +@@ -102,21 +105,27 @@ 1.140 + # use the same for both and see later 1.141 + name=addr 1.142 + 1.143 ++ headers=decode_header(name) 1.144 + try: 1.145 +- # address must be ascii only 1.146 +- addr=addr.encode('ascii') 1.147 ++ addr = addr.encode("ascii") 1.148 + except UnicodeError: 1.149 +- addr='' 1.150 ++ addr = '' 1.151 + else: 1.152 + # address must match adress regex 1.153 + if not email_address_re.match(addr.decode("utf-8")): 1.154 + addr='' 1.155 +- addrs[i]=(self.getmailheader(name), addr.decode("utf-8")) 1.156 ++ addrs[i]=(self.getmailheader(name), "utf-8") 1.157 + return addrs 1.158 + 1.159 + def getSubject(self): 1.160 +- if not hasattr(self, 'subject'): 1.161 +- self.subject = self.getmailheader(self.msg.get('Subject', '')) 1.162 ++# if not hasattr(self, 'subject'): 1.163 ++# self.subject = self.getmailheader(self.msg.get('Subject'),'') 1.164 ++ headers=decode_header(self.msg.get('Subject')) 1.165 ++ for i, (text, charset) in enumerate(headers): 1.166 ++ if charset: 1.167 ++ self.subject = unicode(text,charset) 1.168 ++ else: 1.169 ++ self.subject=text.decode('utf-8') 1.170 + return self.subject 1.171 + 1.172 + def getFrom(self): 1.173 +@@ -172,6 +181,7 @@ 1.174 + json_file.write(data) 1.175 + 1.176 + json_file.close() 1.177 ++ os.utime('%s/metadata.json' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date'])))) 1.178 + 1.179 + 1.180 + 1.181 +@@ -180,6 +190,7 @@ 1.182 + f = gzip.open('%s/raw.eml.gz' %(self.directory), 'wb') 1.183 + f.write(data) 1.184 + f.close() 1.185 ++ os.utime('%s/raw.eml.gz' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date'])))) 1.186 + 1.187 + 1.188 + def getPartCharset(self, part): 1.189 +@@ -192,14 +203,23 @@ 1.190 + if not hasattr(self, 'text_content'): 1.191 + self.text_content = '' 1.192 + for part in parts: 1.193 ++ raw_content_0 = part.get_payload() 1.194 + raw_content = part.get_payload(decode=True) 1.195 + charset = self.getPartCharset(part) 1.196 + self.text_content += raw_content.decode(charset, "replace") 1.197 ++ if charset!='utf-8' and self.text_content: 1.198 ++ try: 1.199 ++ self.text_content=self.text_content.encode('raw_unicode_escape').decode('utf-8') # Double-decoding unicode 1.200 ++ except UnicodeError: 1.201 ++ self.text_content=raw_content_0.decode('utf_8') 1.202 + return self.text_content 1.203 + 1.204 + 1.205 + def createTextFile(self, parts): 1.206 +- utf8_content = self.getTextContent(parts) 1.207 ++ intro='' 1.208 ++ if self.getSubject(): 1.209 ++ intro='==> ' + self.getSubject() + '\r\n\r\n' 1.210 ++ utf8_content = intro + self.getTextContent(parts) 1.211 + with open(os.path.join(self.directory, 'message.txt'), 'wb') as fp: 1.212 + fp.write(bytearray(utf8_content, 'utf-8')) 1.213 + 1.214 +@@ -208,9 +228,15 @@ 1.215 + self.html_content = '' 1.216 + 1.217 + for part in parts: 1.218 ++ raw_content_0 = part.get_payload() 1.219 + raw_content = part.get_payload(decode=True) 1.220 + charset = self.getPartCharset(part) 1.221 + self.html_content += raw_content.decode(charset, "replace") 1.222 ++ if charset!='utf-8' and self.html_content: 1.223 ++ try: 1.224 ++ self.html_content.encode('raw_unicode_escape').decode('utf-8') # Double-decoding unicode 1.225 ++ except UnicodeError: 1.226 ++ self.html_content=raw_content_0.decode('utf_8') 1.227 + 1.228 + m = re.search('<body[^>]*>(.+)<\/body>', self.html_content, re.S | re.I) 1.229 + if (m != None): 1.230 +@@ -304,9 +330,11 @@ 1.231 + 1.232 + if message_parts['text']: 1.233 + self.createTextFile(message_parts['text']) 1.234 ++ os.utime('%s/message.txt' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date'])))) 1.235 + 1.236 + if message_parts['html']: 1.237 + self.createHtmlFile(message_parts['html'], message_parts['embed_images']) 1.238 ++ os.utime('%s/message.html' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date'])))) 1.239 + 1.240 + if message_parts['files']: 1.241 + attdir = os.path.join(self.directory, 'attachments') 1.242 +@@ -317,6 +345,8 @@ 1.243 + payload = afile[0].get_payload(decode=True) 1.244 + if payload: 1.245 + fp.write(payload) 1.246 ++ os.utime('%s/attachments/%s' %(self.directory,afile[1]),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date'])))) 1.247 ++ os.utime('%s/attachments' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date'])))) 1.248 + 1.249 + 1.250 + def createPdfFile(self, wkhtmltopdf): 1.251 +@@ -325,5 +355,6 @@ 1.252 + pdf_path = os.path.join(self.directory, 'message.pdf') 1.253 + config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf) 1.254 + pdfkit.from_file(html_path, pdf_path, configuration=config) 1.255 ++ os.utime('%s/message.pdf' %(self.directory),(time.mktime(rfc822.parsedate(self.msg['Date'])), time.mktime(rfc822.parsedate(self.msg['Date'])))) 1.256 + else: 1.257 + print("Couldn't create PDF message, since \"pdfkit\" module isn't installed.")