1

Add help, debug log, fix pid file creation

+ Add help in group chat
+ Add debug log ability
* A bit of cleanup
* Check against start uid rather than effective uid when creating pid-file
This commit is contained in:
eleksir
2020-09-06 20:44:54 +03:00
parent f7ecdb870f
commit 20bbfab333
4 changed files with 59 additions and 14 deletions

View File

@@ -18,17 +18,17 @@ BEGIN {
use lib ("$workdir/lib", "$workdir/vendor_perl", "$workdir/vendor_perl/lib/perl5"); use lib ("$workdir/lib", "$workdir/vendor_perl", "$workdir/vendor_perl/lib/perl5");
use POSIX qw(setgid setuid setsid); use POSIX qw(setgid setuid setsid);
use Fcntl qw(O_WRONLY O_TRUNC O_CREAT);
use File::Path qw(make_path); use File::Path qw(make_path);
use File::Basename qw(basename); use File::Basename qw(basename dirname);
use telegrambot; use conf qw(loadConf);
use conf; use botlib qw(logger);
use telegrambot qw(run_telegrambot);
sub daemonize (); sub daemonize ();
sub __fork (); sub __fork ();
sub __mkpidfile ();
$| = 1; $| = 1;
my $runAsUid = $<;
my $piddir = "/var/run/" . basename (abs_path ($0)); my $piddir = "/var/run/" . basename (abs_path ($0));
my $C = loadConf (); my $C = loadConf ();
@@ -48,6 +48,17 @@ if ($< == 0) {
); );
} }
if ($C->{'debug_log'}) {
unless (-d dirname($C->{'debug_log'})) {
make_path (
dirname($C->{'debug_log'}), {
uid => $uid,
group => $gid
}
);
}
}
# script intended to run as "unit" user, so drop privs to "unit" user # script intended to run as "unit" user, so drop privs to "unit" user
setgid ($gid) or die "Unable to switch to group of $user($!)\n"; setgid ($gid) or die "Unable to switch to group of $user($!)\n";
setuid ($uid) or die "Unable to switch to account $user($!)\n"; setuid ($uid) or die "Unable to switch to account $user($!)\n";
@@ -59,11 +70,12 @@ daemonize ();
# write pidfile # write pidfile
my $pidfile; my $pidfile;
if ($< == 0) { if ($runAsUid == 0) {
# TODO: handle case where we run with unpriv. user # TODO: handle case where we run with unpriv. user
$pidfile = sprintf ("%s/%s.pid", $piddir, basename (abs_path ($0))); $pidfile = sprintf ("%s/%s.pid", $piddir, basename (abs_path ($0)));
sysopen (PID, $pidfile, O_WRONLY|O_TRUNC|O_CREAT) or die "Error opening $pidfile: $!\n"; logger ("Creating pidfile at $pidfile");
syswrite PID, $$ or die "Error writing to $pidfile: $!\n"; open (PID, ">", $pidfile) or die "Error opening $pidfile: $!\n";
print PID $$ or die "Error writing to $pidfile: $!\n";
close PID; close PID;
} }
@@ -72,7 +84,6 @@ $0 = abs_path ($0);
# run bot, at last # run bot, at last
run_telegrambot (); run_telegrambot ();
unlink ($pidfile) if (-f $pidfile); unlink ($pidfile) if (-f $pidfile);
exit 0; exit 0;
@@ -92,6 +103,5 @@ sub __fork () {
exit if ($pid != 0); exit if ($pid != 0);
} }
__END__ __END__
# vim: set ft=perl noet ai ts=4 sw=4 sts=4: # vim: set ft=perl noet ai ts=4 sw=4 sts=4:

View File

@@ -11,5 +11,6 @@
"cachetime": "data/cache/openweathermap.Id-Timestamp.db", "cachetime": "data/cache/openweathermap.Id-Timestamp.db",
"cachedata": "data/cache/openweathermap.Id-Data.db", "cachedata": "data/cache/openweathermap.Id-Data.db",
"appid": "get_it_on_openweathermap.org" "appid": "get_it_on_openweathermap.org"
} },
"debug_log" : "/var/log/aleesa-telegram-bot/debug.log"
} }

View File

@@ -11,12 +11,12 @@ use JSON::XS qw(decode_json encode_json);
use Digest::MD5 qw(md5_base64); use Digest::MD5 qw(md5_base64);
use DB_File; use DB_File;
use conf; use conf qw(loadConf);
use vars qw/$VERSION/; use vars qw/$VERSION/;
use Exporter qw(import); use Exporter qw(import);
our @EXPORT = qw(weather); our @EXPORT = qw(weather logger);
$VERSION = "1.0"; $VERSION = "1.0";
@@ -188,6 +188,19 @@ sub __urlencode($) {
return $str; return $str;
} }
sub logger {
my $msg = shift;
if ($c->{debug_log}) {
my $mode = '>';
$mode = '>>' if (-f $c->{debug_log});
if (open (LOG, $mode, $c->{debug_log})) {
print LOG $msg . "\n";
close LOG;
}
}
}
1; 1;

View File

@@ -5,6 +5,7 @@ use warnings "all";
use vars qw/$VERSION/; use vars qw/$VERSION/;
use v5.10.0; use v5.10.0;
use utf8; use utf8;
use Data::Dumper;
use open qw(:std :utf8); use open qw(:std :utf8);
use File::Path qw( mkpath ); use File::Path qw( mkpath );
use Hailo; use Hailo;
@@ -32,6 +33,13 @@ sub __cron {
sub __on_msg { sub __on_msg {
my ($self, $msg) = @_; my ($self, $msg) = @_;
if ($msg->{'new_chat_members'}) {
# we have newcommers here
$msg->reply("Дратути. Представьтес, пожалуйста, и расскажите, что вас сюда привело.");
return;
}
# lazy init chat-bot brains # lazy init chat-bot brains
unless (defined($hailo->{$msg->chat->id})) { unless (defined($hailo->{$msg->chat->id})) {
$hailo->{$msg->chat->id} = Hailo->new( $hailo->{$msg->chat->id} = Hailo->new(
@@ -65,6 +73,7 @@ sub __on_msg {
if (defined($reply) && $reply ne '') { if (defined($reply) && $reply ne '') {
$msg->reply($reply); $msg->reply($reply);
logger ("private chat reply: $reply");
} else { } else {
# if we have no answer, say something default in private chat # if we have no answer, say something default in private chat
$msg->reply("Давайте ещё пообщаемся, а то я ещё не научилась от вас плохому."); $msg->reply("Давайте ещё пообщаемся, а то я ещё не научилась от вас плохому.");
@@ -83,7 +92,16 @@ sub __on_msg {
# simple commands # simple commands
if (substr($text, 0, 1) eq $c->{telegrambot}->{csign}) { if (substr($text, 0, 1) eq $c->{telegrambot}->{csign}) {
if (substr($text, 1) eq "ping") { if (substr($text, 1) eq "help") {
$reply = '```
!help | !помощь - список команд
!w город | !п город - погода в указанном городе
!ping | !пинг - попинговать бота
```
Но на самом деле я бот больше для общения, чем для исполнения команд.
Поговоришь со мной?
';
} elsif (substr($text, 1) eq "ping") {
$reply = "Pong."; $reply = "Pong.";
} elsif (substr($text, 1) eq "пинг") { } elsif (substr($text, 1) eq "пинг") {
$reply = "Понг."; $reply = "Понг.";
@@ -119,7 +137,10 @@ sub __on_msg {
} }
if (defined($reply) && $reply ne '') { if (defined($reply) && $reply ne '') {
logger ("reply with: $reply");
$msg->reply($reply); $msg->reply($reply);
} else {
logger ("no reply");
} }
# should be channel, so we can't talk # should be channel, so we can't talk
} else { } else {