Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 238 lines (185 sloc) 5.238 kb
c078e53 @jeresig Add initial bot work.
authored
1 #!/usr/bin/perl
2 # An IRC bot for managing events
3 # Written by John Resig
4 # http://ejohn.org/
5
772566f @jeresig Changes from last night. Switch to using JSON everywhere, make sure stri...
authored
6 use 5.010;
7 use strict;
8 use warnings;
9
10 package main;
11
12 our $ini = (new Config::Abstract::Ini( 'config.ini' ))->get_all_settings;
13
14 my $bot = WTPABot->new(
15 server => $ini->{irc}{server},
16 channels => [ '#' . $ini->{irc}{channel} ],
17 nick => $ini->{irc}{nick},
18 username => $ini->{irc}{nick},
19 name => $ini->{irc}{name},
20 port => $ini->{irc}{port},
21 ssl => $ini->{irc}{ssl}
22 );
23
24 $bot->run();
25
c078e53 @jeresig Add initial bot work.
authored
26 package WTPABot;
27 use base 'Bot::BasicBot';
28
29 # Main Code
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
30 my @events;
31 my %places;
32 my $lastPull = time();
c078e53 @jeresig Add initial bot work.
authored
33
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
34 require "utils.pl";
c078e53 @jeresig Add initial bot work.
authored
35
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
36 # Load places, connect to Google Calendar and PingFM
c078e53 @jeresig Add initial bot work.
authored
37 sub init {
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
38 init();
39
40 calConnect();
41 pingConnect();
c078e53 @jeresig Add initial bot work.
authored
42 }
43
44 # Watch for changes to the topic
45 sub topic {
46 my $self = shift;
47 my $msg = shift;
48
49 # If no user was specified (e.g. it was done before we entered the channel)
772566f @jeresig Changes from last night. Switch to using JSON everywhere, make sure stri...
authored
50 if ( !(defined $msg->{who}) || $msg->{who} eq "" ) {
c078e53 @jeresig Add initial bot work.
authored
51 # Override the topic with ours
55c3957 @jeresig Support private messages and add a new topic command.
authored
52 $self->update_topic( $msg->{topic} );
c078e53 @jeresig Add initial bot work.
authored
53
54 # If we didn't change the topic
55 } elsif ( $msg->{who} ne $self->nick() ) {
56 # Override the topic with ours
55c3957 @jeresig Support private messages and add a new topic command.
authored
57 $self->update_topic( $msg->{topic} );
c078e53 @jeresig Add initial bot work.
authored
58
59 # And chastise the user who changed it
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
60 $self->re( 1, $msg, "Please use me to update the topic!" );
c078e53 @jeresig Add initial bot work.
authored
61 }
62 }
63
64 # A useful help message
65 sub help {
79020e1 @jeresig Add in some basic documentation on how to use the bot.
authored
66 return "How to use me: http://github.com/jeresig/wtpa-bot";
c078e53 @jeresig Add initial bot work.
authored
67 }
68
69 # Watch for when messages are said
70 sub said {
71 my $self = shift;
72 my $msg = shift;
73
74 # Check to see if the message was addressed to us
772566f @jeresig Changes from last night. Switch to using JSON everywhere, make sure stri...
authored
75 if ( defined $msg->{address} && ($msg->{address} eq $self->nick() || $msg->{address} eq "msg") ) {
9ce9630 @jeresig Add a new 'wtpa' command that dumps out addresses for today's events and...
authored
76 my $re = "";
77
772566f @jeresig Changes from last night. Switch to using JSON everywhere, make sure stri...
authored
78 print STDERR "WHO: $msg->{who} MSG: $msg->{body}\n";
79
9ce9630 @jeresig Add a new 'wtpa' command that dumps out addresses for today's events and...
authored
80 # Dump a status report for today
81 if ( $msg->{body} eq "" || $msg->{body} eq "wtpa" ) {
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
82 $self->re( 0, $msg, getToday() );
55c3957 @jeresig Support private messages and add a new topic command.
authored
83
84 # Get the current topic
9ce9630 @jeresig Add a new 'wtpa' command that dumps out addresses for today's events and...
authored
85 } elsif ( $msg->{body} eq "topic" ) {
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
86 return getTopic();
c078e53 @jeresig Add initial bot work.
authored
87
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
88 # Is the user attempting to do add a place
89 } elsif ( $msg->{body} =~ /^place add ([^ ]+) (.+)/i ) {
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
90 addPlace( $1, $2 );
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
91
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
92 $self->re( 0, $msg, "Place list updated." );
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
93
94 # Is the user attempting to update a place
95 } elsif ( $msg->{body} =~ /^place update (.*?): ([^ ]+) (.+)/i ) {
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
96 updatePlace( $1, $2, $3 );
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
97
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
98 $self->re( 0, $msg, "Place list updated." );
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
99
9ce9630 @jeresig Add a new 'wtpa' command that dumps out addresses for today's events and...
authored
100 # Dump a list of places
772566f @jeresig Changes from last night. Switch to using JSON everywhere, make sure stri...
authored
101 } elsif ( $msg->{body} eq "places" && defined $ini->{places}{url} ) {
102 $self->re( 0, $msg, $ini->{places}{url} );
9ce9630 @jeresig Add a new 'wtpa' command that dumps out addresses for today's events and...
authored
103
c078e53 @jeresig Add initial bot work.
authored
104 # Is the user attempting to cancel an event
55c3957 @jeresig Support private messages and add a new topic command.
authored
105 } elsif ( $msg->{body} =~ /^cancel (.*)/i ) {
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
106 my $err = cancelEvent( $1 );
107
108 if ( $err ) {
109 $self->re( 1, $msg, $err );
110
111 } else {
112 $self->update_topic();
c078e53 @jeresig Add initial bot work.
authored
113 }
114
66ee8c5 @jeresig Add in the ability to update events. Also make sure that cancelling an e...
authored
115 # Update an event with new details
116 } elsif ( $msg->{body} =~ /^update (.*?): ((.+) @ (?:(.+?), )?(.+))$/i ) {
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
117 my $err = updateEvent( $1, $2, {
66ee8c5 @jeresig Add in the ability to update events. Also make sure that cancelling an e...
authored
118 name => $3,
119 place => $5 ? $4 : "",
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
120 when => $5 || $4
121 });
122
123 if ( $err ) {
124 $self->re( 1, $msg, $err );
125
126 } else {
127 $self->update_topic();
66ee8c5 @jeresig Add in the ability to update events. Also make sure that cancelling an e...
authored
128 }
129
c078e53 @jeresig Add initial bot work.
authored
130 # Otherwise check to see if we're adding an event
0943db9 @jeresig Make the location optional.
authored
131 } elsif ( $msg->{body} =~ /^(.+) @ (?:(.+?), )?(.+)$/ ) {
c078e53 @jeresig Add initial bot work.
authored
132 print STDERR "Adding new entry.\n";
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
133
134 my $err = addEvent({
c078e53 @jeresig Add initial bot work.
authored
135 name => $1,
0943db9 @jeresig Make the location optional.
authored
136 place => $3 ? $2 : "",
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
137 when => $3 ? $3 : $2
138 });
139
140 if ( $err ) {
141 $self->re( 1, $msg, $err );
142
143 } else {
144 # Update the topic
145 $self->update_topic();
772566f @jeresig Changes from last night. Switch to using JSON everywhere, make sure stri...
authored
146 }
c078e53 @jeresig Add initial bot work.
authored
147
e9d482e @mike-burns Small docs update
mike-burns authored
148 } else {
c078e53 @jeresig Add initial bot work.
authored
149 return $self->help();
150 }
151 }
152
153 # Return undefined to not display a response
154 return;
155 }
156
157 # Simple routine for displaying error messages
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
158 sub re {
c078e53 @jeresig Add initial bot work.
authored
159 my $self = shift;
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
160 my $error = shift;
161 my $orig = shift;
c078e53 @jeresig Add initial bot work.
authored
162 my $msg = shift;
163
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
164 if ( $error ) {
165 print STDERR "ERROR: $msg\n";
166 $msg = "ERROR: $msg";
167 }
c078e53 @jeresig Add initial bot work.
authored
168
5662f62 @jeresig Add place management in and simplify the logic for replying to the user.
authored
169 $self->say(
170 who => $orig->{who},
171 channel => $orig->{channel},
172 body => ($orig->{channel} eq "msg" ? "" : "$orig->{who}: ") . $msg
c078e53 @jeresig Add initial bot work.
authored
173 );
174 }
175
176 # Simple method called every 5 seconds
177 # check for old events to remove
178 sub tick {
179 my $self = shift;
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
180 my $mtime = stat( FILE )[9];
181
182 # Check to see if the data was updated via the web interface
183 if ( $mtime > $lastPull ) {
184 loadBackup();
185 $lastPull = $mtime;
186 $remove = 1;
187 }
c078e53 @jeresig Add initial bot work.
authored
188
189 # Get the current day of the year for comparison
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
190 my $now = getTime( time() );
887265a @jeresig Make sure that old events are removed properly.
authored
191 my $cur = $now->doy();
c078e53 @jeresig Add initial bot work.
authored
192 my $remove = 0;
193
194 # Go through all the events
195 for ( my $i = 0; $i <= $#events; $i++ ) {
196 # Get their day of the year
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
197 my $when = getTime( $events[$i]->{when} );
c078e53 @jeresig Add initial bot work.
authored
198 my $day = $when->doy();
199
200 # If the event day is old we need to remove it
887265a @jeresig Make sure that old events are removed properly.
authored
201 if ( time() > $events[$i]->{when} && $day != $cur ) {
c078e53 @jeresig Add initial bot work.
authored
202 print STDERR "Cleaning up $events[$i]->{name}\n";
203
204 # Remove the event (but don't remove the calendar entry)
205 splice( @events, $i, 1 );
206 $i--;
207
208 $remove++;
209 }
210 }
211
212 # Only update the topic if an item should be removed
213 if ( $remove > 0 ) {
214 $self->update_topic();
215 }
216
217 # Call the tick method again in 5 seconds
218 return 5;
219 }
220
221 # Utility method for updating the topic
222 sub update_topic {
223 my $self = shift;
55c3957 @jeresig Support private messages and add a new topic command.
authored
224 my $cur_topic = shift;
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
225 my $topic = getTopic();
55c3957 @jeresig Support private messages and add a new topic command.
authored
226
227 # The topic is identical to what's there, don't update
772566f @jeresig Changes from last night. Switch to using JSON everywhere, make sure stri...
authored
228 if ( defined $cur_topic && $topic eq $cur_topic ) {
55c3957 @jeresig Support private messages and add a new topic command.
authored
229 return;
230 }
231
c078e53 @jeresig Add initial bot work.
authored
232 print STDERR "Updating: $topic\n";
233
234 # Update the topic in the channel
772566f @jeresig Changes from last night. Switch to using JSON everywhere, make sure stri...
authored
235 $self->{IRCOBJ}->yield(
236 sl_prioritized => 30,
237 "TOPIC #" . $ini->{irc}{channel} . " :$topic" );
f6fb30e @jeresig Create a web interface for interacting with the WTPA data. Split much of...
authored
238 }
Something went wrong with that request. Please try again.