]> git.donarmstrong.com Git - infobot.git/blob - src/Process.pl
fixed up ignore code. discovered by debian@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
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                 &setFactInfo($faqtoid, "factoid_key", $faqtoid." #DEL#");
348
349                 ### delete info. modified_ isn't really used.
350                 &setFactInfo($faqtoid, "modified_by", $who);
351                 &setFactInfo($faqtoid, "modified_time", time());
352             } else {
353                 &status("forget: <$who> '$faqtoid' =is=> '$result'");
354                 &delFactoid($faqtoid);
355             }
356
357             &performReply("i forgot $faqtoid");
358
359             $count{'Update'}++;
360         } else {
361             &performReply("i didn't have anything called '$faqtoid'");
362         }
363
364         return;
365     }
366
367     # factoid unforget/undelete.
368     if ($message =~ s/^un(forget|delete)\s+//i) {
369         return 'unforget: no addr' unless ($addressed);
370
371         if (!&IsParam("factoidDeleteDelay")) {
372             &performReply("safe delete has been disable so what is there to undelete?");
373             return;
374         }
375
376         my $faqtoid = $message;
377         if ($faqtoid eq "") {
378             &help("undelete");
379             return;
380         }
381
382         $faqtoid =~ tr/A-Z/a-z/;
383         my $result = &getFactoid($faqtoid." #DEL#");
384         my $check  = &getFactoid($faqtoid);
385
386         if (!defined $result) {
387             &performReply("i didn't have anything ('$faqtoid') to undelete.");
388             return;
389         }
390
391         if (defined $check) {
392             &performReply("cannot undeleted '$faqtoid' because it already exists?");
393             return;
394         }
395
396         &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
397
398         ### delete info. modified_ isn't really used.
399         &setFactInfo($faqtoid, "modified_by",  "");
400         &setFactInfo($faqtoid, "modified_time", 0);
401
402         &performReply("Successfully recovered '$faqtoid'.  Have fun now.");
403
404         $count{'Undelete'}++;
405
406         return;
407     }
408
409     # factoid locking.
410     if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
411         return 'lock: no addr 2' unless ($addressed);
412
413         my $function = lc $1;
414         my $faqtoid  = lc $4;
415
416         if ($faqtoid eq "") {
417             &help($function);
418             return;
419         }
420
421         if (&getFactoid($faqtoid) eq "") {
422             &msg($who, "factoid \002$faqtoid\002 does not exist");
423             return;
424         }
425
426         if ($function eq "lock") {
427             # strongly requested by #debian on 19991028. -xk
428             if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
429                 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
430                 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
431                 return;
432             }
433
434             &CmdLock($faqtoid);
435         } else {
436             &CmdUnLock($faqtoid);
437         }
438
439         return;
440     }
441
442     # factoid rename.
443     if ($message =~ s/^rename(\s+|$)//) {
444         return 'rename: no addr' unless ($addressed);
445
446         if ($message eq "") {
447             &help("rename");
448             return;
449         }
450
451         if ($message =~ /^'(.*)'\s+'(.*)'$/) {
452             my($from,$to) = (lc $1, lc $2);
453
454             my $result = &getFactoid($from);
455             if (defined $result) {
456                 my $author = &getFactInfo($from, "created_by");
457                 if (&IsFlag("m") and $author =~ /^\Q$who\E\!/i) {
458                     &msg($who, "It's not yours to modify.");
459                     return;
460                 }
461
462                 if ($_ = &getFactoid($to)) {
463                     &performReply("destination factoid already exists.");
464                     return;
465                 }
466
467                 &setFactInfo($from,"factoid_key",$to);
468
469                 &status("rename: <$who> '$from' is now '$to'");
470                 &performReply("i renamed '$from' to '$to'");
471             } else {
472                 &performReply("i didn't have anything called '$from'");
473             }
474         } else {
475             &msg($who,"error: wrong format. ask me about 'help rename'.");
476         }
477
478         return;
479     }
480
481     # factoid substitution. (X =~ s/A/B/FLAG)
482     if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
483         my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
484         return 'subst: no addr' unless ($addressed);
485
486         # incorrect format.
487         if ($np =~ /$delim/) {
488             &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
489             return;
490         }
491
492         # success.
493         if (my $result = &getFactoid($faqtoid)) {
494             return 'subst: locked' if (&IsLocked($faqtoid) == 1);
495             my $was = $result;
496
497             if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
498                 if (length $result > $param{'maxDataSize'}) {
499                     &performReply("that's too long");
500                     return;
501                 }
502                 &setFactInfo($faqtoid, "factoid_value", $result);
503                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
504                 &performReply("OK");
505             } else {
506                 &performReply("that doesn't contain '$op'");
507             }
508         } else {
509             &performReply("i didn't have anything called '$faqtoid'");
510         }
511
512         return;
513     }
514
515
516     # Fix up $message for question.
517     for ($message) {
518         # fix the string.
519         s/^hey([, ]+)where/where/i;
520         s/whois/who is/ig;
521         s/where can i find/where is/i;
522         s/how about/where is/i;
523         s/ da / the /ig;
524
525         # clear the string of useless words.
526         s/^(stupid )?q(uestion)?:\s+//i;
527         s/^(does )?(any|ne)(1|one|body) know //i;
528
529         s/^[uh]+m*[,\.]* +//i;
530
531         s/^well([, ]+)//i;
532         s/^still([, ]+)//i;
533         s/^(gee|boy|golly|gosh)([, ]+)//i;
534         s/^(well|and|but|or|yes)([, ]+)//i;
535
536         s/^o+[hk]+(a+y+)?([,. ]+)//i;
537         s/^g(eez|osh|olly)([,. ]+)//i;
538         s/^w(ow|hee|o+ho+)([,. ]+)//i;
539         s/^heya?,?( folks)?([,. ]+)//i;
540     }
541
542     if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
543         $correction_plausible = 1;
544         &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
545     } else {
546         $correction_plausible = 0;
547     }
548
549     my $result = &doQuestion($message);
550     if (!defined $result or $result eq $noreply) {
551         return 'result from doQ undef.';
552     }
553
554     if (defined $result and $result !~ /^0?$/) {        # question.
555         &status("question: <$who> $message");
556         $count{'Question'}++;
557     } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
558         &loadMyModule("perlMath");
559         my $newresult = &perlMath();
560
561         if (defined $newresult and $newresult ne "") {
562             $result = $newresult;
563             &status("math: <$who> $message => $result");
564         }
565     }
566
567     if ($result !~ /^0?$/) {
568         &performStrictReply($result);
569         return;
570     }
571
572     # why would a friendly bot get passed here?
573     if (&IsParam("friendlyBots")) {
574         return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
575     }
576
577     # do the statement.
578     if (!defined &doStatement($message)) {
579         return;
580     }
581
582     return unless ($addressed);
583
584     if (length $message > 64) {
585         &status("unparseable-moron: $message");
586         &performReply( &getRandom(keys %{$lang{'moron'}}) );
587         $count{'Moron'}++;
588         return;
589     }
590
591     &status("unparseable: $message");
592     &performReply( &getRandom(keys %{$lang{'dunno'}}) );
593     $count{'Dunno'}++;
594 }
595
596 1;