libgulag.py init

This commit is contained in:
Dominik Chilla 2018-12-28 20:09:28 +01:00
parent 406a69757a
commit eb9fa80242
5 changed files with 117 additions and 37 deletions

View File

@ -175,7 +175,6 @@ class Gulag:
part.get_payload(decode=True).decode("utf-8","replace")
)
if(len(curis) > 0):
logging.info(whoami(self) + "CURIS: " + str(curis))
uris = {**uris, **curis}
# End for msg.walk()
# link message with attachments
@ -229,7 +228,10 @@ class Gulag:
whoami(self) + e.message
) from e
if 'rfc822_message' not in args:
return qms_db
return {
'quarmails': qms_db,
'rfc822_messages': {}
}
# recognize all IMAP mailboxes to read from
# and store rfc822-messages under it
mailboxes = {}
@ -287,6 +289,7 @@ class Gulag:
qm_db['rfc822_message'] = imap_mb.get_message(
qm_db['imap_uid']
).decode("utf-8")
imap_mb.close()
return qm_db
except IMAPmailboxException as e:
logging.warning(whoami(self) + e.message)
@ -330,6 +333,7 @@ class Gulag:
except GulagDBException as e:
logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e
imap_mb.close()
return True
def get_quarmail_attachments(self,args):
@ -363,6 +367,7 @@ class Gulag:
qmat_db['data'] = imap_mb.get_attachment(
qmat_db['imap_uid'],qmat_db['filename']
)
imap_mb.close
return qmat_db
except IMAPmailboxException as e:
logging.warning(whoami(self) + e.message)
@ -406,6 +411,7 @@ class Gulag:
"uri": uri,
"fqdn": extract_fqdn(uri)
})
imap_mb.close()
return uris
except IMAPmailboxException as e:
logging.warning(whoami(self) + e.message)
@ -481,6 +487,7 @@ class Gulag:
imap_mb = None
try:
imap_mb = IMAPmailbox(mailbox)
imap_mb.append_message(msg)
imap_mb.add_message(msg)
imap_mb.close()
except IMAPmailboxException as e:
raise GulagException(whoami(self) + e.message) from e

View File

@ -116,7 +116,17 @@ class GulagDB:
filters = json.loads(filters_json)
except json.JSONDecodeError as e:
raise GulagDBException(whoami(self) + "JSON parse error: " + e.msg) from e
if 'rules' not in filters:
raise GulagDBException(whoami(self) + "no 'rules' found in filters!")
if 'groupOp' not in filters:
raise GulagDBException(whoami(self) + "'groupOp' not found in filters!")
for rule in filters['rules']:
if 'field' not in rule:
raise GulagDBException(whoami(self) + "'field' not found in rule!")
if 'op' not in rule:
raise GulagDBException(whoami(self) + "'op' not found in rule!")
if 'data' not in rule:
raise GulagDBException(whoami(self) + "'data' not found in rule!")
field_op_data = None
if(rule['op'] == 'eq'):
field_op_data = rule['field'] + "='" + rule['data'] + "'"
@ -217,12 +227,12 @@ class GulagDB:
raise GulagDBException(whoami(self) + str(e)) from e
def get_quarmails(self,args):
where_clause = ""
if 'filters' in args:
where_clause = self.get_where_clause_from_filters(args['filters'])
else:
where_clause = self.get_where_clause(args)
try:
where_clause = ""
if 'filters' in args:
where_clause = self.get_where_clause_from_filters(args['filters'])
else:
where_clause = self.get_where_clause(args)
cursor = self.conn.cursor()
query = "select *,(select count(*) from QuarMail2Attachment"
query += " where QuarMails.id=QuarMail2Attachment.quarmail_id) as attach_count,"
@ -234,7 +244,7 @@ class GulagDB:
results = []
data = cursor.fetchall()
if not data:
raise GulagDBException(whoami(self) + "No QuarMails found in DB!")
return results
desc = cursor.description
cursor.close()
for tuple in data:
@ -328,7 +338,7 @@ class GulagDB:
results = []
data = cursor.fetchall()
if not data:
raise GulagDBException(whoami(self) + "No attachments found!")
return results
desc = cursor.description
for tuple in data:
dict = {}
@ -376,9 +386,7 @@ class GulagDB:
results = []
data = cursor.fetchall()
if not data:
raise GulagDBException(whoami(self)
+ "QuarMail("+ str(quarmail_id) +") has no attachments!"
)
return results
desc = cursor.description
for tuple in data:
dict = {}
@ -474,9 +482,7 @@ class GulagDB:
results = []
data = cursor.fetchall()
if not data:
raise GulagDBException(whoami(self)
+ "QuarMail("+ str(quarmail_id) +") has no uris!"
)
return results
desc = cursor.description
for tuple in data:
dict = {}

View File

@ -34,7 +34,7 @@ class IMAPmailbox:
raise IMAPmailboxException(whoami(self) +
self.imap_user + ": IMAP server " + self.imap_server + " refused connection"
) from e
rv, data = self.mailbox.select(self.imap_mailbox)
if rv != 'OK':
raise IMAPmailboxException(whoami(self) +
@ -61,10 +61,22 @@ class IMAPmailbox:
})
return results
def add_message(self,message):
rv, data = self.mailbox.append(
self.imap_mailbox,
'UNSEEN',
imaplib.Time2Internaldate(time.time()),
str(message).encode('utf-8')
)
if rv != 'OK':
raise IMAPmailboxException(whoami(self)+
"ERROR appending message: " + rv
)
def get_message(self,imap_uid):
rv, data = self.mailbox.uid('FETCH', str(imap_uid), '(RFC822)')
if rv != 'OK':
raise IMAPmailboxException(whoami(self) +
raise IMAPmailboxException(whoami(self) +
"ERROR getting message: %s", str(imap_uid)
)
return data[0][1]
@ -72,12 +84,12 @@ class IMAPmailbox:
def delete_message(self,imap_uid):
rv, data = self.mailbox.uid('STORE', str(imap_uid), '+FLAGS', '(\\Deleted)')
if rv != 'OK':
raise IMAPmailboxException(whoami(self) +
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) +
raise IMAPmailboxException(whoami(self) +
"ERROR expunging mailbox"
)
return True
@ -100,7 +112,7 @@ class IMAPmailbox:
# End if part.get_filename()
# End msg.walk() loop
raise IMAPmailboxException(whoami(self) +
"Attachment ("+ str(filename) +")@IMAP UID(" + str(imap_uid) + ")@"
"Attachment ("+ str(filename) +")@IMAP UID(" + str(imap_uid) + ")@"
+ str(self.email_address) + " not found!"
)
@ -116,19 +128,3 @@ class IMAPmailbox:
raise IMAPmailboxException(whoami(self) +
"IMAP_UID(" + str(imap_uid)+")@"+str(self.email_address)+" has no main parts!"
)
def append_message(self,message):
rv, data = self.mailbox.append(
self.imap_mailbox,
'UNSEEN',
imaplib.Time2Internaldate(time.time()),
str(message).encode('utf-8')
)
if rv != 'OK':
raise IMAPmailboxException(whoami(self)+
"ERROR appending message: " + rv
)
def expunge_message(self,imap_uid):
return True

49
client/python/libgulag.py Normal file
View File

@ -0,0 +1,49 @@
import requests,json,sys
class GulagClientException(Exception):
message = None
def __init__(self,message):
self.message = message
class GulagClient:
api_uri = None
api_key = None
headers = None
def __init__(self,args):
self.api_uri = args['api_uri']
self.api_key = args['api_key']
self.headers = {
'Content-Type': 'application/json',
'API_KEY': self.api_key
}
def whoami(self):
return type(self).__name__ + "::" + sys._getframe(1).f_code.co_name + "(): "
def handle_response(self,response):
if response.status_code == 200:
return json.loads(response.content.decode('utf-8'))
elif response.status_code == 400 or response.status_code == 500:
error = json.loads(response.content.decode('utf-8'))
raise GulagClientException(self.whoami() + error['message'])
def get_quarmails(self,args):
if 'filters' in args:
try:
# jqgrid-style filters must be JSON-encoded
args['filters'] = json.dumps(args['filters'])
except TypeError as e:
raise GulagClientException(e.__str__)
try:
response = requests.get(
self.api_uri + '/api/v1/quarmails',
headers=self.headers,
params=args
)
except Exception as e:
raise GulagClientException(self.whoami() + str(e)) from e
try:
return self.handle_response(response)
except GulagClientException as e:
raise GulagClientException(self.whoami() + e.message) from e

View File

@ -0,0 +1,22 @@
import libgulag
try:
gulag = libgulag.GulagClient({
'api_uri': 'http://127.0.0.1:9090',
'api_key': 'NotImplemented'
})
quarmails = gulag.get_quarmails({
'filters': {"groupOp":"AND","rules":[
{"field":"uri_count","op":"eq","data":"2"}
]},
'rfc822_message': 'ja, ich will',
'query_limit': 2
})
for qm in quarmails['quarmails']:
print(
"ID: " + str(qm['id'])
+ "\n Subject: " + qm['hdr_subject']
+ "\n ctime: " + qm['ctime']
)
except libgulag.GulagClientException as e:
print("ERROR: " + e.message)