From 491452f326f5d413bc19e3129d3b4477bf78f453 Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 3 Jun 2021 05:42:21 +0200 Subject: Version 1 - Added basic error handling - Decided to keep Debug block: - moved it to the bottom - hardcoded my hostmask - Changed server config variable to Private - Changed access config variable's help text --- plugin.py | 366 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 197 insertions(+), 169 deletions(-) (limited to 'plugin.py') diff --git a/plugin.py b/plugin.py index 0f5dc26..c2c752c 100644 --- a/plugin.py +++ b/plugin.py @@ -60,94 +60,104 @@ class Mailcow(callbacks.Plugin): hostmask = irc.state.nickToHostmask(msg.nick) #Read-Functions: Summary + try: + if 'summary' in variant: + if hostmask in read or write: + URL = server + get + '/domain/' + id + try: + response = requests.get( + URL, + headers = {'accept': 'application/json', 'X-API-Key': api_key}, + ) + data = response.json() + domain = data['domain_name'] + description = data['description'] + bytes_total = data['bytes_total'] + #response.raise_for_status() + irc.reply(f"Domain: {domain} | Description: {description} | Bytes Total: {bytes_total} ") + except requests.exceptions.ConnectionError as err: + irc.error("Connection failed.") + except requests.exceptions.HTTPError as err: + irc.error(err) + else: + irc.reply("Thou shalt not pass.") + print("Intrusion attempt: " + hostmask) - if 'summary' in variant: - if hostmask in read or write: - URL = server + get + '/domain/' + id - response = requests.get( - URL, - headers = {'accept': 'application/json', 'X-API-Key': api_key}, - ) - data = response.json() - domain = data['domain_name'] - description = data['description'] - bytes_total = data['bytes_total'] - irc.reply(f"Domain: {domain} | Description: {description} | Bytes Total: {bytes_total} ") - else: - irc.reply("Thou shall not pass.") - print("Intrusion attempt: " + hostmask) + #Write-Functions: Create/Delete + elif 'create' in variant: + if hostmask in write: + URL = server + api + '/add/domain' + payload = { + "active": "1", + "aliases": "20", + "backupmx": "0", + "defquota": "1024", + "description": id, + "domain": id, + "mailboxes": "10", + "maxquota": "2048", + "quota": "5120", + "relay_all_recipients": "0", + "rl_frame": "s", + "rl_value": "10", + "restart_sogo": "10" + } + response = requests.post( + URL, + headers = {'accept': 'application/json', 'X-API-Key': api_key, 'Content-Type': 'application/json'}, + json = payload, + ) + data = response.json() + print(data) + status = data[0]['type'] + #object = data[0]['domain'] + #active = data[0]['active'] + msg = data[0]['msg'] + #max_aliases = data['aliases'] + #max_mailboxes = data['mailboxes'] + #default_quota = data['defquota'] + #max_mailbox_quota = data['maxquota'] + #max_domain_quota = data['quota'] + irc.reply(f"CREATION: {status} | {msg} | NOTE: SOGo is NOT being restarted automatically.")# Issue 'sogo restart' to make the object visible through Groupware. | Summary of object properties: MaxAliases: {aliases} | MaxMailboxes: {mailboxes} | DefaultQuota: {default_quota} | MaxMailBoxQuota: {max_mailbox_quota} | MaxDomainQuota: {max_domain_quota}") + else: + irc.reply("Thou shall not create.") + print("Intrusion attempt: " + hostmask) + + elif 'delete' in variant: + if hostmask in write: + URL = server + api + '/delete/domain' + payload = [ + id + ] + response = requests.post( + URL, + headers = {'accept': 'application/json', 'X-API-Key': api_key, 'Content-Type': 'application/json'}, + json = payload, + ) + data = response.json() + status = data[0]['type'] + object = data[0]['msg'] + irc.reply(f"DELETION: {status} - {msg} - Hey, where's that backup again?") + else: + irc.reply("Thou shall not delete.") + print("Intrusion attempt: " + hostmask) - #Write-Functions: Create/Delete - elif 'create' in variant: - if hostmask in write: - URL = server + api + '/add/domain' - #domain = id['domain'] - payload = { - "active": "1", - "aliases": "20", - "backupmx": "0", - "defquota": "1024", - "description": id, - "domain": id, - "mailboxes": "10", - "maxquota": "2048", - "quota": "5120", - "relay_all_recipients": "0", - "rl_frame": "s", - "rl_value": "10", - "restart_sogo": "10" - } - response = requests.post( - URL, - headers = {'accept': 'application/json', 'X-API-Key': api_key, 'Content-Type': 'application/json'}, - json = payload, - ) - data = response.json() - print(data) - status = data[0]['type'] - #object = data[0]['domain'] - #active = data[0]['active'] - msg = data[0]['msg'] - #max_aliases = data['aliases'] - #max_mailboxes = data['mailboxes'] - #default_quota = data['defquota'] - #max_mailbox_quota = data['maxquota'] - #max_domain_quota = data['quota'] - irc.reply(f"CREATION: {status} | {msg} | NOTE: SOGo is NOT being restarted automatically.")# Issue 'sogo restart' to make the object visible through Groupware. | Summary of object properties: MaxAliases: {aliases} | MaxMailboxes: {mailboxes} | DefaultQuota: {default_quota} | MaxMailBoxQuota: {max_mailbox_quota} | MaxDomainQuota: {max_domain_quota}") else: - irc.reply("Thou shall not create.") - print("Intrusion attempt: " + hostmask) + irc.reply("Unknown option.") - elif 'delete' in variant: - if hostmask in write: - URL = server + api + '/delete/domain' - payload = [ - id - ] - response = requests.post( - URL, - headers = {'accept': 'application/json', 'X-API-Key': api_key, 'Content-Type': 'application/json'}, - json = payload, - ) - data = response.json() - status = data[0]['type'] - object = data[0]['msg'] - irc.reply(f"DELETION: {status} - {msg} - Hey, where's that backup again?") + except KeyError as err: + key = err.args[0] + if key == 'domain_name': + irc.error("Invalid domain.") else: - irc.reply("Thou shall not delete.") - print("Intrusion attempt: " + hostmask) - - else: - irc.reply("Unknown option.") + irc.error("Unhandled " + str(err)) maildomain = wrap(maildomain, ['anything', 'anything']) - #################### - #FOR DEBUGGING / REMOVE BEFORE RELEASE - #################### - def mcdebug(self, irc, msg, args, variant): - """Prints values.""" + def mailbox(self, irc, msg, args, variant, id): + """