added suggestions for marvinids

This commit is contained in:
Tobias Kessels
2020-02-05 10:58:54 +01:00
parent 55be077d79
commit 94b226c580

View File

@@ -10,8 +10,9 @@ from cursesmenu.items import *
import dialog import dialog
config_file_path=os.path.join(os.path.expanduser('~'),".imap_virus_marvin.ini") config_file_path=os.path.join(os.path.expanduser('~'),".imap_virus_marvin.ini")
dialog=dialog.Dialog() dialog=dialog.Dialog()
# dialog.set_background_title("IMAP-Mail-Renamer") dialog.set_background_title("IMAP-Mail-Renamer")
marvin_pattern=re.compile('MARVIN#\d{14}_') marvin_pattern=re.compile('MARVIN\d{14}_')
marvin_candidates=re.compile('(([mM][aA][rR][vV][iI][nN].{0,3})?\d{14})')
def edit(num): def edit(num):
pass pass
@@ -47,7 +48,19 @@ def get_config():
else: else:
return config_instance return config_instance
def get_header(eml, string):
a=email.header.decode_header(eml[string])
ergebnisse=[]
for eintrag in a:
ergebnisse.append(force_decode(eintrag[0]))
return ergebnisse
#aaaah das mit dem decoden und dann die liste anlegen und dann das ergebniss zurückgeben.... irgendwas mit a und auch wenn nicht encoded einfach so zurückgeben genau!
def force_decode(string, codecs=['utf8', 'cp1252']): def force_decode(string, codecs=['utf8', 'cp1252']):
if isinstance(string, str):
return string
for i in codecs: for i in codecs:
try: try:
return string.decode(i) return string.decode(i)
@@ -73,6 +86,7 @@ def decode(data):
res+= part[0].decode(part[1]) res+= part[0].decode(part[1])
return "".join(res.split()) return "".join(res.split())
def get_subject(num): def get_subject(num):
global im global im
res, data = im.fetch(num,'BODY.PEEK[HEADER.FIELDS (SUBJECT)]') res, data = im.fetch(num,'BODY.PEEK[HEADER.FIELDS (SUBJECT)]')
@@ -104,29 +118,42 @@ def print_mail(num):
eml=get_mail(num) eml=get_mail(num)
dialogit(str(eml)) dialogit(str(eml))
def scan_for_marvins(eml):
results=marvin_candidates.findall(eml.as_string())
ergebnisse=[]
for x in results:
if x[0] not in ergebnisse:
ergebnisse.append(x[0])
return ergebnisse
def edit_mail(num): def edit_mail(num):
global im global im
global config global config
eml=get_mail(num) eml=get_mail(num)
old_subject=eml['Subject'] old_subject=get_header(eml,'Subject')
action,new_subject=dialog.inputbox("Edit Subject",init=old_subject) results=scan_for_marvins(eml)
if action == "OK": suggesttext="Found {} possible marvins".format(len(results))
eml.replace_header('Subject',new_subject) suggesttext+="\n"
# c.append('INBOX', '', imaplib.Time2Internaldate(time.time()), str(new_message)) suggesttext+="\n".join(results)
c,d = im.append('INBOX','', imaplib.Time2Internaldate(time.time()),str(eml).encode('utf-8')) dialog.msgbox(suggesttext,width=110)
# c= OK
# d= [b'[APPENDUID 1252405521 2655] APPEND Ok.'] # action,new_subject=dialog.inputbox(suggesttext,init=old_subject)
# if append ok delete original mailbox # if action == "OK":
if "OK" in c: # eml.replace_header('Subject',new_subject)
delete_mail(num) # c,d = im.append('INBOX','', imaplib.Time2Internaldate(time.time()),str(eml).encode('utf-8'))
# # c= OK
# # d= [b'[APPENDUID 1252405521 2655] APPEND Ok.']
# # if append ok delete original mailbox
# if "OK" in c:
# delete_mail(num)
def quit(): def quit():
exit(0) exit(0)
def dialogit(text): def dialogit(text):
dialog.scrollbox(text,height=60,width=110) dialog.scrollbox(text,height=30,width=110)
def main(): def make_choice():
global config global config
global im global im
config=get_config() config=get_config()
@@ -143,7 +170,7 @@ def main():
if not marvin_pattern.match(subject_line): if not marvin_pattern.match(subject_line):
function_item = FunctionItem(subject_line, edit_mail , [n] ,should_exit=True) function_item = FunctionItem(subject_line, edit_mail , [n] ,should_exit=True)
menu.append_item(function_item) menu.append_item(function_item)
returnval=menu.show() menu.show()
@@ -151,5 +178,14 @@ def main():
im.close() im.close()
im.logout() im.logout()
def main():
f=open('testmail','rb')
eml=email.message_from_binary_file(f)
# dialogit("\n".join(scan_for_marvins(eml)))
make_choice()
if __name__ == "__main__": if __name__ == "__main__":
main() main()