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