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 if (!open(FILE, $file)) {
16 &ERROR("FAILED loadHelp ($file): $!");
20 while (defined(my $help = <FILE>)) {
21 $help =~ s/^[\# ].*//;
24 my ($key, $val) = split(/:/, $help, 2);
27 $val =~ s/^D:/\002 Desc\002:/;
28 $val =~ s/^E:/\002Example\002:/;
29 $val =~ s/^N:/\002 NOTE\002:/;
30 $val =~ s/^U:/\002 Usage\002:/;
35 $help{$key} = "" if (!exists $help{$key});
36 $help{$key} .= $val."\n";
40 if (!defined $topic or $topic eq "") {
41 &msg($who, $help{'main'});
45 my $count = scalar(keys %help);
47 foreach (sort keys %help) {
49 $reply = scalar(@array) ." topics: ".
50 join("\002,\002 ", @array);
53 if (length $reply > 400 or $count == $i) {
62 $topic = &fixString(lc $topic);
64 if (exists $help{$topic}) {
65 foreach (split /\n/, $help{$topic}) {
66 &performStrictReply($_);
69 &pSReply("no help on $topic. Use 'help' without arguments.");
78 ### TODO: gotta hate an if statement.
79 if ($pathnfile =~ /(.*)\/(.*?)$/) {
87 if ($no_timehires) { # fallback.
89 } else { # the real thing.
90 return [gettimeofday()];
95 my($start_time) = shift;
97 if ($no_timehires) { # fallback.
98 return time() - $start_time;
99 } else { # the real thing.
100 return tv_interval ($start_time);
109 # Usage; &formListReply($rand, $prefix, @list);
111 my($rand, $prefix, @list) = @_;
112 my $total = scalar @list;
113 my $maxshow = $param{'maxListReplyCount'} || 10;
114 my $maxlen = $param{'maxListReplyLength'} || 400;
118 return $prefix ."returned no results." unless ($total);
123 foreach (&makeRandom($total)) {
124 push(@rand, $list[$_]);
125 last if (scalar @rand == $maxshow);
128 } elsif ($total > $maxshow) {
129 &status("formListReply: truncating list.");
131 @list = @list[0..$maxshow-1];
136 $reply = $prefix ."(\002". scalar(@list). "\002 shown";
137 $reply .= "; \002$total\002 total" if ($total != scalar @list);
138 $reply .= "): ". join(" \002;;\002 ",@list) .".";
140 last if (length($reply) < $maxlen and scalar(@list) <= $maxshow);
141 last if (scalar(@list) == 1);
149 ### Intelligence joining of arrays.
150 # Usage: &IJoin(@array);
154 } elsif (scalar @_ == 1) {
157 return join(', ',@{_}[0..$#_-1]) . " and $_[$#_]";
162 # Usage: &Time2String(seconds);
168 if (!defined $time or $time !~ /\d+/ or $time <= 0);
170 my $s = int($time) % 60;
171 my $m = int($time / 60) % 60;
172 my $h = int($time / 3600) % 24;
173 my $d = int($time / 86400);
175 $retval .= sprintf(" \002%d\002d", $d) if ($d != 0);
176 $retval .= sprintf(" \002%d\002h", $h) if ($h != 0);
177 $retval .= sprintf(" \002%d\002m", $m) if ($m != 0);
178 $retval .= sprintf(" \002%d\002s", $s) if ($s != 0);
180 return substr($retval, 1);
187 # Usage: &fixFileList(@files);
192 # generate a hash list.
194 if (/^(.*\/)(.*?)$/) {
198 @files = (); # reuse the array.
200 # sort the hash list appropriately.
201 foreach (sort keys %files) {
203 my @keys = sort keys %{$files{$file}};
204 my $i = scalar(@keys);
207 $file .= "\002{\002". join("\002|\002", @keys) ."\002}\002";
218 # Usage: &fixString($str);
220 my ($str, $level) = @_;
222 &WARN("fixString: str == NULL.");
227 s/^\s+//; # remove start whitespaces.
228 s/\s+$//; # remove end whitespaces.
229 s/\s+/ /g; # remove excessive whitespaces.
231 next unless (defined $level);
232 &DEBUG("strip control chars?");
233 s/[\cA-\c_]//ig # remove control characters.
239 # Usage: &fixPlural($str,$int);
244 &WARN("fixPlural: str == NULL.");
249 $str = "have" if ($int > 1);
250 } elsif ($str eq "is") {
251 $str = "are" if ($int > 1);
252 } elsif ($str eq "was") {
253 $str = "were" if ($int > 1);
254 } elsif ($str eq "this") {
255 $str = "these" if ($int > 1);
256 } elsif ($str =~ /y$/) {
259 $str .= "s"; # eg: "money" => "moneys".
265 $str .= "s" if ($int != 1);
277 sub getRandomLineFromFile {
281 &WARN("gRLfF: file '$file' does not exist.");
285 if (open(IN,$file)) {
288 if (!scalar @lines) {
289 &ERROR("GRLF: nothing loaded?");
293 while (my $line = &getRandom(@lines)) {
296 next if ($line =~ /^\#/);
297 next if ($line =~ /^\s*$/);
302 &WARN("gRLfF: could not open file '$file'.");
307 sub getLineFromFile {
308 my($file,$lineno) = @_;
311 &ERROR("getLineFromFile: file '$file' does not exist.");
315 if (open(IN,$file)) {
319 if ($lineno > scalar @lines) {
320 &ERROR("getLineFromFile: lineno exceeds line count from file.");
324 my $line = $lines[$lineno-1];
328 &ERROR("getLineFromFile: could not open file '$file'.");
333 # Usage: &getRandom(@array);
338 return $array[int(rand(scalar @array))];
341 # Usage: &getRandomInt("30-60");
346 &WARN("gRI: str == NULL.");
352 if ($str =~ /^(\d+)$/) {
354 my $fuzzy = int(rand 5);
359 return ($i - $fuzzy)*60;
361 return ($i + $fuzzy)*60;
363 } elsif ($str =~ /^(\d+)-(\d+)$/) {
364 return ($2 - $1)*int(rand $1)*60;
366 return $str; # hope we're safe.
369 &ERROR("getRandomInt: invalid arg '$str'.");
378 my ($left,$right) = @_;
379 return 0 unless defined $right;
380 return 0 unless defined $left;
381 return 1 if ($left =~ /^\Q$right$/i);
385 my $retval = &iseq(@_);
386 return 1 unless ($retval);
390 # Usage: &IsHostMatch($nuh);
395 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
396 $local{'nick'} = lc $1;
397 $local{'user'} = lc $2;
398 $local{'host'} = &makeHostMask(lc $3);
401 if ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
402 $this{'nick'} = lc $1;
403 $this{'user'} = lc $2;
404 $this{'host'} = &makeHostMask(lc $3);
406 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
407 return 1 if ($thisnuh eq "");
411 # auth if 1) user and host match 2) user and nick match.
412 # this may change in the future.
414 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
415 return 2 if ($this{'host'} eq $local{'host'});
416 return 1 if ($this{'nick'} eq $local{'nick'});
422 # Usage: &isStale($file, $age);
424 my ($file, $age) = @_;
426 return 1 unless ( -f $file);
427 return 1 if (time() - (stat($file))[9] > $age*60*60*24);
428 my $delta = time() - (stat($file))[9];
429 my $hage = $age*60*60*24;
437 # Usage: &makeHostMask($host);
441 if ($host =~ /^$mask{ip}$/) {
445 my @array = split(/\./, $host);
446 return $host if (scalar @array <= 3);
447 return "*.".join('.',@{array}[1..$#array]);
450 # Usage: &makeRandom(int);
456 if ($max =~ /^\D+$/) {
457 &ERROR("makeRandom: arg ($max) is not integer.");
462 &ERROR("makeRandom: arg ($max) is not positive.");
467 while (scalar keys %done < $max) {
468 my $rand = int(rand $max);
469 next if (exists $done{$rand});
480 return unless (&IsParam("minLengthBeforePrivate"));
481 return if ($force_public_reply);
483 if (length $reply > $param{'minLengthBeforePrivate'}) {
484 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
485 $msgType = 'private';
493 # Usage: &validExec($string);
497 if ($str =~ /[\'\"\|]/) { # invalid.
504 # Usage: &validFactoid($lhs,$rhs);
510 # allow the following only if they have been made on purpose.
511 if ($rhs ne "" and $rhs !~ /^</) {
512 / \Q$ident$/i and last; # someone said i'm something.
514 /^(it|that|there|what)('s)?(\s+|$)/ and last;
515 /^you('re)?(\s+|$)/ and last;
517 /^(where|who|why|when|how)(\s+|$)/ and last;
518 /^(this|that|these|those|they)(\s+|$)/ and last;
519 /^(every(one|body)|we) / and last;
525 /^add topic / and last; # topic management.
526 /( add$| add |^add )/ and last; # borked teach statement.
527 /^learn / and last; # teach. damn morons.
528 /^tell (\S+) about / and last; # tell.
529 /\=\~/ and last; # substituition.
530 /^\S+ to \S+ \S+/ and last; # babelfish.
532 /^\=/ and last; # botnick = heh is.
533 /wants you to know/ and last;
541 /\=\>/ and last; # '=>'.
542 /\;\;/ and last; # ';;'.
543 /\|\|/ and last; # '||'.
545 /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
547 /\\$/ and last; # forgot shift for '?'.
552 /^because / and last;
554 /^h(is|er) / and last;
561 /^supposedly/ and last;
566 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
570 # weird/special stuff. also old blootbot or stock infobot bugs.
571 $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
574 $rhs =~ /^\Q$lhs /i and last;
575 last if ($rhs =~ /^is /i and / is$/);
583 # Usage: &hasProfanity($string);
590 /dick|dildo/ and last;
591 /shit|turd|crap/ and last;
592 /pussy|[ck]unt/ and last;
593 /wh[0o]re|bitch|slut/ and last;
601 ### rename to hasChanConf() ?
605 ### TODO: specific reason why it failed.
606 if (&IsChanConf($param)) {
609 &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
615 my ($label, $code) = @_;
619 &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid);
621 if (&IsParam("forking") and $$ == $bot_pid) {
622 return unless &addForked($label);
624 $SIG{CHLD} = 'IGNORE';
625 $pid = eval { fork() };
626 return if $pid; # parent does nothing
628 select(undef, undef, undef, 0.2);
629 # &status("fork starting for '$label', PID == $$.");
630 &status("--- fork starting for '$label', PID == $$ ---");
631 &shmWrite($shm,"SET FORKPID $label $$");
636 ### TODO: use AUTOLOAD
638 if ($label !~ /-/ and !&loadMyModule($myModules{$label})) {
639 &DEBUG("Forker: failed?");
644 $code->(); # weird, hey?
646 &WARN("Forker: code not defined!");
653 return 1 unless (exists $file{PID});
654 return 1 unless ( -f $file{PID});
655 return 1 if (unlink $file{PID});
656 return 0 if ( -f $file{PID});