Advertisement
Guest User

Untitled

a guest
May 7th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.02 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Net::Jabber::Bot;
  5. use Log::Log4perl qw(:easy);
  6. use Encode;
  7. use utf8;
  8.  
  9. InitLog4Perl();
  10.  
  11. my @rooms = ('qutim');
  12. our $botName;
  13. $botName = "аввакум";
  14. our $owner;
  15. $owner = "torkve";
  16. our @repos;
  17. @repos = ('qutim', 'oscar', 'jabber', 'mrim');
  18.  
  19. our %commitMessages;
  20.  
  21. our %botActions;
  22. $botActions{'git'} = sub {sayTo(@_, "доступные репозитории:\ncgit - ядро\nigit - ICQ\njgit - Jabber\nmgit - MRIM")};
  23. $botActions{'cgit'} = sub {getGit(@_, "qutim")};
  24. $botActions{'igit'} = sub {getGit(@_, "oscar")};
  25. $botActions{'jgit'} = sub {getGit(@_, "jabber")};
  26. $botActions{'mgit'} = sub {getGit(@_, "mrim")};
  27. $botActions{'благослови'} = sub {sayTo(@_, "благословляю тебя, дитя моё. Иди и больше не греши.")};
  28. $botActions{'bless'} = sub {sayTo(@_, "благословляю тебя, дитя моё. Иди и больше не греши.")};
  29. $botActions{'попингуй'} = sub {sayTo(@_, "сам попингуй")};
  30. $botActions{'ping'} = \&getPing;
  31. $botActions{'пинг'} = \&getPing;
  32. $botActions{'.'} = \&getPing;
  33. $botActions{'литургия'} = \&getQuit;
  34. $botActions{'аминь'} = sub {sayTo(@_, "воистину аминь")};
  35. $botActions{'amen'} = sub {sayTo(@_, "воистину аминь")};
  36.  
  37. my %rooms_and_responses;
  38.  
  39. foreach my $room (@rooms)
  40. {
  41.     my $responses = "аввакум:|протопоп:|брат:|святой отец:|bot:|";
  42.     my @response_array = split(/\|/, $responses);
  43.     push @response_array, "";
  44.     $rooms_and_responses{$room} = \@response_array;
  45. }
  46.  
  47. my $bot = new Net::Jabber::Bot(
  48.     server => 'jabber.ru',
  49.     conference_server => 'conference.jabber.ru',
  50.     port => 5222,
  51.     safety_mode => 0,
  52.     username => 'torkvemada',
  53.     password => '',
  54.     alias => $botName,
  55.     message_function => \&callbackBotMessage,
  56.     background_function => \&callbackBackgroundChecks,
  57.     loop_sleep_time => 20,
  58.     process_timeout => 20,
  59.     forums_and_responses => \%rooms_and_responses,
  60.     ignore_server_messages => 1,
  61.     ignore_self_messages => 1,
  62.     out_messages_per_second => 1,
  63.     max_message_size => 1000,
  64.     max_messages_per_hour => 1000
  65. );
  66.  
  67. foreach my $room (@rooms)
  68. {
  69.     $bot->SendGroupMessage($room, "Мир вам, дети мои");
  70.     callbackBackgroundChecks(\$bot, 0);
  71. }
  72.  
  73. $bot->Start();
  74.  
  75. print "Bye-bye!";
  76. exit;
  77.  
  78. sub callbackBotMessage
  79. {
  80.     my %bot_message_hash = @_;
  81.     foreach my $pararam (%bot_message_hash)
  82.     {
  83.         print $pararam."\n";
  84.     }
  85.     $bot_message_hash{'sender'} = $bot_message_hash{'from_full'};
  86.     $bot_message_hash{'sender'} =~ s{^.+\/([^\/]+)$}{$1};
  87.     return unless $bot_message_hash{'sender'} ne $botName;
  88.  
  89.     my($command, @options) = split(' ', $bot_message_hash{'body'});
  90.     $command = lc($command);
  91.  
  92.     DEBUG("Got command: $command");
  93.     if ($bot_message_hash{'body'} =~ /п[иu]т[оo]н|п[еа]йт[оo]н|python/i)
  94.     {
  95.         $bot_message_hash{'bot_object'}->SendGroupMessage($bot_message_hash{'reply_to'}, $bot_message_hash{'sender'}.": Покайся, грешник! Никогда не произноси этого слова! Пейтон будет гореть в аду!");
  96.     }
  97.     elsif ($bot_message_hash{'body'} =~ /[aаoо]н[eеиu]м[eеэ]/i)
  98.     {
  99.         $bot_message_hash{'bot_object'}->SendGroupMessage($bot_message_hash{'reply_to'}, $bot_message_hash{'sender'}.": окстись, несчастный! Онемэ - удел содомитов и мужеложцев!");
  100.     }
  101.     elsif (defined $botActions{$command})
  102.     {
  103.         $botActions{$command}->(\%bot_message_hash, @options);
  104.     }
  105. }
  106.  
  107. sub callbackBackgroundChecks
  108. {
  109.     my $bot = shift;
  110.     my $counter = shift;
  111.     foreach my $dir (@repos)
  112.     {
  113.         $commitMessages{$dir} = decode_utf8(`cd $dir >/dev/null 2>&1; git pull >/dev/null 2>&1; git log -1; cd .. >/dev/null 2>&1;`);
  114.         $commitMessages{$dir} =~ s/^\s+//s;
  115.         $commitMessages{$dir} =~ s/\s+$//s;
  116.     }
  117. }
  118.  
  119. sub getGit
  120. {
  121.     DEBUG("Requested Git info");
  122.     my %bot_message_hash = %{shift @_};
  123.     my $repo = shift;
  124.     my $bot_object = $bot_message_hash{'bot_object'};
  125.     my $reply_to = $bot_message_hash{'reply_to'};
  126.     $bot_object->SendGroupMessage($reply_to, $commitMessages{$repo});
  127. }
  128.  
  129. sub sayTo
  130. {
  131.     my %bot_message_hash = %{shift @_};
  132.     my $bot_object = $bot_message_hash{'bot_object'};
  133.     my $reply_to = $bot_message_hash{'reply_to'};
  134.     my $author = $bot_message_hash{'sender'};
  135.     my $message = shift;
  136.     $bot_object->SendGroupMessage($reply_to, "$author: $message");
  137. }
  138.  
  139. sub getPing
  140. {
  141.     my %bot_message_hash = %{shift @_};
  142.     my $target = shift || $bot_message_hash{'sender'};
  143.     if ($target eq $botName)
  144.     {
  145.         my $author = $bot_message_hash{'sender'};
  146.         $bot_message_hash{'bot_object'}->SendGroupMessage($bot_message_hash{'reply_to'}, "$author: я отвечаю на запросы мгновенно, с Божьей помощью, ведомый рукой Господа.");
  147.         return;
  148.     }
  149.     DEBUG("Trying to get ping from $target");
  150.     my $address = $bot_message_hash{'from_full'};
  151.     $address =~ s#^(.*?)/.*#$1#;
  152.     $address .= "/".$target;
  153.     $bot_message_hash{'bot_object'}->RegisterPing($address, $target, \&getPing_sendMessage, \&getPing_sendError, \%bot_message_hash);
  154. }
  155.  
  156. sub getPing_sendMessage
  157. {
  158.     my $pingTime = shift;
  159.     my $target = shift;
  160.     my %bot_messager = %${shift @_};
  161.     sayTo(\%bot_messager, "дитя божье $target среагировало за $pingTime секунд");
  162. }
  163.  
  164. sub getPing_sendError
  165. {
  166.     my $target = shift;
  167.     my %bot_messager = %${shift @_};
  168.     sayTo(\%bot_messager, "неизвестен мне $target, раб ли он Божий али нехристь?");
  169. }
  170.  
  171. sub getQuit
  172. {
  173.     my %bot_message_hash = %{shift @_};
  174.     if ($bot_message_hash{'sender'} eq $owner)
  175.     {
  176.         sayTo(\%bot_message_hash, "спешу молиться!");
  177.         exit;
  178.     }
  179.     else
  180.     {
  181.         sayTo(\%bot_message_hash, "всё врёшь, окаянный, не началась ещё!");
  182.     }
  183. }
  184.  
  185. sub InitLog4Perl
  186. {
  187.     my $debug_level = 'DEBUG';
  188.     my $layout = '%d %p (%L): %m%n';
  189.     my $config_file = <<"CONFIG_DATA";
  190.     log4perl.appender.Screen = Log::Log4perl::Appender::Screen
  191.     log4perl.appender.Screen.stderr = 0
  192.     log4perl.appender.Screen.layout = PatternLayout
  193.     log4perl.appender.Screen.layout.ConversionPattern = $layout
  194.     log4perl.category = $debug_level, Screen
  195. CONFIG_DATA
  196.     Log::Log4perl->init(\$config_file);
  197.     $| = 1;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement