]> git.donarmstrong.com Git - infobot.git/blob - src/Process.pl
- typo for return val of &Modules()
[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) and &IsFlag("o") ne "o") {
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     if ($addressed) {
102         my $retval;
103         return 'returned from pCH'   if &parseCmdHook("main",$message);
104
105         $retval = &userCommands();
106         return unless (defined $retval);
107         return if ($retval eq $noreply);
108     }
109
110     ###
111     # once useless messages have been parsed out, we match them.
112     ###
113
114     # confused? is this for infobot communications?
115     foreach (keys %{$lang{'confused'}}) {
116         my $y = $_;
117
118         next unless ($message =~ /^\Q$y\E\s*/);
119         return 'CONFUSO';
120     }
121
122     # hello. [took me a while to fix this. -xk]
123     if ($orig{message} =~ /^(\Q$ident\E\S?[:, ]\S?)?\s*(h(ello|i( there)?|owdy|ey|ola))( \Q$ident\E)?\s*$/i) {
124         return '' unless ($talkok);
125
126         # 'mynick: hi' or 'hi mynick' or 'hi'.
127         &status("somebody said hello");
128
129         # 50% chance of replying to a random greeting when not addressed
130         if (!defined $5 and $addressed == 0 and rand() < 0.5) {
131             &status("not returning unaddressed greeting");
132             return;
133         }
134
135         # customized random message.
136         my $tmp = (rand() < 0.5) ? ", $who" : "";
137         &performStrictReply(&getRandom(keys %{$lang{'hello'}}) . $tmp);
138         return;
139     }
140
141     # greetings.
142     if ($message =~ /how (the hell )?are (ya|you)( doin\'?g?)?\?*$/) {
143         my $reply = &getRandom(keys %{$lang{'howareyou'}});
144         
145         &performReply($reply);
146         
147         return;
148     }
149
150     # praise.
151     if ($message =~ /you (rock|rewl|rule|are so+ coo+l)/ ||
152         $message =~ /(good (bo(t|y)|g([ui]|r+)rl))|(bot( |\-)?snack)/i)
153     {
154         return 'praise: no addr' unless ($addressed);
155
156         &status("random praise detected");
157
158         my $tmp = (rand() < 0.5) ? "thanks $who " : "";
159         &performStrictReply($tmp.":)");
160
161         return;
162     }
163
164     # thanks.
165     if ($message =~ /^than(ks?|x)( you)?( \S+)?/i) {
166         &DEBUG("thanks: talkok => '$talkok', addressed => '$addressed'.");
167         return 'thank: no addr' unless ($message =~ /$ident/ or $talkok);
168
169         &performReply( &getRandom(keys %{$lang{'welcome'}}) );
170         return;
171     }
172
173
174     ###
175     ### bot commands...
176     ###
177
178     # override msgType.
179     if ($msgType =~ /public/ and $message =~ s/^\+//) {
180         &status("found '+' flag; setting msgType to public.");
181         $force_public_reply++;
182         $msgType = 'public';
183     }
184
185
186     # karma. set...
187     if ($message =~ /^(\S+)(--|\+\+)\s*$/ and $addressed) {
188         return '' unless (&hasParam("karma"));
189
190         my($term,$inc) = (lc $1,$2);
191
192         if ($msgType !~ /public/i) {
193             &msg($who, "karma must be done in public!");
194             return;
195         }
196
197         if (lc($term) eq lc($who)) {
198             &msg($who, "please don't karma yourself");
199             return;
200         }
201
202         my $karma = &dbGet("karma", "nick",$term,"karma") || 0;
203         if ($inc eq '++') {
204             $karma++;
205         } else {
206             $karma--;
207         }
208
209         &dbSet("karma", "nick",$term,"karma",$karma);
210
211         return;
212     }
213
214     # here's where the external routines get called.
215     # if they return anything but null, that's the "answer".
216     if ($addressed) {
217         if ( &parseCmdHook("extra",$message) ) {
218             return 'DID SOMETHING IN PCH.';
219         }
220
221         my $er = &Modules();
222         if (!defined $er) {
223             return 'SOMETHING 1';
224         }
225
226         ### FIXME: should this only apply to public messages?
227         if ($addrchar) {
228             &DEBUG("floodwho => '$floodwho'.");
229             delete $flood{$floodwho}{$message};
230             &status("short return due to unknown command.");
231             return 'ADDR CHAR';
232         }
233     }
234
235     if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pg|postgres|dbm)/i) {
236         &FactoidStuff();
237     } elsif ($param{'DBType'} =~ /^none$/i) {
238         return "NO FACTOIDS.";
239     } else {
240         &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
241         &shutdown();
242         exit 0;
243     }
244 }
245
246 sub FactoidStuff {
247     # inter-infobot.
248     if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
249         ### identification.
250         &status("infobot <$nuh> identified") unless $bots{$nuh};
251         $bots{$nuh} = $who;
252
253         ### communication.
254
255         # query.
256         if ($message =~ /^QUERY (<.*?>) (.*)/) {        # query.
257             my ($target,$item) = ($1,$2);
258             $item =~ s/[.\?]$//;
259
260             &status(":INFOBOT:QUERY $who: $message");
261
262             if ($_ = &getFactoid($item)) {
263                 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
264             }
265
266             return 'INFOBOT QUERY';
267         } elsif ($message =~ /^REPLY <(.*?)> (.*)/) {   # reply.
268             my ($target,$item) = ($1,$2);
269
270             &status(":INFOBOT:REPLY $who: $message");
271
272             my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
273
274             if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
275                 &msg($target, "$who knew: $lhs $mhs $rhs");
276
277                 # "are" hack :)
278                 $rhs = "<REPLY> are" if ($mhs eq "are");
279                 &setFactInfo($lhs, "factoid_value", $rhs);
280             }
281
282             return 'INFOBOT REPLY';
283         } else {
284             &ERROR(":INFOBOT:UNKNOWN $who: $message");
285             return 'INFOBOT UNKNOWN';
286         }
287     }
288
289
290     # factoid forget.
291     if ($message =~ s/^forget\s+//i) {
292         return 'forget: no addr' unless ($addressed);
293
294         my $faqtoid = $message;
295         if ($faqtoid eq "") {
296             &help("forget");
297             return;
298         }
299
300         $faqtoid =~ tr/A-Z/a-z/;
301         my $result = &getFactoid($faqtoid);
302
303         if (defined $result) {
304             my $author = &getFactInfo($faqtoid, "created_by");
305             if (IsFlag("r") ne "r" && $author =~ /^\Q$who\E\!/i) {
306                 &msg($who, "you don't have access to remove that factoid");
307                 return;
308             }
309
310             return 'locked factoid' if (&IsLocked($faqtoid) == 1);
311
312             if (&IsParam("factoidDeleteDelay")) {
313                 &status("forgot (safe delete): <$who> '$faqtoid' =is=> '$result'");
314                 &setFactInfo($faqtoid, "factoid_key", $faqtoid." #DEL#");
315
316                 ### delete info. modified_ isn't really used.
317                 &setFactInfo($faqtoid, "modified_by", $who);
318                 &setFactInfo($faqtoid, "modified_time", time());
319             } else {
320                 &status("forget: <$who> '$faqtoid' =is=> '$result'");
321                 &delFactoid($faqtoid);
322             }
323
324             &performReply("i forgot $faqtoid");
325
326             $count{'Update'}++;
327         } else {
328             &performReply("i didn't have anything called '$faqtoid'");
329         }
330
331         return;
332     }
333
334     # factoid unforget/undelete.
335     if ($message =~ s/^un(forget|delete)\s+//i) {
336         return 'unforget: no addr' unless ($addressed);
337
338         if (!&IsParam("factoidDeleteDelay")) {
339             &performReply("safe delete has been disable so what is there to undelete?");
340             return;
341         }
342
343         my $faqtoid = $message;
344         if ($faqtoid eq "") {
345             &help("undelete");
346             return;
347         }
348
349         $faqtoid =~ tr/A-Z/a-z/;
350         my $result = &getFactoid($faqtoid." #DEL#");
351         my $check  = &getFactoid($faqtoid);
352
353         if (!defined $result) {
354             &performReply("i didn't have anything ('$faqtoid') to undelete.");
355             return;
356         }
357
358         if (defined $check) {
359             &performReply("cannot undeleted '$faqtoid' because it already exists?");
360             return;
361         }
362
363         &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
364
365         ### delete info. modified_ isn't really used.
366         &setFactInfo($faqtoid, "modified_by",  "");
367         &setFactInfo($faqtoid, "modified_time", 0);
368
369         &performReply("Successfully recovered '$faqtoid'.  Have fun now.");
370
371         $count{'Undelete'}++;
372
373         return;
374     }
375
376     # factoid locking.
377     if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
378         return 'lock: no addr 2' unless ($addressed);
379
380         my $function = lc $1;
381         my $faqtoid  = lc $4;
382
383         if ($faqtoid eq "") {
384             &help($function);
385             return;
386         }
387
388         # strongly requested by #debian on 19991028. -xk
389         if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
390             &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
391             &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
392             return;
393         }
394
395         if (&getFactoid($faqtoid) eq "") {
396             &msg($who, "factoid \002$faqtoid\002 does not exist");
397             return;
398         }
399
400         if ($function eq "lock") {
401             &CmdLock($faqtoid);
402         } else {
403             &CmdUnLock($faqtoid);
404         }
405
406         return;
407     }
408
409     # factoid rename.
410     if ($message =~ s/^rename(\s+|$)//) {
411         return 'rename: no addr' unless ($addressed);
412
413         if ($message eq "") {
414             &help("rename");
415             return;
416         }
417
418         if ($message =~ /^'(.*)'\s+'(.*)'$/) {
419             my($from,$to) = (lc $1, lc $2);
420
421             my $result = &getFactoid($from);
422             if (defined $result) {
423                 my $author = &getFactInfo($from, "created_by");
424                 if (&IsFlag("m") and $author =~ /^\Q$who\E\!/i) {
425                     &msg($who, "It's not yours to modify.");
426                     return;
427                 }
428
429                 if ($_ = &getFactoid($to)) {
430                     &performReply("destination factoid already exists.");
431                     return;
432                 }
433
434                 &setFactInfo($from,"factoid_key",$to);
435
436                 &status("rename: <$who> '$from' is now '$to'");
437                 &performReply("i renamed '$from' to '$to'");
438             } else {
439                 &performReply("i didn't have anything called '$from'");
440             }
441         } else {
442             &msg($who,"error: wrong format. ask me about 'help rename'.");
443         }
444
445         return;
446     }
447
448     # factoid substitution. (X =~ s/A/B/FLAG)
449     if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
450         my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
451         return 'subst: no addr' unless ($addressed);
452
453         # incorrect format.
454         if ($np =~ /$delim/) {
455             &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
456             return;
457         }
458
459         # success.
460         if (my $result = &getFactoid($faqtoid)) {
461             return 'subst: locked' if (&IsLocked($faqtoid) == 1);
462             my $was = $result;
463
464             if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
465                 if (length $result > $param{'maxDataSize'}) {
466                     &performReply("that's too long");
467                     return;
468                 }
469                 &setFactInfo($faqtoid, "factoid_value", $result);
470                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
471                 &performReply("OK");
472             } else {
473                 &performReply("that doesn't contain '$op'");
474             }
475         } else {
476             &performReply("i didn't have anything called '$faqtoid'");
477         }
478
479         return;
480     }
481
482
483     # Fix up $message for question.
484     for ($message) {
485         # fix the string.
486         s/^hey([, ]+)where/where/i;
487         s/whois/who is/ig;
488         s/where can i find/where is/i;
489         s/how about/where is/i;
490         s/ da / the /ig;
491
492         # clear the string of useless words.
493         s/^(stupid )?q(uestion)?:\s+//i;
494         s/^(does )?(any|ne)(1|one|body) know //i;
495
496         s/^[uh]+m*[,\.]* +//i;
497
498         s/^well([, ]+)//i;
499         s/^still([, ]+)//i;
500         s/^(gee|boy|golly|gosh)([, ]+)//i;
501         s/^(well|and|but|or|yes)([, ]+)//i;
502
503         s/^o+[hk]+(a+y+)?([,. ]+)//i;
504         s/^g(eez|osh|olly)([,. ]+)//i;
505         s/^w(ow|hee|o+ho+)([,. ]+)//i;
506         s/^heya?,?( folks)?([,. ]+)//i;
507     }
508
509     if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
510         $correction_plausible = 1;
511         &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
512     } else {
513         $correction_plausible = 0;
514     }
515
516     my $result = &doQuestion($message);
517     if (!defined $result or $result eq $noreply) {
518         return 'result from doQ undef.';
519     }
520
521     if (defined $result and $result ne "") {            # question.
522         &status("question: <$who> $message");
523         $count{'Question'}++;
524     } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
525         &loadMyModule("perlMath");
526         my $newresult = &perlMath();
527
528         if (defined $newresult and $newresult ne "") {
529             $result = $newresult;
530             &status("math: <$who> $message => $result");
531         }
532     }
533
534     if ($result ne "") {
535         &performStrictReply($result);
536         return;
537     } else {
538         # why would a friendly bot get passed here?
539         if (&IsParam("friendlyBots")) {
540             return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
541         }
542
543         # do the statement.
544         if (defined &doStatement($message)) {
545             return;
546         }
547
548         return unless ($addressed);
549
550         if (length $message > 64) {
551             &status("unparseable-moron: $message");
552             &performReply( &getRandom(keys %{$lang{'moron'}}) );
553             $count{'Moron'}++;
554             return;
555         }
556
557         &status("unparseable: $message");
558         &performReply( &getRandom(keys %{$lang{'dunno'}}) );
559         $count{'Dunno'}++;
560     }
561 }
562
563 1;