def addToMapfile(self, to): str1 = to + self.cfg['delimiter'] + self.cfg['noentry'] + self.cfg['nl'] with open(self.cfg['mappingfile'], "a") as fh: fh.write(str1) cupw("added to mapfile=", str1.rstrip()) self.mapfile = self.loadMapFile() # reload self.mapfile!!! TODO not working????
def parseArg(self, larg): #<list >opt-dict, word-list larg = larg[1:] #remove 1. arg (=caller name) for a in larg: if a.startswith("--"): l1 = a.split("=") self.opt[l1[0]] = l1[1] else: cupw("TODO: what to do with opt=", a)
def setMail(self, mail): #<mail data raw_email = mail[0][1] raw_email_string = raw_email.decode('utf-8') self.mail = email.message_from_string(raw_email_string) cupw("self.mail=", self.mail) self.maildict = {} # Header Details self.maildict['date'] = "empty date" #TODO from cfg date_tuple = email.utils.parsedate_tz(self.mail['Date']) if date_tuple: self.maildict['local_date'] = datetime.datetime.fromtimestamp( email.utils.mktime_tz(date_tuple)) self.maildict['local_message_date'] = "%s" % (str( self.maildict['local_date'].strftime("%a, %d %b %Y %H:%M:%S"))) self.maildict['date'] = str(self.maildict['local_date']) if 'From' in self.mail: self.maildict['from'] = str( email.header.make_header( email.header.decode_header(self.mail['From']))) else: self.maildict['from'] = "empty From" #TODO from cfg if 'To' in self.mail: self.maildict['to'] = str( email.header.make_header( email.header.decode_header(self.mail['To']))) else: self.maildict['to'] = "empty To" #TODO from cfg if 'Subject' in self.mail: self.maildict['subject'] = str( email.header.make_header( email.header.decode_header(self.mail['Subject']))) else: self.maildict['subject'] = "empty Subject!!!" #TODO from cfg # Body details self.maildict['body'] = "empty body" #TODO for part in self.mail.walk(): if part.get_content_type() == "text/plain": self.maildict['body'] = part.get_payload( decode=True).decode('utf-8')
def getExportFilename(self, to): #<str1@str2 >(filename, exporter) #TODO check for unwandet chars (eg <asdjlfasd>) to = to.lower() #TODO use only lowercase filename = self.cfg['exportdir'] + self.splitTo(to) + ".txt" #default filename if to in self.mapfile: if self.mapfile[to] != self.cfg['noentry']: filename = self.mapfile[to] else: cupw("??? already in mapfile for TO=", to) else: self.addToMapfile(to) exporter = self.getFileSuffix(filename) return filename, exporter
#!/usr/bin/env python3 from cmdtutil import prnwo as cupw cupw("here") import datetime import email import re class Maily: def __init__(self, cfg): self.cfg = cfg #self.mail = # email.message_from_string(raw_email_string) #self.maildict['from'] = # #self.maildict['to'] = # #self.maildict['local_date'] = # datetime object #self.maildict['local_message_date'] = #Wed, 10 Feb 2021 07:50:59 #self.maildict['date'] = str(self.maildict'local_date') #2021-02-10 08:53:18 #self.maildict['subject'] = subject #self.maildict['body'] = body def setMail(self, mail): #<mail data raw_email = mail[0][1] raw_email_string = raw_email.decode('utf-8') self.mail = email.message_from_string(raw_email_string) cupw("self.mail=", self.mail) self.maildict = {} # Header Details self.maildict['date'] = "empty date" #TODO from cfg
def connect(self): cupw("connect to mailserver=", self.cfg) self.server = imaplib.IMAP4_SSL(self.cfg['server']) self.server.login(self.cfg['user'], self.cfg['pwd'])
def closer(self): self.server.expunge() self.server.close() self.server.logout() cupw("mailserver expunged, closed and logouted")
#!/usr/bin/env python3 from cmdtutil import prnwo as cupw cupw("here") ''' >>> readme.txt ''' import sys import lib.mailserver import lib.writer.txt import lib.mappingfile import lib.maily class App: def __init__(self, larg): self.larg = larg self.opt = self.setDefaultOpt() #1: default options self.parseArg(self.larg) #2: parse arg and overwrite options <list >dict self.cfg = self.setCfg() #3: set configs according to options def setCfg(self): cfg = { 'mailserver': { 'server': self.opt['--mailserver'], 'user': self.opt['--mailuser'], 'pwd': self.opt['--mailpwd'], 'maildel': self.opt['--maildel'] },