From cc4ef9b16bcc694bdf536adae00169f7dfb9507a Mon Sep 17 00:00:00 2001 From: Georg Date: Sat, 22 May 2021 03:13:53 +0200 Subject: [PATCH] Initial --- README.md | 1 + __init__.py | 71 +++++++++ __pycache__/__init__.cpython-38.pyc | Bin 0 -> 621 bytes __pycache__/config.cpython-38.pyc | Bin 0 -> 689 bytes __pycache__/plugin.cpython-38.pyc | Bin 0 -> 3738 bytes config.py | 56 +++++++ local/__init__.py | 1 + plugin.py | 193 +++++++++++++++++++++++ response.py | 0 test-conf/test.conf | 18 +++ test-data/web/default.css.example | 55 +++++++ test-data/web/generic/error.html.example | 12 ++ test-data/web/index.html.example | 14 ++ test-data/web/robots.txt.example | 0 test-logs/messages.log | 33 ++++ test.py | 40 +++++ 16 files changed, 494 insertions(+) create mode 100644 README.md create mode 100644 __init__.py create mode 100644 __pycache__/__init__.cpython-38.pyc create mode 100644 __pycache__/config.cpython-38.pyc create mode 100644 __pycache__/plugin.cpython-38.pyc create mode 100644 config.py create mode 100644 local/__init__.py create mode 100644 plugin.py create mode 100644 response.py create mode 100644 test-conf/test.conf create mode 100644 test-data/web/default.css.example create mode 100644 test-data/web/generic/error.html.example create mode 100644 test-data/web/index.html.example create mode 100644 test-data/web/robots.txt.example create mode 100644 test-logs/messages.log create mode 100644 test.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..d0beb18 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Mailcow API through IRC diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..0bb69fd --- /dev/null +++ b/__init__.py @@ -0,0 +1,71 @@ +### +# Copyright (c) 2021, Georg Pfuetzenreuter +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written consent. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +### + +""" +Mailcow: Mailcow API through IRC +""" + +import sys +import supybot +from supybot import world + +# Use this for the version of this plugin. +__version__ = "v1" + +# XXX Replace this with an appropriate author or supybot.Author instance. +__author__ = supybot.authors.unknown + +# This is a dictionary mapping supybot.Author instances to lists of +# contributions. +__contributors__ = {} + +# This is a url where the most recent plugin package can be downloaded. +__url__ = '' + +from . import config +from . import plugin +if sys.version_info >= (3, 4): + from importlib import reload +else: + from imp import reload +# In case we're being reloaded. +reload(config) +reload(plugin) +# Add more reloads here if you add third-party modules and want them to be +# reloaded when this plugin is reloaded. Don't forget to import them as well! + +if world.testing: + from . import test + +Class = plugin.Class +configure = config.configure + + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/__pycache__/__init__.cpython-38.pyc b/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e28c4a2cc4d315dadf5f4eb9edbf7b21882fd0a4 GIT binary patch literal 621 zcmYjOy^a$x5VpO)*>x_*-OO2lQb#I6k@qZ*hr)z(?wgd68Kc)5K~4mlDHX*(M{q6C=~jn zov;Z;hVIen0m+Fh?=jR)B;EU{dEmZ+N(m}w>JlOy7REn)WMahk7-=`gmFFU>5X#hJCWJP76kEUwE{`kc3>iwB}mXAzJI9R%S+a z5m3AM$UCX+TmT>Uvg>CLea5-4OU`}NZ9AWJ!+N{0y$et0OU`el>y$OXXY-+7SqC+P zxiNNQe8Kr42ka^5(7JbOG4xOr>Ny{r=G=w;E(s&;qDX=MMOh~aB~(NMHEF!MZpOvL4X@@yok0xn|iUqX@a+Dd&KudPPRlZ1J3f9QA17>hAT~DXMv5x z*;-V+O-eQPr&856H8|)LdZW@ra-7BXjv3bDN!Ksm55kZ%M&HIydPXoOSP*y!b}?xx z^rUANVtf9fRF&ZWE&45!PN+PcWEix_Fm<}Ja2NDl%vM=y{6uLI p#ljm`pm#8FTl{&ADRNxNUfc1Z5&IADH$yAzfCn=?k-zYh^e1{~mh=Ds literal 0 HcmV?d00001 diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3d3c6632729f3916bca7d7ba39dc6aae010578b4 GIT binary patch literal 3738 zcmbW4OK;rB5rFv~4(BD=vi0zb_HJy95zXjf@7ju^l`Y9$Wi46p%84@wz@?^JBT5{S zZuUqTVzdF`y&CX+ZNR<+@GUXEFC03k8O>Ib{3tBYUv zJSmkd39cXi{-LPGO4^!8>b&gWPs@3Nkpf)t)b^?;|x91 zI7{TOrRv~&@LF1_>O${u&yPhe@;farh=u8~X1^-mbbWu_ZEnXxhf1$yYaTLnr`H-d%nlJ;*{3{_vr%N za63M~>-!KD^zh_`!I*J;U_eOgw?VCN57^BjD9-cXPTJSXCS?I}cbdvAo zzn6&Bvp^Pl1(3zpId1UWa~S`vEK4utz$7KK`ch^F*fqg!8EujZWb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dXXTWI`XvUVYaRtRA6i27$KJ;?gtiFY|_!|(%#4}_Q`K9~@$Tc9B;kJ}M z?|^K__dkr{e-TZ&if?>~b;^jER4{+w%%8FjvUa7K6Q+|M5*$ZZj?)fF$ETnd9p_oc z^>NXmlye*sHXVnpf|EkqWp2b;pq`jfIyYo_db-7Bjj_sqOo4kTV6}R8inTo9QUJ1Xofi7M59$4(Nx|QjerRSd!C3JX5dH1w bJZv!vY$Qdwtd + i.e. 'get liberta.casa' will print infos about the respective MX zone""" + + if 'summary' in variant: + 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} " + str(capability)) + + elif 'create' in variant: + 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}") + + elif 'delete' in variant: + 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("Unknown option.") + maildomain = wrap(maildomain, ['anything', 'anything']) + + def mailbox(self, irc, msg, args, variant, id): + """ + Modifies Mailboxes.""" + + if 'summary' in variant: + 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'])) + + elif 'create' in variant: + 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'] + + irc.reply(f"CREATION: {status} | {msg} | " + str(random) + server) + + elif 'delete' in variant: + 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'] + + irc.reply(f"DELETION: {status} - {msg}") + + else: + irc.reply('Unknown function.') + + mailbox = wrap(mailbox, ['anything', 'anything']) + +Class = Mailcow + + +# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/response.py b/response.py new file mode 100644 index 0000000..e69de29 diff --git a/test-conf/test.conf b/test-conf/test.conf new file mode 100644 index 0000000..6ae02fd --- /dev/null +++ b/test-conf/test.conf @@ -0,0 +1,18 @@ + +supybot.directories.data: /home/georg/limnoria/Mailcow/test-data +supybot.directories.conf: /home/georg/limnoria/Mailcow/test-conf +supybot.directories.log: /home/georg/limnoria/Mailcow/test-logs +supybot.reply.whenNotCommand: True +supybot.log.stdout: False +supybot.log.stdout.level: ERROR +supybot.log.level: DEBUG +supybot.log.format: %(levelname)s %(message)s +supybot.log.plugins.individualLogfiles: False +supybot.protocols.irc.throttleTime: 0 +supybot.reply.whenAddressedBy.chars: @ +supybot.networks.test.server: should.not.need.this +supybot.networks.testnet1.server: should.not.need.this +supybot.networks.testnet2.server: should.not.need.this +supybot.networks.testnet3.server: should.not.need.this +supybot.nick: test +supybot.databases.users.allowUnregistration: True diff --git a/test-data/web/default.css.example b/test-data/web/default.css.example new file mode 100644 index 0000000..09219ad --- /dev/null +++ b/test-data/web/default.css.example @@ -0,0 +1,55 @@ +body { + background-color: #F0F0F0; +} + +/************************************ + * Classes that plugins should use. * + ************************************/ + +/* Error pages */ +body.error { + text-align: center; +} +body.error p { + background-color: #FFE0E0; + border: 1px #FFA0A0 solid; +} + +/* Pages that only contain a list. */ +.purelisting { + text-align: center; +} +.purelisting ul { + margin: 0; + padding: 0; +} +.purelisting ul li { + margin: 0; + padding: 0; + list-style-type: none; +} + +/* Pages that only contain a table. */ +.puretable { + text-align: center; +} +.puretable table +{ + width: 100%; + border-collapse: collapse; + text-align: center; +} + +.puretable table th +{ + /*color: #039;*/ + padding: 10px 8px; + border-bottom: 2px solid #6678b1; +} + +.puretable table td +{ + padding: 9px 8px 0px 8px; + border-bottom: 1px solid #ccc; +} + diff --git a/test-data/web/generic/error.html.example b/test-data/web/generic/error.html.example new file mode 100644 index 0000000..9c7162a --- /dev/null +++ b/test-data/web/generic/error.html.example @@ -0,0 +1,12 @@ + + + + + %(title)s + + + +

Error

+

%(error)s

+ + \ No newline at end of file diff --git a/test-data/web/index.html.example b/test-data/web/index.html.example new file mode 100644 index 0000000..ff105c4 --- /dev/null +++ b/test-data/web/index.html.example @@ -0,0 +1,14 @@ + + + + + Supybot Web server index + + + +

Supybot web server index

+

Here is a list of the plugins that have a Web interface: +

+ %(list)s + + \ No newline at end of file diff --git a/test-data/web/robots.txt.example b/test-data/web/robots.txt.example new file mode 100644 index 0000000..e69de29 diff --git a/test-logs/messages.log b/test-logs/messages.log new file mode 100644 index 0000000..30010f8 --- /dev/null +++ b/test-logs/messages.log @@ -0,0 +1,33 @@ +ERROR Invalid user dictionary file, resetting to empty. +ERROR Exact error: FileNotFoundError: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/users.conf' +ERROR Invalid channel database, resetting to empty. +ERROR Exact error: FileNotFoundError: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/channels.conf' +ERROR Invalid network database, resetting to empty. +ERROR Exact error: FileNotFoundError: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/networks.conf' +WARNING Couldn't open ignore database: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/ignores.conf' +INFO Shutdown initiated. +INFO Killing Driver objects. +INFO Killing Irc objects. +INFO Shutdown complete. +ERROR Invalid user dictionary file, resetting to empty. +ERROR Exact error: FileNotFoundError: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/users.conf' +ERROR Invalid channel database, resetting to empty. +ERROR Exact error: FileNotFoundError: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/channels.conf' +ERROR Invalid network database, resetting to empty. +ERROR Exact error: FileNotFoundError: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/networks.conf' +WARNING Couldn't open ignore database: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/ignores.conf' +INFO Shutdown initiated. +INFO Killing Driver objects. +INFO Killing Irc objects. +INFO Shutdown complete. +ERROR Invalid user dictionary file, resetting to empty. +ERROR Exact error: FileNotFoundError: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/users.conf' +ERROR Invalid channel database, resetting to empty. +ERROR Exact error: FileNotFoundError: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/channels.conf' +ERROR Invalid network database, resetting to empty. +ERROR Exact error: FileNotFoundError: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/networks.conf' +WARNING Couldn't open ignore database: [Errno 2] No such file or directory: '/home/georg/limnoria/Mailcow/test-conf/ignores.conf' +INFO Shutdown initiated. +INFO Killing Driver objects. +INFO Killing Irc objects. +INFO Shutdown complete. diff --git a/test.py b/test.py new file mode 100644 index 0000000..edf0b23 --- /dev/null +++ b/test.py @@ -0,0 +1,40 @@ +### +# Copyright (c) 2021, Georg Pfuetzenreuter +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written consent. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +### + +from supybot.test import * + + +class MailcowTestCase(PluginTestCase): + plugins = ('Mailcow',) +def testGet(self): + self.assertNotError('domain') + + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: