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 = $param{'maxListReplyCount'} || 10;
121 my $maxlen = $param{'maxListReplyLength'} || 400;
125 return $prefix ."returned no results." unless ($total);
130 foreach (&makeRandom($total)) {
131 push(@rand, $list[$_]);
132 last if (scalar @rand == $maxshow);
135 } elsif ($total > $maxshow) {
136 &status("formListReply: truncating list.");
138 @list = @list[0..$maxshow-1];
144 $reply = $prefix ."(\002". scalar(@list). "\002 shown";
145 $reply .= "; \002$total\002 total" if ($total != scalar @list);
146 $reply .= "): ". join(" \002;;\002 ", @list) .".";
148 last if (length($reply) < $maxlen and scalar(@list) <= $maxshow);
149 last if (scalar(@list) == 1);
157 ### Intelligence joining of arrays.
158 # Usage: &IJoin(@array);
162 } elsif (scalar @_ == 1) {
165 return join(', ',@{_}[0..$#_-1]) . " and $_[$#_]";
170 # Usage: &Time2String(seconds);
176 return "NULL" if (!defined $time);
177 return $time if ($time !~ /\d+/);
184 $t[0] = int($time) % 60;
185 $t[1] = int($time / 60) % 60;
186 $t[2] = int($time / 3600) % 24;
187 $t[3] = int($time / 86400);
189 push(@s, "$t[3]d") if ($t[3] != 0);
190 push(@s, "$t[2]h") if ($t[2] != 0);
191 push(@s, "$t[1]m") if ($t[1] != 0);
192 push(@s, "$t[0]s") if ($t[0] != 0 or !@s);
194 my $retval = $prefix.join(' ', @s);
195 $retval =~ s/(\d+)/\002$1\002/g;
203 # Usage: &fixFileList(@files);
208 # generate a hash list.
210 next unless /^(.*\/)(.*?)$/;
214 @files = (); # reuse the array.
216 # sort the hash list appropriately.
217 foreach (sort keys %files) {
219 my @keys = sort keys %{ $files{$file} };
220 my $i = scalar(@keys);
222 if (scalar @keys > 3) {
223 pop @keys while (scalar @keys > 3);
228 $file .= "\002{\002". join("\002|\002", @keys) ."\002}\002";
239 # Usage: &fixString($str);
241 my ($str, $level) = @_;
243 &WARN("fixString: str == NULL.");
248 s/^\s+//; # remove start whitespaces.
249 s/\s+$//; # remove end whitespaces.
250 s/\s+/ /g; # remove excessive whitespaces.
252 next unless (defined $level);
253 if (s/[\cA-\c_]//ig) { # remove control characters.
254 &DEBUG("stripped control chars");
261 # Usage: &fixPlural($str,$int);
266 &WARN("fixPlural: str == NULL.");
270 if (!defined $int or $int =~ /^\D+$/) {
271 &WARN("fixPlural: int != defined or int");
276 $str = "have" if ($int > 1);
277 } elsif ($str eq "is") {
278 $str = "are" if ($int > 1);
279 } elsif ($str eq "was") {
280 $str = "were" if ($int > 1);
281 } elsif ($str eq "this") {
282 $str = "these" if ($int > 1);
283 } elsif ($str =~ /y$/) {
286 $str .= "s"; # eg: "money" => "moneys".
292 $str .= "s" if ($int != 1);
302 sub getRandomLineFromFile {
305 if (!open(IN, $file)) {
306 &WARN("gRLfF: could not open ($file): $!");
313 if (!scalar @lines) {
314 &ERROR("GRLF: nothing loaded?");
318 # could we use the filehandler instead and put it through getRandom?
319 while (my $line = &getRandom(@lines)) {
322 next if ($line =~ /^\#/);
323 next if ($line =~ /^\s*$/);
329 sub getLineFromFile {
330 my($file,$lineno) = @_;
333 &ERROR("getLineFromFile: file '$file' does not exist.");
337 if (open(IN,$file)) {
341 if ($lineno > scalar @lines) {
342 &ERROR("getLineFromFile: lineno exceeds line count from file.");
346 my $line = $lines[$lineno-1];
350 &ERROR("gLFF: Could not open file ($file): $!");
355 # Usage: &getRandom(@array);
360 return $array[int(rand(scalar @array))];
363 # Usage: &getRandomInt("30-60");
368 &WARN("gRI: str == NULL.");
374 if ($str =~ /^(\d+(\.\d+)?)$/) {
376 my $fuzzy = int(rand 5);
381 return ($i - $fuzzy)*60;
383 return ($i + $fuzzy)*60;
385 } elsif ($str =~ /^(\d+)-(\d+)$/) {
386 return ($2 - $1)*int(rand $1)*60;
388 return $str; # hope we're safe.
391 &ERROR("getRandomInt: invalid arg '$str'.");
400 my ($left,$right) = @_;
401 return 0 unless defined $right;
402 return 0 unless defined $left;
403 return 1 if ($left =~ /^\Q$right$/i);
407 my $retval = &iseq(@_);
408 return 1 unless ($retval);
412 # Usage: &IsHostMatch($nuh);
417 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
418 $local{'nick'} = lc $1;
419 $local{'user'} = lc $2;
420 $local{'host'} = &makeHostMask(lc $3);
423 if (!defined $thisnuh) {
424 &WARN("IHM: thisnuh == NULL.");
426 } elsif ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
427 $this{'nick'} = lc $1;
428 $this{'user'} = lc $2;
429 $this{'host'} = &makeHostMask(lc $3);
431 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
432 return 1 if ($thisnuh eq "");
436 # auth if 1) user and host match 2) user and nick match.
437 # this may change in the future.
439 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
440 return 2 if ($this{'host'} eq $local{'host'});
441 return 1 if ($this{'nick'} eq $local{'nick'});
447 # Usage: &isStale($file, $age);
449 my ($file, $age) = @_;
452 &WARN("isStale: age == NULL.");
456 if (!defined $file) {
457 &WARN("isStale: file == NULL.");
461 &DEBUG("!exist $file") if (! -f $file);
463 return 1 unless ( -f $file);
464 if ($file =~ /idx/) {
465 my $age2 = time() - (stat($file))[9];
466 &VERB("stale: $age2. (". &Time2String($age2) .")",2);
468 $age *= 60*60*24 if ($age >= 0 and $age < 30);
470 return 1 if (time() - (stat($file))[9] > $age);
475 my ($file, $time) = @_;
481 my $time_file = (stat $file)[9];
483 if ($time <= $time_file) {
494 # Usage: &makeHostMask($host);
499 if ($host =~ s/^(\S+!\S+\@)//) {
500 &DEBUG("mHM: detected nick!user\@ for host arg; fixing");
505 if ($host =~ /^$mask{ip}$/) {
506 return $nu."$1.$2.$3.*";
509 my @array = split(/\./, $host);
510 return $nu.$host if (scalar @array <= 3);
511 return $nu."*.".join('.',@{array}[1..$#array]);
514 # Usage: &makeRandom(int);
520 if ($max =~ /^\D+$/) {
521 &ERROR("makeRandom: arg ($max) is not integer.");
526 &ERROR("makeRandom: arg ($max) is not positive.");
531 while (scalar keys %done < $max) {
532 my $rand = int(rand $max);
533 next if (exists $done{$rand});
544 return unless (&IsParam("minLengthBeforePrivate"));
545 return if ($force_public_reply);
547 if (length $reply > $param{'minLengthBeforePrivate'}) {
548 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
549 $msgType = 'private';
557 # Usage: &validExec($string);
561 if ($str =~ /[\'\"\|]/) { # invalid.
568 # Usage: &hasProfanity($string);
575 /dick|dildo/ and last;
576 /shit|turd|crap/ and last;
577 /pussy|[ck]unt/ and last;
578 /wh[0o]re|bitch|slut/ and last;
589 if (&IsChanConf($param) or &IsParam($param)) {
592 ### TODO: specific reason why it failed.
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 unless &addForked($label);
608 $SIG{CHLD} = 'IGNORE';
609 $pid = eval { fork() };
610 return if $pid; # parent does nothing
612 select(undef, undef, undef, 0.2);
613 # &status("fork starting for '$label', PID == $$.");
614 &status("--- fork starting for '$label', PID == $$ ---");
615 &shmWrite($shm,"SET FORKPID $label $$");
620 ### TODO: use AUTOLOAD
622 if ($label !~ /-/ and !&loadMyModule($myModules{$label})) {
623 &DEBUG("Forker: failed?");
628 $code->(); # weird, hey?
630 &WARN("Forker: code not defined!");
637 return 1 unless (exists $file{PID});
638 return 1 unless ( -f $file{PID});
639 return 1 if (unlink $file{PID});
640 return 0 if ( -f $file{PID});
645 my $salt = join '',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64];
647 return crypt($str, $salt);
651 return unless (&getChanConfList("ircTextCounters"));
653 foreach (keys %cmdstats) {
655 my $i = &sqlSelect("stats", "counter", {
662 $i += $cmdstats{$type};
665 &sqlReplace("stats", {