root/veekun/trunk/script/perlbui.pl

Revision 427, 4.0 kB (checked in by eevee, 8 months ago)

Changed Perlbui to use a hashref of channels rather than an arrayref, so I can provide settings later.

Line 
1use strict;
2use warnings;
3
4# this should not be this hard!
5use File::Basename;
6use File::Spec;
7use Cwd qw/abs_path/;
8
9# change directories to the root so we can find veekun files
10BEGIN {
11    my (undef, $path) = fileparse( File::Spec->rel2abs($0) );
12    chdir $path;
13    chdir '..'# we run from script/
14}
15use lib abs_path('lib');
16
17use Net::IRC;
18use Net::Jabber;
19
20use Vee::Bot;
21
22# BEGIN SETUP
23
24# need to find other veekun files..
25
26# XXX: only one supported at a time right now
27my %Connections = (
28    'irc.veekun.com' => {
29        '#veekun' => {},
30        '#tcod'   => {},
31    },
32#    'irc.pocketmonsters.net' => [ '#bulbagarden' ],
33);
34
35REPLACEME('Setup done');
36
37# END SETUP
38################################################################################
39# real code down here!
40
41# TODO: pull these options from somewhere
42# TODO: Log::Log4perl?
43
44# initialize IRC stuff
45my $irc      = new Net::IRC;
46my $server   = (keys %Connections)[0];
47my $irc_conn = $irc->newconn(
48    Nick     => 'Perlbui',
49    Server   => $server,
50    Port     => 6667,
51    Username => 'perlbui@veekun.com',
52);
53
54$irc_conn->add_handler(376,      \&irc_connect);  # end of MOTD
55$irc_conn->add_handler(422,      \&irc_connect);  # no MOTD
56$irc_conn->add_handler('public', \&irc_public);
57
58# ------------------------------------------------------------------------------
59
60# initialize Jabber stuff
61my $jabber_conn = new Net::Jabber::Client;
62# XXX: look into presence!  cool.  also what is IQ?
63$jabber_conn->SetCallBacks( message => \&jabber_message );
64
65$jabber_conn->Connect( hostname => 'jabber.org' ) or die "Can't connect to Jabber server: $!";
66
67my @result = $jabber_conn->AuthSend( username => 'perlbui', password => 'pretzel', resource => 'Perlbui 0.1' );
68
69if ($result[0] ne "ok") {
70    die "Jabber auth failed: $result[0] - $result[1]";
71}
72
73REPLACEME("Connected to Jabber");
74
75$jabber_conn->RosterGet;
76$jabber_conn->PresenceSend;
77
78# ------------------------------------------------------------------------------
79
80# for cleanliness!
81$SIG{TERM} = \&abort;
82
83# enter event loop
84while (1) {
85    $irc->do_one_loop;
86    my $jabber_status = $jabber_conn->Process(0);
87    die if not defined $jabber_status;
88}
89
90# END CODE THAT WILL ACTUALLY BE RUN
91################################################################################
92# HANDLERS
93
94sub abort {
95    $jabber_conn->Disconnect;
96    undef $irc;
97    exit 0;
98}
99
100# ------------------------------------------------------------------------------
101
102sub irc_connect {
103    my ($self) = @_;
104
105    REPLACEME("Successfully connected to " . $self->server);
106
107    for my $channel (keys %{$Connections{ $self->server }}) {
108        REPLACEME("Joining $channel");
109        $self->join($channel);
110    }
111}
112
113# ------------------------------------------------------------------------------
114
115sub irc_public {
116    my ($self, $event) = @_;
117    my ($line) = $event->args;
118
119    my $nick = $self->nick;
120    my ($to_me) = ($line =~ /^ $nick[:,] \s+ (.+?) [?!.]? $/ix);
121    return if not defined $to_me;
122
123    my $response = Vee::Bot::dispatcher($to_me);
124
125    # undef means dispatcher found nothing
126    $response ||= 'What?';
127
128    $self->privmsg($event->to, $event->nick . ': ' . $response);
129}
130
131# ------------------------------------------------------------------------------
132
133sub jabber_message {
134    my ($sid, $message) = @_;
135   
136    my $type = $message->GetType;
137    my $from = $message->GetFrom("jid")->GetUserID;
138   
139#    my $resource = $fromJID->GetResource();
140#    my $subject = $message->GetSubject();
141    my $body = $message->GetBody;
142    return if not $body;
143   
144    my $response = Vee::Bot::dispatcher($body);
145    $response ||= 'What?';
146
147    my $reply = $message->Reply;
148    $reply->SetMessage( body => $response );
149    $jabber_conn->Send($reply);
150}
151
152################################################################################
153# ETC
154
155# this needs to be a real logger or something
156sub REPLACEME {
157    warn shift;
158}
Note: See TracBrowser for help on using the browser.