chiark / gitweb /
c17621f245434ae47d11a3a9e1aa9333f114246f
[bin.git] / taskbot.pl
1 #! /usr/bin/perl -w
2 use diagnostics;
3 use strict;
4
5 use Net::IRC;
6
7 sub usage ()
8 {
9         die <<"EOF";
10 Usage: $0 server bot-name target-channel
11 EOF
12 }
13
14 my %tasks = ();
15
16 my $server = shift or usage;
17 my $botname = shift or usage;
18 my $channel = shift or usage;
19
20 my $irc = new Net::IRC;
21
22 my $conn = $irc->newconn(Nick => $botname, Server => $server)
23         or die "$0: can't connect to IRC server";
24
25 my $joined = 0;
26
27 sub on_connect
28 {
29         my $self = shift;
30         $self->join($channel);
31 }
32
33 sub on_topic
34 {
35         my $self = shift;
36         $joined = 1;
37 }
38
39 sub on_public
40 {
41         my ($self, $event) = @_;
42         my ($args) = $event->args;
43         my $from = $event->from;
44         $from =~ s/!.*//;
45         if ($args =~ s/^$botname:?\s*//io)
46         {
47                 my ($victim, $task, $tasksref);
48                 (my $command, $args) = split ' ', $args, 2;
49                 $command = lc $command;
50                 if ($command eq 'add' && $args =~ /^([^ \t]+)\s+(.*[^ \t])/)
51                 {
52                         ($victim, $task) = ($1, $2);
53                         $tasksref = $tasks{$victim};
54                         $tasksref = $tasks{$victim} = [] if not defined $tasksref;
55                         $$tasksref[scalar @$tasksref] = $task;
56                         $self->privmsg($channel, "cheers, added task for $victim:");
57                         $self->privmsg($channel, "  $task");
58                 }
59                 elsif ($command eq 'delete' && $args =~ /^([^ \t]+)\s+(.*[^ \t])/)
60                 {
61                         ($victim, $task) = ($1, $2);
62                         $tasksref = $tasks{$victim};
63                         if (defined $tasksref && scalar grep {$_ eq $task} @$tasksref)
64                         {
65                                 @$tasksref = grep {$_ ne $task} @$tasksref;
66                                 $self->privmsg($channel, "cheers, deleted task for $victim:");
67                                 $self->privmsg($channel, "  $task");
68                         }
69                         else
70                         {
71                                 $self->privmsg($channel,
72                                                            "sorry, never heard of that task for $victim:");
73                                 $self->privmsg($channel, "  $task");
74                         }
75                 }
76                 elsif ($command eq 'deleteall' && $args =~ /^([^ \t]+)/)
77                 {
78                         $victim = $1;
79                         undef $tasks{$victim};
80                         $self->privmsg($channel, "$victim is free! For a short time ...");
81                 }
82                 elsif ($command eq 'help')
83                 {
84                         $self->privmsg($channel, "No.");
85                 }
86                 elsif ($command eq 'list' && $args =~ /^([^ \t]+)/)
87                 {
88                         $victim = $1;
89                         $tasksref = $tasks{$victim};
90                         if (defined $tasksref && scalar @{$tasks{$victim}})
91                         {
92                                 $self->privmsg($channel, "$victim is supposed to be doing:");
93                         }
94                         else
95                         {
96                                 $self->privmsg($channel, "No tasks for $victim.");
97                         }
98                         foreach my $task (@{$tasks{$victim}})
99                         {
100                                 $self->privmsg($channel, "  $task");
101                         }
102                 }
103                 elsif ($command eq 'quit')
104                 {
105                         $self->privmsg($channel, "They killed $botname! The bastards ...");
106                         $self->quit;
107                         exit;
108                 }
109         }
110 }
111
112 $conn->add_handler('376', \&on_connect);
113 $conn->add_handler('332', \&on_topic);
114 $conn->add_handler('public', \&on_public);
115
116 $irc->start;