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