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