2 # Misc.pl: Miscellaneous stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
10 use vars qw(%file %mask %param %cmdstats %myModules);
11 use vars qw($msgType $who $bot_pid $nuh $shm $force_public_reply
12 $no_timehires $bot_data_dir $addrchar);
16 my $file = $bot_data_dir."/blootbot.help";
19 # crude hack for pSReply() to work as expected.
20 $msgType = "private" if ($msgType eq "public");
22 if (!open(FILE, $file)) {
23 &ERROR("Failed reading help file ($file): $!");
27 while (defined(my $help = <FILE>)) {
28 $help =~ s/^[\# ].*//;
31 my ($key, $val) = split(/:/, $help, 2);
34 $val =~ s/^D:/\002 Desc\002:/;
35 $val =~ s/^E:/\002Example\002:/;
36 $val =~ s/^N:/\002 NOTE\002:/;
37 $val =~ s/^U:/\002 Usage\002:/;
42 $help{$key} = "" if (!exists $help{$key});
43 $help{$key} .= $val."\n";
47 if (!defined $topic or $topic eq "") {
48 &msg($who, $help{'main'});
52 my $count = scalar(keys %help);
54 foreach (sort keys %help) {
56 $reply = scalar(@array) ." topics: ".
57 join("\002,\002 ", @array);
60 if (length $reply > 400 or $count == $i) {
69 $topic = &fixString(lc $topic);
71 if (exists $help{$topic}) {
72 foreach (split /\n/, $help{$topic}) {
76 &pSReply("no help on $topic. Use 'help' without arguments.");
85 ### TODO: gotta hate an if statement.
86 if ($pathnfile =~ /(.*)\/(.*?)$/) {
94 if ($no_timehires) { # fallback.
96 } else { # the real thing.
97 return [gettimeofday()];
102 my($start_time) = shift;
104 if ($no_timehires) { # fallback.
105 return time() - $start_time;
106 } else { # the real thing.
107 return tv_interval ($start_time);
116 # Usage; &formListReply($rand, $prefix, @list);
118 my($rand, $prefix, @list) = @_;
119 my $total = scalar @list;
120 my $maxshow = &getChanConfDefault('maxListReplyCount', 15);
121 my $maxlen = &getChanConfDefault('maxListReplyLength', 400);
124 # remove irc overhead
128 return $prefix ."returned no results." unless ($total);
133 foreach (&makeRandom($total)) {
134 push(@rand, $list[$_]);
135 last if (scalar @rand == $maxshow);
138 } elsif ($total > $maxshow) {
139 &status("formListReply: truncating list.");
141 @list = @list[0..$maxshow-1];
146 $reply = $prefix ."(\002". scalar(@list). "\002";
147 $reply .= " of \002$total\002" if ($total != scalar @list);
148 $reply .= "): " . join(" \002;;\002 ", @list) .".";
150 last if (length($reply) < $maxlen and scalar(@list) <= $maxshow);
151 last if (scalar(@list) == 1);
159 ### Intelligence joining of arrays.
160 # Usage: &IJoin(@array);
164 } elsif (scalar @_ == 1) {
167 return join(', ',@{_}[0..$#_-1]) . " and $_[$#_]";
172 # Usage: &Time2String(seconds);
178 return "NULL" if (!defined $time);
179 return $time if ($time !~ /\d+/);
186 $t[0] = int($time) % 60;
187 $t[1] = int($time / 60) % 60;
188 $t[2] = int($time / 3600) % 24;
189 $t[3] = int($time / 86400);
191 push(@s, "$t[3]d") if ($t[3] != 0);
192 push(@s, "$t[2]h") if ($t[2] != 0);
193 push(@s, "$t[1]m") if ($t[1] != 0);
194 push(@s, "$t[0]s") if ($t[0] != 0 or !@s);
196 my $retval = $prefix.join(' ', @s);
197 $retval =~ s/(\d+)/\002$1\002/g;
205 # Usage: &fixFileList(@files);
210 # generate a hash list.
212 next unless /^(.*\/)(.*?)$/;
216 @files = (); # reuse the array.
218 # sort the hash list appropriately.
219 foreach (sort keys %files) {
221 my @keys = sort keys %{ $files{$file} };
222 my $i = scalar(@keys);
224 if (scalar @keys > 3) {
225 pop @keys while (scalar @keys > 3);
230 $file .= "\002{\002". join("\002|\002", @keys) ."\002}\002";
241 # Usage: &fixString($str);
243 my ($str, $level) = @_;
245 &WARN("fixString: str == NULL.");
250 s/^\s+//; # remove start whitespaces.
251 s/\s+$//; # remove end whitespaces.
252 s/\s+/ /g; # remove excessive whitespaces.
254 next unless (defined $level);
255 if (s/[\cA-\c_]//ig) { # remove control characters.
256 &DEBUG("stripped control chars");
263 # Usage: &fixPlural($str,$int);
268 &WARN("fixPlural: str == NULL.");
272 if (!defined $int or $int =~ /^\D+$/) {
273 &WARN("fixPlural: int != defined or int");
278 $str = "have" if ($int > 1);
279 } elsif ($str eq "is") {
280 $str = "are" if ($int > 1);
281 } elsif ($str eq "was") {
282 $str = "were" if ($int > 1);
283 } elsif ($str eq "this") {
284 $str = "these" if ($int > 1);
285 } elsif ($str =~ /y$/) {
288 $str .= "s"; # eg: "money" => "moneys".
294 $str .= "s" if ($int != 1);
304 sub getRandomLineFromFile {
307 if (!open(IN, $file)) {
308 &WARN("gRLfF: could not open ($file): $!");
315 if (!scalar @lines) {
316 &ERROR("GRLF: nothing loaded?");
320 # could we use the filehandler instead and put it through getRandom?
321 while (my $line = &getRandom(@lines)) {
324 next if ($line =~ /^\#/);
325 next if ($line =~ /^\s*$/);
331 sub getLineFromFile {
332 my($file,$lineno) = @_;
335 &ERROR("getLineFromFile: file '$file' does not exist.");
339 if (open(IN,$file)) {
343 if ($lineno > scalar @lines) {
344 &ERROR("getLineFromFile: lineno exceeds line count from file.");
348 my $line = $lines[$lineno-1];
352 &ERROR("gLFF: Could not open file ($file): $!");
357 # Usage: &getRandom(@array);
362 return $array[int(rand(scalar @array))];
365 # Usage: &getRandomInt("30-60");
370 &WARN("gRI: str == NULL.");
376 if ($str =~ /^(\d+(\.\d+)?)$/) {
378 my $fuzzy = int(rand 5);
383 return ($i - $fuzzy)*60;
385 return ($i + $fuzzy)*60;
387 } elsif ($str =~ /^(\d+)-(\d+)$/) {
388 return ($2 - $1)*int(rand $1)*60;
390 return $str; # hope we're safe.
393 &ERROR("getRandomInt: invalid arg '$str'.");
402 my ($left,$right) = @_;
403 return 0 unless defined $right;
404 return 0 unless defined $left;
405 return 1 if ($left =~ /^\Q$right$/i);
409 my $retval = &iseq(@_);
410 return 1 unless ($retval);
414 # Usage: &IsHostMatch($nuh);
419 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
420 $local{'nick'} = lc $1;
421 $local{'user'} = lc $2;
422 $local{'host'} = &makeHostMask(lc $3);
425 if (!defined $thisnuh) {
426 &WARN("IHM: thisnuh == NULL.");
428 } elsif ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
429 $this{'nick'} = lc $1;
430 $this{'user'} = lc $2;
431 $this{'host'} = &makeHostMask(lc $3);
433 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
434 return 1 if ($thisnuh eq "");
438 # auth if 1) user and host match 2) user and nick match.
439 # this may change in the future.
441 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
442 return 2 if ($this{'host'} eq $local{'host'});
443 return 1 if ($this{'nick'} eq $local{'nick'});
449 # Usage: &isStale($file, $age);
451 my ($file, $age) = @_;
454 &WARN("isStale: age == NULL.");
458 if (!defined $file) {
459 &WARN("isStale: file == NULL.");
463 &DEBUG("!exist $file") if (! -f $file);
465 return 1 unless ( -f $file);
466 if ($file =~ /idx/) {
467 my $age2 = time() - (stat($file))[9];
468 &VERB("stale: $age2. (". &Time2String($age2) .")",2);
470 $age *= 60*60*24 if ($age >= 0 and $age < 30);
472 return 1 if (time() - (stat($file))[9] > $age);
477 my ($file, $time) = @_;
483 my $time_file = (stat $file)[9];
485 if ($time <= $time_file) {
496 # Usage: &makeHostMask($host);
501 if ($host =~ s/^(\S+!\S+\@)//) {
502 &DEBUG("mHM: detected nick!user\@ for host arg; fixing");
507 if ($host =~ /^$mask{ip}$/) {
508 return $nu."$1.$2.$3.*";
511 my @array = split(/\./, $host);
512 return $nu.$host if (scalar @array <= 3);
513 return $nu."*.".join('.',@{array}[1..$#array]);
516 # Usage: &makeRandom(int);
522 if ($max =~ /^\D+$/) {
523 &ERROR("makeRandom: arg ($max) is not integer.");
528 &ERROR("makeRandom: arg ($max) is not positive.");
533 while (scalar keys %done < $max) {
534 my $rand = int(rand $max);
535 next if (exists $done{$rand});
546 return unless (&IsParam("minLengthBeforePrivate"));
547 return if ($force_public_reply);
549 if (length $reply > $param{'minLengthBeforePrivate'}) {
550 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
551 $msgType = 'private';
559 # Usage: &validExec($string);
563 if ($str =~ /[\`\'\"\|]/) { # invalid.
570 # Usage: &hasProfanity($string);
577 /dick|dildo/ and last;
579 /pussy|[ck]unt/ and last;
580 /wh[0o]re|bitch|slut/ and last;
591 if (&IsChanConf($param) or &IsParam($param)) {
594 ### TODO: specific reason why it failed.
595 &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
601 my ($label, $code) = @_;
605 &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid);
607 if (&IsParam("forking") and $$ == $bot_pid) {
608 return unless &addForked($label);
610 $SIG{CHLD} = 'IGNORE';
611 $pid = eval { fork() };
612 return if $pid; # parent does nothing
614 select(undef, undef, undef, 0.2);
615 # &status("fork starting for '$label', PID == $$.");
616 &status("--- fork starting for '$label', PID == $$ ---");
617 &shmWrite($shm,"SET FORKPID $label $$");
622 ### TODO: use AUTOLOAD
624 if ($label !~ /-/ and !&loadMyModule($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});
647 my $salt = join '',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64];
649 return crypt($str, $salt);
653 return unless (&getChanConfList("ircTextCounters"));
655 foreach (keys %cmdstats) {
657 my $i = &sqlSelect("stats", "counter", {
664 $i += $cmdstats{$type};
667 &sqlReplace("stats", {