]> git.donarmstrong.com Git - infobot.git/blob - src/Process.pl
typo for ckpasswd
[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 (!&ckpasswd($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         return 'thank: no addr' unless ($message =~ /$ident/ or $talkok);
209
210         &performReply( &getRandom(keys %{$lang{'welcome'}}) );
211         return;
212     }
213
214
215     ###
216     ### bot commands...
217     ###
218
219     # override msgType.
220     if ($msgType =~ /public/ and $message =~ s/^\+//) {
221         &status("found '+' flag; setting msgType to public.");
222         $force_public_reply++;
223         $msgType = 'public';
224     }
225
226
227     # karma. set...
228     if ($message =~ /^(\S+)(--|\+\+)\s*$/ and $addressed) {
229         return '' unless (&hasParam("karma"));
230
231         my($term,$inc) = (lc $1,$2);
232
233         if ($msgType !~ /public/i) {
234             &msg($who, "karma must be done in public!");
235             return;
236         }
237
238         if (lc($term) eq lc($who)) {
239             &msg($who, "please don't karma yourself");
240             return;
241         }
242
243         my $karma = &dbGet("karma", "nick",$term,"karma") || 0;
244         if ($inc eq '++') {
245             $karma++;
246         } else {
247             $karma--;
248         }
249
250         &dbSet("karma", "nick",$term,"karma",$karma);
251
252         return;
253     }
254
255     # here's where the external routines get called.
256     # if they return anything but null, that's the "answer".
257     if ($addressed) {
258         if ( &parseCmdHook("extra",$message) ) {
259             return 'DID SOMETHING IN PCH.';
260         }
261
262         my $er = &Modules();
263         if (!defined $er) {
264             return 'SOMETHING 1';
265         }
266
267         ### FIXME: should this only apply to public messages?
268         if ($addrchar) {
269             &DEBUG("floodwho => '$floodwho'.");
270             delete $flood{$floodwho}{$message};
271             &status("short return due to unknown command.");
272             return 'ADDR CHAR';
273         }
274     }
275
276     if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pg|postgres|dbm)/i) {
277         &FactoidStuff();
278     } elsif ($param{'DBType'} =~ /^none$/i) {
279         return "NO FACTOIDS.";
280     } else {
281         &ERROR("INVALID FACTOID SUPPORT? ($param{'DBType'})");
282         &shutdown();
283         exit 0;
284     }
285 }
286
287 sub FactoidStuff {
288     # inter-infobot.
289     if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
290         ### identification.
291         &status("infobot <$nuh> identified") unless $bots{$nuh};
292         $bots{$nuh} = $who;
293
294         ### communication.
295
296         # query.
297         if ($message =~ /^QUERY (<.*?>) (.*)/) {        # query.
298             my ($target,$item) = ($1,$2);
299             $item =~ s/[.\?]$//;
300
301             &status(":INFOBOT:QUERY $who: $message");
302
303             if ($_ = &getFactoid($item)) {
304                 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
305             }
306
307             return 'INFOBOT QUERY';
308         } elsif ($message =~ /^REPLY <(.*?)> (.*)/) {   # reply.
309             my ($target,$item) = ($1,$2);
310
311             &status(":INFOBOT:REPLY $who: $message");
312
313             my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
314
315             if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
316                 &msg($target, "$who knew: $lhs $mhs $rhs");
317
318                 # "are" hack :)
319                 $rhs = "<REPLY> are" if ($mhs eq "are");
320                 &setFactInfo($lhs, "factoid_value", $rhs);
321             }
322
323             return 'INFOBOT REPLY';
324         } else {
325             &ERROR(":INFOBOT:UNKNOWN $who: $message");
326             return 'INFOBOT UNKNOWN';
327         }
328     }
329
330
331     # factoid forget.
332     if ($message =~ s/^forget\s+//i) {
333         return 'forget: no addr' unless ($addressed);
334
335         my $faqtoid = $message;
336         if ($faqtoid eq "") {
337             &help("forget");
338             return;
339         }
340
341         $faqtoid =~ tr/A-Z/a-z/;
342         my $result = &getFactoid($faqtoid);
343
344         if (defined $result) {
345             my $author = &getFactInfo($faqtoid, "created_by");
346             if (IsFlag("r") ne "r" && $author =~ /^\Q$who\E\!/i) {
347                 &msg($who, "you don't have access to remove that factoid");
348                 return;
349             }
350
351             return 'locked factoid' if (&IsLocked($faqtoid) == 1);
352
353             if (&IsParam("factoidDeleteDelay")) {
354                 &status("forgot (safe delete): <$who> '$faqtoid' =is=> '$result'");
355                 &setFactInfo($faqtoid, "factoid_key", $faqtoid." #DEL#");
356
357                 ### delete info. modified_ isn't really used.
358                 &setFactInfo($faqtoid, "modified_by", $who);
359                 &setFactInfo($faqtoid, "modified_time", time());
360             } else {
361                 &status("forget: <$who> '$faqtoid' =is=> '$result'");
362                 &delFactoid($faqtoid);
363             }
364
365             &performReply("i forgot $faqtoid");
366
367             $count{'Update'}++;
368         } else {
369             &performReply("i didn't have anything called '$faqtoid'");
370         }
371
372         return;
373     }
374
375     # factoid unforget/undelete.
376     if ($message =~ s/^un(forget|delete)\s+//i) {
377         return 'unforget: no addr' unless ($addressed);
378
379         if (!&IsParam("factoidDeleteDelay")) {
380             &performReply("safe delete has been disable so what is there to undelete?");
381             return;
382         }
383
384         my $faqtoid = $message;
385         if ($faqtoid eq "") {
386             &help("undelete");
387             return;
388         }
389
390         $faqtoid =~ tr/A-Z/a-z/;
391         my $result = &getFactoid($faqtoid." #DEL#");
392         my $check  = &getFactoid($faqtoid);
393
394         if (!defined $result) {
395             &performReply("i didn't have anything ('$faqtoid') to undelete.");
396             return;
397         }
398
399         if (defined $check) {
400             &performReply("cannot undeleted '$faqtoid' because it already exists?");
401             return;
402         }
403
404         &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
405
406         ### delete info. modified_ isn't really used.
407         &setFactInfo($faqtoid, "modified_by",  "");
408         &setFactInfo($faqtoid, "modified_time", 0);
409
410         &performReply("Successfully recovered '$faqtoid'.  Have fun now.");
411
412         $count{'Undelete'}++;
413
414         return;
415     }
416
417     # factoid locking.
418     if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
419         return 'lock: no addr 2' unless ($addressed);
420
421         my $function = lc $1;
422         my $faqtoid  = lc $4;
423
424         if ($faqtoid eq "") {
425             &help($function);
426             return;
427         }
428
429         # strongly requested by #debian on 19991028. -xk
430         if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
431             &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
432             &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
433             return;
434         }
435
436         if (&getFactoid($faqtoid) eq "") {
437             &msg($who, "factoid \002$faqtoid\002 does not exist");
438             return;
439         }
440
441         if ($function eq "lock") {
442             &CmdLock($faqtoid);
443         } else {
444             &CmdUnLock($faqtoid);
445         }
446
447         return;
448     }
449
450     # factoid rename.
451     if ($message =~ s/^rename(\s+|$)//) {
452         return 'rename: no addr' unless ($addressed);
453
454         if ($message eq "") {
455             &help("rename");
456             return;
457         }
458
459         if ($message =~ /^'(.*)'\s+'(.*)'$/) {
460             my($from,$to) = (lc $1, lc $2);
461
462             my $result = &getFactoid($from);
463             if (defined $result) {
464                 my $author = &getFactInfo($from, "created_by");
465                 if (&IsFlag("m") and $author =~ /^\Q$who\E\!/i) {
466                     &msg($who, "It's not yours to modify.");
467                     return;
468                 }
469
470                 if ($_ = &getFactoid($to)) {
471                     &performReply("destination factoid already exists.");
472                     return;
473                 }
474
475                 &setFactInfo($from,"factoid_key",$to);
476
477                 &status("rename: <$who> '$from' is now '$to'");
478                 &performReply("i renamed '$from' to '$to'");
479             } else {
480                 &performReply("i didn't have anything called '$from'");
481             }
482         } else {
483             &msg($who,"error: wrong format. ask me about 'help rename'.");
484         }
485
486         return;
487     }
488
489     # factoid substitution. (X =~ s/A/B/FLAG)
490     if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
491         my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
492         return 'subst: no addr' unless ($addressed);
493
494         # incorrect format.
495         if ($np =~ /$delim/) {
496             &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
497             return;
498         }
499
500         # success.
501         if (my $result = &getFactoid($faqtoid)) {
502             return 'subst: locked' if (&IsLocked($faqtoid) == 1);
503             my $was = $result;
504
505             if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
506                 if (length $result > $param{'maxDataSize'}) {
507                     &performReply("that's too long");
508                     return;
509                 }
510                 &setFactInfo($faqtoid, "factoid_value", $result);
511                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
512                 &performReply("OK");
513             } else {
514                 &performReply("that doesn't contain '$op'");
515             }
516         } else {
517             &performReply("i didn't have anything called '$faqtoid'");
518         }
519
520         return;
521     }
522
523
524     # Fix up $message for question.
525     for ($message) {
526         # fix the string.
527         s/^hey([, ]+)where/where/i;
528         s/whois/who is/ig;
529         s/where can i find/where is/i;
530         s/how about/where is/i;
531         s/ da / the /ig;
532
533         # clear the string of useless words.
534         s/^(stupid )?q(uestion)?:\s+//i;
535         s/^(does )?(any|ne)(1|one|body) know //i;
536
537         s/^[uh]+m*[,\.]* +//i;
538
539         s/^well([, ]+)//i;
540         s/^still([, ]+)//i;
541         s/^(gee|boy|golly|gosh)([, ]+)//i;
542         s/^(well|and|but|or|yes)([, ]+)//i;
543
544         s/^o+[hk]+(a+y+)?([,. ]+)//i;
545         s/^g(eez|osh|olly)([,. ]+)//i;
546         s/^w(ow|hee|o+ho+)([,. ]+)//i;
547         s/^heya?,?( folks)?([,. ]+)//i;
548     }
549
550     if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
551         $correction_plausible = 1;
552         &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
553     } else {
554         $correction_plausible = 0;
555     }
556
557     my $result = &doQuestion($message);
558     if (!defined $result or $result eq $noreply) {
559         return 'result from doQ undef.';
560     }
561
562     if (defined $result and $result !~ /^0?$/) {        # question.
563         &status("question: <$who> $message");
564         $count{'Question'}++;
565     } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
566         &loadMyModule("perlMath");
567         my $newresult = &perlMath();
568
569         if (defined $newresult and $newresult ne "") {
570             $result = $newresult;
571             &status("math: <$who> $message => $result");
572         }
573     }
574
575     if ($result !~ /^0?$/) {
576         &performStrictReply($result);
577         return;
578     }
579
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 1;