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