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}) {
69 &msg($who, "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);
167 return("0s") if ($time !~ /\d+/ or $time <= 0);
169 my $s = int($time) % 60;
170 my $m = int($time / 60) % 60;
171 my $h = int($time / 3600) % 24;
172 my $d = int($time / 86400);
174 $retval .= sprintf(" \002%d\002d", $d) if ($d != 0);
175 $retval .= sprintf(" \002%d\002h", $h) if ($h != 0);
176 $retval .= sprintf(" \002%d\002m", $m) if ($m != 0);
177 $retval .= sprintf(" \002%d\002s", $s) if ($s != 0);
179 return substr($retval, 1);
186 # Usage: &fixFileList(@files);
191 # generate a hash list.
193 if (/^(.*\/)(.*?)$/) {
197 @files = (); # reuse the array.
199 # sort the hash list appropriately.
200 foreach (sort keys %files) {
202 my @keys = sort keys %{$files{$file}};
203 my $i = scalar(@keys);
206 $file .= "\002{\002". join("\002|\002", @keys) ."\002}\002";
217 # Usage: &fixString($str);
219 my ($str, $level) = @_;
221 &WARN("fixString: str == NULL.");
226 s/^\s+//; # remove start whitespaces.
227 s/\s+$//; # remove end whitespaces.
228 s/\s+/ /g; # remove excessive whitespaces.
230 next unless (defined $level);
231 &DEBUG("strip control chars?");
232 s/[\cA-\c_]//ig # remove control characters.
238 # Usage: &fixPlural($str,$int);
243 $str = "have" if ($int > 1);
244 } elsif ($str eq "is") {
245 $str = "are" if ($int > 1);
246 } elsif ($str eq "was") {
247 $str = "were" if ($int > 1);
248 } elsif ($str eq "this") {
249 $str = "these" if ($int > 1);
250 } elsif ($str =~ /y$/) {
253 $str .= "s"; # eg: "money" => "moneys".
259 $str .= "s" if ($int != 1);
271 sub getRandomLineFromFile {
275 &WARN("gRLfF: file '$file' does not exist.");
279 if (open(IN,$file)) {
282 if (!scalar @lines) {
283 &ERROR("GRLF: nothing loaded?");
287 while (my $line = &getRandom(@lines)) {
290 next if ($line =~ /^\#/);
291 next if ($line =~ /^\s*$/);
296 &WARN("gRLfF: could not open file '$file'.");
301 sub getLineFromFile {
302 my($file,$lineno) = @_;
305 &ERROR("getLineFromFile: file '$file' does not exist.");
309 if (open(IN,$file)) {
313 if ($lineno > scalar @lines) {
314 &ERROR("getLineFromFile: lineno exceeds line count from file.");
318 my $line = $lines[$lineno-1];
322 &ERROR("getLineFromFile: could not open file '$file'.");
327 # Usage: &getRandom(@array);
332 return $array[int(rand(scalar @array))];
335 # Usage: &getRandomInt("30-60");
341 if ($str =~ /^(\d+)$/) {
343 my $fuzzy = int(rand 5);
348 return ($i - $fuzzy)*60;
350 return ($i + $fuzzy)*60;
352 } elsif ($str =~ /^(\d+)-(\d+)$/) {
353 return ($2 - $1)*int(rand $1)*60;
355 return $str; # hope we're safe.
358 &ERROR("getRandomInt: invalid arg '$str'.");
367 my ($left,$right) = @_;
368 return 0 unless defined $right;
369 return 0 unless defined $left;
370 return 1 if ($left =~ /^\Q$right$/i);
374 my $retval = &iseq(@_);
375 return 1 unless ($retval);
379 # Usage: &IsHostMatch($nuh);
384 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
385 $local{'nick'} = lc $1;
386 $local{'user'} = lc $2;
387 $local{'host'} = &makeHostMask(lc $3);
390 if ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
391 $this{'nick'} = lc $1;
392 $this{'user'} = lc $2;
393 $this{'host'} = &makeHostMask(lc $3);
395 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
396 return 1 if ($thisnuh eq "");
400 # auth if 1) user and host match 2) user and nick match.
401 # this may change in the future.
403 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
404 return 2 if ($this{'host'} eq $local{'host'});
405 return 1 if ($this{'nick'} eq $local{'nick'});
411 # Usage: &isStale($file, $age);
413 my ($file, $age) = @_;
415 return 1 unless ( -f $file);
416 return 1 if (time() - (stat($file))[9] > $age*60*60*24);
417 my $delta = time() - (stat($file))[9];
418 my $hage = $age*60*60*24;
426 # Usage: &makeHostMask($host);
430 if ($host =~ /^$mask{ip}$/) {
434 my @array = split(/\./, $host);
435 return $host if (scalar @array <= 3);
436 return "*.".join('.',@{array}[1..$#array]);
439 # Usage: &makeRandom(int);
445 if ($max =~ /^\D+$/) {
446 &ERROR("makeRandom: arg ($max) is not integer.");
451 &ERROR("makeRandom: arg ($max) is not positive.");
456 while (scalar keys %done < $max) {
457 my $rand = int(rand $max);
458 next if (exists $done{$rand});
469 return unless (&IsParam("minLengthBeforePrivate"));
470 return if ($force_public_reply);
472 if (length $reply > $param{'minLengthBeforePrivate'}) {
473 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
474 $msgType = 'private';
482 # Usage: &validExec($string);
486 if ($str =~ /[\'\"\|]/) { # invalid.
493 # Usage: &validFactoid($lhs,$rhs);
499 # allow the following only if they have been made on purpose.
500 if ($rhs ne "" and $rhs !~ /^</) {
501 / \Q$ident$/i and last; # someone said i'm something.
503 /^(it|that|there|what)('s)?(\s+|$)/ and last;
504 /^you('re)?(\s+|$)/ and last;
506 /^(where|who|why|when|how)(\s+|$)/ and last;
507 /^(this|that|these|those|they)(\s+|$)/ and last;
508 /^(every(one|body)|we) / and last;
514 /^add topic / and last; # topic management.
515 /( add$| add |^add )/ and last; # borked teach statement.
516 /^learn / and last; # teach. damn morons.
517 /^tell (\S+) about / and last; # tell.
518 /\=\~/ and last; # substituition.
519 /^\S+ to \S+ \S+/ and last; # babelfish.
521 /^\=/ and last; # botnick = heh is.
522 /wants you to know/ and last;
530 /\=\>/ and last; # '=>'.
531 /\;\;/ and last; # ';;'.
532 /\|\|/ and last; # '||'.
534 /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
536 /\\$/ and last; # forgot shift for '?'.
541 /^because / and last;
543 /^h(is|er) / and last;
550 /^supposedly/ and last;
555 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
559 # weird/special stuff. also old blootbot or stock infobot bugs.
560 $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
563 $rhs =~ /^\Q$lhs /i and last;
564 last if ($rhs =~ /^is /i and / is$/);
572 # Usage: &hasProfanity($string);
579 /dick|dildo/ and last;
580 /shit|turd|crap/ and last;
581 /pussy|[ck]unt/ and last;
582 /wh[0o]re|bitch|slut/ and last;
593 if (&IsParam($param)) {
596 &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
602 my ($label, $code) = @_;
606 &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid);
608 if (&IsParam("forking") and $$ == $bot_pid) {
609 return $noreply unless (&addForked($label));
611 $SIG{CHLD} = 'IGNORE';
612 $pid = eval { fork() };
613 return $noreply if $pid; # parent does nothing
615 select(undef, undef, undef, 0.2);
616 &status("fork starting for '$label', PID == $$.");
617 &shmWrite($shm,"SET FORKPID $label $$");
622 ### TODO: use AUTOLOAD
624 if ($label !~ /-/ and !&loadMyModule($myModules{$label})) {
625 &DEBUG("Forker: failed?");
630 $code->(); # weird, hey?
632 &WARN("Forker: code not defined!");
639 return 1 unless (exists $file{PID});
640 return 1 unless ( -f $file{PID});
641 return 1 if (unlink $file{PID});
642 return 0 if ( -f $file{PID});