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