From 99a8a3420f63e1c43b2c663c84ed3d171f00be6b Mon Sep 17 00:00:00 2001 From: Michael De Roover Date: Sun, 2 Jan 2022 04:23:43 +0100 Subject: [PATCH] Add init system integration --- log.py => bin/log.py | 12 ++++---- setup.py => bin/setup.py | 6 ++-- init/openrc | 7 +++++ init/systemd | 12 ++++++++ install | 63 +++++++++++++++++++++++++++++++++++----- 5 files changed, 86 insertions(+), 14 deletions(-) rename log.py => bin/log.py (78%) rename setup.py => bin/setup.py (63%) create mode 100644 init/openrc create mode 100644 init/systemd diff --git a/log.py b/bin/log.py similarity index 78% rename from log.py rename to bin/log.py index b3a7c92..d4b81f1 100755 --- a/log.py +++ b/bin/log.py @@ -1,8 +1,10 @@ #!/usr/bin/python3 +import sys +sys.path.append("/var/telelog") from telethon import TelegramClient, events, functions, types from config import * -client = TelegramClient('telelog', api_id, api_hash) +client = TelegramClient('/var/telelog/log', api_id, api_hash) @client.on(events.NewMessage) async def log(event): @@ -10,7 +12,7 @@ async def log(event): if event.sender_id != myid: #Incoming message try: - f = open(f'logs/{event.chat_id}') + f = open(f'/var/telelog/logs/{event.chat_id}') except IOError: chat = await event.get_input_chat() rpl = "This account does not accept private messages. " @@ -18,11 +20,11 @@ async def log(event): rpl += "Please reach out to me in a common group if applicable. " await event.respond(rpl) await client(functions.contacts.BlockRequest(chat)) - f = open(f'logs/{event.chat_id}', "a") + f = open(f'/var/telelog/logs/{event.chat_id}', "a") f.write("← ") else: #Outgoing message - f = open(f'logs/{event.chat_id}', "a") + f = open(f'/var/telelog/logs/{event.chat_id}', "a") f.write("→ ") f.write(event.raw_text + "\n") f.close() @@ -44,7 +46,7 @@ async def log(event): rpl += os.popen("ls logs").read() await event.edit(rpl) else: - chat = f'logs/{arr[1]}' + chat = f'/var/telelog/logs/{arr[1]}' f = open(chat) await event.edit(f.read()) diff --git a/setup.py b/bin/setup.py similarity index 63% rename from setup.py rename to bin/setup.py index 7670034..27c93f3 100644 --- a/setup.py +++ b/bin/setup.py @@ -1,13 +1,15 @@ #!/usr/bin/python3 +import sys +sys.path.append("/var/telelog") from telethon.sync import TelegramClient from config import * from telethon.tl.types import User -with TelegramClient('telelog', api_id, api_hash) as client: +with TelegramClient('/var/telelog/log', api_id, api_hash) as client: for dialog in client.iter_dialogs(): entity = dialog.entity if isinstance(entity, User): - f = open(f'logs/{dialog.id}', "a") + f = open(f'/var/telelog/logs/{dialog.id}', "a") f.write("Message history for " + str(dialog.id) + ":\n") f.close() diff --git a/init/openrc b/init/openrc new file mode 100644 index 0000000..b30bdbe --- /dev/null +++ b/init/openrc @@ -0,0 +1,7 @@ +#!/sbin/openrc-run + +description="Telegram logger" +command="/usr/bin/telelog" +command_user="log:log" +command_background=true +pidfile="/run/ubot.pid" diff --git a/init/systemd b/init/systemd new file mode 100644 index 0000000..06a03d6 --- /dev/null +++ b/init/systemd @@ -0,0 +1,12 @@ +[Unit] +Description=Telegram logger + +[Service] +Type=simple +User=log +ExecStart=/usr/bin/telelog +Restart=always +RestartSec=0 + +[Install] +WantedBy=multi-user.target diff --git a/install b/install index 738984b..0afef3f 100755 --- a/install +++ b/install @@ -1,5 +1,9 @@ #!/bin/sh +# Preliminary sanity checks +[ $(id -u) != 0 ] && echo "Please run this installer as root." && exit 1 +[ $0 != ./install ] && echo "Not launched from repository root." && exit 1 + # Functions mkconfig(){ echo "Please get your credentials from https://my.telegram.org." @@ -13,13 +17,58 @@ mkconfig(){ echo "myid = $myid" >> config.py } -# Preliminary sanity checks -[ $(id -u) = 0 ] && echo "Please do not run as root." && exit 1 -[ ! -f log.py ] && echo "log.py not found, quitting." && exit 1 -[ $0 != ./install ] && echo "Not launched from current directory." && exit 1 +mkuser(){ + # Create user account, distro mess awaits here... + # Debian has both useradd and adduser + if [ -e /sbin/useradd ] + then + useradd -r telelog + return 0 + fi + # Alpine only has adduser (Busybox) + if [ -e /sbin/adduser ] + then + adduser -S telelog + return 0 + fi +} +addfiles(){ + # Install executable and config + install -dm770 -o root -g telelog /var/telelog + install -m640 -o root -g telelog config.py /var/ubot/config.py + install -m755 -o root -g root bin/telelog /usr/bin/telelog + + # Install service files on installed inits + if [ -e /sbin/openrc ] + then + install -m755 init/openrc /etc/init.d/telelog + ln -s /sbin/openrc /etc/periodic/15min/openrc + rc-update add telelog default + elif [ -e /lib/systemd/systemd ] + then + install -m644 init/systemd /etc/systemd/system/telelog.service + systemctl enable telelog + fi +} + +# Install dependencies +[ $(which apt) ] && apt install python3-pip +[ $(which apk) ] && apk add py3-pip +pip3 install -U telethon +# Generate config if not built yet [ ! -f config.py ] && mkconfig -pip3 install -U telethon --user -mkdir logs -[ -f setup.py ] && python3 setup.py +# Create a new user +mkuser +# Install files if config is (now) present +[ -f config.py ] && addfiles + +# Run Telethon for the first time +echo "Please log in to Telegram now." +umask 0077 +[ -f setup.py ] && su bot -s /usr/bin/python3 $PWD/setup.py + +# Start the service +service telelog start + return 0