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");
75 &joinNextChan(); # hack.
81 if ($msgType =~ /private/ and $message =~ s/^identify//i) {
82 $message =~ s/^\s+|\s+$//g;
83 my @array = split / /, $message;
85 if ($who =~ /^_default$/i) {
86 &pSReply("you are too eleet.");
90 if (!scalar @array or scalar @array > 2) {
95 my $do_nick = $array[1] || $who;
97 if (!exists $users{$do_nick}) {
98 &pSReply("nick $do_nick is not in user list.");
102 my $crypt = $users{$do_nick}{PASS};
103 if (!defined $crypt) {
104 &pSReply("user $do_nick has no passwd set.");
108 if (!&ckpasswd($array[0], $crypt)) {
109 &pSReply("invalid passwd for $do_nick.");
113 my $mask = "*!$user@".&makeHostMask($host);
114 ### TODO: prevent adding multiple dupe masks?
115 ### TODO: make &addHostMask() CMD?
116 &pSReply("Added $mask for $do_nick...");
117 $users{$do_nick}{HOSTS}{$mask} = 1;
123 if ($msgType =~ /private/ and $message =~ s/^pass//i) {
124 $message =~ s/^\s+|\s+$//g;
125 my @array = split ' ', $message;
127 if ($who =~ /^_default$/i) {
128 &pSReply("you are too eleet.");
132 if (scalar @array != 1) {
137 # todo: use &getUser()?
139 foreach (keys %users) {
140 if ($users{$_}{FLAGS} =~ /n/) {
146 if (!exists $users{$who} and !$first) {
147 &pSReply("nick $who is not in user list.");
152 &pSReply("First time user... adding you as Master.");
153 $users{$who}{FLAGS} = "mrsteon";
156 my $crypt = $users{$who}{PASS};
157 if (defined $crypt) {
158 &pSReply("user $who already has pass set.");
162 if (!defined $host) {
163 &WARN("pass: host == NULL.");
167 if (!scalar keys %{ $users{$who}{HOSTS} }) {
168 my $mask = "*!$user@".&makeHostMask($host);
169 &pSReply("Added hostmask '\002$mask\002' to $who");
170 $users{$who}{HOSTS}{$mask} = 1;
173 $crypt = &mkcrypt($array[0]);
174 $users{$who}{PASS} = $crypt;
175 &pSReply("new pass for $who, crypt $crypt.");
181 if (&IsParam("disallowOutsiders") and $msgType =~ /private/i) {
184 foreach (keys %channels) {
185 next unless (&IsNickInChan($who,$_));
191 if (!$found and scalar(keys %channels)) {
192 &status("OUTSIDER <$who> $message");
198 if ($msgType =~ /public/ and $message =~ s/^\+//) {
199 &status("Process: '+' flag detected; changing reply to public");
201 $who = $chan; # major hack to fix &msg().
202 $force_public_reply++;
203 # notice is still NOTICE but to whole channel => good.
206 # User Processing, for all users.
209 return 'returned from pCH' if &parseCmdHook("main",$message);
211 $retval = &userCommands();
212 return unless (defined $retval);
213 return if ($retval eq $noreply);
217 # once useless messages have been parsed out, we match them.
220 # confused? is this for infobot communications?
221 foreach (keys %{ $lang{'confused'} }) {
224 next unless ($message =~ /^\Q$y\E\s*/);
228 # hello. [took me a while to fix this. -xk]
229 if ($orig{message} =~ /^(\Q$ident\E\S?[:, ]\S?)?\s*(h(ello|i( there)?|owdy|ey|ola))( \Q$ident\E)?\s*$/i) {
230 return '' unless ($talkok);
232 # 'mynick: hi' or 'hi mynick' or 'hi'.
233 &status("somebody said hello");
235 # 50% chance of replying to a random greeting when not addressed
236 if (!defined $5 and $addressed == 0 and rand() < 0.5) {
237 &status("not returning unaddressed greeting");
241 # customized random message.
242 my $tmp = (rand() < 0.5) ? ", $who" : "";
243 &performStrictReply(&getRandom(keys %{ $lang{'hello'} }) . $tmp);
248 if ($message =~ /how (the hell )?are (ya|you)( doin\'?g?)?\?*$/) {
249 my $reply = &getRandom(keys %{ $lang{'howareyou'} });
251 &performReply($reply);
257 if ($message =~ /you (rock|rewl|rule|are so+ coo+l)/ ||
258 $message =~ /(good (bo(t|y)|g([ui]|r+)rl))|(bot( |\-)?snack)/i)
260 return 'praise: no addr' unless ($addressed);
262 &status("random praise detected");
264 my $tmp = (rand() < 0.5) ? "thanks $who " : "";
265 &performStrictReply($tmp.":)");
271 if ($message =~ /^than(ks?|x)( you)?( \S+)?/i) {
272 return 'thank: no addr' unless ($message =~ /$ident/ or $talkok);
274 &performReply( &getRandom(keys %{ $lang{'welcome'} }) );
283 if ($message =~ /^(\S+)(--|\+\+)\s*$/ and $addressed) {
284 return '' unless (&hasParam("karma"));
286 my($term,$inc) = (lc $1,$2);
288 if ($msgType !~ /public/i) {
289 &msg($who, "karma must be done in public!");
293 if (lc $term eq lc $who) {
294 &msg($who, "please don't karma yourself");
298 my $karma = &dbGet("stats", "counter", "nick=".&dbQuote($term).
299 " AND type='karma'") || 0;
307 { nick => $term, type => "karma" },
308 { counter => $karma }
314 # here's where the external routines get called.
315 # if they return anything but null, that's the "answer".
317 if ( &parseCmdHook("extra",$message) ) {
318 return 'DID SOMETHING IN PCH.';
323 return 'SOMETHING 1';
326 if (0 and $addrchar) {
327 &msg($who, "I don't trust people to use the core commands while addressing me in a short-cut way.");
332 if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pg|postgres|dbm)/i) {
334 } elsif ($param{'DBType'} =~ /^none$/i) {
335 return "NO FACTOIDS.";
337 &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
345 if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
347 &status("infobot <$nuh> identified") unless $bots{$nuh};
353 if ($message =~ /^QUERY (<.*?>) (.*)/) { # query.
354 my ($target,$item) = ($1,$2);
357 &status(":INFOBOT:QUERY $who: $message");
359 if ($_ = &getFactoid($item)) {
360 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
363 return 'INFOBOT QUERY';
364 } elsif ($message =~ /^REPLY <(.*?)> (.*)/) { # reply.
365 my ($target,$item) = ($1,$2);
367 &status(":INFOBOT:REPLY $who: $message");
369 my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
371 if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
372 &msg($target, "$who knew: $lhs $mhs $rhs");
375 $rhs = "<REPLY> are" if ($mhs eq "are");
376 &setFactInfo($lhs, "factoid_value", $rhs);
379 return 'INFOBOT REPLY';
381 &ERROR(":INFOBOT:UNKNOWN $who: $message");
382 return 'INFOBOT UNKNOWN';
387 if ($message =~ s/^forget\s+//i) {
388 return 'forget: no addr' unless ($addressed);
390 my $faqtoid = $message;
391 if ($faqtoid eq "") {
396 $faqtoid =~ tr/A-Z/a-z/;
397 my $result = &getFactoid($faqtoid);
399 if (defined $result) {
400 my $author = &getFactInfo($faqtoid, "created_by");
401 my $count = &getFactInfo($faqtoid, "requested_count") || 0;
402 my $limit = &getChanConfDefault("factoidPreventForgetLimit",
405 &DEBUG("forget: limit = $limit");
406 &DEBUG("forget: count = $count");
408 if (IsFlag("r") ne "r") {
409 &msg($who, "you don't have access to remove factoids");
413 return 'locked factoid' if (&IsLocked($faqtoid) == 1);
415 # factoidPreventForgetLimit:
416 if ($limit and $count > $limit and (&IsFlag("o") ne "o")) {
417 &msg($who, "will not delete '$faqtoid', count > limit ($count > $limit)");
421 if (&IsParam("factoidDeleteDelay") or &IsChanConf("factoidDeleteDelay")) {
422 if ($faqtoid =~ / #DEL#$/ and !&IsFlag("o")) {
423 &msg($who, "cannot delete it ($faqtoid).");
427 &status("forgot (safe delete): <$who> '$faqtoid' =is=> '$result'");
428 ### TODO: check if the "backup" exists and overwrite it
429 my $check = &getFactoid("$faqtoid #DEL#");
431 if (!defined $check or $check =~ /^\s*$/) {
432 if ($faqtoid !~ / #DEL#$/) {
433 my $new = $faqtoid." #DEL#";
435 my $backup = &getFactoid($faqtoid);
436 # this looks weird but does it work?
438 &DEBUG("forget: not overwriting backup: $faqtoid");
440 &status("forget: backing up '$faqtoid'");
441 &setFactInfo($faqtoid, "factoid_key", $new);
442 &setFactInfo($new, "modified_by", $who);
443 &setFactInfo($new, "modified_time", time());
447 &status("forget: not backing up $faqtoid.");
451 &status("forget: not overwriting backup!");
455 &status("forget: <$who> '$faqtoid' =is=> '$result'");
457 &delFactoid($faqtoid);
459 &performReply("i forgot $faqtoid");
463 &performReply("i didn't have anything called '$faqtoid'");
469 # factoid unforget/undelete.
470 if ($message =~ s/^un(forget|delete)\s+//i) {
471 return 'unforget: no addr' unless ($addressed);
474 $i++ if (&IsParam("factoidDeleteDelay"));
475 $i++ if (&IsChanConf("factoidDeleteDelay"));
477 &performReply("safe delete has been disable so what is there to undelete?");
481 my $faqtoid = $message;
482 if ($faqtoid eq "") {
487 $faqtoid =~ tr/A-Z/a-z/;
488 my $result = &getFactoid($faqtoid." #DEL#");
489 my $check = &getFactoid($faqtoid);
491 if (!defined $result) {
492 &performReply("i didn't have anything ('$faqtoid') to undelete.");
496 if (defined $check) {
497 &performReply("cannot undeleted '$faqtoid' because it already exists?");
501 &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
503 ### delete info. modified_ isn't really used.
504 &setFactInfo($faqtoid, "modified_by", "");
505 &setFactInfo($faqtoid, "modified_time", 0);
507 &performReply("Successfully recovered '$faqtoid'. Have fun now.");
509 $count{'Undelete'}++;
515 if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
516 return 'lock: no addr 2' unless ($addressed);
518 my $function = lc $1;
521 if ($faqtoid eq "") {
526 if (&getFactoid($faqtoid) eq "") {
527 &msg($who, "factoid \002$faqtoid\002 does not exist");
531 if ($function eq "lock") {
532 # strongly requested by #debian on 19991028. -xk
533 if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
534 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
535 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
541 &CmdUnLock($faqtoid);
548 if ($message =~ s/^rename(\s+|$)//) {
549 return 'rename: no addr' unless ($addressed);
551 if ($message eq "") {
556 if ($message =~ /^'(.*)'\s+'(.*)'$/) {
557 my($from,$to) = (lc $1, lc $2);
559 my $result = &getFactoid($from);
560 if (defined $result) {
561 my $author = &getFactInfo($from, "created_by");
563 if (&IsFlag("m") or $author =~ /^\Q$who\E\!/i) {
564 &msg($who, "It's not yours to modify.");
568 if ($_ = &getFactoid($to)) {
569 &performReply("destination factoid already exists.");
573 &setFactInfo($from,"factoid_key",$to);
575 &status("rename: <$who> '$from' is now '$to'");
576 &performReply("i renamed '$from' to '$to'");
578 &performReply("i didn't have anything called '$from'");
581 &msg($who,"error: wrong format. ask me about 'help rename'.");
587 # factoid substitution. (X =~ s/A/B/FLAG)
588 if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
589 my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
590 return 'subst: no addr' unless ($addressed);
593 if ($np =~ /$delim/) {
594 &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
599 if (my $result = &getFactoid($faqtoid)) {
600 return 'subst: locked' if (&IsLocked($faqtoid) == 1);
603 if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
604 if (length $result > $param{'maxDataSize'}) {
605 &performReply("that's too long");
608 &setFactInfo($faqtoid, "factoid_value", $result);
609 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
612 &performReply("that doesn't contain '$op'");
615 &performReply("i didn't have anything called '$faqtoid'");
621 # Fix up $message for question.
622 my $question = $message;
625 s/^hey([, ]+)where/where/i;
628 s/where can i find/where is/i;
629 s/how about/where is/i;
632 # clear the string of useless words.
633 s/^(stupid )?q(uestion)?:\s+//i;
634 s/^(does )?(any|ne)(1|one|body) know //i;
636 s/^[uh]+m*[,\.]* +//i;
640 s/^(gee|boy|golly|gosh)([, ]+)//i;
641 s/^(well|and|but|or|yes)([, ]+)//i;
643 s/^o+[hk]+(a+y+)?([,. ]+)//i;
644 s/^g(eez|osh|olly)([,. ]+)//i;
645 s/^w(ow|hee|o+ho+)([,. ]+)//i;
646 s/^heya?,?( folks)?([,. ]+)//i;
649 if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
650 $correction_plausible = 1;
651 &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
653 $correction_plausible = 0;
656 my $result = &doQuestion($question);
657 if (!defined $result or $result eq $noreply) {
658 return 'result from doQ undef.';
661 if (defined $result and $result !~ /^0?$/) { # question.
662 &status("question: <$who> $message");
663 $count{'Question'}++;
664 } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
665 &loadMyModule("perlMath");
666 my $newresult = &perlMath();
668 if (defined $newresult and $newresult ne "") {
669 $cmdstats{'Maths'}++;
670 $result = $newresult;
671 &status("math: <$who> $message => $result");
675 if ($result !~ /^0?$/) {
676 &performStrictReply($result);
680 # why would a friendly bot get passed here?
681 if (&IsParam("friendlyBots")) {
682 return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
686 if (!defined &doStatement($message)) {
690 return unless ($addressed);
692 if (length $message > 64) {
693 &status("unparseable-moron: $message");
694 # &performReply( &getRandom(keys %{ $lang{'moron'} }) );
697 &performReply("You are moron \002#". $count{'Moron'} ."\002");
701 &status("unparseable: $message");
702 &performReply( &getRandom(keys %{ $lang{'dunno'} }) );