From d1c90cbcd973e446091728d46ad8ae7a91c0d10c Mon Sep 17 00:00:00 2001 From: Michael De Roover Date: Thu, 30 Dec 2021 14:29:00 +0100 Subject: [PATCH] Add init system integration --- ubot.py => bin/ubot | 44 ++++++++++++++++++++++++++++++++++++++++++++ install | 45 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 83 insertions(+), 6 deletions(-) rename ubot.py => bin/ubot (78%) diff --git a/ubot.py b/bin/ubot similarity index 78% rename from ubot.py rename to bin/ubot index 118396f..2ee894a 100755 --- a/ubot.py +++ b/bin/ubot @@ -144,6 +144,49 @@ async def edit(event): rpl += f'{outmsg}\n' await event.edit(rpl) + if msg.startswith('.cv'): + # Date multiplier + multi = str(msg.split(' ', 2)[1]) + # Hour and minutes + time = str(msg.split(' ', 2)[2]) + hour = str(time.split(':', 1)[0]) + mins = str(time.split(':', 1)[1]) + await event.edit(multi + " " + hour + mins) + + async def schedule(multi, hour, mins, msg): + print(multi + hour + mins + msg) + + import datetime as dt + # Current time + ctime = dt.datetime.now() + # Target date + tdate = ctime.day + int(multi) + tmonth = ctime.month + tyear = ctime.year + # Target time + ttime = dt.datetime(tyear, tdate, tmonth, hour, mins, 00) + # Date multiplier in minutes + mmins = multi * 60 * 24 + # Minutes passed now since midnight + nmins = ctime.hour * 60 + ctime.minute + # Minutes passed at target time since midnight + tmins = mmins = ttime.hour * 60 + ttime.minute + # Calculate delta between now and target time + delta = int(tmins) - int(nmins) + await client.send_message(logchat, delta) + + #for i in range(0,multi): + await schedule(multi, hour, mins, "test message") + + #for i in range(0, + + #day = + #import datetime as dt + #ctime = dt.datetime.now() + + #dmins = day * 60 * 24 + #nmins = ctime.our * 60 + ctime.minute + if msg.startswith('.sauce'): rpl = 'Sauce: [ghnou/ubot](https://git.nixmagic.com/ghnou/ubot)' await event.edit(rpl) @@ -159,6 +202,7 @@ async def edit(event): rpl += '.sys - Show system information.\n' rpl += '.spam - Spam messages.. yea.\n' rpl += '.re - s/foo/bar/\n' + rpl += '.cv - Schedule /cv commands in Konata.\n' rpl += '.sauce - Get the bot\'s source code.\n' rpl += '.help - Show this help message.\n' await event.edit(rpl) diff --git a/install b/install index cd29e58..59215a0 100755 --- a/install +++ b/install @@ -1,5 +1,9 @@ #!/bin/sh +# Preliminary sanity checks +[ $(id -u) = 0 ] && echo "Please do not run 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." @@ -19,13 +23,42 @@ mkconfig(){ echo "logchat = $logchat" >> config.py } -# Preliminary sanity checks -[ $(id -u) = 0 ] && echo "Please do not run as root." && exit 1 -[ ! -f ubot.py ] && echo "ubot.py not found, quitting." && exit 1 -[ $0 != ./install ] && echo "Not launched from current directory." && exit 1 +addfiles(){ + # Create user account, distro mess awaits here... + # Debian has both useradd and adduser + if [ -e /sbin/useradd ] + then + sudo useradd -r bot + return 0 + fi + # Alpine only has adduser (Busybox) + if [ -e /sbin/adduser ] + then + sudo adduser -S bot + return 0 + fi + # Install executable and config + sudo mkdir /etc/ubot + sudo install -o root -g bot -m640 config.py /etc/ubot/config.py + sudo install -m755 bin/ubot /usr/bin/ubot + + # Install service files on installed inits + if [ -e /sbin/openrc ] + then + sudo install -m755 init/openrc /etc/init.d/ubot + sudo ln -s /sbin/openrc /etc/periodic/15min/openrc + elif [ -e /lib/systemd/systemd ] + then + sudo install -m644 init/systemd /etc/systemd/system/ubot.service + fi +} + +# Install dependencies +sudo pip3 install -U telethon +# Generate config if not built yet [ ! -f config.py ] && mkconfig -git pull -pip3 install -U telethon --user +# Install files if config is (now) present +[ -f config.py ] && addfiles return 0