Remove desktop notifications, move assist-stuff to separate dir
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package BotLib::Command;
|
||||
package BotLib::Chan::Assist;
|
||||
# Парсит команды
|
||||
|
||||
use 5.030; ## no critic (ProhibitImplicitImport)
|
||||
@@ -13,11 +13,11 @@ use Log::Any qw ($log);
|
||||
|
||||
use BotLib::Conf qw (LoadConf);
|
||||
use BotLib::Util qw (storedata rakedata deletedata timems utf2md5hex utf2sha1hex utf2sha224hex utf2sha256hex
|
||||
utf2sha384hex utf2sha512hex utf2crc32hex utf2murmurhash utf2b64 urlencode irandom);
|
||||
utf2sha384hex utf2sha512hex utf2crc32hex utf2murmurhash utf2b64 urlencode irandom PrintMsg);
|
||||
|
||||
use version; our $VERSION = qw (1.0);
|
||||
use Exporter qw (import);
|
||||
our @EXPORT_OK = qw (Help Notify DelayedNotify PrintMsg Todo B64Str Md5Str Sha1Str Sha224Str Sha256Str Sha384Str
|
||||
our @EXPORT_OK = qw (Help PrintMsg Todo B64Str Md5Str Sha1Str Sha224Str Sha256Str Sha384Str
|
||||
Sha512Str Crc32Str MurmurhashStr UrlencodeStr IrandNum);
|
||||
|
||||
local $OUTPUT_AUTOFLUSH = 1;
|
||||
@@ -56,96 +56,6 @@ sub Help {
|
||||
return $text;
|
||||
}
|
||||
|
||||
# Одноразовая напоминалка. Сохраняет сообщение в базу notifications и отправляет его в delayed notifications.
|
||||
# Возвращает либо строку-подтверждение, либо подсказку как пользоваться командой.
|
||||
# $string Notify($chatid, $amount, $units, $message)
|
||||
sub Notify {
|
||||
my $chatid = shift;
|
||||
my $amount = shift;
|
||||
my $units = shift;
|
||||
my $message = shift;
|
||||
|
||||
if (defined $units) {
|
||||
if ($units eq 'm' || $units eq 'minute' || $units eq 'minutes') {
|
||||
$amount = eval { $amount * 60 };
|
||||
|
||||
unless (defined $units) {
|
||||
return sprintf ('Usage: %s NUM [s|seconds|m|minutes|h|hours]', $units);
|
||||
}
|
||||
} elsif ($units eq 'h' || $units eq 'hour' || $units eq 'hours') {
|
||||
$amount = eval { $amount * 60 * 60 };
|
||||
|
||||
unless (defined $units) {
|
||||
return sprintf ('Usage: %s NUM [s|seconds|m|minutes|h|hours]', $units);
|
||||
}
|
||||
} elsif ($units eq 's' || $units eq 'second' || $units eq 'seconds') {
|
||||
$amount = eval { $amount + 0 };
|
||||
|
||||
unless (defined $units) {
|
||||
return sprintf ('Usage: %s NUM [s|seconds|m|minutes|h|hours]', $units);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$amount = eval { $amount + 0 };
|
||||
|
||||
unless (defined $units) {
|
||||
return sprintf ('Usage: %s NUM [s|seconds|m|minutes|h|hours]', $units);
|
||||
}
|
||||
}
|
||||
|
||||
my $timestamp = time () + $amount;
|
||||
my $msg->{title} = 'Meow-bot notification';
|
||||
my $json;
|
||||
|
||||
if (defined $message && $message ne '') {
|
||||
$msg->{text} = $message;
|
||||
} else {
|
||||
$msg->{text} = 'Напоминалка!';
|
||||
}
|
||||
|
||||
$json = encode_json $msg;
|
||||
storedata ('notifications', $timestamp, $json, $c->{notifications}->{retention_days});
|
||||
DelayedNotify ($c->{channels}->{notify}, $msg->{text});
|
||||
|
||||
return sprintf 'Notification will be sent after %d seconds', $amount;
|
||||
}
|
||||
|
||||
# Пишет сообщение в чятик и складывает в БД, чтобы позже показать юзеру, когда он появится в чятике
|
||||
# undef PrinfMsgForUser($userid, $chatid, $message)
|
||||
sub DelayedNotify {
|
||||
my $channel = shift;
|
||||
my $message = shift;
|
||||
|
||||
my $timestamp = timems ();
|
||||
my $data;
|
||||
$data->{message} = $message;
|
||||
$data->{channel} = $channel;
|
||||
$data->{timestamp} = $timestamp;
|
||||
$data->{expiration_time} = $c->{delayed_notifications}->{retention_days} * 24 * 60 * 60 * 1000 + $timestamp;
|
||||
$data->{retention_days} = $c->{delayed_notifications}->{retention_days} ;
|
||||
|
||||
# Достанем список тех, кто есть в канальчике notify, им на джоине показывать сообщение не надо
|
||||
my @users = keys %{$MAIN::IRC->channel_list->{$c->{channels}->{notify}}}; ## no critic (Modules::RequireExplicitInclusion)
|
||||
$data->{users_shown} = \@users;
|
||||
|
||||
# TODO: Pretty jsonl
|
||||
my $json = encode_json $data;
|
||||
storedata ('delayed_notifications', timems (), $json, $c->{delayed_notifications}->{retention_days});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
# Пишет сообщение в чятик или юзеру
|
||||
# undef PrintMsg($chatid, $text)
|
||||
sub PrintMsg {
|
||||
my $channel = shift;
|
||||
my $message = shift;
|
||||
|
||||
$MAIN::IRC->send_long_message ('utf8', 0, 'PRIVMSG', $channel, $message); ## no critic (Modules::RequireExplicitInclusion)
|
||||
$log->debug ("[DEBUG] message '$message' sent to $channel");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
# Выгребает список todo-шек из базы
|
||||
# $msg = _todoPrint()
|
||||
@@ -1,165 +0,0 @@
|
||||
package BotLib::Periodic;
|
||||
|
||||
use 5.030; ## no critic (ProhibitImplicitImport)
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use utf8;
|
||||
use open qw (:std :utf8);
|
||||
use English qw ( -no_match_vars );
|
||||
|
||||
use JSON::XS qw (decode_json);
|
||||
use Log::Any qw ($log);
|
||||
|
||||
use BotLib::Command qw (PrintMsg);
|
||||
use BotLib::Conf qw (LoadConf);
|
||||
use BotLib::Util qw (runcmd cleanexpireddata rakedata deletedata);
|
||||
|
||||
use version; our $VERSION = qw (1.0);
|
||||
use Exporter qw (import);
|
||||
our @EXPORT_OK = qw (PollNotifications CleanExpiredEntries);
|
||||
|
||||
local $OUTPUT_AUTOFLUSH = 1;
|
||||
|
||||
my $c = LoadConf ();
|
||||
|
||||
# Регулярно выгребаем нотификашки из базы, запускаем нотификашки и пишем уведомления в канальчик с оповещениями
|
||||
# undef PollNotifications()
|
||||
sub PollNotifications {
|
||||
# Достаём все нотифаи в формате ссылки на хэш
|
||||
my $notification = rakedata ('notifications');
|
||||
$log->debug ('[DEBUG] Raked notifications from ' . $c->{db}->{notifications} . ' db');
|
||||
|
||||
my $deadline = time ();
|
||||
my @notifications = sort (keys %{$notification});
|
||||
|
||||
foreach my $timestamp (@notifications) {
|
||||
if ($timestamp <= $deadline) {
|
||||
$log->debug ("[Debug] Notification timestamp $timestamp <= $deadline, parsing");
|
||||
|
||||
my $alarm = eval { decode_json ($notification->{$timestamp}); };
|
||||
deletedata ('notifications', $timestamp);
|
||||
|
||||
next unless ($c->{notifications}->{enabled});
|
||||
|
||||
# TODO: Показывать нотификации не только для mac os
|
||||
if (defined $alarm) {
|
||||
my $res;
|
||||
my @cmd;
|
||||
|
||||
if (defined ($c->{notifications}->{sound}) && -e $c->{notifications}->{sound}) {
|
||||
if ($OSNAME eq 'darwin') {
|
||||
# Mac OS
|
||||
$log->debug ("[DEBUG] use macos-specific methods of playing sounds");
|
||||
@cmd = ('afplay', $c->{notifications}->{sound}, '--volume', '6');
|
||||
} elsif ($OSNAME eq 'MSWin32') {
|
||||
# Windows
|
||||
# Хрен знает как проиграть штатными средствами, без всплывающих окон звук в windows
|
||||
# TODO: Научиться проигрывать звук в windows :)
|
||||
$log->debug ("[DEBUG] windows detected, i don't know how to play sounds there")
|
||||
} elsif ($OSNAME eq 'linux' || $OSNAME eq 'freebsd') {
|
||||
# Linux/FreeBSD
|
||||
$log->debug ("[DEBUG] use linux/freebsd-specific methods of playing sounds");
|
||||
|
||||
if (-x '/usr/bin/mpg123' || -x '/bin/mpg123' || -x '/usr/local/bin/mpg123') {
|
||||
@cmd = ('mpg123', '-q', $c->{notifications}->{sound});
|
||||
} elsif (-x '/usr/bin/mpg321' || -x '/bin/mpg321' || -x '/usr/local/bin/mpg321') {
|
||||
@cmd = ('mpg321', '-q', $c->{notifications}->{sound});
|
||||
} elsif (-x '/usr/bin/madplay' || -x '/bin/madplay' || -x '/usr/local/bin/madplay') {
|
||||
@cmd = ('madplay', '-q', '--no-tty-control', $c->{notifications}->{sound});
|
||||
} elsif (-x '/usr/bin/mp3blaster' || -x '/bin/mp3blaster' || -x '/usr/local/bin/mp3blaster') {
|
||||
@cmd = ('mp3blaster', $c->{notifications}->{sound});
|
||||
}
|
||||
} else {
|
||||
$log->debug ("[DEBUG] don't know how to play sounds on $OSNAME");
|
||||
}
|
||||
|
||||
if ($#cmd > 0) {
|
||||
$res = runcmd (@cmd);
|
||||
}
|
||||
|
||||
$#cmd = -1;
|
||||
}
|
||||
|
||||
# Хрен знает, как показывать нотификашки стандартными средствами windows
|
||||
# TODO: Научиться показывать нотификашки в windows :)
|
||||
if ($OSNAME ne 'MSWin32') {
|
||||
if (defined $alarm->{text} && $alarm->{text} ne '') {
|
||||
if (defined $alarm->{title} && $alarm->{title} ne '') {
|
||||
if ($OSNAME eq 'darwin') {
|
||||
$log->debug ("[DEBUG] use macos-specific method of desktop notifications");
|
||||
my $applescript = sprintf (
|
||||
'display notification "%s" with title "%s"',
|
||||
$alarm->{text}, $alarm->{title},
|
||||
);
|
||||
|
||||
@cmd = ('osascript', '-e', "'$applescript'");
|
||||
} else {
|
||||
$log->debug ("[DEBUG] use linux/freebsd-specific method of desktop notifications");
|
||||
@cmd = ('notify-send', '--expire-time=5000', $alarm->{title}, $alarm->{text});
|
||||
}
|
||||
} else {
|
||||
if ($OSNAME eq 'darwin') {
|
||||
$log->debug ("[DEBUG] use macos-specific method of desktop notifications");
|
||||
my $applescript = sprintf (
|
||||
'display notification "%s" with title "%s"',
|
||||
$alarm->{text}, $alarm->{text},
|
||||
);
|
||||
|
||||
@cmd = ('osascript', '-e', "'$applescript'");
|
||||
} else {
|
||||
$log->debug ("[DEBUG] use linux/freebsd-specific method of desktop notifications");
|
||||
@cmd = ('notify-send', '--expire-time=5000', 'Напоминание от meow-бота', $alarm->{text});
|
||||
}
|
||||
}
|
||||
|
||||
PrintMsg($c->{channels}->{notify}, $alarm->{text});
|
||||
} else {
|
||||
if ($OSNAME eq 'darwin') {
|
||||
$log->debug ("[DEBUG] use macos-specific method of desktop notifications");
|
||||
my $applescript = 'display notification "Настало время придти времени." with title "Напоминание от meow-бота"';
|
||||
@cmd = ('osascript', '-e', "'$applescript'");
|
||||
} else {
|
||||
$log->debug ("[DEBUG] use linux/freebsd-specific method of desktop notifications");
|
||||
@cmd = ('notify-send', '--expire-time=5000', 'Напоминание от meow-бота', 'Настало время придти времени.');
|
||||
}
|
||||
|
||||
PrintMsg ($c->{channels}->{notify}, 'Настало время придти времени.');
|
||||
}
|
||||
|
||||
$res = runcmd (@cmd);
|
||||
} else {
|
||||
$log->debug ('[DEBUG] skip desktopn notification on windows');
|
||||
}
|
||||
} else {
|
||||
$log->error (sprintf (
|
||||
'[ERROR] Bad notification data in db %s: %s',
|
||||
$c->{db}->{notifications}, $EVAL_ERROR,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
$log->debug ("[Debug] Notification timestamp $timestamp > $deadline, skipping");
|
||||
}
|
||||
|
||||
# Не выплёвываем все нотификации сразу, если их более одной штуки
|
||||
if ($#notifications > -1) {
|
||||
sleep 1;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
# Вычищает протухшие записи из всех баз
|
||||
# undef CleanExpiredEntries()
|
||||
sub CleanExpiredEntries {
|
||||
foreach my $db (keys %{$c->{db}}) {
|
||||
cleanexpireddata ($db);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
||||
@@ -26,7 +26,7 @@ use version; our $VERSION = qw (1.0);
|
||||
use Exporter qw (import);
|
||||
our @EXPORT_OK = qw (timems trim urlencode utf2b64 utf2sha1 utf2md5 utf2sha1hex utf2sha224hex utf2sha256hex
|
||||
utf2sha384hex utf2sha512hex utf2md5hex utf2crc32hex utf2murmurhash irandom runcmd storedata
|
||||
cleanexpireddata rakedata deletedata);
|
||||
cleanexpireddata rakedata deletedata PrintMsg);
|
||||
|
||||
local $OUTPUT_AUTOFLUSH = 1;
|
||||
|
||||
@@ -405,6 +405,18 @@ sub deletedata {
|
||||
return;
|
||||
}
|
||||
|
||||
# Пишет сообщение в чятик или юзеру
|
||||
# undef PrintMsg($chatid, $text)
|
||||
sub PrintMsg {
|
||||
my $channel = shift;
|
||||
my $message = shift;
|
||||
|
||||
$MAIN::IRC->send_long_message ('utf8', 0, 'PRIVMSG', $channel, $message); ## no critic (Modules::RequireExplicitInclusion)
|
||||
$log->debug ("[DEBUG] message '$message' sent to $channel");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
||||
|
||||
Reference in New Issue
Block a user