Add init system integration

This commit is contained in:
Michael De Roover 2022-01-02 04:23:43 +01:00
parent 018ad977c2
commit 99a8a3420f
Signed by: vim
GPG Key ID: 075496E232CE04CB
5 changed files with 86 additions and 14 deletions

View File

@ -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())

View File

@ -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()

7
init/openrc Normal file
View File

@ -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"

12
init/systemd Normal file
View File

@ -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

63
install
View File

@ -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