2 ### Process.pl: Kevin Lenzo 1997-1999
6 # process the incoming message
9 if (&IsParam("useStrict")) { use strict; }
12 $learnok = 0; # Able to learn?
13 $talkok = 0; # Able to yap?
14 $force_public_reply = 0;
17 return 'X' if $who eq $ident; # self-message.
18 return 'addressedother set' if ($addressedother);
20 $talkok = ($param{'addressing'} =~ /^OPTIONAL$/i or $addressed);
21 $learnok = ($param{'learn'} =~ /^HUNGRY$/i or $addressed);
25 # check if we have our head intact.
27 if ($addressed and IsFlag("o") eq "o") {
28 my $delta_time = time() - ($cache{lobotomy}{$who} || 0);
29 &msg($who, "give me an unlobotomy.") if ($delta_time > 60*60);
30 $cache{lobotomy}{$who} = time();
36 if ($param{'talkMethod'} =~ /^PRIVATE$/i) {
37 if ($msgType =~ /public/ and $addressed) {
38 &msg($who, "sorry. i'm in 'PRIVATE' talkMethod mode ".
39 "while you sent a message to me ${msgType}ly.");
45 # join, must be done before outsider checking.
46 if ($message =~ /^join(\s+(.*))?\s*$/i) {
47 return 'join: not addr' unless ($addressed);
49 $2 =~ /^($mask{chan})(,(\S+))?/;
50 my($thischan, $key) = (lc $1, $3);
51 my $chankey = lc $thischan;
52 $chankey .= " $key" if (defined $key);
54 if ($thischan eq "") {
59 if (&IsFlag("o") ne "o") {
60 if (!exists $chanconf{$thischan}) {
61 &msg($who, "I am not allowed to join $thischan.");
65 if (&validChan($thischan)) {
66 &msg($who,"warn: I'm already on $thischan, joining anyway...");
70 $cache{join}{$thischan} = $who; # used for on_join self.
73 &status("JOIN $chankey <$who>");
74 &msg($who, "joining $chankey");
80 if ($msgType =~ /private/ and $message =~ s/^identify//i) {
81 $message =~ s/^\s+|\s+$//g;
82 my @array = split / /, $message;
84 if ($who =~ /^_default$/i) {
85 &pSReply("you are too eleet.");
89 if (!scalar @array or scalar @array > 2) {
94 my $do_nick = $array[1] || $who;
96 if (!exists $users{$do_nick}) {
97 &pSReply("nick $do_nick is not in user list.");
101 my $crypt = $users{$do_nick}{PASS};
102 if (!defined $crypt) {
103 &pSReply("user $do_nick has no passwd set.");
107 if (!&ckpasswd($array[0], $crypt)) {
108 &pSReply("invalid passwd for $do_nick.");
112 my $mask = "*!$user@".&makeHostMask($host);
113 ### TODO: prevent adding multiple dupe masks?
114 ### TODO: make &addHostMask() CMD?
115 &pSReply("Added $mask for $do_nick...");
116 $users{$do_nick}{HOSTS}{$mask} = 1;
122 if ($msgType =~ /private/ and $message =~ s/^pass//i) {
123 $message =~ s/^\s+|\s+$//g;
124 my @array = split ' ', $message;
126 if ($who =~ /^_default$/i) {
127 &pSReply("you are too eleet.");
131 if (scalar @array != 1) {
136 # todo: use &getUser()?
137 my $first = (scalar keys %users < 2) ? 1 : 0;
139 if (!exists $users{$who} and !$first) {
140 &pSReply("nick $who is not in user list.");
145 &pSReply("First time user... adding you as Master.");
146 $users{$who}{FLAGS} = "mrsteon";
149 my $crypt = $users{$who}{PASS};
150 if (defined $crypt) {
151 &pSReply("user $who already has pass set.");
155 if (!defined $host) {
156 &WARN("pass: host == NULL.");
160 if (!scalar keys %{ $users{$who}{HOSTS} }) {
161 my $mask = "*!$user@".&makeHostMask($host);
162 &pSReply("Added hostmask '\002$mask\002' to $who");
163 $users{$who}{HOSTS}{$mask} = 1;
166 $crypt = &mkcrypt($array[0]);
167 $users{$who}{PASS} = $crypt;
168 &pSReply("new pass for $who, crypt $crypt.");
174 if (&IsParam("disallowOutsiders") and $msgType =~ /private/i) {
177 foreach (keys %channels) {
178 next unless (&IsNickInChan($who,$_));
184 if (!$found and scalar(keys %channels)) {
185 &status("OUTSIDER <$who> $message");
191 if ($msgType =~ /public/ and $message =~ s/^\+//) {
192 &status("Process: '+' flag detected; changing reply to public");
194 $who = $chan; # major hack to fix &msg().
195 $force_public_reply++;
196 # notice is still NOTICE but to whole channel => good.
199 # User Processing, for all users.
202 return 'returned from pCH' if &parseCmdHook("main",$message);
204 $retval = &userCommands();
205 return unless (defined $retval);
206 return if ($retval eq $noreply);
210 # once useless messages have been parsed out, we match them.
213 # confused? is this for infobot communications?
214 foreach (keys %{ $lang{'confused'} }) {
217 next unless ($message =~ /^\Q$y\E\s*/);
221 # hello. [took me a while to fix this. -xk]
222 if ($orig{message} =~ /^(\Q$ident\E\S?[:, ]\S?)?\s*(h(ello|i( there)?|owdy|ey|ola))( \Q$ident\E)?\s*$/i) {
223 return '' unless ($talkok);
225 # 'mynick: hi' or 'hi mynick' or 'hi'.
226 &status("somebody said hello");
228 # 50% chance of replying to a random greeting when not addressed
229 if (!defined $5 and $addressed == 0 and rand() < 0.5) {
230 &status("not returning unaddressed greeting");
234 # customized random message.
235 my $tmp = (rand() < 0.5) ? ", $who" : "";
236 &performStrictReply(&getRandom(keys %{ $lang{'hello'} }) . $tmp);
241 if ($message =~ /how (the hell )?are (ya|you)( doin\'?g?)?\?*$/) {
242 my $reply = &getRandom(keys %{ $lang{'howareyou'} });
244 &performReply($reply);
250 if ($message =~ /you (rock|rewl|rule|are so+ coo+l)/ ||
251 $message =~ /(good (bo(t|y)|g([ui]|r+)rl))|(bot( |\-)?snack)/i)
253 return 'praise: no addr' unless ($addressed);
255 &status("random praise detected");
257 my $tmp = (rand() < 0.5) ? "thanks $who " : "";
258 &performStrictReply($tmp.":)");
264 if ($message =~ /^than(ks?|x)( you)?( \S+)?/i) {
265 return 'thank: no addr' unless ($message =~ /$ident/ or $talkok);
267 &performReply( &getRandom(keys %{ $lang{'welcome'} }) );
276 if ($message =~ /^(\S+)(--|\+\+)\s*$/ and $addressed) {
277 return '' unless (&hasParam("karma"));
279 my($term,$inc) = (lc $1,$2);
281 if ($msgType !~ /public/i) {
282 &msg($who, "karma must be done in public!");
286 if (lc $term eq lc $who) {
287 &msg($who, "please don't karma yourself");
291 my $karma = &dbGet("stats", "counter", "nick='$term' and type='karma'") || 0;
299 { nick => $term, type => "karma" },
300 { counter => $karma }
306 # here's where the external routines get called.
307 # if they return anything but null, that's the "answer".
309 if ( &parseCmdHook("extra",$message) ) {
310 return 'DID SOMETHING IN PCH.';
315 return 'SOMETHING 1';
318 if (0 and $addrchar) {
319 &msg($who, "I don't trust people to use the core commands while addressing me in a short-cut way.");
324 if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pg|postgres|dbm)/i) {
326 } elsif ($param{'DBType'} =~ /^none$/i) {
327 return "NO FACTOIDS.";
329 &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
337 if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
339 &status("infobot <$nuh> identified") unless $bots{$nuh};
345 if ($message =~ /^QUERY (<.*?>) (.*)/) { # query.
346 my ($target,$item) = ($1,$2);
349 &status(":INFOBOT:QUERY $who: $message");
351 if ($_ = &getFactoid($item)) {
352 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
355 return 'INFOBOT QUERY';
356 } elsif ($message =~ /^REPLY <(.*?)> (.*)/) { # reply.
357 my ($target,$item) = ($1,$2);
359 &status(":INFOBOT:REPLY $who: $message");
361 my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
363 if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
364 &msg($target, "$who knew: $lhs $mhs $rhs");
367 $rhs = "<REPLY> are" if ($mhs eq "are");
368 &setFactInfo($lhs, "factoid_value", $rhs);
371 return 'INFOBOT REPLY';
373 &ERROR(":INFOBOT:UNKNOWN $who: $message");
374 return 'INFOBOT UNKNOWN';
379 if ($message =~ s/^forget\s+//i) {
380 return 'forget: no addr' unless ($addressed);
382 my $faqtoid = $message;
383 if ($faqtoid eq "") {
388 $faqtoid =~ tr/A-Z/a-z/;
389 my $result = &getFactoid($faqtoid);
391 if (defined $result) {
392 my $author = &getFactInfo($faqtoid, "created_by");
393 my $count = &getFactInfo($faqtoid, "requested_count") || 0;
394 my $limit = &getChanConfDefault("factoidPreventForgetLimit",
397 if (IsFlag("r") ne "r") {
398 &msg($who, "you don't have access to remove factoids");
402 return 'locked factoid' if (&IsLocked($faqtoid) == 1);
404 # factoidPreventForgetLimit:
405 if ($limit and $count > $limit and (&IsFlag("o") ne "o")) {
406 &msg($who, "will not delete '$faqtoid', count > limit ($count > $limit)");
410 if (&IsParam("factoidDeleteDelay") or &IsChanConf("factoidDeleteDelay")) {
411 if ($faqtoid =~ / #DEL#$/ and !&IsFlag("o")) {
412 &msg($who, "cannot delete it ($faqtoid).");
416 &status("forgot (safe delete): <$who> '$faqtoid' =is=> '$result'");
417 ### TODO: check if the "backup" exists and overwrite it
418 my $check = &getFactoid("$faqtoid #DEL#");
420 if (!defined $check or $check =~ /^\s*$/) {
421 if ($faqtoid !~ / #DEL#$/) {
422 my $new = $faqtoid." #DEL#";
423 &DEBUG("Process: backing up $faqtoid to '$new'.");
425 # this looks weird but does it work?
426 &setFactInfo($faqtoid, "factoid_key", $new);
427 &setFactInfo($new, "modified_by", $who);
428 &setFactInfo($new, "modified_time", time());
431 &status("not backing up $faqtoid.");
435 &status("forget: not overwriting backup!");
439 &status("forget: <$who> '$faqtoid' =is=> '$result'");
441 &delFactoid($faqtoid);
443 &performReply("i forgot $faqtoid");
447 &performReply("i didn't have anything called '$faqtoid'");
453 # factoid unforget/undelete.
454 if ($message =~ s/^un(forget|delete)\s+//i) {
455 return 'unforget: no addr' unless ($addressed);
458 $i++ if (&IsParam("factoidDeleteDelay"));
459 $i++ if (&IsChanConf("factoidDeleteDelay"));
461 &performReply("safe delete has been disable so what is there to undelete?");
465 my $faqtoid = $message;
466 if ($faqtoid eq "") {
471 $faqtoid =~ tr/A-Z/a-z/;
472 my $result = &getFactoid($faqtoid." #DEL#");
473 my $check = &getFactoid($faqtoid);
475 if (!defined $result) {
476 &performReply("i didn't have anything ('$faqtoid') to undelete.");
480 if (defined $check) {
481 &performReply("cannot undeleted '$faqtoid' because it already exists?");
485 &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
487 ### delete info. modified_ isn't really used.
488 &setFactInfo($faqtoid, "modified_by", "");
489 &setFactInfo($faqtoid, "modified_time", 0);
491 &performReply("Successfully recovered '$faqtoid'. Have fun now.");
493 $count{'Undelete'}++;
499 if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
500 return 'lock: no addr 2' unless ($addressed);
502 my $function = lc $1;
505 if ($faqtoid eq "") {
510 if (&getFactoid($faqtoid) eq "") {
511 &msg($who, "factoid \002$faqtoid\002 does not exist");
515 if ($function eq "lock") {
516 # strongly requested by #debian on 19991028. -xk
517 if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
518 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
519 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
525 &CmdUnLock($faqtoid);
532 if ($message =~ s/^rename(\s+|$)//) {
533 return 'rename: no addr' unless ($addressed);
535 if ($message eq "") {
540 if ($message =~ /^'(.*)'\s+'(.*)'$/) {
541 my($from,$to) = (lc $1, lc $2);
543 my $result = &getFactoid($from);
544 if (defined $result) {
545 my $author = &getFactInfo($from, "created_by");
547 if (&IsFlag("m") or $author =~ /^\Q$who\E\!/i) {
548 &msg($who, "It's not yours to modify.");
552 if ($_ = &getFactoid($to)) {
553 &performReply("destination factoid already exists.");
557 &setFactInfo($from,"factoid_key",$to);
559 &status("rename: <$who> '$from' is now '$to'");
560 &performReply("i renamed '$from' to '$to'");
562 &performReply("i didn't have anything called '$from'");
565 &msg($who,"error: wrong format. ask me about 'help rename'.");
571 # factoid substitution. (X =~ s/A/B/FLAG)
572 if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
573 my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
574 return 'subst: no addr' unless ($addressed);
577 if ($np =~ /$delim/) {
578 &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
583 if (my $result = &getFactoid($faqtoid)) {
584 return 'subst: locked' if (&IsLocked($faqtoid) == 1);
587 if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
588 if (length $result > $param{'maxDataSize'}) {
589 &performReply("that's too long");
592 &setFactInfo($faqtoid, "factoid_value", $result);
593 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
596 &performReply("that doesn't contain '$op'");
599 &performReply("i didn't have anything called '$faqtoid'");
605 # Fix up $message for question.
606 my $question = $message;
609 s/^hey([, ]+)where/where/i;
612 s/where can i find/where is/i;
613 s/how about/where is/i;
616 # clear the string of useless words.
617 s/^(stupid )?q(uestion)?:\s+//i;
618 s/^(does )?(any|ne)(1|one|body) know //i;
620 s/^[uh]+m*[,\.]* +//i;
624 s/^(gee|boy|golly|gosh)([, ]+)//i;
625 s/^(well|and|but|or|yes)([, ]+)//i;
627 s/^o+[hk]+(a+y+)?([,. ]+)//i;
628 s/^g(eez|osh|olly)([,. ]+)//i;
629 s/^w(ow|hee|o+ho+)([,. ]+)//i;
630 s/^heya?,?( folks)?([,. ]+)//i;
633 if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
634 $correction_plausible = 1;
635 &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
637 $correction_plausible = 0;
640 my $result = &doQuestion($question);
641 if (!defined $result or $result eq $noreply) {
642 return 'result from doQ undef.';
645 if (defined $result and $result !~ /^0?$/) { # question.
646 &status("question: <$who> $message");
647 $count{'Question'}++;
648 } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
649 &loadMyModule("perlMath");
650 my $newresult = &perlMath();
652 if (defined $newresult and $newresult ne "") {
653 $cmdstats{'Maths'}++;
654 $result = $newresult;
655 &status("math: <$who> $message => $result");
659 if ($result !~ /^0?$/) {
660 &performStrictReply($result);
664 # why would a friendly bot get passed here?
665 if (&IsParam("friendlyBots")) {
666 return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
670 if (!defined &doStatement($message)) {
674 return unless ($addressed);
676 if (length $message > 64) {
677 &status("unparseable-moron: $message");
678 # &performReply( &getRandom(keys %{ $lang{'moron'} }) );
681 &performReply("You are moron #".$count{'Moron'}."!");
685 &status("unparseable: $message");
686 &performReply( &getRandom(keys %{ $lang{'dunno'} }) );