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