ChkNet

Simple IRC bot for ChkNet with SSL

Dec 11th, 2018
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.36 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # A simple asynchronous IRC bot for ChkNet ( https://carding.network )
  4.  
  5. # Tutorial on understanding this code: https://chknet.org/viewtopic.php?f=21&t=2362
  6.  
  7. use Mojo::IRC;
  8. use strict; use warnings;
  9. use utf8;
  10. use feature 'say';
  11.  
  12. my ($botnick, $botnickpass) = ('T-800', 'hunter2');
  13.  
  14. my ($channel, $channelkey) = ('#coders', 's3cr3tk3y');
  15.  
  16. my @admins = qw/l0pht/;
  17. my $cmdprefixes = '!.';
  18.  
  19. my $irc = Mojo::IRC->new(
  20.             nick => $botnick,
  21.             user => $botnick,
  22.             server => 'irc.chknet.cc:6697',
  23.             pass => ''
  24.           );
  25.  
  26. $irc->tls({insecure => 1}); # enable SSL
  27.  
  28. our %c = (
  29. k => chr 3, # color
  30. b => chr 2, # bold
  31. u => chr 31,# underline
  32. i => chr 29,# italics
  33. r => chr 22,# reverse
  34. a => chr 1, # action (used as first character in a message)
  35. );
  36.  
  37. $irc->on(irc_privmsg => sub {
  38.   my($self, $data) = @_;
  39.   my ( $nick, $ident, $host ) = ($1,$2,$3) if $data->{prefix} =~ /([^\:\!\@\s]+)\!([^\:\!\@\s]+)\@([^\:\!\@\s]+)/;
  40.   my $rchannel = $data->{params}->[0];
  41.   my $message = $data->{params}->[1];
  42.   return if lc $rchannel ne lc $channel;
  43.  
  44.   say $nick, " said: ", $message; # simply comment in case you don't want STDOUT messages
  45.  
  46.   if(defined $nick and $message =~ /^\s*[\Q$cmdprefixes\E]([^\s]+)\s*(.*)/)
  47.   {
  48.         my $command = lc $1;
  49.         my @args = ( $2 =~ /([^\s]+)\s*/g );
  50.        
  51.         if ( $command eq 'test' ) {
  52.            
  53.             $irc->write(privmsg => $rchannel => "$c{k}03Test passed ✓");
  54.            
  55.         }elsif ( $command eq 'time' ) {
  56.            
  57.             $irc->write(privmsg => $rchannel => "Current time: $c{k}02$c{b}$c{u}" . CORE::localtime);
  58.            
  59.         }elsif ( $command eq 'op' ) {
  60.            
  61.             if ( grep $_ eq lc $nick, @admins ) {
  62.                 $irc->write(mode => $rchannel => "+o $nick");
  63.             } else {
  64.                 $irc->write(privmsg => $rchannel => "$c{k}03DENIED: $nick, you are not my \$admin!");
  65.             }
  66.            
  67.         }elsif ( $command eq 'dns' or $command eq 'ping' or $command eq 'whois' ) {
  68.            
  69.             (my $host = lc $args[0]) =~ s/[^a-z0-9_\-\.]//g;
  70.  
  71.             do {
  72.                 $irc->write(privmsg => $rchannel => "$c{i}Usage: !${command} example.com");
  73.                 return;
  74.             } unless $host;
  75.            
  76.             my %linuxcmds = (
  77.                 'dns' => "host $host",
  78.                 'ping' => "ping -W1 -c4 $host",
  79.                 'whois' => "whois $host"
  80.             );
  81.            
  82.             $irc->write(privmsg => $rchannel => "$c{k}15Executing \`$linuxcmds{$command}\`... ");
  83.            
  84.             Mojo::IOLoop->subprocess(
  85.                 sub {
  86.                     my @IRC_RESULTS;
  87.                    
  88.                     my $r = `$linuxcmds{$command}`;
  89.                     @IRC_RESULTS = split /[\r\n]+/, $r;
  90.                    
  91.                     return @IRC_RESULTS;
  92.             }, sub {
  93.                 my ($s, $error, @res) = @_;
  94.                 if ( $error ) {
  95.                     $irc->write(privmsg => $rchannel => "$c{k}04ERROR: " . $error);
  96.                     return;
  97.                 }
  98.                 $irc->write(privmsg => $rchannel => $_) foreach @res;
  99.             });
  100.            
  101.         }elsif ( $command eq 'title' ) {
  102.            
  103.             my $host = lc $args[0];
  104.  
  105.             do {
  106.                 $irc->write(privmsg => $rchannel => "$c{i}Usage: !${command} example.com");
  107.                 return;
  108.             } unless $host;
  109.  
  110.             $irc->write(privmsg => $rchannel => "$c{k}15Grabbing $host title...");
  111.            
  112.             Mojo::IOLoop->subprocess(
  113.                 sub {
  114.                    
  115.                     my @IRC_RESULTS;
  116.                    
  117.                     use Encode qw(decode encode);
  118.                     eval "require LWP::UserAgent;";die "I need the LWP::UserAgent package :( To install it try (as root): cpan install LWP::UserAgent\n" if $@;
  119.                    
  120.                     my $ua = LWP::UserAgent->new(agent => ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.32 Safari/537.36');
  121.                     $ua->requests_redirectable(['GET', 'HEAD']);
  122.                     $ua->timeout(7);
  123.                     $host = ( $host =~ /http/ ? $host : 'http://' . $host);
  124.                     my $result = $ua->get($host);
  125.                    
  126.                     if ($result->is_success ) {
  127.                         if ( $result->title ) {
  128.                             push @IRC_RESULTS, "Title for $host:";
  129.                             my $r = decode("utf8", $result->title);
  130.                             push @IRC_RESULTS, $_ foreach split /[\r\n]+/, $r;
  131.                             return @IRC_RESULTS;
  132.                         } else {
  133.                             die("$c{k}04Can't find title on $host\n");
  134.                         }
  135.                     } else {
  136.                         die("$c{k}04Can't connect to $host\n");
  137.                     }
  138.                    
  139.             }, sub {
  140.                 my ($s, $error, @res) = @_;
  141.                 if ( $error ) {
  142.                     $irc->write(privmsg => $rchannel => "$c{k}04ERROR: " . $error);
  143.                     return;
  144.                 }
  145.                 $irc->write(privmsg => $rchannel => $_) foreach @res;
  146.             });
  147.            
  148.         }elsif ( $command eq 'help' ) {
  149.            
  150.             my @help = (
  151.                 "!test - print the test message",
  152.                 "!time - print the current time",
  153.                 "!op - give channel OP to the nick which exists in \@admins",
  154.                 "!dns, !ping, !whois - standard linux utilities",
  155.                 "!title - grab a web-site title",
  156.                 "!help - print this help",
  157.                 "From ChkNet with ♥"
  158.             );
  159.            
  160.             $irc->write(privmsg => $rchannel => $_) foreach (@help);
  161.            
  162.         }
  163.     }
  164. });
  165.  
  166. $irc->on(irc_join => sub {
  167.   my($self, $message) = @_;
  168.   # do something when someone joins a channel
  169. });
  170.  
  171. $irc->on(error => sub { my ($self, $err) = @_;
  172.      warn 'ERROR:' . $err;
  173. });
  174.  
  175. $irc->on(irc_rpl_welcome => sub {
  176.   my($irc, $err) = @_;
  177.   say 'Joined IRC server';
  178.  
  179.   authcmd($irc);
  180.  
  181.   Mojo::IOLoop->recurring(3600, sub {
  182.     authcmd($irc);
  183.   });
  184.  
  185.   $irc->write(join => $channel, $channelkey);
  186. });
  187.  
  188. $irc->connect(sub {
  189.   my($irc, $err) = @_;
  190.   return warn $err if $err;
  191.   say 'Connection initiated';
  192. });
  193.  
  194. sub authcmd {
  195.   my $irc = shift;
  196.   $irc->write(privmsg => 'nickserv', "recover $botnickpass");
  197.   $irc->write(privmsg => 'nickserv', "identify $botnickpass");
  198. }
  199.  
  200. Mojo::IOLoop->start;
Add Comment
Please, Sign In to add comment