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