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