sub new {
# Create a nameless UNIX socket pair which will be used to
# facilitate communication between the child listener process
# and the master PMBot process.
my($unix_forked,$unix_main) =
IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, 0);
# Set up the poll object
my $poll = new IO::Poll;
$poll->mask($unix_forked => POLLIN);
# Create the TCP/IP socket which will be used to
# listen for incoming client connections. The port should be
# spiffed via a command like arg or a config file, but that
# is a task for another time.
my $oscar = Net::OSCAR->new();
$oscar->set_callback_im_in(sub { im_in(@_,\$unix_forked,\$poll) });
$oscar->signon("pmbot2","rf89obdl");
# Create the object for returning from the
# parent process after the fork();
my $self = bless {
unix_socket => $unix_main,
client => $oscar,
};
# Fork and return the Listener::Basic object form the
# parent, leaving the child to continue on into an
# infinite $sock->accept() loop.
my $pid;
return $self if $pid = fork;
# Infinite loop that reads data from the OSCAR server
while(1) {
$oscar->do_one_loop();
}
}
# This is the sub that runs when we get a message
sub im_in {
my($oscar, $sender, $message, $is_away, $unix_forked, $poll) = @_;
print "A message for me oboy!\n";
print $sender;
$unix_forked->print($message);
$poll->poll;
my $message_to_send = $unix_forked->getline;
$oscar->send_im($sender, $message);
}