1

Dee-Jay files around, rename subs according perlcritic convertions, update .gitignore

This commit is contained in:
eleksir
2021-03-27 13:14:58 +03:00
parent cf88be06db
commit 91888e1c24
26 changed files with 142 additions and 148 deletions

View File

@@ -1,6 +1,4 @@
package botlib;
# TODO: исправить #/bin/bash и #/bin/env
# TODO: добавить сов и кроликов flickr и imgur
package BotLib;
use 5.018;
use strict;
@@ -10,30 +8,30 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use Math::Random::Secure qw (irand);
use archeologist qw (dig);
use buni qw (buni);
use conf qw (loadConf);
use drink qw (drink);
use fisher qw (fish);
use fortune qw (fortune);
use fox qw (fox);
use friday qw (friday);
use image wq (rabbit owl);
use karma qw (karmaGet);
use kitty qw (kitty);
use lat qw (latAnswer);
use monkeyuser qw (monkeyuser);
use util qw (trim utf2sha1);
use weather qw (weather);
use xkcd qw (xkcd);
use BotLib::Archeologist qw (Dig);
use BotLib::Buni qw (Buni);
use BotLib::Conf qw (LoadConf);
use BotLib::Drink qw (Drink);
use BotLib::Fisher qw (Fish);
use BotLib::Fortune qw (Fortune);
use BotLib::Fox qw (Fox);
use BotLib::Friday qw (Friday);
use BotLib::Image qw (Rabbit Owl);
use BotLib::Karma qw (KarmaGet);
use BotLib::Kitty qw (Kitty);
use BotLib::Lat qw (Lat);
use BotLib::Monkeyuser qw (Monkeyuser);
use BotLib::Util qw (trim utf2sha1);
use BotLib::Weather qw (Weather);
use BotLib::Xkcd qw (Xkcd);
use version; our $VERSION = qw (1.0);
use Exporter qw(import);
our @EXPORT_OK = qw(command randomCommonPhrase realjid);
use Exporter qw (import);
our @EXPORT_OK = qw (Command RandomCommonPhrase RealJID);
my $c = loadConf ();
my $c = LoadConf ();
sub randomCommonPhrase () {
sub RandomCommonPhrase () {
my @myphrase = (
'Так, блядь...',
'*Закатывает рукава* И ради этого ты меня позвал?',
@@ -50,11 +48,11 @@ sub randomCommonPhrase () {
return $myphrase[irand ($#myphrase + 1)];
}
sub command {
sub Command {
my %hash = @_;
my $bot = $hash{bot_object};
my $text = $hash{body};
my %jid = realjid (%hash);
my %jid = RealJID (%hash);
my $chattername = $jid{'name'};
my $chatid;
@@ -180,7 +178,7 @@ sub command {
$reply = '/me наливает абсент в стопку. Смочив кубик сахара в абсенте кладёт его на дырявую ложечку и пожигает. Как только пламя потухнет, ' . $bot->{'alias'} . ' размешивает оплавившийся кубик в абсенте и подносит стопку ' . $target . '.';
} elsif (substr ($text, 1, 2) eq 'w ' || substr ($text, 1, 2) eq 'п ') {
my $city = substr $text, 2;
$reply = weather ($city) =~ tr/\n/ /r;
$reply = Weather ($city) =~ tr/\n/ /r;
} elsif (substr ($text, 1) eq 'version' || substr ($text, 1) eq 'ver') {
$reply = 'Версия нуль.чего-то_там.чего-то_там';
} elsif (substr ($text, 1) eq 'help' || substr ($text, 1) eq 'помощь') {
@@ -214,27 +212,27 @@ ${csign}xkcd - комикс-стрип с xkcb.ru
EOL
} elsif (substr ($text, 1) eq 'lat' || substr ($text, 1) eq 'лат') {
$reply = latAnswer ();
$reply = Lat ();
} elsif (substr ($text, 1) eq 'cat' || substr ($text, 1) eq 'кис') {
$reply = kitty ();
$reply = Kitty ();
} elsif (substr ($text, 1) eq 'fox' || substr ($text, 1) eq 'лис') {
$reply = fox ();
$reply = Fox ();
} elsif (substr ($text, 1) eq 'dig' || substr ($text, 1) eq 'копать') {
$reply = dig ($chattername);
$reply = Dig ($chattername);
} elsif (substr ($text, 1) eq 'fish' || substr ($text, 1) eq 'fisher' || substr ($text, 1) eq 'рыба' || substr ($text, 1) eq 'рыбка' || substr ($text, 1) eq 'рыбалка') {
$reply = fish ($chattername);
$reply = Fish ($chattername);
} elsif (substr ($text, 1) eq 'buni') {
$reply = buni ();
$reply = Buni ();
} elsif (substr ($text, 1) eq 'xkcd') {
$reply = xkcd ();
$reply = Xkcd ();
} elsif (substr ($text, 1) eq 'monkeyuser') {
$reply = monkeyuser ();
$reply = Monkeyuser ();
} elsif (substr ($text, 1) eq 'drink' || substr ($text, 1) eq 'праздник') {
$reply = drink ();
$reply = Drink ();
} elsif (substr ($text, 1) eq 'bunny' || substr ($text, 1) eq 'rabbit' || substr ($text, 1) eq 'кролик') {
$reply = rabbit ();
$reply = Rabbit ();
} elsif (substr ($text, 1) eq 'owl' || substr ($text, 1) eq 'сова') {
$reply = owl ();
$reply = Owl ();
} elsif ((length ($text) >= 6 && (substr ($text, 1, 6) eq 'karma ' || substr ($text, 1, 6) eq 'карма ')) || substr ($text, 1) eq 'karma' || substr ($text, 1) eq 'карма') {
my $mytext = '';
@@ -246,11 +244,11 @@ EOL
$mytext = '';
}
$reply = karmaGet ($chatid, $mytext);
$reply = KarmaGet ($chatid, $mytext);
} elsif (substr ($text, 1) eq 'friday' || substr ($text, 1) eq 'пятница') {
$reply = friday ();
$reply = Friday ();
} elsif (substr ($text, 1) eq 'fortune' || substr ($text, 1) eq 'фортунка' || substr ($text, 1) eq 'f' || substr ($text, 1) eq 'ф') {
my $phrase = fortune ();
my $phrase = Fortune ();
# workaround Net::Jabber::Bot outgoing message ![:print:] replacement
$phrase =~ s/\s\s+\-\-/\n \-\-/xmsg;
$reply = $phrase;
@@ -269,15 +267,13 @@ EOL
$reply = ':)';
} elsif (substr ($text, 1) eq '=)' || substr ($text, 1) eq ':)' || substr ($text, 1) eq '(:') {
$reply = ':D';
} elsif (substr ($text, 1, 11) eq '#/bin/bash' || substr ($text, 1, 9) eq '#/bin/sh' || substr ($text, 1, 14) eq '#/usr/bin/env') {
$reply = 'Кажется, вы ошиблись окном, это не паста.';
}
return $reply;
}
# in this particular case real jid is really hidden :) and i hope that i found where it is hiding.
sub realjid {
sub RealJID {
my %hash = @_;
my $bot = $hash{'bot_object'};
my $myjid = $hash{'from_full'}; # in case of real jid myjid is name@server/resource in case of "groupchat" jid it is group@conf_server/name

View File

@@ -1,4 +1,4 @@
package archeologist;
package BotLib::Archeologist;
use 5.018;
use strict;
@@ -10,9 +10,9 @@ use Math::Random::Secure qw (irand);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (dig);
our @EXPORT_OK = qw (Dig);
sub dig {
sub Dig {
my $name = shift;
my @artifact = (

View File

@@ -1,4 +1,4 @@
package buni;
package BotLib::Buni;
use 5.018;
use strict;
@@ -12,9 +12,9 @@ use HTML::TokeParser;
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (buni);
our @EXPORT_OK = qw (Buni);
sub buni {
sub Buni {
my $r;
my $ret = 'Нету Buni';

View File

@@ -1,4 +1,4 @@
package conf;
package BotLib::Conf;
# loads config
use 5.018;
@@ -11,9 +11,9 @@ use JSON::XS;
use version; our $VERSION = qw (1.0);
use Exporter qw(import);
our @EXPORT_OK = qw(loadConf);
our @EXPORT_OK = qw(LoadConf);
sub loadConf {
sub LoadConf {
my $c = 'data/config.json';
open my $CH, '<', $c or die "[FATA] No conf at $c: $OS_ERROR\n";
my $len = (stat $c) [7];

View File

@@ -1,4 +1,4 @@
package drink;
package BotLib::Drink;
use 5.018;
use strict;
@@ -13,11 +13,11 @@ use HTML::TokeParser;
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (drink drink2);
our @EXPORT_OK = qw (Drink Drink2);
my @MONTH = qw (yanvar fevral mart aprel may iyun iyul avgust sentyabr oktyabr noyabr dekabr);
sub drink {
sub Drink {
my $r;
my $ret = 'Не знаю праздников - вджобываю весь день на шахтах, как проклятая.';
my ($dayNum, $monthNum) = (localtime ())[3, 4];

View File

@@ -1,4 +1,4 @@
package fisher;
package BotLib::Fisher;
use 5.018;
use strict;
@@ -10,10 +10,10 @@ use Math::Random::Secure qw (irand);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (fish);
our @EXPORT_OK = qw (Fish);
sub fish {
sub Fish {
my $name = shift;
my @artifact = (

View File

@@ -1,4 +1,4 @@
package fortune;
package BotLib::Fortune;
# Any fortune_mod sources are suitable. Text file with "\n%\n" sentence delimiters.
use 5.018;
@@ -12,17 +12,17 @@ use SQLite_File;
use Math::Random::Secure qw (irand);
use MIME::Base64;
use File::Path qw (make_path);
use conf qw (loadConf);
use BotLib::Conf qw (LoadConf);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (seed fortune);
our @EXPORT_OK = qw (Seed Fortune);
my $c = loadConf ();
my $c = LoadConf ();
my $dir = $c->{fortune}->{dir};
my $srcdir = $c->{fortune}->{srcdir};
sub seed () {
sub Seed () {
unless (-d $dir) {
make_path ($dir) || croak "Unable to create $dir: $OS_ERROR";
}
@@ -65,7 +65,7 @@ sub seed () {
}
# just return answer
sub fortune () {
sub Fortune () {
my $backingfile = sprintf '%s/fortune.sqlite', $dir;
tie my @array, 'SQLite_File', $backingfile || do {

View File

@@ -1,4 +1,4 @@
package fox;
package BotLib::Fox;
use 5.018;
use strict;
@@ -12,10 +12,10 @@ use HTTP::Tiny;
use vars qw/$VERSION/;
use Exporter qw (import);
our @EXPORT_OK = qw (fox);
our @EXPORT_OK = qw (Fox);
$VERSION = '1.0';
sub fox {
sub Fox {
my $r;
my $ret = 'Нету лисичек, все разбежались.';
@@ -36,7 +36,7 @@ sub fox {
};
unless (defined $jfox) {
cluck "[ERROR] Unable to decode JSON: $EVAL_ERROR";
carp "[ERROR] Unable to decode JSON: $EVAL_ERROR";
} else {
if ($jfox->{image}) {
$jfox->{image} =~ s/\\//xmsg;

View File

@@ -1,4 +1,4 @@
package friday;
package BotLib::Friday;
# for friday data stolen from https://raw.githubusercontent.com/isida/vi/master/data/friday.txt
use 5.018;
@@ -11,18 +11,18 @@ use Carp qw (carp croak);
use File::Path qw (make_path);
use Math::Random::Secure qw (irand);
use SQLite_File;
use conf qw (loadConf);
use BotLib::Conf qw (LoadConf);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (seed friday);
our @EXPORT_OK = qw (Seed Friday);
my $c = loadConf ();
my $c = LoadConf ();
my $dir = $c->{friday}->{dir};
my $srcfile = $c->{friday}->{src};
my @dow = qw (monday tuesday wednesday thursday friday saturday sunday);
sub seed () {
sub Seed () {
unless (-d $dir) {
make_path ($dir) || croak "Unable to create $dir: $OS_ERROR";
}
@@ -75,7 +75,7 @@ sub seed () {
}
# just return answer
sub friday () {
sub Friday () {
my $today = (localtime (time))[6] - 1;
my $backingfile = sprintf '%s/%s.sqlite', $dir, $dow[$today];

View File

@@ -1,23 +1,19 @@
package image;
package BotLib::Image;
use 5.018;
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use flickr qw (flickr_by_tags);
use BotLib::Image::Flickr qw (FlickrByTags);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (rabbit owl);
our @EXPORT_OK = qw (Rabbit Owl);
my $c = loadConf ();
my $dir = $c->{image}->{dir};
sub rabbit {
sub Rabbit {
# rabbit, but bunny
my $url = flickr_by_tags ('animal,bunny');
my $url = FlickrByTags ('animal,bunny');
if (defined $url) {
return $url;
@@ -26,8 +22,8 @@ sub rabbit {
}
}
sub owl {
my $url = flickr_by_tags ('bird,owl');
sub Owl {
my $url = FlickrByTags ('bird,owl');
if (defined $url) {
return $url;

View File

@@ -1,4 +1,4 @@
package flickr;
package BotLib::Image::Flickr;
use 5.018;
use strict;
@@ -16,16 +16,16 @@ use MIME::Base64;
use SQLite_File;
use URI::Encode::XS qw (uri_encode);
use conf qw (loadConf);
use util qw (urlencode);
use BotLib::Conf qw (LoadConf);
use BotLib::Util qw (urlencode);
use Data::Dumper;
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (flickr_init flickr_by_tags flickr_by_text flickr_test_login);
our @EXPORT_OK = qw (FlickrInit FlickrByTags FlickrByText FlickrTestLogin);
my $c = loadConf ();
my $c = LoadConf ();
my $dir = $c->{image}->{dir};
my $flickr_consumer_key = $c->{image}->{flickr}->{consumer_key};
@@ -464,7 +464,7 @@ sub flickrSearchByTags {
}
}
sub flickr_init {
sub FlickrInit {
my $backingfile = sprintf '%s/secrets.sqlite', $dir;
unless (-d $dir) {
@@ -527,7 +527,7 @@ sub flickr_init {
return 0;
}
sub flickr_test_login {
sub FlickrTestLogin {
my $text = shift;
my $backingfile = sprintf '%s/secrets.sqlite', $dir;
@@ -554,7 +554,7 @@ sub flickr_test_login {
}
}
sub flickr_by_tags {
sub FlickrByTags {
my $text = shift;
my $backingfile = sprintf '%s/secrets.sqlite', $dir;
@@ -595,7 +595,7 @@ sub flickr_by_tags {
}
}
sub flickr_by_text {
sub FlickrByText {
my $text = shift;
my $backingfile = sprintf '%s/secrets.sqlite', $dir;

View File

@@ -1,4 +1,4 @@
package karma;
package BotLib::Karma;
use 5.018;
use strict;
@@ -9,19 +9,19 @@ use English qw ( -no_match_vars );
use Carp qw (carp);
use DB_File;
use File::Path qw (make_path);
use conf qw (loadConf);
use util qw (utf2sha1 trim);
use BotLib::Conf qw (LoadConf);
use BotLib::Util qw (utf2sha1 trim);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (karmaSet karmaGet);
our @EXPORT_OK = qw (KarmaSet KarmaGet);
my $c = loadConf ();
my $c = LoadConf ();
my $karmadir = $c->{karma}->{dir};
my $max = 5;
# swallow phrase and return answer
sub karmaSet (@) {
sub KarmaSet (@) {
my $chatid = shift;
my $phrase = shift;
my $action = shift;
@@ -84,7 +84,7 @@ sub karmaSet (@) {
}
# just return answer
sub karmaGet (@) {
sub KarmaGet (@) {
my $chatid = shift;
my $phrase = shift;
$phrase = trim $phrase;

View File

@@ -1,4 +1,4 @@
package kitty;
package BotLib::Kitty;
use 5.018;
use strict;
@@ -12,9 +12,9 @@ use HTTP::Tiny;
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (kitty);
our @EXPORT_OK = qw (Kitty);
sub kitty {
sub Kitty {
my $r;
my $ret = 'Нету кошечек, все разбежались.';

View File

@@ -1,4 +1,4 @@
package lat;
package BotLib::Lat;
# famous latin phases taken from https://ru.wikipedia.org/wiki/Список_крылатых_латинских_выражений
use 5.018;
@@ -11,17 +11,17 @@ use Carp qw (carp);
use File::Basename qw (dirname);
use File::Path qw (make_path);
use Hailo;
use conf qw (loadConf);
use BotLib::Conf qw (LoadConf);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (train latAnswer);
our @EXPORT_OK = qw (Train Lat);
my $c = loadConf ();
my $c = LoadConf ();
my $brain = $c->{lat}->{brain};
my $srcfile = $c->{lat}->{src};
sub train () {
sub Train () {
my $braindir = dirname $brain;
unless (-d $braindir) {
@@ -45,7 +45,7 @@ sub train () {
}
# just return answer
sub latAnswer () {
sub Lat () {
my $braindir = dirname $brain;
unless (-d $braindir) {

View File

@@ -1,4 +1,4 @@
package monkeyuser;
package BotLib::Monkeyuser;
use 5.018;
use strict;
@@ -13,9 +13,9 @@ use Math::Random::Secure qw (irand);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (monkeyuser);
our @EXPORT_OK = qw (Monkeyuser);
sub monkeyuser {
sub Monkeyuser {
my $r;
my $ret = 'Нету Monkey User-ов, они все спрятались.';
my @link;

View File

@@ -1,4 +1,4 @@
package util;
package BotLib::Util;
use 5.018;
use strict;

View File

@@ -1,4 +1,4 @@
package weather;
package BotLib::Weather;
use 5.018;
use strict;
@@ -12,16 +12,16 @@ use Digest::MD5 qw (md5_base64);
use File::Path qw (make_path);
use HTTP::Tiny;
use JSON::XS;
use conf qw (loadConf);
use util qw (trim urlencode);
use BotLib::Conf qw (LoadConf);
use BotLib::Util qw (trim urlencode);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (weather);
our @EXPORT_OK = qw (Weather);
my $c = loadConf ();
my $c = LoadConf ();
sub weather {
sub Weather {
my $city = shift;
$city = trim $city;

View File

@@ -1,4 +1,4 @@
package xkcd;
package BotLib::Xkcd;
use 5.018;
use strict;
@@ -9,9 +9,9 @@ use HTTP::Tiny;
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (xkcd);
our @EXPORT_OK = qw (Xkcd);
sub xkcd {
sub Xkcd {
my $http = HTTP::Tiny->new (timeout => 5, max_redirect => 0);
my $r = $http->get ('https://xkcd.ru/random/');

View File

@@ -1,7 +1,7 @@
# note http://www.gentoo.ru/node/21449
# необходимо закомментировать строку $response{authzid} = $authzid;
# в vendor_perl/5.24.3/Authen/SASL/Perl/DIGEST_MD5.pm
package jabberbot;
package JabberBot;
use 5.018;
use strict;
@@ -13,16 +13,16 @@ use Carp qw(carp);
use File::Path qw (make_path);
use Hailo;
use Net::Jabber::Bot;
use botlib qw (command randomCommonPhrase realjid);
use conf qw (loadConf);
use karma qw (karmaSet);
use util qw (trim utf2sha1);
use BotLib qw (Command RandomCommonPhrase RealJID);
use BotLib::Conf qw (LoadConf);
use BotLib::Karma qw (KarmaSet);
use BotLib::Util qw (trim utf2sha1);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (run_jabberbot);
our @EXPORT_OK = qw (RunJabberBot);
my $c = loadConf ();
my $c = LoadConf ();
my $hailo;
my $imonline = 0;
@@ -47,8 +47,7 @@ sub __new_bot_message {
}
my $text = $hash{body};
my %jid = realjid %hash;
my %jid = RealJID (%hash);
my $chattername = $jid{'name'};
my $chatid;
@@ -93,14 +92,12 @@ sub __new_bot_message {
$reply = 'Чего тебе?';
} elsif (($text =~ /^$qname[\,|\:]? (.+)/) && ($hash{'type'} eq 'groupchat')) {
$reply = $hailo->{$chatid}->learn_reply ($1);
} elsif (substr ($text, 0, 1) eq $c->{jabberbot}->{aleesa}->{csign}) {
$reply = command %hash;
# karma adjustment
} elsif (substr ($text, -2) eq '++' || substr ($text, -2) eq '--') {
my @arr = split /\n/, $text;
if ($#arr < 1) {
$reply = karmaSet ($chatid, trim (substr ($text, 0, -2)), substr ($text, -2));
$reply = KarmaSet ($chatid, trim (substr ($text, 0, -2)), substr ($text, -2));
} else {
# just message in chat
# in groupchat we answer only to phrases that mention us
@@ -111,6 +108,8 @@ sub __new_bot_message {
$reply = $hailo->{$chatid}->learn_reply ($text);
}
}
} elsif (substr ($text, 0, 1) eq $c->{jabberbot}->{aleesa}->{csign}) {
$reply = Command (%hash);
# just message in chat
} else {
# in groupchat we answer only to phrases that mention us
@@ -134,7 +133,7 @@ sub __new_bot_message {
}
sub run_jabberbot {
sub RunJabberBot {
my %conflist = ();
$conflist{$c->{jabberbot}->{aleesa}->{room}} = [];
my $qname = quotemeta $c->{jabberbot}->{aleesa}->{mucname};
@@ -173,6 +172,7 @@ sub run_jabberbot {
return;
}
1;
# vim: set ft=perl noet ai ts=4 sw=4 sts=4: