diff options
| author | Georg | 2021-06-03 05:42:21 +0200 |
|---|---|---|
| committer | Georg | 2021-06-03 05:42:21 +0200 |
| commit | 491452f326f5d413bc19e3129d3b4477bf78f453 (patch) | |
| tree | 32263d8e45d8a97924e5d725589fac2e6372fa33 | |
| parent | eda43f1d3cb0ecd5682d5073c547d37228531c32 (diff) | |
| download | mailcow-491452f326f5d413bc19e3129d3b4477bf78f453.tar.gz mailcow-491452f326f5d413bc19e3129d3b4477bf78f453.tar.bz2 mailcow-491452f326f5d413bc19e3129d3b4477bf78f453.zip | |
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
| -rw-r--r-- | __pycache__/plugin.cpython-38.pyc | bin | 4995 -> 5731 bytes | |||
| -rw-r--r-- | config.py | 6 | ||||
| -rw-r--r-- | plugin.py | 366 |
3 files changed, 200 insertions, 172 deletions
diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc Binary files differindex 139a39b..33a9f2d 100644 --- a/__pycache__/plugin.cpython-38.pyc +++ b/__pycache__/plugin.cpython-38.pyc @@ -68,7 +68,7 @@ registry.String('', """ Your Mailcow server \(https://example.com\) """ -, private=False +, private=True )) conf.registerGroup(Mailcow, 'access') @@ -76,7 +76,7 @@ conf.registerGroup(Mailcow, 'access') conf.registerGlobalValue(Mailcow.access, 'read', registry.CommaSeparatedListOfStrings('', """ -Nicknames to grant Read-Only access +Hostmasks to grant Read-Only access """ , private=True )) @@ -84,7 +84,7 @@ Nicknames to grant Read-Only access conf.registerGlobalValue(Mailcow.access, 'write', registry.CommaSeparatedListOfStrings('', """ -Nicknames to grant Write access +Hostmasks to grant Write access """ , private=True )) @@ -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): + """<option> <id> + i.e. 'summary cranberrry@liberta.casa' will print some details about his mailbox""" server = self.registryValue('api.server') api_key = self.registryValue('api.key') @@ -156,39 +166,98 @@ class Mailcow(callbacks.Plugin): nick = msg.nick hostmask = irc.state.nickToHostmask(msg.nick) + #Read-Functions: Summary + try: + if 'summary' in variant: + if hostmask in read or write: + URL = server + get + '/mailbox/' + id + try: + response = requests.get( + URL, + params={'q': 'requests'}, + headers={'accept': 'application/json', 'X-API-Key': api_key}, + ) + data = response.json() + #return(print(data)) + irc.reply('Username: ' + data['username'] + ' | Quota: ' + str(data['quota']) + ' | Messages: ' + str(data['messages']) + ' | Mail Active: ' + str(data['active']) + ' | XMPP Active: ' + str(data['domain_xmpp'])) + except requests.exceptions.ConnectionError as err: + irc.error("Connection failed.") + except requests.exceptions.HTTPError as err: + irc.error(err) + else: + irc.reply("Thou shall not pass.") + print("Intrusion attempt: " + hostmask) - if hostmask in read or hostmask in write: + #Write-Functions: Create/Delete + elif 'create' in variant: + if hostmask in write: + URL = server + api + '/add/mailbox/' + id + user = id.split('@')[0] + domain = id.split('@')[1] + random = secrets.token_urlsafe(64) + payload = { + "active": "1", + "domain": domain, + "local_part": user, + "name": user + '@' + domain, + "password": random, + "password2": random, + "quota": "512", + "force_pw_update": "1", + "tls_enforce_in": "1", + "tls_enforce_out": "1" + } + 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'] + msg = data[0]['msg'] - if 'key' in variant: - irc.reply('> ' + api_key + ' <') - - elif 'server' in variant: - irc.reply('> ' + server + ' <') - - elif 'user' in variant: - u = ircdb.users.getUser(msg.prefix) - nick = msg.nick - hm = irc.state.nickToHostmask(nick) - print(hm) - irc.reply('> ' + u.name + ' ' + hm + ' <') + irc.reply(f"CREATION: {status} | {msg} | " + str(random) + server) + else: + irc.reply("Thou shall not create.") + print("Intrusion attempt: " + hostmask) - elif 'read' in variant: - irc.reply('> ' + str(read) + ' <') + elif 'delete' in variant: + if hostmask in write: + URL = server + api + '/delete/mailbox/' + id + 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'] - elif 'write' in variant: - irc.reply('> ' + str(write) + ' <') + irc.reply(f"DELETION: {status} - {msg}") + else: + irc.reply("Thou shall not delete.") + print("Intrusion attempt: " + hostmask) + + else: + irc.reply('Unknown function.') + except KeyError as err: + key = err.args[0] + if key == 'username': + irc.error("Invalid mailbox.") else: - irc.reply('What?') - else: - irc.reply('Go home') + irc.error("Unhandled " + str(err)) - mcdebug = wrap(mcdebug, ['anything']) - #################### + mailbox = wrap(mailbox, ['anything', 'anything']) - def mailbox(self, irc, msg, args, variant, id): - """<option> <id> - i.e. 'summary cranberrry@liberta.casa' will print some details about his mailbox""" + ############################################################ + #FOR DEBUGGING / REMOVE BEFORE RELEASE (or don't, developers hostmask is hardcoded) + ############################################################ + def mcdebug(self, irc, msg, args, variant): + """Prints values. If you were supposed to use this, you would know how.""" server = self.registryValue('api.server') api_key = self.registryValue('api.key') @@ -197,78 +266,37 @@ class Mailcow(callbacks.Plugin): nick = msg.nick hostmask = irc.state.nickToHostmask(msg.nick) - #Read-Functions: Summary - if 'summary' in variant: - if hostmask in read or write: - URL = server + get + '/mailbox/' + id - response = requests.get( - URL, - params={'q': 'requests'}, - headers={'accept': 'application/json', 'X-API-Key': api_key}, - ) - data = response.json() - #return(print(data)) - irc.reply('Username: ' + data['username'] + ' | Quota: ' + str(data['quota']) + ' | Messages: ' + str(data['messages']) + ' | Mail Active: ' + str(data['active']) + ' | XMPP Active: ' + str(data['domain_xmpp'])) - 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/mailbox/' + id - user = id.split('@')[0] - domain = id.split('@')[1] - random = secrets.token_urlsafe(64) - payload = { - "active": "1", - "domain": domain, - "local_part": user, - "name": user + '@' + domain, - "password": random, - "password2": random, - "quota": "512", - "force_pw_update": "1", - "tls_enforce_in": "1", - "tls_enforce_out": "1" - } - 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'] - msg = data[0]['msg'] + if hostmask in read or hostmask in write: + if hostmask == 'cranberry!~u@cranberry.juice': + if 'key' in variant: + irc.reply('> ' + api_key + ' <') + + elif 'server' in variant: + irc.reply('> ' + server + ' <') + + elif 'user' in variant: + u = ircdb.users.getUser(msg.prefix) + nick = msg.nick + hm = irc.state.nickToHostmask(nick) + print(hm) + irc.reply('> ' + u.name + ' ' + hm + ' <') - irc.reply(f"CREATION: {status} | {msg} | " + str(random) + server) - else: - irc.reply("Thou shall not create.") - print("Intrusion attempt: " + hostmask) + elif 'read' in variant: + irc.reply('> ' + str(read) + ' <') - elif 'delete' in variant: - if hostmask in write: - URL = server + api + '/delete/mailbox/' + id - 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'] + elif 'write' in variant: + irc.reply('> ' + str(write) + ' <') - irc.reply(f"DELETION: {status} - {msg}") + else: + irc.reply("What?") else: - irc.reply("Thou shall not delete.") - print("Intrusion attempt: " + hostmask) + irc.error("You're not a dev. Firstly, install Arch Linux.") else: - irc.reply('Unknown function.') + irc.reply("Go home") - mailbox = wrap(mailbox, ['anything', 'anything']) + mcdebug = wrap(mcdebug, ['anything']) + ############################################################ Class = Mailcow |
