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 # crude hack for pSReply() to work as expected.
16 $msgType = "private" if ($msgType eq "public");
18 if (!open(FILE, $file)) {
19 &ERROR("FAILED loadHelp ($file): $!");
23 while (defined(my $help = <FILE>)) {
24 $help =~ s/^[\# ].*//;
27 my ($key, $val) = split(/:/, $help, 2);
30 $val =~ s/^D:/\002 Desc\002:/;
31 $val =~ s/^E:/\002Example\002:/;
32 $val =~ s/^N:/\002 NOTE\002:/;
33 $val =~ s/^U:/\002 Usage\002:/;
38 $help{$key} = "" if (!exists $help{$key});
39 $help{$key} .= $val."\n";
43 if (!defined $topic or $topic eq "") {
44 &msg($who, $help{'main'});
48 my $count = scalar(keys %help);
50 foreach (sort keys %help) {
52 $reply = scalar(@array) ." topics: ".
53 join("\002,\002 ", @array);
56 if (length $reply > 400 or $count == $i) {
65 $topic = &fixString(lc $topic);
67 if (exists $help{$topic}) {
68 foreach (split /\n/, $help{$topic}) {
69 &performStrictReply($_);
72 &pSReply("no help on $topic. Use 'help' without arguments.");
81 ### TODO: gotta hate an if statement.
82 if ($pathnfile =~ /(.*)\/(.*?)$/) {
90 if ($no_timehires) { # fallback.
92 } else { # the real thing.
93 return [gettimeofday()];
98 my($start_time) = shift;
100 if ($no_timehires) { # fallback.
101 return time() - $start_time;
102 } else { # the real thing.
103 return tv_interval ($start_time);
112 # Usage; &formListReply($rand, $prefix, @list);
114 my($rand, $prefix, @list) = @_;
115 my $total = scalar @list;
116 my $maxshow = $param{'maxListReplyCount'} || 10;
117 my $maxlen = $param{'maxListReplyLength'} || 400;
121 return $prefix ."returned no results." unless ($total);
126 foreach (&makeRandom($total)) {
127 push(@rand, $list[$_]);
128 last if (scalar @rand == $maxshow);
131 } elsif ($total > $maxshow) {
132 &status("formListReply: truncating list.");
134 @list = @list[0..$maxshow-1];
139 $reply = $prefix ."(\002". scalar(@list). "\002 shown";
140 $reply .= "; \002$total\002 total" if ($total != scalar @list);
141 $reply .= "): ". join(" \002;;\002 ",@list) .".";
143 last if (length($reply) < $maxlen and scalar(@list) <= $maxshow);
144 last if (scalar(@list) == 1);
152 ### Intelligence joining of arrays.
153 # Usage: &IJoin(@array);
157 } elsif (scalar @_ == 1) {
160 return join(', ',@{_}[0..$#_-1]) . " and $_[$#_]";
165 # Usage: &Time2String(seconds);
171 if (!defined $time or $time !~ /\d+/ or $time <= 0);
173 my $s = int($time) % 60;
174 my $m = int($time / 60) % 60;
175 my $h = int($time / 3600) % 24;
176 my $d = int($time / 86400);
178 $retval .= sprintf(" \002%d\002d", $d) if ($d != 0);
179 $retval .= sprintf(" \002%d\002h", $h) if ($h != 0);
180 $retval .= sprintf(" \002%d\002m", $m) if ($m != 0);
181 $retval .= sprintf(" \002%d\002s", $s) if ($s != 0);
183 return substr($retval, 1);
190 # Usage: &fixFileList(@files);
195 # generate a hash list.
197 if (/^(.*\/)(.*?)$/) {
201 @files = (); # reuse the array.
203 # sort the hash list appropriately.
204 foreach (sort keys %files) {
206 my @keys = sort keys %{$files{$file}};
207 my $i = scalar(@keys);
210 $file .= "\002{\002". join("\002|\002", @keys) ."\002}\002";
221 # Usage: &fixString($str);
223 my ($str, $level) = @_;
225 &WARN("fixString: str == NULL.");
230 s/^\s+//; # remove start whitespaces.
231 s/\s+$//; # remove end whitespaces.
232 s/\s+/ /g; # remove excessive whitespaces.
234 next unless (defined $level);
235 if (s/[\cA-\c_]//ig) { # remove control characters.
236 &DEBUG("stripped control chars");
243 # Usage: &fixPlural($str,$int);
248 &WARN("fixPlural: str == NULL.");
252 if (!defined $int or $int =~ /^\D+$/) {
253 &WARN("fixPlural: int != defined or int");
258 $str = "have" if ($int > 1);
259 } elsif ($str eq "is") {
260 $str = "are" if ($int > 1);
261 } elsif ($str eq "was") {
262 $str = "were" if ($int > 1);
263 } elsif ($str eq "this") {
264 $str = "these" if ($int > 1);
265 } elsif ($str =~ /y$/) {
268 $str .= "s"; # eg: "money" => "moneys".
274 $str .= "s" if ($int != 1);
286 sub getRandomLineFromFile {
290 &WARN("gRLfF: file '$file' does not exist.");
294 if (open(IN,$file)) {
297 if (!scalar @lines) {
298 &ERROR("GRLF: nothing loaded?");
302 while (my $line = &getRandom(@lines)) {
305 next if ($line =~ /^\#/);
306 next if ($line =~ /^\s*$/);
311 &WARN("gRLfF: could not open file '$file'.");
316 sub getLineFromFile {
317 my($file,$lineno) = @_;
320 &ERROR("getLineFromFile: file '$file' does not exist.");
324 if (open(IN,$file)) {
328 if ($lineno > scalar @lines) {
329 &ERROR("getLineFromFile: lineno exceeds line count from file.");
333 my $line = $lines[$lineno-1];
337 &ERROR("getLineFromFile: could not open file '$file'.");
342 # Usage: &getRandom(@array);
347 return $array[int(rand(scalar @array))];
350 # Usage: &getRandomInt("30-60");
355 &WARN("gRI: str == NULL.");
361 if ($str =~ /^(\d+(\.\d+)?)$/) {
363 my $fuzzy = int(rand 5);
368 return ($i - $fuzzy)*60;
370 return ($i + $fuzzy)*60;
372 } elsif ($str =~ /^(\d+)-(\d+)$/) {
373 return ($2 - $1)*int(rand $1)*60;
375 return $str; # hope we're safe.
378 &ERROR("getRandomInt: invalid arg '$str'.");
387 my ($left,$right) = @_;
388 return 0 unless defined $right;
389 return 0 unless defined $left;
390 return 1 if ($left =~ /^\Q$right$/i);
394 my $retval = &iseq(@_);
395 return 1 unless ($retval);
399 # Usage: &IsHostMatch($nuh);
404 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
405 $local{'nick'} = lc $1;
406 $local{'user'} = lc $2;
407 $local{'host'} = &makeHostMask(lc $3);
410 if ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
411 $this{'nick'} = lc $1;
412 $this{'user'} = lc $2;
413 $this{'host'} = &makeHostMask(lc $3);
415 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
416 return 1 if ($thisnuh eq "");
420 # auth if 1) user and host match 2) user and nick match.
421 # this may change in the future.
423 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
424 return 2 if ($this{'host'} eq $local{'host'});
425 return 1 if ($this{'nick'} eq $local{'nick'});
431 # Usage: &isStale($file, $age);
433 my ($file, $age) = @_;
436 &WARN("isStale: age == NULL.");
440 return 1 unless ( -f $file);
441 return 1 if (time() - (stat($file))[9] > $age*60*60*24);
442 my $delta = time() - (stat($file))[9];
443 my $hage = $age*60*60*24;
451 # Usage: &makeHostMask($host);
455 if ($host =~ /^$mask{ip}$/) {
459 my @array = split(/\./, $host);
460 return $host if (scalar @array <= 3);
461 return "*.".join('.',@{array}[1..$#array]);
464 # Usage: &makeRandom(int);
470 if ($max =~ /^\D+$/) {
471 &ERROR("makeRandom: arg ($max) is not integer.");
476 &ERROR("makeRandom: arg ($max) is not positive.");
481 while (scalar keys %done < $max) {
482 my $rand = int(rand $max);
483 next if (exists $done{$rand});
494 return unless (&IsParam("minLengthBeforePrivate"));
495 return if ($force_public_reply);
497 if (length $reply > $param{'minLengthBeforePrivate'}) {
498 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
499 $msgType = 'private';
507 # Usage: &validExec($string);
511 if ($str =~ /[\'\"\|]/) { # invalid.
518 # Usage: &validFactoid($lhs,$rhs);
524 # allow the following only if they have been made on purpose.
525 if ($rhs ne "" and $rhs !~ /^</) {
526 / \Q$ident$/i and last; # someone said i'm something.
528 /^(it|that|there|what)('s)?(\s+|$)/ and last;
529 /^you('re)?(\s+|$)/ and last;
531 /^(where|who|why|when|how)(\s+|$)/ and last;
532 /^(this|that|these|those|they)(\s+|$)/ and last;
533 /^(every(one|body)|we) / and last;
539 /^add topic / and last; # topic management.
540 /( add$| add |^add )/ and last; # borked teach statement.
541 /^learn / and last; # teach. damn morons.
542 /^tell (\S+) about / and last; # tell.
543 /\=\~/ and last; # substituition.
544 /^\S+ to \S+ \S+/ and last; # babelfish.
546 /^\=/ and last; # botnick = heh is.
547 /wants you to know/ and last;
555 /\=\>/ and last; # '=>'.
556 /\;\;/ and last; # ';;'.
557 /\|\|/ and last; # '||'.
559 /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
561 /\\$/ and last; # forgot shift for '?'.
566 /^because / and last;
568 /^h(is|er) / and last;
575 /^supposedly/ and last;
580 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
584 # weird/special stuff. also old blootbot or stock infobot bugs.
585 $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
588 $rhs =~ /^\Q$lhs /i and last;
589 last if ($rhs =~ /^is /i and / is$/);
597 # Usage: &hasProfanity($string);
604 /dick|dildo/ and last;
605 /shit|turd|crap/ and last;
606 /pussy|[ck]unt/ and last;
607 /wh[0o]re|bitch|slut/ and last;
615 ### rename to hasChanConf() ?
619 if (&IsChanConf($param)) {
622 ### TODO: specific reason why it failed.
623 &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
629 my ($label, $code) = @_;
633 &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid);
635 if (&IsParam("forking") and $$ == $bot_pid) {
636 return unless &addForked($label);
638 $SIG{CHLD} = 'IGNORE';
639 $pid = eval { fork() };
640 return if $pid; # parent does nothing
642 select(undef, undef, undef, 0.2);
643 # &status("fork starting for '$label', PID == $$.");
644 &status("--- fork starting for '$label', PID == $$ ---");
645 &shmWrite($shm,"SET FORKPID $label $$");
650 ### TODO: use AUTOLOAD
652 if ($label !~ /-/ and !&loadMyModule($myModules{$label})) {
653 &DEBUG("Forker: failed?");
658 $code->(); # weird, hey?
660 &WARN("Forker: code not defined!");
667 return 1 unless (exists $file{PID});
668 return 1 unless ( -f $file{PID});
669 return 1 if (unlink $file{PID});
670 return 0 if ( -f $file{PID});