]> git.donarmstrong.com Git - infobot.git/blob - src/Process.pl
don't backup #DEL# factoids. asuffield.
[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
381     # factoid forget.
382     if ($message =~ s/^forget\s+//i) {
383         return 'forget: no addr' unless ($addressed);
384
385         my $faqtoid = $message;
386         if ($faqtoid eq "") {
387             &help("forget");
388             return;
389         }
390
391         $faqtoid =~ tr/A-Z/a-z/;
392         my $result = &getFactoid($faqtoid);
393
394         if (defined $result) {
395             my $author = &getFactInfo($faqtoid, "created_by");
396             if (IsFlag("r") ne "r") {
397                 &msg($who, "you don't have access to remove factoids");
398                 return;
399             }
400
401             return 'locked factoid' if (&IsLocked($faqtoid) == 1);
402
403             if (&IsParam("factoidDeleteDelay")) {
404                 if ($faqtoid =~ /#DEL#/ and !&IsFlag("o")) {
405                     &msg($who, "cannot delete it ($faqtoid).");
406                     return;
407                 }
408                 &status("forgot (safe delete): <$who> '$faqtoid' =is=> '$result'");
409                 ### TODO: check if the "backup" exists and overwrite it
410                 my $check = &getFactoid("$faqtoid #DEL#");
411                 if (!$check) {
412                     if ($faqtoid !~ /#DEL#/) {
413                         &setFactInfo($faqtoid, "factoid_key", $faqtoid." #DEL#");
414
415                         &setFactInfo($faqtoid, "modified_by", $who);
416                         &setFactInfo($faqtoid, "modified_time", time());
417                     } else {
418                         &status("not backing up $faqtoid.");
419                     }
420                 } else {
421                     &status("forget: not overwriting backup!");
422                 }
423             } else {
424                 &status("forget: <$who> '$faqtoid' =is=> '$result'");
425             }
426             &delFactoid($faqtoid);
427
428             &performReply("i forgot $faqtoid");
429
430             $count{'Update'}++;
431         } else {
432             &performReply("i didn't have anything called '$faqtoid'");
433         }
434
435         return;
436     }
437
438     # factoid unforget/undelete.
439     if ($message =~ s/^un(forget|delete)\s+//i) {
440         return 'unforget: no addr' unless ($addressed);
441
442         if (!&IsParam("factoidDeleteDelay")) {
443             &performReply("safe delete has been disable so what is there to undelete?");
444             return;
445         }
446
447         my $faqtoid = $message;
448         if ($faqtoid eq "") {
449             &help("undelete");
450             return;
451         }
452
453         $faqtoid =~ tr/A-Z/a-z/;
454         my $result = &getFactoid($faqtoid." #DEL#");
455         my $check  = &getFactoid($faqtoid);
456
457         if (!defined $result) {
458             &performReply("i didn't have anything ('$faqtoid') to undelete.");
459             return;
460         }
461
462         &DEBUG("unforget: check => $check");
463         if (defined $check) {
464             &performReply("cannot undeleted '$faqtoid' because it already exists?");
465             return;
466         }
467
468         &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
469
470         ### delete info. modified_ isn't really used.
471         &setFactInfo($faqtoid, "modified_by",  "");
472         &setFactInfo($faqtoid, "modified_time", 0);
473
474         &performReply("Successfully recovered '$faqtoid'.  Have fun now.");
475
476         $count{'Undelete'}++;
477
478         return;
479     }
480
481     # factoid locking.
482     if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
483         return 'lock: no addr 2' unless ($addressed);
484
485         my $function = lc $1;
486         my $faqtoid  = lc $4;
487
488         if ($faqtoid eq "") {
489             &help($function);
490             return;
491         }
492
493         if (&getFactoid($faqtoid) eq "") {
494             &msg($who, "factoid \002$faqtoid\002 does not exist");
495             return;
496         }
497
498         if ($function eq "lock") {
499             # strongly requested by #debian on 19991028. -xk
500             if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
501                 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
502                 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
503                 return;
504             }
505
506             &CmdLock($faqtoid);
507         } else {
508             &CmdUnLock($faqtoid);
509         }
510
511         return;
512     }
513
514     # factoid rename.
515     if ($message =~ s/^rename(\s+|$)//) {
516         return 'rename: no addr' unless ($addressed);
517
518         if ($message eq "") {
519             &help("rename");
520             return;
521         }
522
523         if ($message =~ /^'(.*)'\s+'(.*)'$/) {
524             my($from,$to) = (lc $1, lc $2);
525
526             my $result = &getFactoid($from);
527             if (defined $result) {
528                 my $author = &getFactInfo($from, "created_by");
529
530                 if (&IsFlag("m") or $author =~ /^\Q$who\E\!/i) {
531                     &msg($who, "It's not yours to modify.");
532                     return;
533                 }
534
535                 if ($_ = &getFactoid($to)) {
536                     &performReply("destination factoid already exists.");
537                     return;
538                 }
539
540                 &setFactInfo($from,"factoid_key",$to);
541
542                 &status("rename: <$who> '$from' is now '$to'");
543                 &performReply("i renamed '$from' to '$to'");
544             } else {
545                 &performReply("i didn't have anything called '$from'");
546             }
547         } else {
548             &msg($who,"error: wrong format. ask me about 'help rename'.");
549         }
550
551         return;
552     }
553
554     # factoid substitution. (X =~ s/A/B/FLAG)
555     if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
556         my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
557         return 'subst: no addr' unless ($addressed);
558
559         # incorrect format.
560         if ($np =~ /$delim/) {
561             &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
562             return;
563         }
564
565         # success.
566         if (my $result = &getFactoid($faqtoid)) {
567             return 'subst: locked' if (&IsLocked($faqtoid) == 1);
568             my $was = $result;
569
570             if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
571                 if (length $result > $param{'maxDataSize'}) {
572                     &performReply("that's too long");
573                     return;
574                 }
575                 &setFactInfo($faqtoid, "factoid_value", $result);
576                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
577                 &performReply("OK");
578             } else {
579                 &performReply("that doesn't contain '$op'");
580             }
581         } else {
582             &performReply("i didn't have anything called '$faqtoid'");
583         }
584
585         return;
586     }
587
588
589     # Fix up $message for question.
590     for ($message) {
591         # fix the string.
592         s/^hey([, ]+)where/where/i;
593         s/whois/who is/ig;
594         s/where can i find/where is/i;
595         s/how about/where is/i;
596         s/ da / the /ig;
597
598         # clear the string of useless words.
599         s/^(stupid )?q(uestion)?:\s+//i;
600         s/^(does )?(any|ne)(1|one|body) know //i;
601
602         s/^[uh]+m*[,\.]* +//i;
603
604         s/^well([, ]+)//i;
605         s/^still([, ]+)//i;
606         s/^(gee|boy|golly|gosh)([, ]+)//i;
607         s/^(well|and|but|or|yes)([, ]+)//i;
608
609         s/^o+[hk]+(a+y+)?([,. ]+)//i;
610         s/^g(eez|osh|olly)([,. ]+)//i;
611         s/^w(ow|hee|o+ho+)([,. ]+)//i;
612         s/^heya?,?( folks)?([,. ]+)//i;
613     }
614
615     if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
616         $correction_plausible = 1;
617         &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
618     } else {
619         $correction_plausible = 0;
620     }
621
622     my $result = &doQuestion($message);
623     if (!defined $result or $result eq $noreply) {
624         return 'result from doQ undef.';
625     }
626
627     if (defined $result and $result !~ /^0?$/) {        # question.
628         &status("question: <$who> $message");
629         $count{'Question'}++;
630     } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
631         &loadMyModule("perlMath");
632         my $newresult = &perlMath();
633
634         if (defined $newresult and $newresult ne "") {
635             $result = $newresult;
636             &status("math: <$who> $message => $result");
637         }
638     }
639
640     if ($result !~ /^0?$/) {
641         &performStrictReply($result);
642         return;
643     }
644
645     # why would a friendly bot get passed here?
646     if (&IsParam("friendlyBots")) {
647         return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
648     }
649
650     # do the statement.
651     if (!defined &doStatement($message)) {
652         return;
653     }
654
655     return unless ($addressed);
656
657     if (length $message > 64) {
658         &status("unparseable-moron: $message");
659         &performReply( &getRandom(keys %{$lang{'moron'}}) );
660         $count{'Moron'}++;
661         return;
662     }
663
664     &status("unparseable: $message");
665     &performReply( &getRandom(keys %{$lang{'dunno'}}) );
666     $count{'Dunno'}++;
667 }
668
669 1;