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