deletion of QuarMail + relations

This commit is contained in:
Dominik Chilla 2018-12-23 19:29:15 +01:00
parent 6c0b07aa08
commit 76acc94391
5 changed files with 103 additions and 21 deletions

View File

@ -83,7 +83,6 @@ class Gulag:
uris = {} uris = {}
uid = unseen['imap_uid'] uid = unseen['imap_uid']
msg = email.message_from_bytes(unseen['msg']) msg = email.message_from_bytes(unseen['msg'])
# msg_size = len(msg)
msg_size = len(msg.as_string()) msg_size = len(msg.as_string())
r5321_from = email.header.decode_header(msg['Return-Path'])[0][0] r5321_from = email.header.decode_header(msg['Return-Path'])[0][0]
if(r5321_from is not '<>'): if(r5321_from is not '<>'):
@ -168,7 +167,6 @@ class Gulag:
ctype = part.get_content_type() ctype = part.get_content_type()
if(ctype == 'text/plain' or ctype == 'text/html'): if(ctype == 'text/plain' or ctype == 'text/html'):
curis = {} curis = {}
# curis = extract_uris(part.get_payload(decode=True).decode("utf-8"))
curis = extract_uris(part.get_payload(decode=True).decode("utf-8","replace")) curis = extract_uris(part.get_payload(decode=True).decode("utf-8","replace"))
if(len(curis) > 0): if(len(curis) > 0):
logging.info(whoami(self) + "CURIS: " + str(curis)) logging.info(whoami(self) + "CURIS: " + str(curis))
@ -225,7 +223,7 @@ class Gulag:
) from e ) from e
if 'rfc822_message' not in args: if 'rfc822_message' not in args:
return qms_db return qms_db
# collect all IMAP mailboxes to read from # recognise all IMAP mailboxes to read from
mailboxes = {} mailboxes = {}
for qm in qms_db: for qm in qms_db:
if qm['mailbox_id'] not in mailboxes: if qm['mailbox_id'] not in mailboxes:
@ -244,10 +242,13 @@ class Gulag:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
for qm_db in qms_db: for qm_db in qms_db:
qm_db['rfc822_message'] = imap_mb.get_message(qm_db['imap_uid']).decode("utf-8") try:
logging.info(whoami(self) + qm_db['rfc822_message'] = imap_mb.get_message(
str(qm_db['imap_uid']) + " size: " + str(qm_db['msg_size']) qm_db['imap_uid']
) ).decode("utf-8")
except IMAPmailboxException as e:
logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e
return qms_db return qms_db
def get_quarmail(self,args): def get_quarmail(self,args):
@ -275,6 +276,46 @@ class Gulag:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
def delete_quarmail(self, args):
qm_db = None
try:
qm_db = self.db.get_quarmail({"id": args['quarmail_id']})
except GulagDBException as e:
logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e
mailbox = None
try:
mailbox = self.db.get_mailbox(qm_db['mailbox_id'])
except GulagDBException as e:
logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e
# Delete QuarMail from IMAP mailbox
imap_mb = None
try:
imap_mb = IMAPmailbox(mailbox)
imap_mb.delete_message(qm_db['imap_uid'])
except IMAPmailboxException as e:
logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e
# Try to remove related objects (attachments, uris, ...)
try:
self.db.delete_quarmail_attachments(args['quarmail_id'])
except GulagDBException as e:
logging.warning(whoami(self) + e.message)
# No exception, as other quarmails may pointer to one of the attachments as well
try:
self.db.delete_quarmail_uris(args['quarmail_id'])
except GulagDBException as e:
logging.warning(whoami(self) + e.message)
# No exception, as other quarmails may pointer to one of the uris as well
# Finally delete QuarMail from DB
try:
self.db.delete_quarmail(args['quarmail_id'])
except GulagDBException as e:
logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e
return True
def get_quarmail_attachments(self,args): def get_quarmail_attachments(self,args):
try: try:
return self.db.get_quarmail_attachments(args['quarmail_id']) return self.db.get_quarmail_attachments(args['quarmail_id'])

View File

@ -173,10 +173,10 @@ class GulagDB:
except mariadb.Error as e: except mariadb.Error as e:
raise GulagDBException(whoami(self) + (e)) from e raise GulagDBException(whoami(self) + (e)) from e
def del_quarmail(self, id): def delete_quarmail(self, id):
try: try:
cursor = self.conn.cursor() cursor = self.conn.cursor()
cursor.execute("delete from QuarMails where id=%s;", (id)) cursor.execute("delete from QuarMails where id=" + str(id))
cursor.close() cursor.close()
return True return True
except mariadb.Error as e: except mariadb.Error as e:
@ -378,6 +378,19 @@ class GulagDB:
except mariadb.Error as e: except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e raise GulagDBException(whoami(self) + str(e)) from e
def delete_quarmail_attachments(self, quarmail_id):
cursor = None
try:
cursor = self.conn.cursor()
except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e
for qm_at in self.get_quarmail_attachments(quarmail_id):
try:
cursor.execute("delete from Attachments where id=" + str(qm_at['id']))
except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e
cursor.close()
return True
def quarmail2attachment(self,quarmail_id,attachment_id): def quarmail2attachment(self,quarmail_id,attachment_id):
try: try:
@ -400,17 +413,6 @@ class GulagDB:
except mariadb.Error as e: except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e raise GulagDBException(whoami(self) + str(e)) from e
def del_uri(self,uri_id):
try:
cursor = self.conn.cursor()
cursor.execute(
"delete from URIs where uri_id=" + uri_id + ";"
)
return cursor.lastrowid
except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e
def quarmail2uri(self,quarmail_id,uri_id): def quarmail2uri(self,quarmail_id,uri_id):
try: try:
cursor = self.conn.cursor() cursor = self.conn.cursor()
@ -447,3 +449,18 @@ class GulagDB:
return results return results
except mariadb.Error as e: except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e raise GulagDBException(whoami(self) + str(e)) from e
def delete_quarmail_uris(self, quarmail_id):
cursor = None
try:
cursor = self.conn.cursor()
except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e
for qm_uri in self.get_quarmail_uris(quarmail_id):
try:
cursor.execute("delete from URIs where id=" + str(qm_uri['id']))
except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e
cursor.close()
return True

View File

@ -69,6 +69,19 @@ class IMAPmailbox:
) )
return data[0][1] return data[0][1]
def delete_message(self,imap_uid):
rv, data = self.mailbox.uid('STORE', str(imap_uid), '+FLAGS', '(\\Deleted)')
if rv != 'OK':
raise IMAPmailboxException(whoami(self) +
"ERROR flagging message for deletion: %s", str(imap_uid)
)
rv, data = self.mailbox.expunge()
if rv != 'OK':
raise IMAPmailboxException(whoami(self) +
"ERROR expunging mailbox"
)
return True
def get_attachment(self,imap_uid,filename): def get_attachment(self,imap_uid,filename):
msg = email.message_from_bytes(self.get_message(imap_uid)) msg = email.message_from_bytes(self.get_message(imap_uid))
for part in msg.walk(): for part in msg.walk():

View File

@ -58,6 +58,12 @@ class ResQuarMail(GulagResource):
return self.gulag.get_quarmail(args) return self.gulag.get_quarmail(args)
except GulagException as e: except GulagException as e:
abort(400, message=e.message) abort(400, message=e.message)
def delete(self,quarmail_id):
args = {"quarmail_id": quarmail_id}
try:
return self.gulag.delete_quarmail(args)
except GulagException as e:
abort(400, message=e.message)
class ResQuarMailAttachments(GulagResource): class ResQuarMailAttachments(GulagResource):
def get(self,quarmail_id): def get(self,quarmail_id):

View File

@ -113,6 +113,11 @@ paths:
description: IMAP UID of a quarantined email description: IMAP UID of a quarantined email
type: string type: string
required: false required: false
- in: query
name: rfc822_message
type: string
required: false
description: get full RFC822 email message for each QuarMail object
responses: responses:
200: 200:
description: search results matching criteria description: search results matching criteria