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 &msg($who, "give me an unlobotomy.");
34 if ($param{'talkMethod'} =~ /^PRIVATE$/i) {
35 if ($msgType =~ /public/ and $addressed) {
36 &msg($who, "sorry. i'm in 'PRIVATE' talkMethod mode ".
37 "while you sent a message to me ${msgType}ly.");
43 # join, must be done before outsider checking.
44 if ($message =~ /^join(\s+(.*))?\s*$/i) {
45 return 'join: not addr' unless ($addressed);
47 $2 =~ /^($mask{chan})(,(\S+))?/;
48 my($thischan, $key) = (lc $1, $3);
49 my $chankey = lc $thischan;
50 $chankey .= " $key" if (defined $key);
52 if ($thischan eq "") {
57 if (&IsFlag("o") ne "o") {
58 if (!exists $chanconf{$thischan}) {
59 &msg($who, "I am not allowed to join $thischan.");
63 if (&validChan($thischan)) {
64 &msg($who,"I'm already on $thischan...");
68 $cache{join}{$thischan} = $who; # used for on_join self.
71 &status("JOIN $chankey <$who>");
72 &msg($who, "joining $chankey");
78 if ($msgType =~ /private/ and $message =~ s/^identify//i) {
79 $message =~ s/^\s+|\s+$//g;
80 my @array = split / /, $message;
82 if ($who =~ /^_default$/i) {
83 &pSReply("you are too eleet.");
87 if (!scalar @array or scalar @array > 2) {
92 my $do_nick = $array[1] || $who;
94 if (!exists $users{$do_nick}) {
95 &pSReply("nick $do_nick is not in user list.");
99 my $crypt = $users{$do_nick}{PASS};
100 if (!defined $crypt) {
101 &pSReply("user $do_nick has no passwd set.");
105 if (!&ckpasswd($array[0], $crypt)) {
106 &pSReply("invalid passwd for $do_nick.");
110 my $mask = "*!$user@".&makeHostMask($host);
111 ### TODO: prevent adding multiple dupe masks?
112 ### TODO: make &addHostMask() CMD?
113 &pSReply("Added $mask for $do_nick...");
114 $users{$do_nick}{HOSTS}{$mask} = 1;
120 if ($msgType =~ /private/ and $message =~ s/^pass//i) {
121 $message =~ s/^\s+|\s+$//g;
122 my @array = split / /, $message;
124 if ($who =~ /^_default$/i) {
125 &pSReply("you are too eleet.");
129 if (scalar @array != 1) {
134 # todo: use &getUser()?
135 my $first = (scalar keys %users) ? 1 : 0;
136 if (!exists $users{$who} and !$first) {
137 &pSReply("nick $who is not in user list.");
142 &pSReply("first time user... adding you as master.");
143 $users{$who}{FLAGS} = "mrsteon";
146 my $crypt = $users{$who}{PASS};
147 if (defined $crypt) {
148 &pSReply("user $who already has pass set.");
152 if (!defined $host) {
153 &WARN("pass: host == NULL.");
157 if (!scalar keys %{ $users{$who}{HOSTS} }) {
158 my $mask = "*!$user@".&makeHostMask($host);
159 &pSReply("added mask $mask to $who.");
160 $users{$who}{HOSTS}{$mask} = 1;
163 my $salt = join '',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64];
165 &pSReply("new pass for $who, salt $salt.");
166 $users{$who}{PASS} = crypt($array[0], $salt);
172 if (&IsParam("disallowOutsiders") and $msgType =~ /private/i) {
175 foreach (keys %channels) {
176 next unless (&IsNickInChan($who,$_));
182 if (!$found and scalar(keys %channels)) {
183 &status("OUTSIDER <$who> $message");
188 # User Processing, for all users.
191 return 'returned from pCH' if &parseCmdHook("main",$message);
193 $retval = &userCommands();
194 return unless (defined $retval);
195 return if ($retval eq $noreply);
199 # once useless messages have been parsed out, we match them.
202 # confused? is this for infobot communications?
203 foreach (keys %{ $lang{'confused'} }) {
206 next unless ($message =~ /^\Q$y\E\s*/);
210 # hello. [took me a while to fix this. -xk]
211 if ($orig{message} =~ /^(\Q$ident\E\S?[:, ]\S?)?\s*(h(ello|i( there)?|owdy|ey|ola))( \Q$ident\E)?\s*$/i) {
212 return '' unless ($talkok);
214 # 'mynick: hi' or 'hi mynick' or 'hi'.
215 &status("somebody said hello");
217 # 50% chance of replying to a random greeting when not addressed
218 if (!defined $5 and $addressed == 0 and rand() < 0.5) {
219 &status("not returning unaddressed greeting");
223 # customized random message.
224 my $tmp = (rand() < 0.5) ? ", $who" : "";
225 &performStrictReply(&getRandom(keys %{ $lang{'hello'} }) . $tmp);
230 if ($message =~ /how (the hell )?are (ya|you)( doin\'?g?)?\?*$/) {
231 my $reply = &getRandom(keys %{ $lang{'howareyou'} });
233 &performReply($reply);
239 if ($message =~ /you (rock|rewl|rule|are so+ coo+l)/ ||
240 $message =~ /(good (bo(t|y)|g([ui]|r+)rl))|(bot( |\-)?snack)/i)
242 return 'praise: no addr' unless ($addressed);
244 &status("random praise detected");
246 my $tmp = (rand() < 0.5) ? "thanks $who " : "";
247 &performStrictReply($tmp.":)");
253 if ($message =~ /^than(ks?|x)( you)?( \S+)?/i) {
254 return 'thank: no addr' unless ($message =~ /$ident/ or $talkok);
256 &performReply( &getRandom(keys %{$lang{'welcome'}}) );
265 if ($msgType =~ /public/ and $message =~ s/^\+//) {
266 &status("found '+' flag; setting msgType to public.");
267 $force_public_reply++;
271 if ($message =~ s/^literal\s+//i) {
272 &status("literal ask of '$message'.");
277 if ($message =~ /^(\S+)(--|\+\+)\s*$/ and $addressed) {
278 return '' unless (&hasParam("karma"));
280 my($term,$inc) = (lc $1,$2);
282 if ($msgType !~ /public/i) {
283 &msg($who, "karma must be done in public!");
287 if (lc($term) eq lc($who)) {
288 &msg($who, "please don't karma yourself");
292 my $karma = &dbGet("karma", "nick",$term,"karma") || 0;
299 &dbSet("karma", "nick",$term,"karma",$karma);
304 # here's where the external routines get called.
305 # if they return anything but null, that's the "answer".
307 if ( &parseCmdHook("extra",$message) ) {
308 return 'DID SOMETHING IN PCH.';
313 return 'SOMETHING 1';
316 ### FIXME: should this only apply to public messages?
318 &DEBUG("floodwho => '$floodwho'.");
319 delete $flood{$floodwho}{$message};
320 &status("short return due to unknown command.");
325 if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pg|postgres|dbm)/i) {
327 } elsif ($param{'DBType'} =~ /^none$/i) {
328 return "NO FACTOIDS.";
330 &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
338 if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
340 &status("infobot <$nuh> identified") unless $bots{$nuh};
346 if ($message =~ /^QUERY (<.*?>) (.*)/) { # query.
347 my ($target,$item) = ($1,$2);
350 &status(":INFOBOT:QUERY $who: $message");
352 if ($_ = &getFactoid($item)) {
353 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
356 return 'INFOBOT QUERY';
357 } elsif ($message =~ /^REPLY <(.*?)> (.*)/) { # reply.
358 my ($target,$item) = ($1,$2);
360 &status(":INFOBOT:REPLY $who: $message");
362 my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
364 if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
365 &msg($target, "$who knew: $lhs $mhs $rhs");
368 $rhs = "<REPLY> are" if ($mhs eq "are");
369 &setFactInfo($lhs, "factoid_value", $rhs);
372 return 'INFOBOT REPLY';
374 &ERROR(":INFOBOT:UNKNOWN $who: $message");
375 return 'INFOBOT UNKNOWN';
381 if ($message =~ s/^forget\s+//i) {
382 return 'forget: no addr' unless ($addressed);
384 my $faqtoid = $message;
385 if ($faqtoid eq "") {
390 $faqtoid =~ tr/A-Z/a-z/;
391 my $result = &getFactoid($faqtoid);
393 if (defined $result) {
394 my $author = &getFactInfo($faqtoid, "created_by");
395 if (IsFlag("r") ne "r") {
396 &msg($who, "you don't have access to remove factoids");
400 return 'locked factoid' if (&IsLocked($faqtoid) == 1);
402 if (&IsParam("factoidDeleteDelay")) {
403 &status("forgot (safe delete): <$who> '$faqtoid' =is=> '$result'");
404 ### TODO: check if the "backup" exists and overwrite it
405 my $check = &getFactoid("$faqtoid #DEL#");
407 &setFactInfo($faqtoid, "factoid_key", $faqtoid." #DEL#");
409 &setFactInfo($faqtoid, "modified_by", $who);
410 &setFactInfo($faqtoid, "modified_time", time());
412 &status("forget: not overwriting backup!");
415 &status("forget: <$who> '$faqtoid' =is=> '$result'");
417 &delFactoid($faqtoid);
419 &performReply("i forgot $faqtoid");
423 &performReply("i didn't have anything called '$faqtoid'");
429 # factoid unforget/undelete.
430 if ($message =~ s/^un(forget|delete)\s+//i) {
431 return 'unforget: no addr' unless ($addressed);
433 if (!&IsParam("factoidDeleteDelay")) {
434 &performReply("safe delete has been disable so what is there to undelete?");
438 my $faqtoid = $message;
439 if ($faqtoid eq "") {
444 $faqtoid =~ tr/A-Z/a-z/;
445 my $result = &getFactoid($faqtoid." #DEL#");
446 my $check = &getFactoid($faqtoid);
448 if (!defined $result) {
449 &performReply("i didn't have anything ('$faqtoid') to undelete.");
453 &DEBUG("unforget: check => $check");
454 if (defined $check) {
455 &performReply("cannot undeleted '$faqtoid' because it already exists?");
459 &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
461 ### delete info. modified_ isn't really used.
462 &setFactInfo($faqtoid, "modified_by", "");
463 &setFactInfo($faqtoid, "modified_time", 0);
465 &performReply("Successfully recovered '$faqtoid'. Have fun now.");
467 $count{'Undelete'}++;
473 if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
474 return 'lock: no addr 2' unless ($addressed);
476 my $function = lc $1;
479 if ($faqtoid eq "") {
484 if (&getFactoid($faqtoid) eq "") {
485 &msg($who, "factoid \002$faqtoid\002 does not exist");
489 if ($function eq "lock") {
490 # strongly requested by #debian on 19991028. -xk
491 if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
492 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
493 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
499 &CmdUnLock($faqtoid);
506 if ($message =~ s/^rename(\s+|$)//) {
507 return 'rename: no addr' unless ($addressed);
509 if ($message eq "") {
514 if ($message =~ /^'(.*)'\s+'(.*)'$/) {
515 my($from,$to) = (lc $1, lc $2);
517 my $result = &getFactoid($from);
518 if (defined $result) {
519 my $author = &getFactInfo($from, "created_by");
520 if (&IsFlag("m") and $author =~ /^\Q$who\E\!/i) {
521 &msg($who, "It's not yours to modify.");
525 if ($_ = &getFactoid($to)) {
526 &performReply("destination factoid already exists.");
530 &setFactInfo($from,"factoid_key",$to);
532 &status("rename: <$who> '$from' is now '$to'");
533 &performReply("i renamed '$from' to '$to'");
535 &performReply("i didn't have anything called '$from'");
538 &msg($who,"error: wrong format. ask me about 'help rename'.");
544 # factoid substitution. (X =~ s/A/B/FLAG)
545 if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
546 my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
547 return 'subst: no addr' unless ($addressed);
550 if ($np =~ /$delim/) {
551 &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
556 if (my $result = &getFactoid($faqtoid)) {
557 return 'subst: locked' if (&IsLocked($faqtoid) == 1);
560 if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
561 if (length $result > $param{'maxDataSize'}) {
562 &performReply("that's too long");
565 &setFactInfo($faqtoid, "factoid_value", $result);
566 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
569 &performReply("that doesn't contain '$op'");
572 &performReply("i didn't have anything called '$faqtoid'");
579 # Fix up $message for question.
582 s/^hey([, ]+)where/where/i;
584 s/where can i find/where is/i;
585 s/how about/where is/i;
588 # clear the string of useless words.
589 s/^(stupid )?q(uestion)?:\s+//i;
590 s/^(does )?(any|ne)(1|one|body) know //i;
592 s/^[uh]+m*[,\.]* +//i;
596 s/^(gee|boy|golly|gosh)([, ]+)//i;
597 s/^(well|and|but|or|yes)([, ]+)//i;
599 s/^o+[hk]+(a+y+)?([,. ]+)//i;
600 s/^g(eez|osh|olly)([,. ]+)//i;
601 s/^w(ow|hee|o+ho+)([,. ]+)//i;
602 s/^heya?,?( folks)?([,. ]+)//i;
605 if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
606 $correction_plausible = 1;
607 &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
609 $correction_plausible = 0;
612 my $result = &doQuestion($message);
613 if (!defined $result or $result eq $noreply) {
614 return 'result from doQ undef.';
617 if (defined $result and $result !~ /^0?$/) { # question.
618 &status("question: <$who> $message");
619 $count{'Question'}++;
620 } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
621 &loadMyModule("perlMath");
622 my $newresult = &perlMath();
624 if (defined $newresult and $newresult ne "") {
625 $result = $newresult;
626 &status("math: <$who> $message => $result");
630 if ($result !~ /^0?$/) {
631 &performStrictReply($result);
635 # why would a friendly bot get passed here?
636 if (&IsParam("friendlyBots")) {
637 return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
641 if (!defined &doStatement($message)) {
645 return unless ($addressed);
647 if (length $message > 64) {
648 &status("unparseable-moron: $message");
649 &performReply( &getRandom(keys %{$lang{'moron'}}) );
654 &status("unparseable: $message");
655 &performReply( &getRandom(keys %{$lang{'dunno'}}) );