#!/usr/bin/perl -w # apbteambot use strict; use Net::IRC; use IO::Socket::UNIX; use POSIX; # # Config. # my $server = 'irc.freenode.net'; my $nick = 'APBTeamBot'; my $username = 'apbteambot'; my $chan = '#apbteam'; my $socketname = '/var/run/apbteambot/sock'; my $pidname = '/var/run/apbteambot/pid'; # # Create the IRC and Connection objects # my $irc = new Net::IRC; print "Creating connection to $server as $nick...\n"; my $conn = $irc->newconn(Server => $server, Nick => $nick, Username => $username) or die "can not connect to $server: $!\n"; # # Here are the handler subroutines. # # What to do when the bot successfully connects. sub on_connect { my $self = shift; #print "Joining $chan...\n"; $self->join($chan); $self->privmsg($chan, "Coucou"); } # Handles some messages you get when you connect sub on_init { my ($self, $event) = @_; my (@args) = ($event->args); shift (@args); #print "*** @args\n"; } # Prints the names of people in a channel when we enter. sub on_names { my ($self, $event) = @_; my (@list, $channel) = ($event->args); ($channel, @list) = splice @list, 2; #print "Users on $channel: @list\n"; } # Quit if taken. sub on_nick_taken { die "Nick taken"; } # Reconnect to the server when we die. sub on_disconnect { my ($self, $event) = @_; #print "Disconnected from ", $event->from(), " (", #($event->args())[0], "). Attempting to reconnect...\n"; $self->connect(); } print "Installing handler routines..."; $conn->add_global_handler([ 251,252,253,254,302,255 ], \&on_init); $conn->add_global_handler('disconnect', \&on_disconnect); $conn->add_global_handler(376, \&on_connect); $conn->add_global_handler(433, \&on_nick_taken); $conn->add_global_handler(353, \&on_names); print " done.\n"; # Server socket. unlink $socketname; my $socket = new IO::Socket::UNIX (Local => $socketname, Listen => 5) or die $!; chmod 0664, $socketname; sub on_socket { my $client = $socket->accept (); while (<$client>) { chomp; $conn->privmsg($chan, $_); } $client->close (); } $irc->addfh ($socket, \&on_socket, "r"); # Daemonize. print "starting...\n"; my $pid = fork; die $! unless defined $pid; if ($pid) { open PID, ">$pidname" or die $!; print PID "$pid\n"; close PID; exit 0; } POSIX::setsid (); close STDIN; close STDOUT; close STDERR; chdir "/"; $irc->start;