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:
@@ -18,17 +18,17 @@ BEGIN {
|
||||
|
||||
use lib ("$workdir/lib", "$workdir/vendor_perl", "$workdir/vendor_perl/lib/perl5");
|
||||
use POSIX qw(setgid setuid setsid);
|
||||
use Fcntl qw(O_WRONLY O_TRUNC O_CREAT);
|
||||
use File::Path qw(make_path);
|
||||
use File::Basename qw(basename);
|
||||
use telegrambot;
|
||||
use conf;
|
||||
use File::Basename qw(basename dirname);
|
||||
use conf qw(loadConf);
|
||||
use botlib qw(logger);
|
||||
use telegrambot qw(run_telegrambot);
|
||||
|
||||
sub daemonize ();
|
||||
sub __fork ();
|
||||
sub __mkpidfile ();
|
||||
|
||||
$| = 1;
|
||||
my $runAsUid = $<;
|
||||
|
||||
my $piddir = "/var/run/" . basename (abs_path ($0));
|
||||
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
|
||||
setgid ($gid) or die "Unable to switch to group of $user($!)\n";
|
||||
setuid ($uid) or die "Unable to switch to account $user($!)\n";
|
||||
@@ -59,11 +70,12 @@ daemonize ();
|
||||
# write pidfile
|
||||
my $pidfile;
|
||||
|
||||
if ($< == 0) {
|
||||
if ($runAsUid == 0) {
|
||||
# TODO: handle case where we run with unpriv. user
|
||||
$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";
|
||||
syswrite PID, $$ or die "Error writing to $pidfile: $!\n";
|
||||
logger ("Creating pidfile at $pidfile");
|
||||
open (PID, ">", $pidfile) or die "Error opening $pidfile: $!\n";
|
||||
print PID $$ or die "Error writing to $pidfile: $!\n";
|
||||
close PID;
|
||||
}
|
||||
|
||||
@@ -72,7 +84,6 @@ $0 = abs_path ($0);
|
||||
|
||||
# run bot, at last
|
||||
run_telegrambot ();
|
||||
|
||||
unlink ($pidfile) if (-f $pidfile);
|
||||
exit 0;
|
||||
|
||||
@@ -92,6 +103,5 @@ sub __fork () {
|
||||
exit if ($pid != 0);
|
||||
}
|
||||
|
||||
|
||||
__END__
|
||||
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
||||
|
||||
@@ -11,5 +11,6 @@
|
||||
"cachetime": "data/cache/openweathermap.Id-Timestamp.db",
|
||||
"cachedata": "data/cache/openweathermap.Id-Data.db",
|
||||
"appid": "get_it_on_openweathermap.org"
|
||||
}
|
||||
},
|
||||
"debug_log" : "/var/log/aleesa-telegram-bot/debug.log"
|
||||
}
|
||||
|
||||
@@ -11,12 +11,12 @@ use JSON::XS qw(decode_json encode_json);
|
||||
use Digest::MD5 qw(md5_base64);
|
||||
use DB_File;
|
||||
|
||||
use conf;
|
||||
use conf qw(loadConf);
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
|
||||
use Exporter qw(import);
|
||||
our @EXPORT = qw(weather);
|
||||
our @EXPORT = qw(weather logger);
|
||||
|
||||
$VERSION = "1.0";
|
||||
|
||||
@@ -188,6 +188,19 @@ sub __urlencode($) {
|
||||
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;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ use warnings "all";
|
||||
use vars qw/$VERSION/;
|
||||
use v5.10.0;
|
||||
use utf8;
|
||||
use Data::Dumper;
|
||||
use open qw(:std :utf8);
|
||||
use File::Path qw( mkpath );
|
||||
use Hailo;
|
||||
@@ -32,6 +33,13 @@ sub __cron {
|
||||
|
||||
sub __on_msg {
|
||||
my ($self, $msg) = @_;
|
||||
|
||||
if ($msg->{'new_chat_members'}) {
|
||||
# we have newcommers here
|
||||
$msg->reply("Дратути. Представьтес, пожалуйста, и расскажите, что вас сюда привело.");
|
||||
return;
|
||||
}
|
||||
|
||||
# lazy init chat-bot brains
|
||||
unless (defined($hailo->{$msg->chat->id})) {
|
||||
$hailo->{$msg->chat->id} = Hailo->new(
|
||||
@@ -65,6 +73,7 @@ sub __on_msg {
|
||||
|
||||
if (defined($reply) && $reply ne '') {
|
||||
$msg->reply($reply);
|
||||
logger ("private chat reply: $reply");
|
||||
} else {
|
||||
# if we have no answer, say something default in private chat
|
||||
$msg->reply("Давайте ещё пообщаемся, а то я ещё не научилась от вас плохому.");
|
||||
@@ -83,7 +92,16 @@ sub __on_msg {
|
||||
|
||||
# simple commands
|
||||
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.";
|
||||
} elsif (substr($text, 1) eq "пинг") {
|
||||
$reply = "Понг.";
|
||||
@@ -119,7 +137,10 @@ sub __on_msg {
|
||||
}
|
||||
|
||||
if (defined($reply) && $reply ne '') {
|
||||
logger ("reply with: $reply");
|
||||
$msg->reply($reply);
|
||||
} else {
|
||||
logger ("no reply");
|
||||
}
|
||||
# should be channel, so we can't talk
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user