2 # Misc.pl: Miscellaneous stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
8 if (&IsParam("useStrict")) { use strict; }
14 my $file = $bot_misc_dir."/blootbot.help";
17 if (!open(FILE, $file)) {
18 &ERROR("FAILED loadHelp ($file): $!");
22 while (defined(my $help = <FILE>)) {
23 $help =~ s/^[\# ].*//;
26 my ($key, $val) = split(/:/, $help, 2);
29 $val =~ s/^D:/\002 Desc\002:/;
30 $val =~ s/^E:/\002Example\002:/;
31 $val =~ s/^N:/\002 NOTE\002:/;
32 $val =~ s/^U:/\002 Usage\002:/;
37 $help{$key} = "" if (!exists $help{$key});
38 $help{$key} .= $val."\n";
42 if (!defined $topic or $topic eq "") {
43 &msg($who, $help{'main'});
47 my $count = scalar(keys %help);
49 foreach (sort keys %help) {
51 $reply = scalar(@array) ." topics: ".
52 join("\002,\002 ", @array);
55 if (length $reply > 400 or $count == $i) {
64 $topic = &fixString(lc $topic);
66 if (exists $help{$topic}) {
67 foreach (split /\n/, $help{$topic}) {
71 &msg($who, "no help on $topic. Use 'help' without arguments.");
80 ### TODO: gotta hate an if statement.
81 if ($pathnfile =~ /(.*)\/(.*?)$/) {
89 if ($no_syscall) { # fallback.
91 } else { # the real thing.
92 my $time = pack("LL", 0);
94 syscall(&SYS_gettimeofday, $time, 0);
95 my @time = unpack("LL",$time);
97 return sprintf("%d.%d", @time);
106 # Usage; &formListReply($rand, $prefix, @list);
108 my($rand, $prefix, @list) = @_;
109 my $total = scalar @list;
110 my $maxshow = $param{'maxListReplyCount'} || 10;
111 my $maxlen = $param{'maxListReplyLength'} || 400;
115 return $prefix ."returned no results." unless ($total);
120 foreach (&makeRandom($total)) {
121 push(@rand, $list[$_]);
122 last if (scalar @rand == $maxshow);
125 } elsif ($total > $maxshow) {
126 &status("formListReply: truncating list.");
128 @list = @list[0..$maxshow-1];
133 $reply = $prefix ."(\002". scalar(@list). "\002 shown";
134 $reply .= "; \002$total\002 total" if ($total != scalar @list);
135 $reply .= "): ". join(" \002;;\002 ",@list) .".";
137 last if (length($reply) < $maxlen and scalar(@list) <= $maxshow);
138 last if (scalar(@list) == 1);
146 ### Intelligence joining of arrays.
147 # Usage: &IJoin(@array);
151 } elsif (scalar @_ == 1) {
154 return join(', ',@{_}[0..$#_-1]) . " and $_[$#_]";
159 # Usage: &Time2String(seconds);
164 return("0s") if ($time !~ /\d+/ or $time <= 0);
166 my $s = int($time) % 60;
167 my $m = int($time / 60) % 60;
168 my $h = int($time / 3600) % 24;
169 my $d = int($time / 86400);
171 $retval .= sprintf(" \002%d\002d", $d) if ($d != 0);
172 $retval .= sprintf(" \002%d\002h", $h) if ($h != 0);
173 $retval .= sprintf(" \002%d\002m", $m) if ($m != 0);
174 $retval .= sprintf(" \002%d\002s", $s) if ($s != 0);
176 return substr($retval, 1);
183 # Usage: &fixFileList(@files);
188 # generate a hash list.
190 if (/^(.*\/)(.*?)$/) {
194 @files = (); # reuse the array.
196 # sort the hash list appropriately.
197 foreach (sort keys %files) {
199 my @keys = sort keys %{$files{$file}};
200 my $i = scalar(@keys);
203 $file .= "\002{\002". join("\002|\002", @keys) ."\002}\002";
214 # Usage: &fixString($str);
216 my ($str, $level) = @_;
218 &WARN("fixString: str == NULL.");
223 s/^\s+//; # remove start whitespaces.
224 s/\s+$//; # remove end whitespaces.
225 s/\s+/ /g; # remove excessive whitespaces.
227 next unless (defined $level);
228 &DEBUG("strip control chars?");
229 s/[\cA-\c_]//ig # remove control characters.
235 # Usage: &fixPlural($str,$int);
240 $str = "have" if ($int > 1);
241 } elsif ($str eq "is") {
242 $str = "are" if ($int > 1);
243 } elsif ($str eq "was") {
244 $str = "were" if ($int > 1);
245 } elsif ($str eq "this") {
246 $str = "these" if ($int > 1);
247 } elsif ($str =~ /y$/) {
250 $str .= "s"; # eg: "money" => "moneys".
256 $str .= "s" if ($int != 1);
268 sub getRandomLineFromFile {
272 &WARN("gRLfF: file '$file' does not exist.");
276 if (open(IN,$file)) {
279 if (!scalar @lines) {
280 &ERROR("GRLF: nothing loaded?");
284 while (my $line = &getRandom(@lines)) {
287 next if ($line =~ /^\#/);
288 next if ($line =~ /^\s*$/);
293 &WARN("gRLfF: could not open file '$file'.");
298 sub getLineFromFile {
299 my($file,$lineno) = @_;
302 &ERROR("getLineFromFile: file '$file' does not exist.");
306 if (open(IN,$file)) {
310 if ($lineno > scalar @lines) {
311 &ERROR("getLineFromFile: lineno exceeds line count from file.");
315 my $line = $lines[$lineno-1];
319 &ERROR("getLineFromFile: could not open file '$file'.");
324 # Usage: &getRandom(@array);
329 return $array[int(rand(scalar @array))];
332 # Usage: &getRandomInt("30-60");
338 if ($str =~ /^(\d+)$/) {
340 my $fuzzy = int(rand 5);
345 return ($i - $fuzzy)*60;
347 return ($i + $fuzzy)*60;
349 } elsif ($str =~ /^(\d+)-(\d+)$/) {
350 return ($2 - $1)*int(rand $1)*60;
352 return $str; # hope we're safe.
355 &ERROR("getRandomInt: invalid arg '$str'.");
364 my ($left,$right) = @_;
365 return 0 unless defined $right;
366 return 0 unless defined $left;
367 return 1 if ($left =~ /^\Q$right$/i);
371 my $retval = &iseq(@_);
372 return 1 unless ($retval);
376 # Usage: &IsHostMatch($nuh);
381 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
382 $local{'nick'} = lc $1;
383 $local{'user'} = lc $2;
384 $local{'host'} = &makeHostMask(lc $3);
387 if ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
388 $this{'nick'} = lc $1;
389 $this{'user'} = lc $2;
390 $this{'host'} = &makeHostMask(lc $3);
392 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
393 return 1 if ($thisnuh eq "");
397 # auth if 1) user and host match 2) user and nick match.
398 # this may change in the future.
400 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
401 return 2 if ($this{'host'} eq $local{'host'});
402 return 1 if ($this{'nick'} eq $local{'nick'});
408 # Usage: &isStale($file, $age);
410 my ($file, $age) = @_;
412 return 1 unless ( -f $file);
413 return 1 if (time() - (stat($file))[9] > $age*60*60*24);
414 my $delta = time() - (stat($file))[9];
415 my $hage = $age*60*60*24;
423 # Usage: &makeHostMask($host);
427 if ($host =~ /^$mask{ip}$/) {
431 my @array = split(/\./, $host);
432 return $host if (scalar @array <= 3);
433 return "*.".join('.',@{array}[1..$#array]);
436 # Usage: &makeRandom(int);
442 if ($max =~ /^\D+$/) {
443 &ERROR("makeRandom: arg ($max) is not integer.");
448 &ERROR("makeRandom: arg ($max) is not positive.");
453 while (scalar keys %done < $max) {
454 my $rand = int(rand $max);
455 next if (exists $done{$rand});
466 return unless (&IsParam("minLengthBeforePrivate"));
467 return if ($force_public_reply);
469 if (length $reply > $param{'minLengthBeforePrivate'}) {
470 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
471 $msgType = 'private';
479 # Usage: &validExec($string);
483 if ($str =~ /[\'\"\|]/) { # invalid.
490 # Usage: &validFactoid($lhs,$rhs);
496 # allow the following only if they have been made on purpose.
497 if ($rhs ne "" and $rhs !~ /^</) {
498 / \Q$ident$/i and last; # someone said i'm something.
500 /^(it|that|there|what)('s)?(\s+|$)/ and last;
501 /^you('re)?(\s+|$)/ and last;
503 /^(where|who|why|when|how)(\s+|$)/ and last;
504 /^(this|that|these|those|they)(\s+|$)/ and last;
505 /^(every(one|body)|we) / and last;
511 /^add topic / and last; # topic management.
512 /( add$| add |^add )/ and last; # borked teach statement.
513 /^learn / and last; # teach. damn morons.
514 /^tell (\S+) about / and last; # tell.
515 /\=\~/ and last; # substituition.
516 /^\S+ to \S+ \S+/ and last; # babelfish.
518 /^\=/ and last; # botnick = heh is.
519 /wants you to know/ and last;
527 /\=\>/ and last; # '=>'.
528 /\;\;/ and last; # ';;'.
529 /\|\|/ and last; # '||'.
531 /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
533 /\\$/ and last; # forgot shift for '?'.
538 /^because / and last;
540 /^h(is|er) / and last;
547 /^supposedly/ and last;
552 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
556 # weird/special stuff. also old blootbot or stock infobot bugs.
557 $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
560 $rhs =~ /^\Q$lhs /i and last;
561 last if ($rhs =~ /^is /i and / is$/);
569 # Usage: &hasProfanity($string);
576 /dick|dildo/ and last;
577 /shit|turd|crap/ and last;
578 /pussy|[ck]unt/ and last;
579 /wh[0o]re|bitch|slut/ and last;
590 if (&IsParam($param)) {
593 &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
599 my ($label, $code) = @_;
603 &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid);
605 if (&IsParam("forking") and $$ == $bot_pid) {
606 return $noreply unless (&addForked($label));
607 ### use select(undef,undef,undef,0.2); ...
608 $SIG{CHLD} = 'IGNORE';
609 $pid = eval { fork() };
610 return $noreply if $pid; # parent does nothing
611 &status("fork starting for '$label', PID == $$.");
612 &shmWrite($shm,"SET FORKPID $label $$");
615 ### TODO: use AUTOLOAD
616 if (!&loadMyModule($myModules{$label})) {
617 &DEBUG("Forker: failed?");
622 $code->(); # weird, hey?
624 &WARN("Forker: code not defined!");
627 if (defined $pid) { # child.
629 &status("fork finished for '$label'.");
635 return 1 unless (exists $file{PID});
636 return 1 unless ( -f $file{PID});
637 return 1 if (unlink $file{PID});
638 return 0 if ( -f $file{PID});