]> git.donarmstrong.com Git - infobot.git/blob - src/Process.pl
Changed $infobot_ to $bot_
[infobot.git] / src / Process.pl
1 ###
2 ### Process.pl: Kevin Lenzo 1997-1999
3 ###
4
5 #
6 # process the incoming message
7 #
8
9 if (&IsParam("useStrict")) { use strict; }
10
11 sub process {
12     $learnok    = 0;    # Able to learn?
13     $talkok     = 0;    # Able to yap?
14     $force_public_reply = 0;
15
16     return 'X'                  if $who eq $ident;      # self-message.
17     return 'addressedother set' if ($addressedother);
18
19     $talkok     = ($param{'addressing'} =~ /^OPTIONAL$/i or $addressed);
20     $learnok    = ($param{'learn'}      =~ /^HUNGRY$/i   or $addressed);
21
22     &shmFlush();                # hack.
23
24     # check if we have our head intact.
25     if ($lobotomized) {
26         if ($addressed and IsFlag("o") eq "o") {
27             &msg($who, "give me an unlobotomy.");
28         }
29         return 'LOBOTOMY';
30     }
31
32     # talkMethod.
33     if ($param{'talkMethod'} =~ /^PRIVATE$/i) {
34         if ($msgType =~ /public/ and $addressed) {
35             &msg($who, "sorry. i'm in 'PRIVATE' talkMethod mode ".
36                   "while you sent a message to me ${msgType}ly.");
37
38             return 'TALKMETHOD';
39         }
40     }
41
42     # ignore.
43     if ($ignore) {
44         return 'IGNORE';
45     }
46
47     # join, must be done before outsider checking.
48     if ($message =~ /^join(\s+(.*))?\s*$/i) {
49         return 'join: not addr' unless ($addressed);
50
51         $2 =~ /^($mask{chan})(,(\S+))?/;
52         my($thischan, $key) = (lc $1, $3);
53         my $chankey     = $thischan;
54         $chankey        .= " $key"      if (defined $key);
55
56         if ($thischan eq "") {
57             &help("join");
58             return;
59         }
60
61         # Thanks to Eden Li (tile) for the channel key patch
62         my @chans = split(/[\s\t]+/, $param{'join_channels'});
63         if (!grep /^$thischan$/i, @chans) {
64             if (&IsFlag("o") ne "o") {
65                 &msg($who, "I am not allowed to join $thischan.");
66                 return;
67             }
68         }
69
70         if (&validChan($thischan)) {
71             &msg($who,"I'm already on $thischan...");
72             return;
73         }
74         $joinverb{$thischan} = $who;    # used for on_join self.
75
76         &joinchan($chankey);
77         &status("JOIN $chankey <$who>");
78         &msg($who, "joining $chankey");
79
80         return;
81     }
82
83     # allowOutsiders.
84     if (&IsParam("disallowOutsiders") and $msgType =~ /private/i) {
85         my $found = 0;
86
87         foreach (keys %channels) {
88             next unless (&IsNickInChan($who,$_));
89
90             $found++;
91             last;
92         }
93
94         if (!$found and scalar(keys %channels)) {
95             &status("OUTSIDER <$who> $message");
96             return 'OUTSIDER';
97         }
98     }
99
100     # User Processing, for all users.
101     return '$noreply from userC' if &userCommands() eq $noreply;
102
103     ###
104     # once useless messages have been parsed out, we match them.
105     ###
106
107     # addressed.
108     if ($message =~ /^\Q$ident\E\s*\?*$/i) {
109         &status("feedback addressing from $who");
110
111         &performReply("yes?");
112
113         return;
114     }
115
116     # confused? is this for infobot communications?
117     foreach (keys %{$lang{'confused'}}) {
118         my $y = $_;
119
120         next unless ($message =~ /^\Q$y\E\s*/);
121         return 'CONFUSO';
122     }
123
124     # hello. [took me a while to fix this. -xk]
125     if ($orig{message} =~ /^(\Q$ident\E\S?[:, ]\S?)?\s*(h(ello|i( there)?|owdy|ey|ola))( \Q$ident\E)?\s*$/i) {
126         return '' unless ($talkok);
127
128         # 'mynick: hi' or 'hi mynick' or 'hi'.
129         &status("somebody said hello");
130
131         # 50% chance of replying to a random greeting when not addressed
132         if (!defined $5 and $addressed == 0 and rand() < 0.5) {
133             &status("not returning unaddressed greeting");
134             return;
135         }
136
137         # customized random message.
138         my $tmp = (rand() < 0.5) ? ", $who" : "";
139         &performStrictReply(&getRandom(keys %{$lang{'hello'}}) . $tmp);
140         return;
141     }
142
143     # greetings.
144     if ($message =~ /how (the hell )?are (ya|you)( doin\'?g?)?\?*$/) {
145         my $reply = &getRandom(keys %{$lang{'howareyou'}});
146         
147         &performReply($reply);
148         
149         return;
150     }
151
152     # praise.
153     if ($message =~ /you (rock|rewl|rule|are so+ coo+l)/ ||
154         $message =~ /(good (bo(t|y)|g([ui]|r+)rl))|(bot( |\-)?snack)/i)
155     {
156         return 'praise: no addr' unless ($addressed);
157
158         &status("random praise detected");
159
160         my $tmp = (rand() < 0.5) ? "thanks $who " : "";
161         &performStrictReply($tmp.":)");
162
163         return;
164     }
165
166     # thanks.
167     if ($message =~ /^than(ks?|x)( you)?( \S+)?/i) {
168         &DEBUG("thanks: talkok => '$talkok', addressed => '$addressed'.");
169         return 'thank: no addr' unless ($message =~ /$ident/ or $talkok);
170
171         &performReply( &getRandom(keys %{$lang{'welcome'}}) );
172         return;
173     }
174
175
176     ###
177     ### bot commands...
178     ###
179
180     # override msgType.
181     if ($msgType =~ /public/ and $message =~ s/^\+//) {
182         &status("found '+' flag; setting msgType to public.");
183         $force_public_reply++;
184         $msgType = 'public';
185     }
186
187
188     # karma. set...
189     if ($message =~ /^(\S+)(--|\+\+)\s*$/ and $addressed) {
190         return '' unless (&hasParam("karma"));
191
192         my($term,$inc) = (lc $1,$2);
193
194         if ($msgType !~ /public/i) {
195             &msg($who, "karma must be done in public!");
196             return;
197         }
198
199         if (lc($term) eq lc($who)) {
200             &msg($who, "please don't karma yourself");
201             return;
202         }
203
204         my $karma = &dbGet("karma", "nick",$term,"karma") || 0;
205         if ($inc eq '++') {
206             $karma++;
207         } else {
208             $karma--;
209         }
210
211         &dbSet("karma", "nick",$term,"karma",$karma);
212
213         return;
214     }
215
216     # here's where the external routines get called.
217     # if they return anything but null, that's the "answer".
218     if ($addressed) {
219         my $er = &Modules();
220         if ($er =~ /\S/) {
221             &performStrictReply($er) if ($er ne $noreply);
222             return 'SOMETHING 1';
223         }
224
225         ### FIXME: should this only apply to public messages?
226         if ($addrchar) {
227             &DEBUG("floodwho => '$floodwho'.");
228             delete $flood{$floodwho}{$message};
229             &status("short return due to unknown command.");
230             return 'ADDR CHAR';
231         }
232     }
233
234     if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pg|postgres|dbm)/i) {
235         &FactoidStuff();
236     } elsif ($param{'DBType'} =~ /^none$/i) {
237         return "NO FACTOIDS.";
238     } else {
239         &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
240         &shutdown();
241         exit 0;
242     }
243 }
244
245 sub FactoidStuff {
246     # inter-infobot.
247     if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
248         ### identification.
249         &status("infobot <$nuh> identified") unless $bots{$nuh};
250         $bots{$nuh} = $who;
251
252         ### communication.
253
254         # query.
255         if ($message =~ /^QUERY (<.*?>) (.*)/) {        # query.
256             my ($target,$item) = ($1,$2);
257             $item =~ s/[.\?]$//;
258
259             &status(":INFOBOT:QUERY $who: $message");
260
261             if ($_ = &getFactoid($item)) {
262                 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
263             }
264
265             return 'INFOBOT QUERY';
266         } elsif ($message =~ /^REPLY <(.*?)> (.*)/) {   # reply.
267             my ($target,$item) = ($1,$2);
268
269             &status(":INFOBOT:REPLY $who: $message");
270
271             my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
272
273             if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
274                 &msg($target, "$who knew: $lhs $mhs $rhs");
275
276                 # "are" hack :)
277                 $rhs = "<REPLY> are" if ($mhs eq "are");
278                 &setFactInfo($lhs, "factoid_value", $rhs);
279             }
280
281             return 'INFOBOT REPLY';
282         } else {
283             &ERROR(":INFOBOT:UNKNOWN $who: $message");
284             return 'INFOBOT UNKNOWN';
285         }
286     }
287
288
289     # factoid forget.
290     if ($message =~ s/^forget\s+//i) {
291         return 'forget: no addr' unless ($addressed);
292
293         my $faqtoid = $message;
294         if ($faqtoid eq "") {
295             &help("forget");
296             return;
297         }
298
299         $faqtoid =~ tr/A-Z/a-z/;
300         my $result = &getFactoid($faqtoid);
301
302         if (defined $result) {
303             my $author = &getFactInfo($faqtoid, "created_by");
304             if (IsFlag("r") ne "r" && $author =~ /^\Q$who\E\!/i) {
305                 &msg($who, "you don't have access to remove that factoid");
306                 return;
307             }
308
309             return 'locked factoid' if (&IsLocked($faqtoid) == 1);
310
311             &status("forget: <$who> '$faqtoid' =is=> '$result'");
312             &delFactoid($faqtoid);
313
314             &performReply("i forgot $faqtoid");
315
316             $count{'Update'}++;
317         } else {
318             &performReply("i didn't have anything called '$faqtoid'");
319         }
320
321         return;
322     }
323
324     # factoid locking.
325     if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
326         return 'lock: no addr 2' unless ($addressed);
327
328         my $function = lc $1;
329         my $faqtoid  = lc $4;
330
331         if ($faqtoid eq "") {
332             &help($function);
333             return;
334         }
335
336         # strongly requested by #debian on 19991028. -xk
337         if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
338             &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
339             &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
340             return;
341         }
342
343         if (&getFactoid($faqtoid) eq "") {
344             &msg($who, "factoid \002$faqtoid\002 does not exist");
345             return;
346         }
347
348         if ($function eq "lock") {
349             &CmdLock($faqtoid);
350         } else {
351             &CmdUnLock($faqtoid);
352         }
353
354         return;
355     }
356
357     # factoid rename.
358     if ($message =~ s/^rename(\s+|$)//) {
359         return 'rename: no addr' unless ($addressed);
360
361         if ($message eq "") {
362             &help("rename");
363             return;
364         }
365
366         if ($message =~ /^'(.*)'\s+'(.*)'$/) {
367             my($from,$to) = (lc $1, lc $2);
368
369             my $result = &getFactoid($from);
370             if (defined $result) {
371                 my $author = &getFactInfo($from, "created_by");
372                 if (&IsFlag("m") and $author =~ /^\Q$who\E\!/i) {
373                     &msg($who, "It's not yours to modify.");
374                     return $noreply;
375                 }
376
377                 if ($_ = &getFactoid($to)) {
378                     &performReply("destination factoid already exists.");
379                     return;
380                 }
381
382                 &setFactInfo($from,"factoid_key",$to);
383
384                 &status("rename: <$who> '$from' is now '$to'");
385                 &performReply("i renamed '$from' to '$to'");
386             } else {
387                 &performReply("i didn't have anything called '$from'");
388             }
389         } else {
390             &msg($who,"error: wrong format. ask me about 'help rename'.");
391         }
392
393         return;
394     }
395
396     # factoid substitution. (X =~ s/A/B/FLAG)
397     if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
398         my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
399         return 'subst: no addr' unless ($addressed);
400
401         # incorrect format.
402         if ($np =~ /$delim/) {
403             &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
404             return;
405         }
406
407         # success.
408         if (my $result = &getFactoid($faqtoid)) {
409             return 'subst: locked' if (&IsLocked($faqtoid) == 1);
410             my $was = $result;
411
412             if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
413                 if (length $result > $param{'maxDataSize'}) {
414                     &performReply("that's too long");
415                     return;
416                 }
417                 &setFactInfo($faqtoid, "factoid_value", $result);
418                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
419                 &performReply("OK");
420             } else {
421                 &performReply("that doesn't contain '$op'");
422             }
423         } else {
424             &performReply("i didn't have anything called '$faqtoid'");
425         }
426
427         return;
428     }
429
430
431     # Fix up $message for question.
432     for ($message) {
433         # fix the string.
434         s/^hey([, ]+)where/where/i;
435         s/whois/who is/ig;
436         s/where can i find/where is/i;
437         s/how about/where is/i;
438         s/ da / the /ig;
439
440         # clear the string of useless words.
441         s/^(stupid )?q(uestion)?:\s+//i;
442         s/^(does )?(any|ne)(1|one|body) know //i;
443
444         s/^[uh]+m*[,\.]* +//i;
445
446         s/^well([, ]+)//i;
447         s/^still([, ]+)//i;
448         s/^(gee|boy|golly|gosh)([, ]+)//i;
449         s/^(well|and|but|or|yes)([, ]+)//i;
450
451         s/^o+[hk]+(a+y+)?([,. ]+)//i;
452         s/^g(eez|osh|olly)([,. ]+)//i;
453         s/^w(ow|hee|o+ho+)([,. ]+)//i;
454         s/^heya?,?( folks)?([,. ]+)//i;
455     }
456
457     if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
458         $correction_plausible = 1;
459         &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
460     } else {
461         $correction_plausible = 0;
462     }
463
464     my $result = &doQuestion($message);
465
466     return 'result is $noreply' if ($result eq $noreply);
467
468     if (defined $result and $result ne "") {            # question.
469         &status("question: <$who> $message");
470         $count{'Question'}++;
471     } elsif (&IsParam("perlMath") and $addressed) {     # perl math.
472         &loadMyModule("perlMath");
473         my $newresult = &perlMath();
474
475         if (defined $newresult and $newresult ne "") {
476             $result = $newresult;
477             &status("math: <$who> $message => $result");
478         }
479     }
480
481     if ($result ne "") {
482         &performStrictReply($result);
483         return;
484     } else {
485         # why would a friendly bot get passed here?
486         if (&IsParam("friendlyBots")) {
487             return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
488         }
489
490         # do the statement.
491         if ($_ = &doStatement($message)) {
492             return;
493         }
494
495         if ($addressed) {
496             &status("unparseable: $message");
497             &performReply( &getRandom(keys %{$lang{'dunno'}}) );
498             $count{'Dunno'}++;
499         }
500     }
501 }
502
503 1;