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