mirror of
https://github.com/chillout2k/gulag.git
synced 2025-12-13 16:00:18 +00:00
libgulag.py init
This commit is contained in:
parent
406a69757a
commit
eb9fa80242
13
app/Gulag.py
13
app/Gulag.py
@ -175,7 +175,6 @@ class Gulag:
|
|||||||
part.get_payload(decode=True).decode("utf-8","replace")
|
part.get_payload(decode=True).decode("utf-8","replace")
|
||||||
)
|
)
|
||||||
if(len(curis) > 0):
|
if(len(curis) > 0):
|
||||||
logging.info(whoami(self) + "CURIS: " + str(curis))
|
|
||||||
uris = {**uris, **curis}
|
uris = {**uris, **curis}
|
||||||
# End for msg.walk()
|
# End for msg.walk()
|
||||||
# link message with attachments
|
# link message with attachments
|
||||||
@ -229,7 +228,10 @@ class Gulag:
|
|||||||
whoami(self) + e.message
|
whoami(self) + e.message
|
||||||
) from e
|
) from e
|
||||||
if 'rfc822_message' not in args:
|
if 'rfc822_message' not in args:
|
||||||
return qms_db
|
return {
|
||||||
|
'quarmails': qms_db,
|
||||||
|
'rfc822_messages': {}
|
||||||
|
}
|
||||||
# recognize all IMAP mailboxes to read from
|
# recognize all IMAP mailboxes to read from
|
||||||
# and store rfc822-messages under it
|
# and store rfc822-messages under it
|
||||||
mailboxes = {}
|
mailboxes = {}
|
||||||
@ -287,6 +289,7 @@ class Gulag:
|
|||||||
qm_db['rfc822_message'] = imap_mb.get_message(
|
qm_db['rfc822_message'] = imap_mb.get_message(
|
||||||
qm_db['imap_uid']
|
qm_db['imap_uid']
|
||||||
).decode("utf-8")
|
).decode("utf-8")
|
||||||
|
imap_mb.close()
|
||||||
return qm_db
|
return qm_db
|
||||||
except IMAPmailboxException as e:
|
except IMAPmailboxException as e:
|
||||||
logging.warning(whoami(self) + e.message)
|
logging.warning(whoami(self) + e.message)
|
||||||
@ -330,6 +333,7 @@ class Gulag:
|
|||||||
except GulagDBException as e:
|
except GulagDBException as e:
|
||||||
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
|
||||||
|
imap_mb.close()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_quarmail_attachments(self,args):
|
def get_quarmail_attachments(self,args):
|
||||||
@ -363,6 +367,7 @@ class Gulag:
|
|||||||
qmat_db['data'] = imap_mb.get_attachment(
|
qmat_db['data'] = imap_mb.get_attachment(
|
||||||
qmat_db['imap_uid'],qmat_db['filename']
|
qmat_db['imap_uid'],qmat_db['filename']
|
||||||
)
|
)
|
||||||
|
imap_mb.close
|
||||||
return qmat_db
|
return qmat_db
|
||||||
except IMAPmailboxException as e:
|
except IMAPmailboxException as e:
|
||||||
logging.warning(whoami(self) + e.message)
|
logging.warning(whoami(self) + e.message)
|
||||||
@ -406,6 +411,7 @@ class Gulag:
|
|||||||
"uri": uri,
|
"uri": uri,
|
||||||
"fqdn": extract_fqdn(uri)
|
"fqdn": extract_fqdn(uri)
|
||||||
})
|
})
|
||||||
|
imap_mb.close()
|
||||||
return uris
|
return uris
|
||||||
except IMAPmailboxException as e:
|
except IMAPmailboxException as e:
|
||||||
logging.warning(whoami(self) + e.message)
|
logging.warning(whoami(self) + e.message)
|
||||||
@ -481,6 +487,7 @@ class Gulag:
|
|||||||
imap_mb = None
|
imap_mb = None
|
||||||
try:
|
try:
|
||||||
imap_mb = IMAPmailbox(mailbox)
|
imap_mb = IMAPmailbox(mailbox)
|
||||||
imap_mb.append_message(msg)
|
imap_mb.add_message(msg)
|
||||||
|
imap_mb.close()
|
||||||
except IMAPmailboxException as e:
|
except IMAPmailboxException as e:
|
||||||
raise GulagException(whoami(self) + e.message) from e
|
raise GulagException(whoami(self) + e.message) from e
|
||||||
|
|||||||
@ -116,7 +116,17 @@ class GulagDB:
|
|||||||
filters = json.loads(filters_json)
|
filters = json.loads(filters_json)
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
raise GulagDBException(whoami(self) + "JSON parse error: " + e.msg) from 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']:
|
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
|
field_op_data = None
|
||||||
if(rule['op'] == 'eq'):
|
if(rule['op'] == 'eq'):
|
||||||
field_op_data = rule['field'] + "='" + rule['data'] + "'"
|
field_op_data = rule['field'] + "='" + rule['data'] + "'"
|
||||||
@ -217,12 +227,12 @@ class GulagDB:
|
|||||||
raise GulagDBException(whoami(self) + str(e)) from e
|
raise GulagDBException(whoami(self) + str(e)) from e
|
||||||
|
|
||||||
def get_quarmails(self,args):
|
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:
|
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()
|
cursor = self.conn.cursor()
|
||||||
query = "select *,(select count(*) from QuarMail2Attachment"
|
query = "select *,(select count(*) from QuarMail2Attachment"
|
||||||
query += " where QuarMails.id=QuarMail2Attachment.quarmail_id) as attach_count,"
|
query += " where QuarMails.id=QuarMail2Attachment.quarmail_id) as attach_count,"
|
||||||
@ -234,7 +244,7 @@ class GulagDB:
|
|||||||
results = []
|
results = []
|
||||||
data = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
if not data:
|
if not data:
|
||||||
raise GulagDBException(whoami(self) + "No QuarMails found in DB!")
|
return results
|
||||||
desc = cursor.description
|
desc = cursor.description
|
||||||
cursor.close()
|
cursor.close()
|
||||||
for tuple in data:
|
for tuple in data:
|
||||||
@ -328,7 +338,7 @@ class GulagDB:
|
|||||||
results = []
|
results = []
|
||||||
data = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
if not data:
|
if not data:
|
||||||
raise GulagDBException(whoami(self) + "No attachments found!")
|
return results
|
||||||
desc = cursor.description
|
desc = cursor.description
|
||||||
for tuple in data:
|
for tuple in data:
|
||||||
dict = {}
|
dict = {}
|
||||||
@ -376,9 +386,7 @@ class GulagDB:
|
|||||||
results = []
|
results = []
|
||||||
data = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
if not data:
|
if not data:
|
||||||
raise GulagDBException(whoami(self)
|
return results
|
||||||
+ "QuarMail("+ str(quarmail_id) +") has no attachments!"
|
|
||||||
)
|
|
||||||
desc = cursor.description
|
desc = cursor.description
|
||||||
for tuple in data:
|
for tuple in data:
|
||||||
dict = {}
|
dict = {}
|
||||||
@ -474,9 +482,7 @@ class GulagDB:
|
|||||||
results = []
|
results = []
|
||||||
data = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
if not data:
|
if not data:
|
||||||
raise GulagDBException(whoami(self)
|
return results
|
||||||
+ "QuarMail("+ str(quarmail_id) +") has no uris!"
|
|
||||||
)
|
|
||||||
desc = cursor.description
|
desc = cursor.description
|
||||||
for tuple in data:
|
for tuple in data:
|
||||||
dict = {}
|
dict = {}
|
||||||
|
|||||||
@ -61,6 +61,18 @@ class IMAPmailbox:
|
|||||||
})
|
})
|
||||||
return results
|
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):
|
def get_message(self,imap_uid):
|
||||||
rv, data = self.mailbox.uid('FETCH', str(imap_uid), '(RFC822)')
|
rv, data = self.mailbox.uid('FETCH', str(imap_uid), '(RFC822)')
|
||||||
if rv != 'OK':
|
if rv != 'OK':
|
||||||
@ -116,19 +128,3 @@ class IMAPmailbox:
|
|||||||
raise IMAPmailboxException(whoami(self) +
|
raise IMAPmailboxException(whoami(self) +
|
||||||
"IMAP_UID(" + str(imap_uid)+")@"+str(self.email_address)+" has no main parts!"
|
"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
49
client/python/libgulag.py
Normal 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
|
||||||
22
client/python/libgulag_test.py
Normal file
22
client/python/libgulag_test.py
Normal 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)
|
||||||
Loading…
Reference in New Issue
Block a user