-
Notifications
You must be signed in to change notification settings - Fork 1
/
wtpa-bot.pl
executable file
·206 lines (159 loc) · 4.38 KB
/
wtpa-bot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/perl
# An IRC bot for managing events
# Written by John Resig
# http://ejohn.org/
use 5.010;
use strict;
use warnings;
package main;
use WTPA;
use Config::Abstract::Ini;
our $ini = (new Config::Abstract::Ini( 'config.ini' ))->get_all_settings;
utilInit( $ini );
my $bot = WTPABot->new(
server => $ini->{irc}{server},
channels => [ '#' . $ini->{irc}{channel} ],
nick => $ini->{irc}{nick},
username => $ini->{irc}{nick},
name => $ini->{irc}{name},
port => $ini->{irc}{port},
ssl => $ini->{irc}{ssl}
);
$bot->run();
package WTPABot;
use base 'Bot::BasicBot';
use WTPA;
# Load places, connect to Google Calendar and PingFM
sub init {
calConnect();
pingConnect();
}
# Watch for changes to the topic
sub topic {
my $self = shift;
my $msg = shift;
# If no user was specified (e.g. it was done before we entered the channel)
if ( !(defined $msg->{who}) || $msg->{who} eq "" ) {
# Override the topic with ours
$self->update_topic( $msg->{topic} );
# If we didn't change the topic
} elsif ( $msg->{who} ne $self->nick() ) {
# Override the topic with ours
$self->update_topic( $msg->{topic} );
# And chastise the user who changed it
$self->re( 1, $msg, "Please use me to update the topic!" );
}
}
# A useful help message
sub help {
return "How to use me: http://github.com/jeresig/wtpa-bot";
}
# Watch for when messages are said
sub said {
my $self = shift;
my $msg = shift;
# Check to see if the message was addressed to us
if ( defined $msg->{address} && ($msg->{address} eq $self->nick() || $msg->{address} eq "msg") ) {
my $re = "";
print STDERR "WHO: $msg->{who} MSG: $msg->{body}\n";
# Dump a status report for today
if ( $msg->{body} eq "" || $msg->{body} eq "wtpa" ) {
$self->re( 0, $msg, getToday() );
# Get the current topic
} elsif ( $msg->{body} eq "topic" ) {
return getTopic();
# Is the user attempting to do add a place
} elsif ( $msg->{body} =~ /^place add ([^ ]+) (.+)/i ) {
addPlace( $1, $2 );
$self->re( 0, $msg, "Place list updated." );
# Is the user attempting to update a place
} elsif ( $msg->{body} =~ /^place update (.*?): ([^ ]+) (.+)/i ) {
updatePlace( $1, $2, $3 );
$self->re( 0, $msg, "Place list updated." );
# Dump a list of places
} elsif ( $msg->{body} eq "places" && defined $ini->{places}{url} ) {
$self->re( 0, $msg, $ini->{places}{url} );
# Is the user attempting to cancel an event
} elsif ( $msg->{body} =~ /^cancel (.*)/i ) {
my $err = cancelEvent( $1 );
if ( $err ) {
$self->re( 1, $msg, $err );
} else {
$self->update_topic();
}
# Update an event with new details
} elsif ( $msg->{body} =~ /^update (.*?): ((.+) @ (?:(.+?), )?(.+))$/i ) {
my $err = updateEvent( $1, $2, {
name => $3,
place => $5 ? $4 : "",
when => $5 || $4
});
if ( $err ) {
$self->re( 1, $msg, $err );
} else {
$self->update_topic();
}
# Otherwise check to see if we're adding an event
} elsif ( $msg->{body} =~ /^(.+) @ (?:(.+?), )?(.+)$/ ) {
print STDERR "Adding new entry.\n";
my $err = addEvent({
name => $1,
place => $3 ? $2 : "",
when => $3 ? $3 : $2
});
if ( $err ) {
$self->re( 1, $msg, $err );
} else {
# Update the topic
$self->update_topic();
}
} else {
return $self->help();
}
}
# Return undefined to not display a response
return;
}
# Simple routine for displaying error messages
sub re {
my $self = shift;
my $error = shift;
my $orig = shift;
my $msg = shift;
if ( $error ) {
print STDERR "ERROR: $msg\n";
$msg = "ERROR: $msg";
}
$self->say(
who => $orig->{who},
channel => $orig->{channel},
body => ($orig->{channel} eq "msg" ? "" : "$orig->{who}: ") . $msg
);
}
# Simple method called every 5 seconds
# check for old events to remove
sub tick {
my $self = shift;
my $remove = trimEvent();
# Only update the topic if an item should be removed
if ( $remove > 0 ) {
$self->update_topic();
}
# Call the tick method again in 5 seconds
return 5;
}
# Utility method for updating the topic
sub update_topic {
my $self = shift;
my $cur_topic = shift;
my $topic = getTopic();
# The topic is identical to what's there, don't update
if ( defined $cur_topic && $topic eq $cur_topic ) {
return;
}
print STDERR "Updating: $topic\n";
# Update the topic in the channel
$self->{IRCOBJ}->yield(
sl_prioritized => 30,
"TOPIC #" . $ini->{irc}{channel} . " :$topic" );
}