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