/
XOmBot.pl
executable file
·268 lines (213 loc) · 7.62 KB
/
XOmBot.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/perl -w
#################################################################################
# XomBot.pl [nick] #
# #
# Your friendly XOmB bot for #xomb #
# #
# This program is free software. It comes without any warranty, to the extent #
# permitted by applicable law. You can redistribute it and/or modify it under #
# the term of the Do What The Fuck You Want To Public License, Version 2, as #
# published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more #
# details #
# #
#################################################################################
package Bot;
use base qw(Bot::BasicBot);
use warnings;
use strict;
use URI::Title qw( title );
use URI::Find::Simple qw( list_uris );
use LWP 5.64;
my($businessChannel) = '#xomb';
my($pleasureChannel) = '##l2l';
my $mynick = shift || 'XOmBot';
my $browser = LWP::UserAgent->new;
my $commitid = "";
my $first = 1;
my $good = 0;
my $bad = 0;
my ($bot) = Bot->new(
server => "irc.freenode.net",
port => "8001",
channels => [ $businessChannel, $pleasureChannel , "##IwantAkitty", "#rstatus", "#osgcc", "#croc"],
nick => $mynick,
charset => 'utf-8',
);
$bot->run();
sub connected {
my $self = shift;
$self->say(channel => $_, body => "$mynick is online. !commands will show what I can do.") for (@{$self->{channels}});
}
sub tick {
#check_rss();
return 60;
}
sub said {
my $self = shift;
my $message = shift;
my $body = $message->{body};
my $nick = $message->{who};
my $channel = $message->{channel};
my $address = $message->{address};
# --- url announce ---
if(my @urls = list_uris($body)){
if($nick ne "github-xombot"){
$self->reply($message, title($_)) for (@urls);
}
}
# --- command list ---
if($body =~ m/^\!commands/ || $body =~ m/^\!h.lp/i){
# show all the commands that xombot listens to
$self->say(channel => $channel, body => "!wiki [search term] - will search the wiki for the given word or phrase.");
$self->say(channel => $channel, body => "!latest - will show the last commit to the offical XOmB repository.");
$self->say(channel => $channel, body => "!google [phrase] for [nick] - answer questions. More bangs to shoot from the hip.");
$self->say(channel => $channel, body => "!coinflip - ...");
$self->say(channel => $channel, body => "!santa - ask Santa whether $mynick has been naughty or nice.");
$self->say(channel => $channel, body => "!d[n] - rolls die with n sides");
}
if ($body =~ m/^\!wiki\s*([\w*\s]*)/){
# check if article exists in the wiki
get_wiki_entry($1, $channel);
}
if($body =~ m/^\!latest/){
# show the latest commit the next loop around
$commitid = "";
}
if($body =~ m/^\!(!*)google(!*) (.*) for (.*)/){
my($term) = $3;
my($target) = $4;
my($lucky);
if($1 ne "" || $2 ne ""){
$lucky = "&l=1";
}
$term =~ s/ /+/g;
$self->say(channel => $channel, who => $target, address => "1", body => "http://lmgtfy.com/?q=$term$lucky");
}
if($body =~ m/^\!coinflip/){
my $outcome;
if($body =~ m/^\!coinflip.* heads (.*) tails (.*)/){
$outcome = $1;
$outcome = $2 if int(rand(2)) == 1;
}else{
$outcome = "heads";
$outcome = "tails" if int(rand(2)) == 1;
}
#$self->say(channel => $channel, who => $nick, address => "1", body => "$outcome");
$self->say(channel => $channel, body => "$outcome");
}
# dice roll
if($body =~ m/^\!d(\d+)/){
$self->say(channel => $channel, body => int(rand($1)) + 1);
}
if($body =~ m/^\!santa/){
if($good >= $bad){
$self->emote(channel => $channel, body => "has been a good little robotic zombie");
}else{
$self->emote(channel => $channel, body => "is getting coal in its metal stocking");
}
}
# --- miscellaneous behaviors ---
# annoy duck
if($body eq "hi" and $nick eq "duckinator"){
$self->say(channel => $channel, who => $nick, body => "hi", address => "1");
}
my($respondedFlag) = 0;
if ($address || $body =~ m/$mynick/){
my $compliment = $body;
if($compliment =~ m/good/i || $compliment =~ m/cookie/i || $compliment =~ m/<3/){
$self->emote(channel => $channel, body => "drools");
$good++;
$respondedFlag = 1;
}elsif($compliment =~ m/bad/i || $compliment =~ m/spank/i){
$self->emote(channel => $channel, body => "cowers");
$bad++;
$respondedFlag = 1;
}
}
if($address && !$respondedFlag){
$self->say(channel => $channel, who => $nick, address => "1", body => "brains...");
}
return undef;
}
sub check_rss {
my $response = $browser->get("http://github.com/feeds/xomboverlord/commits/xomb/unborn");
if($response->is_success && $response->content =~ m/<entry>\s*<id>.*\/(\w*)<\/id>/){
my $commiter = "Unknown";
my $commit_msg = "Unspecified";
my $orig_commitid = $commitid;
#if it's not the last one we announced
if($commitid ne $1){
$commitid = $1;
# get try to get the info to announce it
if($response->content =~ m/<entry>.*?<title>(.*?)<\/title>/s){
$commit_msg = $1;
}
# get try to get the author to announce it
if($response->content =~ m/<name>(\w*)<\/name>/){
$commiter = $1;
}
if($commit_msg ne "Sorry, this commit log is taking too long to generate."){
unless($first){
$bot->say(channel => $businessChannel, body => "Commit made by $commiter: $commit_msg");
$bot->say(channel => $businessChannel, body => "View: http://github.com/xomboverlord/xomb/commit/$commitid");
}else{
$first = 0;
}
}else{
# we don't know if there actually was a new commit
$commitid = $orig_commitid;
}
}
}
}
sub get_wiki_entry {
my $articlename = shift;
my $channel = shift;
#replace spaces with +
$articlename =~ s/\s/\+/gs;
# searching for the article rather than going directly to it allows us to ignore the issue of case sensitivity
my $response = $browser->get("http://wiki.xomb.org/index.php?search=$articlename&go=Go");
#check if article exists
my $exists = 1;
if($response->content =~ m/There is no page titled/)
{
$exists = 0;
}
my $content = $response->content;
# remove the html tags
# the following line breaks syntax highlighting in intype :(
$content =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs;
#replace + with spaces
$articlename =~ s/\+/ /gs;
if ($response->is_success && $exists)
{
# try to get a definition
if( $content =~ m/(.*?$articlename.*?\.)/i)
{
$bot->say(channel => $channel, body => $1);
}
#replace spaces with _
$articlename =~ s/\s/_/gs;
$bot->say(channel => $channel, body => "Full article here: " . $response->base);
}
else
{
$bot->say(channel => $channel, body => "Sorry, there's no article by that name");
# try to search for similar articles
search_for_article($articlename, $channel);
}
}
sub search_for_article {
my $searchterm = shift;
my $channel = shift;
# replace spaces with +
$searchterm =~ s/\s/\+/gs;
my $response = $browser->get("http://wiki.xomb.org/index.php?search=$searchterm&go=Go");
if($response->is_success)
{
if($response->content =~ m/<li><a href="\/index\.php\?title=(\w*)/)
{
$bot->say(channel => $channel, body => "Did you mean $1: http://wiki.xomb.org/index.php?title=$1");
}
}
}