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