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 if (s/[\cA-\c_]//ig) { # remove control characters.
233 &DEBUG("stripped control chars");
240 # Usage: &fixPlural($str,$int);
245 &WARN("fixPlural: str == NULL.");
250 $str = "have" if ($int > 1);
251 } elsif ($str eq "is") {
252 $str = "are" if ($int > 1);
253 } elsif ($str eq "was") {
254 $str = "were" if ($int > 1);
255 } elsif ($str eq "this") {
256 $str = "these" if ($int > 1);
257 } elsif ($str =~ /y$/) {
260 $str .= "s"; # eg: "money" => "moneys".
266 $str .= "s" if ($int != 1);
278 sub getRandomLineFromFile {
282 &WARN("gRLfF: file '$file' does not exist.");
286 if (open(IN,$file)) {
289 if (!scalar @lines) {
290 &ERROR("GRLF: nothing loaded?");
294 while (my $line = &getRandom(@lines)) {
297 next if ($line =~ /^\#/);
298 next if ($line =~ /^\s*$/);
303 &WARN("gRLfF: could not open file '$file'.");
308 sub getLineFromFile {
309 my($file,$lineno) = @_;
312 &ERROR("getLineFromFile: file '$file' does not exist.");
316 if (open(IN,$file)) {
320 if ($lineno > scalar @lines) {
321 &ERROR("getLineFromFile: lineno exceeds line count from file.");
325 my $line = $lines[$lineno-1];
329 &ERROR("getLineFromFile: could not open file '$file'.");
334 # Usage: &getRandom(@array);
339 return $array[int(rand(scalar @array))];
342 # Usage: &getRandomInt("30-60");
347 &WARN("gRI: str == NULL.");
353 if ($str =~ /^(\d+)$/) {
355 my $fuzzy = int(rand 5);
360 return ($i - $fuzzy)*60;
362 return ($i + $fuzzy)*60;
364 } elsif ($str =~ /^(\d+)-(\d+)$/) {
365 return ($2 - $1)*int(rand $1)*60;
367 return $str; # hope we're safe.
370 &ERROR("getRandomInt: invalid arg '$str'.");
379 my ($left,$right) = @_;
380 return 0 unless defined $right;
381 return 0 unless defined $left;
382 return 1 if ($left =~ /^\Q$right$/i);
386 my $retval = &iseq(@_);
387 return 1 unless ($retval);
391 # Usage: &IsHostMatch($nuh);
396 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
397 $local{'nick'} = lc $1;
398 $local{'user'} = lc $2;
399 $local{'host'} = &makeHostMask(lc $3);
402 if ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
403 $this{'nick'} = lc $1;
404 $this{'user'} = lc $2;
405 $this{'host'} = &makeHostMask(lc $3);
407 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
408 return 1 if ($thisnuh eq "");
412 # auth if 1) user and host match 2) user and nick match.
413 # this may change in the future.
415 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
416 return 2 if ($this{'host'} eq $local{'host'});
417 return 1 if ($this{'nick'} eq $local{'nick'});
423 # Usage: &isStale($file, $age);
425 my ($file, $age) = @_;
427 return 1 unless ( -f $file);
428 return 1 if (time() - (stat($file))[9] > $age*60*60*24);
429 my $delta = time() - (stat($file))[9];
430 my $hage = $age*60*60*24;
438 # Usage: &makeHostMask($host);
442 if ($host =~ /^$mask{ip}$/) {
446 my @array = split(/\./, $host);
447 return $host if (scalar @array <= 3);
448 return "*.".join('.',@{array}[1..$#array]);
451 # Usage: &makeRandom(int);
457 if ($max =~ /^\D+$/) {
458 &ERROR("makeRandom: arg ($max) is not integer.");
463 &ERROR("makeRandom: arg ($max) is not positive.");
468 while (scalar keys %done < $max) {
469 my $rand = int(rand $max);
470 next if (exists $done{$rand});
481 return unless (&IsParam("minLengthBeforePrivate"));
482 return if ($force_public_reply);
484 if (length $reply > $param{'minLengthBeforePrivate'}) {
485 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
486 $msgType = 'private';
494 # Usage: &validExec($string);
498 if ($str =~ /[\'\"\|]/) { # invalid.
505 # Usage: &validFactoid($lhs,$rhs);
511 # allow the following only if they have been made on purpose.
512 if ($rhs ne "" and $rhs !~ /^</) {
513 / \Q$ident$/i and last; # someone said i'm something.
515 /^(it|that|there|what)('s)?(\s+|$)/ and last;
516 /^you('re)?(\s+|$)/ and last;
518 /^(where|who|why|when|how)(\s+|$)/ and last;
519 /^(this|that|these|those|they)(\s+|$)/ and last;
520 /^(every(one|body)|we) / and last;
526 /^add topic / and last; # topic management.
527 /( add$| add |^add )/ and last; # borked teach statement.
528 /^learn / and last; # teach. damn morons.
529 /^tell (\S+) about / and last; # tell.
530 /\=\~/ and last; # substituition.
531 /^\S+ to \S+ \S+/ and last; # babelfish.
533 /^\=/ and last; # botnick = heh is.
534 /wants you to know/ and last;
542 /\=\>/ and last; # '=>'.
543 /\;\;/ and last; # ';;'.
544 /\|\|/ and last; # '||'.
546 /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
548 /\\$/ and last; # forgot shift for '?'.
553 /^because / and last;
555 /^h(is|er) / and last;
562 /^supposedly/ and last;
567 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
571 # weird/special stuff. also old blootbot or stock infobot bugs.
572 $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
575 $rhs =~ /^\Q$lhs /i and last;
576 last if ($rhs =~ /^is /i and / is$/);
584 # Usage: &hasProfanity($string);
591 /dick|dildo/ and last;
592 /shit|turd|crap/ and last;
593 /pussy|[ck]unt/ and last;
594 /wh[0o]re|bitch|slut/ and last;
602 ### rename to hasChanConf() ?
606 ### TODO: specific reason why it failed.
607 if (&IsChanConf($param)) {
610 &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
616 my ($label, $code) = @_;
620 &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid);
622 if (&IsParam("forking") and $$ == $bot_pid) {
623 return unless &addForked($label);
625 $SIG{CHLD} = 'IGNORE';
626 $pid = eval { fork() };
627 return if $pid; # parent does nothing
629 select(undef, undef, undef, 0.2);
630 # &status("fork starting for '$label', PID == $$.");
631 &status("--- fork starting for '$label', PID == $$ ---");
632 &shmWrite($shm,"SET FORKPID $label $$");
637 ### TODO: use AUTOLOAD
639 if ($label !~ /-/ and !&loadMyModule($myModules{$label})) {
640 &DEBUG("Forker: failed?");
645 $code->(); # weird, hey?
647 &WARN("Forker: code not defined!");
654 return 1 unless (exists $file{PID});
655 return 1 unless ( -f $file{PID});
656 return 1 if (unlink $file{PID});
657 return 0 if ( -f $file{PID});