Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # A simple asynchronous IRC bot for ChkNet ( https://carding.network )
- # Tutorial on understanding this code: https://chknet.org/viewtopic.php?f=21&t=2362
- use Mojo::IRC;
- use strict; use warnings;
- use utf8;
- use feature 'say';
- my ($botnick, $botnickpass) = ('T-800', 'hunter2');
- my ($channel, $channelkey) = ('#coders', 's3cr3tk3y');
- my @admins = qw/l0pht/;
- my $cmdprefixes = '!.';
- my $irc = Mojo::IRC->new(
- nick => $botnick,
- user => $botnick,
- server => 'irc.chknet.cc:6697',
- pass => ''
- );
- $irc->tls({insecure => 1}); # enable SSL
- our %c = (
- k => chr 3, # color
- b => chr 2, # bold
- u => chr 31,# underline
- i => chr 29,# italics
- r => chr 22,# reverse
- a => chr 1, # action (used as first character in a message)
- );
- $irc->on(irc_privmsg => sub {
- my($self, $data) = @_;
- my ( $nick, $ident, $host ) = ($1,$2,$3) if $data->{prefix} =~ /([^\:\!\@\s]+)\!([^\:\!\@\s]+)\@([^\:\!\@\s]+)/;
- my $rchannel = $data->{params}->[0];
- my $message = $data->{params}->[1];
- return if lc $rchannel ne lc $channel;
- say $nick, " said: ", $message; # simply comment in case you don't want STDOUT messages
- if(defined $nick and $message =~ /^\s*[\Q$cmdprefixes\E]([^\s]+)\s*(.*)/)
- {
- my $command = lc $1;
- my @args = ( $2 =~ /([^\s]+)\s*/g );
- if ( $command eq 'test' ) {
- $irc->write(privmsg => $rchannel => "$c{k}03Test passed ✓");
- }elsif ( $command eq 'time' ) {
- $irc->write(privmsg => $rchannel => "Current time: $c{k}02$c{b}$c{u}" . CORE::localtime);
- }elsif ( $command eq 'op' ) {
- if ( grep $_ eq lc $nick, @admins ) {
- $irc->write(mode => $rchannel => "+o $nick");
- } else {
- $irc->write(privmsg => $rchannel => "$c{k}03DENIED: $nick, you are not my \$admin!");
- }
- }elsif ( $command eq 'dns' or $command eq 'ping' or $command eq 'whois' ) {
- (my $host = lc $args[0]) =~ s/[^a-z0-9_\-\.]//g;
- do {
- $irc->write(privmsg => $rchannel => "$c{i}Usage: !${command} example.com");
- return;
- } unless $host;
- my %linuxcmds = (
- 'dns' => "host $host",
- 'ping' => "ping -W1 -c4 $host",
- 'whois' => "whois $host"
- );
- $irc->write(privmsg => $rchannel => "$c{k}15Executing \`$linuxcmds{$command}\`... ");
- Mojo::IOLoop->subprocess(
- sub {
- my @IRC_RESULTS;
- my $r = `$linuxcmds{$command}`;
- @IRC_RESULTS = split /[\r\n]+/, $r;
- return @IRC_RESULTS;
- }, sub {
- my ($s, $error, @res) = @_;
- if ( $error ) {
- $irc->write(privmsg => $rchannel => "$c{k}04ERROR: " . $error);
- return;
- }
- $irc->write(privmsg => $rchannel => $_) foreach @res;
- });
- }elsif ( $command eq 'title' ) {
- my $host = lc $args[0];
- do {
- $irc->write(privmsg => $rchannel => "$c{i}Usage: !${command} example.com");
- return;
- } unless $host;
- $irc->write(privmsg => $rchannel => "$c{k}15Grabbing $host title...");
- Mojo::IOLoop->subprocess(
- sub {
- my @IRC_RESULTS;
- use Encode qw(decode encode);
- eval "require LWP::UserAgent;";die "I need the LWP::UserAgent package :( To install it try (as root): cpan install LWP::UserAgent\n" if $@;
- 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');
- $ua->requests_redirectable(['GET', 'HEAD']);
- $ua->timeout(7);
- $host = ( $host =~ /http/ ? $host : 'http://' . $host);
- my $result = $ua->get($host);
- if ($result->is_success ) {
- if ( $result->title ) {
- push @IRC_RESULTS, "Title for $host:";
- my $r = decode("utf8", $result->title);
- push @IRC_RESULTS, $_ foreach split /[\r\n]+/, $r;
- return @IRC_RESULTS;
- } else {
- die("$c{k}04Can't find title on $host\n");
- }
- } else {
- die("$c{k}04Can't connect to $host\n");
- }
- }, sub {
- my ($s, $error, @res) = @_;
- if ( $error ) {
- $irc->write(privmsg => $rchannel => "$c{k}04ERROR: " . $error);
- return;
- }
- $irc->write(privmsg => $rchannel => $_) foreach @res;
- });
- }elsif ( $command eq 'help' ) {
- my @help = (
- "!test - print the test message",
- "!time - print the current time",
- "!op - give channel OP to the nick which exists in \@admins",
- "!dns, !ping, !whois - standard linux utilities",
- "!title - grab a web-site title",
- "!help - print this help",
- "From ChkNet with ♥"
- );
- $irc->write(privmsg => $rchannel => $_) foreach (@help);
- }
- }
- });
- $irc->on(irc_join => sub {
- my($self, $message) = @_;
- # do something when someone joins a channel
- });
- $irc->on(error => sub { my ($self, $err) = @_;
- warn 'ERROR:' . $err;
- });
- $irc->on(irc_rpl_welcome => sub {
- my($irc, $err) = @_;
- say 'Joined IRC server';
- authcmd($irc);
- Mojo::IOLoop->recurring(3600, sub {
- authcmd($irc);
- });
- $irc->write(join => $channel, $channelkey);
- });
- $irc->connect(sub {
- my($irc, $err) = @_;
- return warn $err if $err;
- say 'Connection initiated';
- });
- sub authcmd {
- my $irc = shift;
- $irc->write(privmsg => 'nickserv', "recover $botnickpass");
- $irc->write(privmsg => 'nickserv', "identify $botnickpass");
- }
- Mojo::IOLoop->start;
Add Comment
Please, Sign In to add comment