]> git.donarmstrong.com Git - infobot.git/blob - src/Process.pl
- irctextcounters: add percentage to top3
[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=".&dbQuote($term).
298                         " AND type='karma'") || 0;
299         if ($inc eq '++') {
300             $karma++;
301         } else {
302             $karma--;
303         }
304
305         &dbSet("stats", 
306                 { nick => $term, type => "karma" },
307                 { counter => $karma }
308         );
309
310         return;
311     }
312
313     # here's where the external routines get called.
314     # if they return anything but null, that's the "answer".
315     if ($addressed) {
316         if ( &parseCmdHook("extra",$message) ) {
317             return 'DID SOMETHING IN PCH.';
318         }
319
320         my $er = &Modules();
321         if (!defined $er) {
322             return 'SOMETHING 1';
323         }
324
325         if (0 and $addrchar) {
326             &msg($who, "I don't trust people to use the core commands while addressing me in a short-cut way.");
327             return;
328         }
329     }
330
331     if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pg|postgres|dbm)/i) {
332         &FactoidStuff();
333     } elsif ($param{'DBType'} =~ /^none$/i) {
334         return "NO FACTOIDS.";
335     } else {
336         &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
337         &shutdown();
338         exit 0;
339     }
340 }
341
342 sub FactoidStuff {
343     # inter-infobot.
344     if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
345         ### identification.
346         &status("infobot <$nuh> identified") unless $bots{$nuh};
347         $bots{$nuh} = $who;
348
349         ### communication.
350
351         # query.
352         if ($message =~ /^QUERY (<.*?>) (.*)/) {        # query.
353             my ($target,$item) = ($1,$2);
354             $item =~ s/[.\?]$//;
355
356             &status(":INFOBOT:QUERY $who: $message");
357
358             if ($_ = &getFactoid($item)) {
359                 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
360             }
361
362             return 'INFOBOT QUERY';
363         } elsif ($message =~ /^REPLY <(.*?)> (.*)/) {   # reply.
364             my ($target,$item) = ($1,$2);
365
366             &status(":INFOBOT:REPLY $who: $message");
367
368             my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
369
370             if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
371                 &msg($target, "$who knew: $lhs $mhs $rhs");
372
373                 # "are" hack :)
374                 $rhs = "<REPLY> are" if ($mhs eq "are");
375                 &setFactInfo($lhs, "factoid_value", $rhs);
376             }
377
378             return 'INFOBOT REPLY';
379         } else {
380             &ERROR(":INFOBOT:UNKNOWN $who: $message");
381             return 'INFOBOT UNKNOWN';
382         }
383     }
384
385     # factoid forget.
386     if ($message =~ s/^forget\s+//i) {
387         return 'forget: no addr' unless ($addressed);
388
389         my $faqtoid = $message;
390         if ($faqtoid eq "") {
391             &help("forget");
392             return;
393         }
394
395         $faqtoid =~ tr/A-Z/a-z/;
396         my $result = &getFactoid($faqtoid);
397
398         if (defined $result) {
399             my $author  = &getFactInfo($faqtoid, "created_by");
400             my $count   = &getFactInfo($faqtoid, "requested_count") || 0;
401             my $limit   = &getChanConfDefault("factoidPreventForgetLimit", 
402                                 0, $chan);
403
404             &DEBUG("forget: limit = $limit");
405             &DEBUG("forget: count = $count");
406
407             if (IsFlag("r") ne "r") {
408                 &msg($who, "you don't have access to remove factoids");
409                 return;
410             }
411
412             return 'locked factoid' if (&IsLocked($faqtoid) == 1);
413
414             # factoidPreventForgetLimit:
415             if ($limit and $count > $limit and (&IsFlag("o") ne "o")) {
416                 &msg($who, "will not delete '$faqtoid', count > limit ($count > $limit)");
417                 return;
418             }
419
420             if (&IsParam("factoidDeleteDelay") or &IsChanConf("factoidDeleteDelay")) {
421                 if ($faqtoid =~ / #DEL#$/ and !&IsFlag("o")) {
422                     &msg($who, "cannot delete it ($faqtoid).");
423                     return;
424                 }
425
426                 &status("forgot (safe delete): <$who> '$faqtoid' =is=> '$result'");
427                 ### TODO: check if the "backup" exists and overwrite it
428                 my $check = &getFactoid("$faqtoid #DEL#");
429
430                 if (!defined $check or $check =~ /^\s*$/) {
431                     if ($faqtoid !~ / #DEL#$/) {
432                         my $new = $faqtoid." #DEL#";
433
434                         my $backup = &getFactoid($faqtoid);
435                         # this looks weird but does it work?
436                         if ($backup) {
437                             &DEBUG("forget: not overwriting backup: $faqtoid");
438                         } else {
439                             &status("forget: backing up '$faqtoid'");
440                             &setFactInfo($faqtoid, "factoid_key", $new);
441                             &setFactInfo($new, "modified_by", $who);
442                             &setFactInfo($new, "modified_time", time());
443                         }
444
445                     } else {
446                         &status("forget: not backing up $faqtoid.");
447                     }
448
449                 } else {
450                     &status("forget: not overwriting backup!");
451                 }
452
453             } else {
454                 &status("forget: <$who> '$faqtoid' =is=> '$result'");
455             }
456             &delFactoid($faqtoid);
457
458             &performReply("i forgot $faqtoid");
459
460             $count{'Update'}++;
461         } else {
462             &performReply("i didn't have anything called '$faqtoid'");
463         }
464
465         return;
466     }
467
468     # factoid unforget/undelete.
469     if ($message =~ s/^un(forget|delete)\s+//i) {
470         return 'unforget: no addr' unless ($addressed);
471
472         my $i = 0;
473         $i++ if (&IsParam("factoidDeleteDelay"));
474         $i++ if (&IsChanConf("factoidDeleteDelay"));
475         if (!$i) {
476             &performReply("safe delete has been disable so what is there to undelete?");
477             return;
478         }
479
480         my $faqtoid = $message;
481         if ($faqtoid eq "") {
482             &help("undelete");
483             return;
484         }
485
486         $faqtoid =~ tr/A-Z/a-z/;
487         my $result = &getFactoid($faqtoid." #DEL#");
488         my $check  = &getFactoid($faqtoid);
489
490         if (!defined $result) {
491             &performReply("i didn't have anything ('$faqtoid') to undelete.");
492             return;
493         }
494
495         if (defined $check) {
496             &performReply("cannot undeleted '$faqtoid' because it already exists?");
497             return;
498         }
499
500         &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
501
502         ### delete info. modified_ isn't really used.
503         &setFactInfo($faqtoid, "modified_by",  "");
504         &setFactInfo($faqtoid, "modified_time", 0);
505
506         &performReply("Successfully recovered '$faqtoid'.  Have fun now.");
507
508         $count{'Undelete'}++;
509
510         return;
511     }
512
513     # factoid locking.
514     if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
515         return 'lock: no addr 2' unless ($addressed);
516
517         my $function = lc $1;
518         my $faqtoid  = lc $4;
519
520         if ($faqtoid eq "") {
521             &help($function);
522             return;
523         }
524
525         if (&getFactoid($faqtoid) eq "") {
526             &msg($who, "factoid \002$faqtoid\002 does not exist");
527             return;
528         }
529
530         if ($function eq "lock") {
531             # strongly requested by #debian on 19991028. -xk
532             if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
533                 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
534                 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
535                 return;
536             }
537
538             &CmdLock($faqtoid);
539         } else {
540             &CmdUnLock($faqtoid);
541         }
542
543         return;
544     }
545
546     # factoid rename.
547     if ($message =~ s/^rename(\s+|$)//) {
548         return 'rename: no addr' unless ($addressed);
549
550         if ($message eq "") {
551             &help("rename");
552             return;
553         }
554
555         if ($message =~ /^'(.*)'\s+'(.*)'$/) {
556             my($from,$to) = (lc $1, lc $2);
557
558             my $result = &getFactoid($from);
559             if (defined $result) {
560                 my $author = &getFactInfo($from, "created_by");
561
562                 if (&IsFlag("m") or $author =~ /^\Q$who\E\!/i) {
563                     &msg($who, "It's not yours to modify.");
564                     return;
565                 }
566
567                 if ($_ = &getFactoid($to)) {
568                     &performReply("destination factoid already exists.");
569                     return;
570                 }
571
572                 &setFactInfo($from,"factoid_key",$to);
573
574                 &status("rename: <$who> '$from' is now '$to'");
575                 &performReply("i renamed '$from' to '$to'");
576             } else {
577                 &performReply("i didn't have anything called '$from'");
578             }
579         } else {
580             &msg($who,"error: wrong format. ask me about 'help rename'.");
581         }
582
583         return;
584     }
585
586     # factoid substitution. (X =~ s/A/B/FLAG)
587     if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
588         my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
589         return 'subst: no addr' unless ($addressed);
590
591         # incorrect format.
592         if ($np =~ /$delim/) {
593             &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
594             return;
595         }
596
597         # success.
598         if (my $result = &getFactoid($faqtoid)) {
599             return 'subst: locked' if (&IsLocked($faqtoid) == 1);
600             my $was = $result;
601
602             if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
603                 if (length $result > $param{'maxDataSize'}) {
604                     &performReply("that's too long");
605                     return;
606                 }
607                 &setFactInfo($faqtoid, "factoid_value", $result);
608                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
609                 &performReply("OK");
610             } else {
611                 &performReply("that doesn't contain '$op'");
612             }
613         } else {
614             &performReply("i didn't have anything called '$faqtoid'");
615         }
616
617         return;
618     }
619
620     # Fix up $message for question.
621     my $question = $message;
622     for ($question) {
623         # fix the string.
624         s/^hey([, ]+)where/where/i;
625         s/\s+\?$/?/;
626         s/whois/who is/ig;
627         s/where can i find/where is/i;
628         s/how about/where is/i;
629         s/ da / the /ig;
630
631         # clear the string of useless words.
632         s/^(stupid )?q(uestion)?:\s+//i;
633         s/^(does )?(any|ne)(1|one|body) know //i;
634
635         s/^[uh]+m*[,\.]* +//i;
636
637         s/^well([, ]+)//i;
638         s/^still([, ]+)//i;
639         s/^(gee|boy|golly|gosh)([, ]+)//i;
640         s/^(well|and|but|or|yes)([, ]+)//i;
641
642         s/^o+[hk]+(a+y+)?([,. ]+)//i;
643         s/^g(eez|osh|olly)([,. ]+)//i;
644         s/^w(ow|hee|o+ho+)([,. ]+)//i;
645         s/^heya?,?( folks)?([,. ]+)//i;
646     }
647
648     if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
649         $correction_plausible = 1;
650         &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
651     } else {
652         $correction_plausible = 0;
653     }
654
655     my $result = &doQuestion($question);
656     if (!defined $result or $result eq $noreply) {
657         return 'result from doQ undef.';
658     }
659
660     if (defined $result and $result !~ /^0?$/) {        # question.
661         &status("question: <$who> $message");
662         $count{'Question'}++;
663     } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
664         &loadMyModule("perlMath");
665         my $newresult = &perlMath();
666
667         if (defined $newresult and $newresult ne "") {
668             $cmdstats{'Maths'}++;
669             $result = $newresult;
670             &status("math: <$who> $message => $result");
671         }
672     }
673
674     if ($result !~ /^0?$/) {
675         &performStrictReply($result);
676         return;
677     }
678
679     # why would a friendly bot get passed here?
680     if (&IsParam("friendlyBots")) {
681         return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
682     }
683
684     # do the statement.
685     if (!defined &doStatement($message)) {
686         return;
687     }
688
689     return unless ($addressed);
690
691     if (length $message > 64) {
692         &status("unparseable-moron: $message");
693 #       &performReply( &getRandom(keys %{ $lang{'moron'} }) );
694         $count{'Moron'}++;
695
696         &performReply("You are moron \002#". $count{'Moron'} ."\002");
697         return;
698     }
699
700     &status("unparseable: $message");
701     &performReply( &getRandom(keys %{ $lang{'dunno'} }) );
702     $count{'Dunno'}++;
703 }
704
705 1;