-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bot-refactored' into prune-thread
- Loading branch information
Showing
8 changed files
with
306 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,18 +14,29 @@ if [[ $EUID -ne 0 ]]; then | |
exit 1 | ||
fi | ||
|
||
set -xe | ||
|
||
# modify the following variables | ||
export MAIL_DOMAIN=example.org | ||
|
||
export VMAIL_USER=vmail | ||
export VMAIL_HOME=/home/vmail | ||
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" | ||
# load variables from config file | ||
if [ ! -f $SCRIPTPATH/.env ]; then | ||
echo "export MAIL_DOMAIN=example.org" > $SCRIPTPATH/.env | ||
echo "" >> $SCRIPTPATH/.env | ||
echo "export VMAIL_USER=vmail" >> $SCRIPTPATH/.env | ||
echo "export VMAIL_HOME=/home/vmail" >> $SCRIPTPATH/.env | ||
echo "export MAILADM_USER=mailadm" >> $SCRIPTPATH/.env | ||
echo "export MAILADM_HOME=/var/lib/mailadm" >> $SCRIPTPATH/.env | ||
echo "" >> $SCRIPTPATH/.env | ||
echo "export WEB_ENDPOINT=https://example.org/new_email" >> $SCRIPTPATH/.env | ||
echo "export LOCALHOST_WEB_PORT=3691" >> $SCRIPTPATH/.env | ||
echo "" >> $SCRIPTPATH/.env | ||
echo "export [email protected]" >> $SCRIPTPATH/.env | ||
echo "export BOT_PASSWORD=p4ssw0rd" >> $SCRIPTPATH/.env | ||
|
||
echo "Can't get settings from $SCRIPTPATH/.env, please set environment variables there." | ||
exit 1 | ||
else | ||
. ./.env | ||
fi | ||
|
||
export MAILADM_USER=mailadm | ||
export MAILADM_HOME=/var/lib/mailadm | ||
export WEB_ENDPOINT=https://example.org/new_email | ||
export LOCALHOST_WEB_PORT=3691 | ||
set -xe | ||
|
||
# check if vmail user exists | ||
if ! getent passwd $VMAIL_USER > /dev/null 2>&1; then | ||
|
@@ -65,7 +76,15 @@ $MAILADM_HOME/venv/bin/mailadm gen-sysconfig \ | |
--localhost-web-port=$LOCALHOST_WEB_PORT \ | ||
--mailadm-user $MAILADM_USER | ||
|
||
|
||
systemctl daemon-reload | ||
systemctl enable mailadm-web mailadm-prune | ||
systemctl restart mailadm-web mailadm-prune | ||
$MAILADM_HOME/venv/bin/mailadm setup-bot \ | ||
--email $BOT_EMAIL \ | ||
--password $BOT_PASSWORD \ | ||
--db $MAILADM_HOME/admbot.sqlite | ||
|
||
# don't enable services if running in docker | ||
if [ $(cat /proc/1/comm) = "systemd" ] | ||
then | ||
systemctl daemon-reload | ||
systemctl enable mailadm-web mailadm-prune | ||
systemctl restart mailadm-web mailadm-prune | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import deltachat | ||
from deltachat import account_hookimpl, run_cmdline | ||
from mailadm.db import DB | ||
from mailadm.commands import add_user, add_token, list_tokens | ||
import os | ||
from threading import Event | ||
|
||
|
||
class SetupPlugin: | ||
def __init__(self, admingrpid): | ||
self.member_added = Event() | ||
self.admingrpid = admingrpid | ||
self.message_sent = Event() | ||
|
||
@account_hookimpl | ||
def ac_member_added(self, chat: deltachat.Chat, contact, actor, message): | ||
assert chat.num_contacts() == 2 | ||
if chat.id == self.admingrpid: | ||
self.member_added.set() | ||
|
||
@account_hookimpl | ||
def ac_message_delivered(self, message: deltachat.Message): | ||
if not message.is_system_message(): | ||
self.message_sent.set() | ||
|
||
|
||
class AdmBot: | ||
def __init__(self, db): | ||
self.db = db | ||
with self.db.read_connection() as conn: | ||
config = conn.config() | ||
self.admingrpid = config.admingrpid | ||
|
||
@account_hookimpl | ||
def ac_incoming_message(self, command: deltachat.Message): | ||
print("process_incoming message:", command) | ||
command.create_chat() | ||
if not self.check_privileges(command): | ||
command.chat.send_text("Sorry, I only take commands from the admin group.") | ||
|
||
if command.text.strip() == "/help": | ||
text = ("/add-token name expiry prefix token maxuse" | ||
"/add-user addr password token" | ||
"/list-tokens") | ||
command.chat.send_text(text) | ||
|
||
elif command.text.strip() == "/add-token": | ||
arguments = command.text.split(" ") | ||
text = add_token(self.db, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]) | ||
command.chat.send_text(text) | ||
|
||
elif command.text.strip() == "/add-user": | ||
arguments = command.text.split(" ") | ||
text = add_user(self.db, arguments[0], arguments[1], arguments[2]) | ||
command.chat.send_text(text) | ||
|
||
elif command.text.strip() == "/list-tokens": | ||
command.chat.send_text(list_tokens(self.db)) | ||
|
||
def check_privileges(self, command): | ||
""" | ||
Checks whether the incoming message was in the admin group. | ||
""" | ||
if command.chat.is_group() and self.admingrpid == command.chat.id: | ||
if command.chat.is_protected() and command.chat.is_encrypted() and int(command.chat.num_contacts) >= 2: | ||
if command.message.get_sender_contact() in command.chat.get_contacts(): | ||
return True | ||
else: | ||
print("%s is not allowed to give commands to mailadm." % (command.message.get_sender_contact(),)) | ||
else: | ||
print("admin chat is broken. Try `mailadm setup-bot`. Group ID:" + self.admingrpid) | ||
raise ValueError | ||
else: | ||
# reply "This command needs to be sent to the admin group" | ||
return False | ||
|
||
|
||
def main(db, argv=None): | ||
run_cmdline(argv=argv, account_plugins=[AdmBot(db)]) | ||
|
||
|
||
if __name__ == "__main__": | ||
db = DB(os.getenv("MAILADM_DB")) | ||
main(db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.