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,"I'm already on $thischan...");
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) ? 1 : 0;
138 if (!exists $users{$who} and !$first) {
139 &pSReply("nick $who is not in user list.");
144 &pSReply("first time user... adding you as master.");
145 $users{$who}{FLAGS} = "mrsteon";
148 my $crypt = $users{$who}{PASS};
149 if (defined $crypt) {
150 &pSReply("user $who already has pass set.");
154 if (!defined $host) {
155 &WARN("pass: host == NULL.");
159 if (!scalar keys %{ $users{$who}{HOSTS} }) {
160 my $mask = "*!$user@".&makeHostMask($host);
161 &pSReply("added mask $mask to $who.");
162 $users{$who}{HOSTS}{$mask} = 1;
165 $crypt = &mkcrypt($array[0]);
166 $users{$who}{PASS} = $crypt;
167 &pSReply("new pass for $who, crypt $crypt.");
173 if (&IsParam("disallowOutsiders") and $msgType =~ /private/i) {
176 foreach (keys %channels) {
177 next unless (&IsNickInChan($who,$_));
183 if (!$found and scalar(keys %channels)) {
184 &status("OUTSIDER <$who> $message");
189 # User Processing, for all users.
192 return 'returned from pCH' if &parseCmdHook("main",$message);
194 $retval = &userCommands();
195 return unless (defined $retval);
196 return if ($retval eq $noreply);
200 # once useless messages have been parsed out, we match them.
203 # confused? is this for infobot communications?
204 foreach (keys %{ $lang{'confused'} }) {
207 next unless ($message =~ /^\Q$y\E\s*/);
211 # hello. [took me a while to fix this. -xk]
212 if ($orig{message} =~ /^(\Q$ident\E\S?[:, ]\S?)?\s*(h(ello|i( there)?|owdy|ey|ola))( \Q$ident\E)?\s*$/i) {
213 return '' unless ($talkok);
215 # 'mynick: hi' or 'hi mynick' or 'hi'.
216 &status("somebody said hello");
218 # 50% chance of replying to a random greeting when not addressed
219 if (!defined $5 and $addressed == 0 and rand() < 0.5) {
220 &status("not returning unaddressed greeting");
224 # customized random message.
225 my $tmp = (rand() < 0.5) ? ", $who" : "";
226 &performStrictReply(&getRandom(keys %{ $lang{'hello'} }) . $tmp);
231 if ($message =~ /how (the hell )?are (ya|you)( doin\'?g?)?\?*$/) {
232 my $reply = &getRandom(keys %{ $lang{'howareyou'} });
234 &performReply($reply);
240 if ($message =~ /you (rock|rewl|rule|are so+ coo+l)/ ||
241 $message =~ /(good (bo(t|y)|g([ui]|r+)rl))|(bot( |\-)?snack)/i)
243 return 'praise: no addr' unless ($addressed);
245 &status("random praise detected");
247 my $tmp = (rand() < 0.5) ? "thanks $who " : "";
248 &performStrictReply($tmp.":)");
254 if ($message =~ /^than(ks?|x)( you)?( \S+)?/i) {
255 return 'thank: no addr' unless ($message =~ /$ident/ or $talkok);
257 &performReply( &getRandom(keys %{$lang{'welcome'}}) );
266 if ($msgType =~ /public/ and $message =~ s/^\+//) {
267 &status("found '+' flag; setting msgType to public.");
268 $force_public_reply++;
272 if ($message =~ s/^literal\s+//i) {
273 &status("literal ask of '$message'.");
278 if ($message =~ /^(\S+)(--|\+\+)\s*$/ and $addressed) {
279 return '' unless (&hasParam("karma"));
281 my($term,$inc) = (lc $1,$2);
283 if ($msgType !~ /public/i) {
284 &msg($who, "karma must be done in public!");
288 if (lc($term) eq lc($who)) {
289 &msg($who, "please don't karma yourself");
293 my $karma = &dbGet("karma", "nick",$term,"karma") || 0;
300 &dbSet("karma", "nick",$term,"karma",$karma);
305 # here's where the external routines get called.
306 # if they return anything but null, that's the "answer".
308 if ( &parseCmdHook("extra",$message) ) {
309 return 'DID SOMETHING IN PCH.';
314 return 'SOMETHING 1';
317 ### FIXME: should this only apply to public messages?
319 &DEBUG("floodwho => '$floodwho'.");
320 delete $flood{$floodwho}{$message};
321 &status("short return due to unknown command.");
326 if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pg|postgres|dbm)/i) {
328 } elsif ($param{'DBType'} =~ /^none$/i) {
329 return "NO FACTOIDS.";
331 &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
339 if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
341 &status("infobot <$nuh> identified") unless $bots{$nuh};
347 if ($message =~ /^QUERY (<.*?>) (.*)/) { # query.
348 my ($target,$item) = ($1,$2);
351 &status(":INFOBOT:QUERY $who: $message");
353 if ($_ = &getFactoid($item)) {
354 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
357 return 'INFOBOT QUERY';
358 } elsif ($message =~ /^REPLY <(.*?)> (.*)/) { # reply.
359 my ($target,$item) = ($1,$2);
361 &status(":INFOBOT:REPLY $who: $message");
363 my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
365 if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
366 &msg($target, "$who knew: $lhs $mhs $rhs");
369 $rhs = "<REPLY> are" if ($mhs eq "are");
370 &setFactInfo($lhs, "factoid_value", $rhs);
373 return 'INFOBOT REPLY';
375 &ERROR(":INFOBOT:UNKNOWN $who: $message");
376 return 'INFOBOT UNKNOWN';
382 if ($message =~ s/^forget\s+//i) {
383 return 'forget: no addr' unless ($addressed);
385 my $faqtoid = $message;
386 if ($faqtoid eq "") {
391 $faqtoid =~ tr/A-Z/a-z/;
392 my $result = &getFactoid($faqtoid);
394 if (defined $result) {
395 my $author = &getFactInfo($faqtoid, "created_by");
396 if (IsFlag("r") ne "r") {
397 &msg($who, "you don't have access to remove factoids");
401 return 'locked factoid' if (&IsLocked($faqtoid) == 1);
403 if (&IsParam("factoidDeleteDelay")) {
404 &status("forgot (safe delete): <$who> '$faqtoid' =is=> '$result'");
405 ### TODO: check if the "backup" exists and overwrite it
406 my $check = &getFactoid("$faqtoid #DEL#");
408 &setFactInfo($faqtoid, "factoid_key", $faqtoid." #DEL#");
410 &setFactInfo($faqtoid, "modified_by", $who);
411 &setFactInfo($faqtoid, "modified_time", time());
413 &status("forget: not overwriting backup!");
416 &status("forget: <$who> '$faqtoid' =is=> '$result'");
418 &delFactoid($faqtoid);
420 &performReply("i forgot $faqtoid");
424 &performReply("i didn't have anything called '$faqtoid'");
430 # factoid unforget/undelete.
431 if ($message =~ s/^un(forget|delete)\s+//i) {
432 return 'unforget: no addr' unless ($addressed);
434 if (!&IsParam("factoidDeleteDelay")) {
435 &performReply("safe delete has been disable so what is there to undelete?");
439 my $faqtoid = $message;
440 if ($faqtoid eq "") {
445 $faqtoid =~ tr/A-Z/a-z/;
446 my $result = &getFactoid($faqtoid." #DEL#");
447 my $check = &getFactoid($faqtoid);
449 if (!defined $result) {
450 &performReply("i didn't have anything ('$faqtoid') to undelete.");
454 &DEBUG("unforget: check => $check");
455 if (defined $check) {
456 &performReply("cannot undeleted '$faqtoid' because it already exists?");
460 &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
462 ### delete info. modified_ isn't really used.
463 &setFactInfo($faqtoid, "modified_by", "");
464 &setFactInfo($faqtoid, "modified_time", 0);
466 &performReply("Successfully recovered '$faqtoid'. Have fun now.");
468 $count{'Undelete'}++;
474 if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
475 return 'lock: no addr 2' unless ($addressed);
477 my $function = lc $1;
480 if ($faqtoid eq "") {
485 if (&getFactoid($faqtoid) eq "") {
486 &msg($who, "factoid \002$faqtoid\002 does not exist");
490 if ($function eq "lock") {
491 # strongly requested by #debian on 19991028. -xk
492 if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
493 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
494 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
500 &CmdUnLock($faqtoid);
507 if ($message =~ s/^rename(\s+|$)//) {
508 return 'rename: no addr' unless ($addressed);
510 if ($message eq "") {
515 if ($message =~ /^'(.*)'\s+'(.*)'$/) {
516 my($from,$to) = (lc $1, lc $2);
518 my $result = &getFactoid($from);
519 if (defined $result) {
520 my $author = &getFactInfo($from, "created_by");
522 if (&IsFlag("m") or $author =~ /^\Q$who\E\!/i) {
523 &msg($who, "It's not yours to modify.");
527 if ($_ = &getFactoid($to)) {
528 &performReply("destination factoid already exists.");
532 &setFactInfo($from,"factoid_key",$to);
534 &status("rename: <$who> '$from' is now '$to'");
535 &performReply("i renamed '$from' to '$to'");
537 &performReply("i didn't have anything called '$from'");
540 &msg($who,"error: wrong format. ask me about 'help rename'.");
546 # factoid substitution. (X =~ s/A/B/FLAG)
547 if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
548 my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
549 return 'subst: no addr' unless ($addressed);
552 if ($np =~ /$delim/) {
553 &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
558 if (my $result = &getFactoid($faqtoid)) {
559 return 'subst: locked' if (&IsLocked($faqtoid) == 1);
562 if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
563 if (length $result > $param{'maxDataSize'}) {
564 &performReply("that's too long");
567 &setFactInfo($faqtoid, "factoid_value", $result);
568 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
571 &performReply("that doesn't contain '$op'");
574 &performReply("i didn't have anything called '$faqtoid'");
581 # Fix up $message for question.
584 s/^hey([, ]+)where/where/i;
586 s/where can i find/where is/i;
587 s/how about/where is/i;
590 # clear the string of useless words.
591 s/^(stupid )?q(uestion)?:\s+//i;
592 s/^(does )?(any|ne)(1|one|body) know //i;
594 s/^[uh]+m*[,\.]* +//i;
598 s/^(gee|boy|golly|gosh)([, ]+)//i;
599 s/^(well|and|but|or|yes)([, ]+)//i;
601 s/^o+[hk]+(a+y+)?([,. ]+)//i;
602 s/^g(eez|osh|olly)([,. ]+)//i;
603 s/^w(ow|hee|o+ho+)([,. ]+)//i;
604 s/^heya?,?( folks)?([,. ]+)//i;
607 if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
608 $correction_plausible = 1;
609 &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
611 $correction_plausible = 0;
614 my $result = &doQuestion($message);
615 if (!defined $result or $result eq $noreply) {
616 return 'result from doQ undef.';
619 if (defined $result and $result !~ /^0?$/) { # question.
620 &status("question: <$who> $message");
621 $count{'Question'}++;
622 } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
623 &loadMyModule("perlMath");
624 my $newresult = &perlMath();
626 if (defined $newresult and $newresult ne "") {
627 $result = $newresult;
628 &status("math: <$who> $message => $result");
632 if ($result !~ /^0?$/) {
633 &performStrictReply($result);
637 # why would a friendly bot get passed here?
638 if (&IsParam("friendlyBots")) {
639 return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
643 if (!defined &doStatement($message)) {
647 return unless ($addressed);
649 if (length $message > 64) {
650 &status("unparseable-moron: $message");
651 &performReply( &getRandom(keys %{$lang{'moron'}}) );
656 &status("unparseable: $message");
657 &performReply( &getRandom(keys %{$lang{'dunno'}}) );