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