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
config_file_path=os.path.join(os.path.expanduser('~'),".imap_virus_marvin.ini")
dialog=dialog.Dialog()
# dialog.set_background_title("IMAP-Mail-Renamer")
marvin_pattern=re.compile('MARVIN#\d{14}_')
dialog.set_background_title("IMAP-Mail-Renamer")
marvin_pattern=re.compile('MARVIN\d{14}_')
marvin_candidates=re.compile('(([mM][aA][rR][vV][iI][nN].{0,3})?\d{14})')
def edit(num):
pass
@@ -47,7 +48,19 @@ def get_config():
else:
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']):
if isinstance(string, str):
return string
for i in codecs:
try:
return string.decode(i)
@@ -73,6 +86,7 @@ def decode(data):
res+= part[0].decode(part[1])
return "".join(res.split())
def get_subject(num):
global im
res, data = im.fetch(num,'BODY.PEEK[HEADER.FIELDS (SUBJECT)]')
@@ -104,29 +118,42 @@ def print_mail(num):
eml=get_mail(num)
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):
global im
global config
eml=get_mail(num)
old_subject=eml['Subject']
action,new_subject=dialog.inputbox("Edit Subject",init=old_subject)
if action == "OK":
eml.replace_header('Subject',new_subject)
# c.append('INBOX', '', imaplib.Time2Internaldate(time.time()), str(new_message))
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)
old_subject=get_header(eml,'Subject')
results=scan_for_marvins(eml)
suggesttext="Found {} possible marvins".format(len(results))
suggesttext+="\n"
suggesttext+="\n".join(results)
dialog.msgbox(suggesttext,width=110)
# action,new_subject=dialog.inputbox(suggesttext,init=old_subject)
# if action == "OK":
# eml.replace_header('Subject',new_subject)
# 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():
exit(0)
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 im
config=get_config()
@@ -143,7 +170,7 @@ def main():
if not marvin_pattern.match(subject_line):
function_item = FunctionItem(subject_line, edit_mail , [n] ,should_exit=True)
menu.append_item(function_item)
returnval=menu.show()
menu.show()
@@ -151,5 +178,14 @@ def main():
im.close()
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__":
main()