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