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) {
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}) {
69 &msg($who, "no help on $topic. Use 'help' without arguments.");
78 ### TODO: gotta hate an if statement.
79 if ($pathnfile =~ /(.*)\/(.*?)$/) {
87 if ($no_syscall) { # fallback.
89 } else { # the real thing.
90 my $time = pack("LL", 0);
92 syscall(&SYS_gettimeofday, $time, 0);
93 my @time = unpack("LL",$time);
95 return sprintf("%d.%d", @time);
104 # Usage; &formListReply($rand, $prefix, @list);
106 my($rand, $prefix, @list) = @_;
107 my $total = scalar @list;
108 my $maxshow = $param{'maxListReplyCount'} || 10;
109 my $maxlen = $param{'maxListReplyLength'} || 400;
113 return $prefix ."returned no results." unless ($total);
118 foreach (&makeRandom($total)) {
119 push(@rand, $list[$_]);
120 last if (scalar @rand == $maxshow);
123 } elsif ($total > $maxshow) {
124 &status("formListReply: truncating list.");
126 @list = @list[0..$maxshow-1];
131 $reply = $prefix ."(\002". scalar(@list). "\002 shown";
132 $reply .= "; \002$total\002 total" if ($total != scalar @list);
133 $reply .= "): ". join(" \002;;\002 ",@list) .".";
135 last if (length($reply) < $maxlen and scalar(@list) <= $maxshow);
136 last if (scalar(@list) == 1);
144 ### Intelligence joining of arrays.
145 # Usage: &IJoin(@array);
149 } elsif (scalar @_ == 1) {
152 return join(', ',@{_}[0..$#_-1]) . " and $_[$#_]";
157 # Usage: &Time2String(seconds);
162 return("0s") if ($time !~ /\d+/ or $time <= 0);
164 my $s = int($time) % 60;
165 my $m = int($time / 60) % 60;
166 my $h = int($time / 3600) % 24;
167 my $d = int($time / 86400);
169 $retval .= sprintf(" \002%d\002d", $d) if ($d != 0);
170 $retval .= sprintf(" \002%d\002h", $h) if ($h != 0);
171 $retval .= sprintf(" \002%d\002m", $m) if ($m != 0);
172 $retval .= sprintf(" \002%d\002s", $s) if ($s != 0);
174 return substr($retval, 1);
181 # Usage: &fixFileList(@files);
186 # generate a hash list.
188 if (/^(.*\/)(.*?)$/) {
192 @files = (); # reuse the array.
194 # sort the hash list appropriately.
195 foreach (sort keys %files) {
197 my @keys = sort keys %{$files{$file}};
198 my $i = scalar(@keys);
201 $file .= "\002{\002". join("\002|\002", @keys) ."\002}\002";
212 # Usage: &fixString($str);
214 my ($str, $level) = @_;
216 &WARN("fixString: str == NULL.");
221 s/^\s+//; # remove start whitespaces.
222 s/\s+$//; # remove end whitespaces.
223 s/\s+/ /g; # remove excessive whitespaces.
225 next unless (defined $level);
226 &DEBUG("strip control chars?");
227 s/[\cA-\c_]//ig # remove control characters.
233 # Usage: &fixPlural($str,$int);
238 $str = "have" if ($int > 1);
239 } elsif ($str eq "is") {
240 $str = "are" if ($int > 1);
241 } elsif ($str eq "was") {
242 $str = "were" if ($int > 1);
243 } elsif ($str eq "this") {
244 $str = "these" if ($int > 1);
245 } elsif ($str =~ /y$/) {
248 $str .= "s"; # eg: "money" => "moneys".
254 $str .= "s" if ($int != 1);
266 sub getRandomLineFromFile {
270 &WARN("gRLfF: file '$file' does not exist.");
274 if (open(IN,$file)) {
277 if (!scalar @lines) {
278 &ERROR("GRLF: nothing loaded?");
282 while (my $line = &getRandom(@lines)) {
285 next if ($line =~ /^\#/);
286 next if ($line =~ /^\s*$/);
291 &WARN("gRLfF: could not open file '$file'.");
296 sub getLineFromFile {
297 my($file,$lineno) = @_;
300 &ERROR("getLineFromFile: file '$file' does not exist.");
304 if (open(IN,$file)) {
308 if ($lineno > scalar @lines) {
309 &ERROR("getLineFromFile: lineno exceeds line count from file.");
313 my $line = $lines[$lineno-1];
317 &ERROR("getLineFromFile: could not open file '$file'.");
322 # Usage: &getRandom(@array);
327 return $array[int(rand(scalar @array))];
330 # Usage: &getRandomInt("30-60");
336 if ($str =~ /^(\d+)$/) {
338 my $fuzzy = int(rand 5);
343 return ($i - $fuzzy)*60;
345 return ($i + $fuzzy)*60;
347 } elsif ($str =~ /^(\d+)-(\d+)$/) {
348 return ($2 - $1)*int(rand $1)*60;
350 return $str; # hope we're safe.
353 &ERROR("getRandomInt: invalid arg '$str'.");
362 my ($left,$right) = @_;
363 return 0 unless defined $right;
364 return 0 unless defined $left;
365 return 1 if ($left =~ /^\Q$right$/i);
369 my $retval = &iseq(@_);
370 return 1 unless ($retval);
374 # Usage: &IsHostMatch($nuh);
379 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
380 $local{'nick'} = lc $1;
381 $local{'user'} = lc $2;
382 $local{'host'} = &makeHostMask(lc $3);
385 if ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
386 $this{'nick'} = lc $1;
387 $this{'user'} = lc $2;
388 $this{'host'} = &makeHostMask(lc $3);
390 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
391 return 1 if ($thisnuh eq "");
395 # auth if 1) user and host match 2) user and nick match.
396 # this may change in the future.
398 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
399 return 2 if ($this{'host'} eq $local{'host'});
400 return 1 if ($this{'nick'} eq $local{'nick'});
406 # Usage: &isStale($file, $age);
408 my ($file, $age) = @_;
410 return 1 unless ( -f $file);
411 return 1 if (time() - (stat($file))[9] > $age*60*60*24);
412 my $delta = time() - (stat($file))[9];
413 my $hage = $age*60*60*24;
421 # Usage: &makeHostMask($host);
425 if ($host =~ /^$mask{ip}$/) {
429 my @array = split(/\./, $host);
430 return $host if (scalar @array <= 3);
431 return "*.".join('.',@{array}[1..$#array]);
434 # Usage: &makeRandom(int);
440 if ($max =~ /^\D+$/) {
441 &ERROR("makeRandom: arg ($max) is not integer.");
446 &ERROR("makeRandom: arg ($max) is not positive.");
451 while (scalar keys %done < $max) {
452 my $rand = int(rand $max);
453 next if (exists $done{$rand});
464 return unless (&IsParam("minLengthBeforePrivate"));
465 return if ($force_public_reply);
467 if (length $reply > $param{'minLengthBeforePrivate'}) {
468 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
469 $msgType = 'private';
477 # Usage: &validExec($string);
481 if ($str =~ /[\'\"\|]/) { # invalid.
488 # Usage: &validFactoid($lhs,$rhs);
494 # allow the following only if they have been made on purpose.
495 if ($rhs ne "" and $rhs !~ /^</) {
496 / \Q$ident$/i and last; # someone said i'm something.
498 /^(it|that|there|what)('s)?(\s+|$)/ and last;
499 /^you('re)?(\s+|$)/ and last;
501 /^(where|who|why|when|how)(\s+|$)/ and last;
502 /^(this|that|these|those|they)(\s+|$)/ and last;
503 /^(every(one|body)|we) / and last;
509 /^add topic / and last; # topic management.
510 /( add$| add |^add )/ and last; # borked teach statement.
511 /^learn / and last; # teach. damn morons.
512 /^tell (\S+) about / and last; # tell.
513 /\=\~/ and last; # substituition.
514 /^\S+ to \S+ \S+/ and last; # babelfish.
516 /^\=/ and last; # botnick = heh is.
517 /wants you to know/ and last;
525 /\=\>/ and last; # '=>'.
526 /\;\;/ and last; # ';;'.
527 /\|\|/ and last; # '||'.
529 /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
531 /\\$/ and last; # forgot shift for '?'.
536 /^because / and last;
538 /^h(is|er) / and last;
545 /^supposedly/ and last;
550 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
554 # weird/special stuff. also old blootbot or stock infobot bugs.
555 $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
558 $rhs =~ /^\Q$lhs /i and last;
559 last if ($rhs =~ /^is /i and / is$/);
567 # Usage: &hasProfanity($string);
574 /dick|dildo/ and last;
575 /shit|turd|crap/ and last;
576 /pussy|[ck]unt/ and last;
577 /wh[0o]re|bitch|slut/ and last;
588 if (&IsParam($param)) {
591 &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
597 my ($label, $code) = @_;
601 &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid);
603 if (&IsParam("forking") and $$ == $bot_pid) {
604 return $noreply unless (&addForked($label));
605 $SIG{CHLD} = 'IGNORE';
606 $pid = eval { fork() };
607 return $noreply if $pid; # parent does nothing
608 &status("fork starting for '$label', PID == $$.");
609 &shmWrite($shm,"SET FORKPID $label $$");
612 ### TODO: use AUTOLOAD
613 if (!&loadMyModule($myModules{$label})) {
614 &DEBUG("Forker: failed?");
619 $code->(); # weird, hey?
621 &WARN("Forker: code not defined!");
624 if (defined $pid) { # child.
626 &status("fork finished for '$label'.");
632 return 1 unless (exists $file{PID});
633 return 1 unless ( -f $file{PID});
634 return 1 if (unlink $file{PID});
635 return 0 if ( -f $file{PID});