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