2 # Misc.pl: Miscellaneous stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
8 if (&IsParam("useStrict")) { use strict; }
12 my $file = $bot_misc_dir."/blootbot.help";
15 # crude hack for pSReply() to work as expected.
16 $msgType = "private" if ($msgType eq "public");
18 if (!open(FILE, $file)) {
19 &ERROR("FAILED loadHelp ($file): $!");
23 while (defined(my $help = <FILE>)) {
24 $help =~ s/^[\# ].*//;
27 my ($key, $val) = split(/:/, $help, 2);
30 $val =~ s/^D:/\002 Desc\002:/;
31 $val =~ s/^E:/\002Example\002:/;
32 $val =~ s/^N:/\002 NOTE\002:/;
33 $val =~ s/^U:/\002 Usage\002:/;
38 $help{$key} = "" if (!exists $help{$key});
39 $help{$key} .= $val."\n";
43 if (!defined $topic or $topic eq "") {
44 &msg($who, $help{'main'});
48 my $count = scalar(keys %help);
50 foreach (sort keys %help) {
52 $reply = scalar(@array) ." topics: ".
53 join("\002,\002 ", @array);
56 if (length $reply > 400 or $count == $i) {
65 $topic = &fixString(lc $topic);
67 if (exists $help{$topic}) {
68 foreach (split /\n/, $help{$topic}) {
69 &performStrictReply($_);
72 &pSReply("no help on $topic. Use 'help' without arguments.");
81 ### TODO: gotta hate an if statement.
82 if ($pathnfile =~ /(.*)\/(.*?)$/) {
90 if ($no_timehires) { # fallback.
92 } else { # the real thing.
93 return [gettimeofday()];
98 my($start_time) = shift;
100 if ($no_timehires) { # fallback.
101 return time() - $start_time;
102 } else { # the real thing.
103 return tv_interval ($start_time);
112 # Usage; &formListReply($rand, $prefix, @list);
114 my($rand, $prefix, @list) = @_;
115 my $total = scalar @list;
116 my $maxshow = $param{'maxListReplyCount'} || 10;
117 my $maxlen = $param{'maxListReplyLength'} || 400;
121 return $prefix ."returned no results." unless ($total);
126 foreach (&makeRandom($total)) {
127 push(@rand, $list[$_]);
128 last if (scalar @rand == $maxshow);
131 } elsif ($total > $maxshow) {
132 &status("formListReply: truncating list.");
134 @list = @list[0..$maxshow-1];
139 $reply = $prefix ."(\002". scalar(@list). "\002 shown";
140 $reply .= "; \002$total\002 total" if ($total != scalar @list);
141 $reply .= "): ". join(" \002;;\002 ",@list) .".";
143 last if (length($reply) < $maxlen and scalar(@list) <= $maxshow);
144 last if (scalar(@list) == 1);
152 ### Intelligence joining of arrays.
153 # Usage: &IJoin(@array);
157 } elsif (scalar @_ == 1) {
160 return join(', ',@{_}[0..$#_-1]) . " and $_[$#_]";
165 # Usage: &Time2String(seconds);
170 return("NULL s") if (!defined $time or $time !~ /\d+/);
178 my $s = int($time) % 60;
179 my $m = int($time / 60) % 60;
180 my $h = int($time / 3600) % 24;
181 my $d = int($time / 86400);
183 $retval .= sprintf(" \002%d\002d", $d) if ($d != 0);
184 $retval .= sprintf(" \002%d\002h", $h) if ($h != 0);
185 $retval .= sprintf(" \002%d\002m", $m) if ($m != 0);
186 $retval .= sprintf(" \002%d\002s", $s) if ($s != 0);
188 return $prefix.substr($retval, 1);
195 # Usage: &fixFileList(@files);
200 # generate a hash list.
202 if (/^(.*\/)(.*?)$/) {
206 @files = (); # reuse the array.
208 # sort the hash list appropriately.
209 foreach (sort keys %files) {
211 my @keys = sort keys %{$files{$file}};
212 my $i = scalar(@keys);
215 $file .= "\002{\002". join("\002|\002", @keys) ."\002}\002";
226 # Usage: &fixString($str);
228 my ($str, $level) = @_;
230 &WARN("fixString: str == NULL.");
235 s/^\s+//; # remove start whitespaces.
236 s/\s+$//; # remove end whitespaces.
237 s/\s+/ /g; # remove excessive whitespaces.
239 next unless (defined $level);
240 if (s/[\cA-\c_]//ig) { # remove control characters.
241 &DEBUG("stripped control chars");
248 # Usage: &fixPlural($str,$int);
253 &WARN("fixPlural: str == NULL.");
257 if (!defined $int or $int =~ /^\D+$/) {
258 &WARN("fixPlural: int != defined or int");
263 $str = "have" if ($int > 1);
264 } elsif ($str eq "is") {
265 $str = "are" if ($int > 1);
266 } elsif ($str eq "was") {
267 $str = "were" if ($int > 1);
268 } elsif ($str eq "this") {
269 $str = "these" if ($int > 1);
270 } elsif ($str =~ /y$/) {
273 $str .= "s"; # eg: "money" => "moneys".
279 $str .= "s" if ($int != 1);
291 sub getRandomLineFromFile {
295 &WARN("gRLfF: file '$file' does not exist.");
299 if (open(IN,$file)) {
302 if (!scalar @lines) {
303 &ERROR("GRLF: nothing loaded?");
307 while (my $line = &getRandom(@lines)) {
310 next if ($line =~ /^\#/);
311 next if ($line =~ /^\s*$/);
316 &WARN("gRLfF: could not open file '$file'.");
321 sub getLineFromFile {
322 my($file,$lineno) = @_;
325 &ERROR("getLineFromFile: file '$file' does not exist.");
329 if (open(IN,$file)) {
333 if ($lineno > scalar @lines) {
334 &ERROR("getLineFromFile: lineno exceeds line count from file.");
338 my $line = $lines[$lineno-1];
342 &ERROR("getLineFromFile: could not open file '$file'.");
347 # Usage: &getRandom(@array);
352 return $array[int(rand(scalar @array))];
355 # Usage: &getRandomInt("30-60");
360 &WARN("gRI: str == NULL.");
366 if ($str =~ /^(\d+(\.\d+)?)$/) {
368 my $fuzzy = int(rand 5);
373 return ($i - $fuzzy)*60;
375 return ($i + $fuzzy)*60;
377 } elsif ($str =~ /^(\d+)-(\d+)$/) {
378 return ($2 - $1)*int(rand $1)*60;
380 return $str; # hope we're safe.
383 &ERROR("getRandomInt: invalid arg '$str'.");
392 my ($left,$right) = @_;
393 return 0 unless defined $right;
394 return 0 unless defined $left;
395 return 1 if ($left =~ /^\Q$right$/i);
399 my $retval = &iseq(@_);
400 return 1 unless ($retval);
404 # Usage: &IsHostMatch($nuh);
409 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
410 $local{'nick'} = lc $1;
411 $local{'user'} = lc $2;
412 $local{'host'} = &makeHostMask(lc $3);
415 if ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
416 $this{'nick'} = lc $1;
417 $this{'user'} = lc $2;
418 $this{'host'} = &makeHostMask(lc $3);
420 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
421 return 1 if ($thisnuh eq "");
425 # auth if 1) user and host match 2) user and nick match.
426 # this may change in the future.
428 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
429 return 2 if ($this{'host'} eq $local{'host'});
430 return 1 if ($this{'nick'} eq $local{'nick'});
436 # Usage: &isStale($file, $age);
438 my ($file, $age) = @_;
441 &WARN("isStale: age == NULL.");
445 if (!defined $file) {
446 &WARN("isStale: file == NULL.");
450 return 1 unless ( -f $file);
451 if ($file =~ /idx/) {
452 my $age = time() - (stat($file))[9];
453 &DEBUG("stale: $age. (". &Time2String($age) .")");
455 return 1 if (time() - (stat($file))[9] > $age*60*60*24);
463 # Usage: &makeHostMask($host);
467 if ($host =~ /^$mask{ip}$/) {
471 my @array = split(/\./, $host);
472 return $host if (scalar @array <= 3);
473 return "*.".join('.',@{array}[1..$#array]);
476 # Usage: &makeRandom(int);
482 if ($max =~ /^\D+$/) {
483 &ERROR("makeRandom: arg ($max) is not integer.");
488 &ERROR("makeRandom: arg ($max) is not positive.");
493 while (scalar keys %done < $max) {
494 my $rand = int(rand $max);
495 next if (exists $done{$rand});
506 return unless (&IsParam("minLengthBeforePrivate"));
507 return if ($force_public_reply);
509 if (length $reply > $param{'minLengthBeforePrivate'}) {
510 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
511 $msgType = 'private';
519 # Usage: &validExec($string);
523 if ($str =~ /[\'\"\|]/) { # invalid.
530 # Usage: &validFactoid($lhs,$rhs);
536 # allow the following only if they have been made on purpose.
537 if ($rhs ne "" and $rhs !~ /^</) {
538 / \Q$ident$/i and last; # someone said i'm something.
540 /^(it|that|there|what)('s)?(\s+|$)/ and last;
541 /^you('re)?(\s+|$)/ and last;
543 /^(where|who|why|when|how)(\s+|$)/ and last;
544 /^(this|that|these|those|they)(\s+|$)/ and last;
545 /^(every(one|body)|we) / and last;
551 /^add topic / and last; # topic management.
552 /( add$| add |^add )/ and last; # borked teach statement.
553 /^learn / and last; # teach. damn morons.
554 /^tell (\S+) about / and last; # tell.
555 /\=\~/ and last; # substituition.
556 /^\S+ to \S+ \S+/ and last; # babelfish.
558 /^\=/ and last; # botnick = heh is.
559 /wants you to know/ and last;
567 /\=\>/ and last; # '=>'.
568 /\;\;/ and last; # ';;'.
569 /\|\|/ and last; # '||'.
571 /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
573 /\\$/ and last; # forgot shift for '?'.
578 /^because / and last;
580 /^h(is|er) / and last;
587 /^supposedly/ and last;
592 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
596 # weird/special stuff. also old blootbot or stock infobot bugs.
597 $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
600 $rhs =~ /^\Q$lhs /i and last;
601 last if ($rhs =~ /^is /i and / is$/);
609 # Usage: &hasProfanity($string);
616 /dick|dildo/ and last;
617 /shit|turd|crap/ and last;
618 /pussy|[ck]unt/ and last;
619 /wh[0o]re|bitch|slut/ and last;
630 if (&IsChanConf($param) or &IsParam($param)) {
633 ### TODO: specific reason why it failed.
634 &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
640 my ($label, $code) = @_;
644 &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid);
646 if (&IsParam("forking") and $$ == $bot_pid) {
647 return unless &addForked($label);
649 $SIG{CHLD} = 'IGNORE';
650 $pid = eval { fork() };
651 return if $pid; # parent does nothing
653 select(undef, undef, undef, 0.2);
654 # &status("fork starting for '$label', PID == $$.");
655 &status("--- fork starting for '$label', PID == $$ ---");
656 &shmWrite($shm,"SET FORKPID $label $$");
661 ### TODO: use AUTOLOAD
663 if ($label !~ /-/ and !&loadMyModule($myModules{$label})) {
664 &DEBUG("Forker: failed?");
669 $code->(); # weird, hey?
671 &WARN("Forker: code not defined!");
678 return 1 unless (exists $file{PID});
679 return 1 unless ( -f $file{PID});
680 return 1 if (unlink $file{PID});
681 return 0 if ( -f $file{PID});
686 my $salt = join '',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64];
688 return crypt($str, $salt);