Skip to content

Instantly share code, notes, and snippets.

@n7st
Last active November 12, 2015 17:19
Show Gist options
  • Save n7st/546fed5d9c4f89d54295 to your computer and use it in GitHub Desktop.
Save n7st/546fed5d9c4f89d54295 to your computer and use it in GitHub Desktop.
IRC/cron example
#!/usr/bin/perl
use Modern::Perl;
use POE qw/Component::IRC Component::Cron/;
my @schedule;
my ($irc) = POE::Component::IRC->spawn();
my %config = (
# Connection info
'server' => '',
'port' => 6667,
'name' => '',
'ident' => '',
'ns_password' => '',
'channels' => [ '#channels_here', '#another_channel' ],
);
my $session = POE::Session->create(
inline_states => {
_start => \&bot_start,
irc_001 => \&on_connect,
Tick => sub {
$irc->yield(privmsg => '##mike' => 'This message was scheduled to be sent at 22:56');
},
Tock => sub {
$irc->yield(privmsg => '##mike' => 'This message was scheduled to be sent at 22:58');
},
},
);
push @schedule, POE::Component::Cron->from_cron(
'56 22 * * *' => $session->ID => 'Tick'
);
push @schedule, POE::Component::Cron->from_cron(
'58 22 * * *' => $session->ID => 'Tock'
);
# _start
sub bot_start {
$irc->yield(register => 'all');
$irc->yield(
connect => {
Nick => $config{name},
Username => $config{ident},
Ircname => $config{name},
Server => $config{server},
Port => $config{port},
}
);
$_[KERNEL]->delay('_die_', 120);
return;
}
# irc_001
sub on_connect {
if ($config{ns_password}) {
print "Identifying with NickServ.\n";
$irc->yield(privmsg => 'nickserv' => 'identify '.$config{ns_password});
}
$irc->yield(join => $_) foreach @{$config{channels}};
return;
}
# Nothing below this line
$poe_kernel->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment