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];
143 $reply = $prefix ."(\002". scalar(@list). "\002 shown";
144 $reply .= "; \002$total\002 total" if ($total != scalar @list);
145 $reply .= "): ". join(" \002;;\002 ",@list) .".";
147 last if (length($reply) < $maxlen and scalar(@list) <= $maxshow);
148 last if (scalar(@list) == 1);
156 ### Intelligence joining of arrays.
157 # Usage: &IJoin(@array);
161 } elsif (scalar @_ == 1) {
164 return join(', ',@{_}[0..$#_-1]) . " and $_[$#_]";
169 # Usage: &Time2String(seconds);
175 return "NULL" if (!defined $time);
176 return $time if ($time !~ /\d+/);
183 $t[0] = int($time) % 60;
184 $t[1] = int($time / 60) % 60;
185 $t[2] = int($time / 3600) % 24;
186 $t[3] = int($time / 86400);
188 push(@s, "$t[3]d") if ($t[3] != 0);
189 push(@s, "$t[2]h") if ($t[2] != 0);
190 push(@s, "$t[1]m") if ($t[1] != 0);
191 push(@s, "$t[0]s") if ($t[0] != 0 or !@s);
193 my $retval = $prefix.join(' ', @s);
194 $retval =~ s/(\d+)/\002$1\002/g;
202 # Usage: &fixFileList(@files);
207 # generate a hash list.
209 next unless /^(.*\/)(.*?)$/;
213 @files = (); # reuse the array.
215 # sort the hash list appropriately.
216 foreach (sort keys %files) {
218 my @keys = sort keys %{ $files{$file} };
219 my $i = scalar(@keys);
221 if (scalar @keys > 3) {
222 pop @keys while (scalar @keys > 3);
227 $file .= "\002{\002". join("\002|\002", @keys) ."\002}\002";
238 # Usage: &fixString($str);
240 my ($str, $level) = @_;
242 &WARN("fixString: str == NULL.");
247 s/^\s+//; # remove start whitespaces.
248 s/\s+$//; # remove end whitespaces.
249 s/\s+/ /g; # remove excessive whitespaces.
251 next unless (defined $level);
252 if (s/[\cA-\c_]//ig) { # remove control characters.
253 &DEBUG("stripped control chars");
260 # Usage: &fixPlural($str,$int);
265 &WARN("fixPlural: str == NULL.");
269 if (!defined $int or $int =~ /^\D+$/) {
270 &WARN("fixPlural: int != defined or int");
275 $str = "have" if ($int > 1);
276 } elsif ($str eq "is") {
277 $str = "are" if ($int > 1);
278 } elsif ($str eq "was") {
279 $str = "were" if ($int > 1);
280 } elsif ($str eq "this") {
281 $str = "these" if ($int > 1);
282 } elsif ($str =~ /y$/) {
285 $str .= "s"; # eg: "money" => "moneys".
291 $str .= "s" if ($int != 1);
301 sub getRandomLineFromFile {
304 if (!open(IN, $file)) {
305 &WARN("gRLfF: could not open ($file): $!");
312 if (!scalar @lines) {
313 &ERROR("GRLF: nothing loaded?");
317 # could we use the filehandler instead and put it through getRandom?
318 while (my $line = &getRandom(@lines)) {
321 next if ($line =~ /^\#/);
322 next if ($line =~ /^\s*$/);
328 sub getLineFromFile {
329 my($file,$lineno) = @_;
332 &ERROR("getLineFromFile: file '$file' does not exist.");
336 if (open(IN,$file)) {
340 if ($lineno > scalar @lines) {
341 &ERROR("getLineFromFile: lineno exceeds line count from file.");
345 my $line = $lines[$lineno-1];
349 &ERROR("gLFF: Could not open file ($file): $!");
354 # Usage: &getRandom(@array);
359 return $array[int(rand(scalar @array))];
362 # Usage: &getRandomInt("30-60");
367 &WARN("gRI: str == NULL.");
373 if ($str =~ /^(\d+(\.\d+)?)$/) {
375 my $fuzzy = int(rand 5);
380 return ($i - $fuzzy)*60;
382 return ($i + $fuzzy)*60;
384 } elsif ($str =~ /^(\d+)-(\d+)$/) {
385 return ($2 - $1)*int(rand $1)*60;
387 return $str; # hope we're safe.
390 &ERROR("getRandomInt: invalid arg '$str'.");
399 my ($left,$right) = @_;
400 return 0 unless defined $right;
401 return 0 unless defined $left;
402 return 1 if ($left =~ /^\Q$right$/i);
406 my $retval = &iseq(@_);
407 return 1 unless ($retval);
411 # Usage: &IsHostMatch($nuh);
416 if ($nuh =~ /^(\S+)!(\S+)@(\S+)/) {
417 $local{'nick'} = lc $1;
418 $local{'user'} = lc $2;
419 $local{'host'} = &makeHostMask(lc $3);
422 if (!defined $thisnuh) {
423 &WARN("IHM: thisnuh == NULL.");
425 } elsif ($thisnuh =~ /^(\S+)!(\S+)@(\S+)/) {
426 $this{'nick'} = lc $1;
427 $this{'user'} = lc $2;
428 $this{'host'} = &makeHostMask(lc $3);
430 &WARN("IHM: thisnuh is invalid '$thisnuh'.");
431 return 1 if ($thisnuh eq "");
435 # auth if 1) user and host match 2) user and nick match.
436 # this may change in the future.
438 if ($this{'user'} =~ /^\Q$local{'user'}\E$/i) {
439 return 2 if ($this{'host'} eq $local{'host'});
440 return 1 if ($this{'nick'} eq $local{'nick'});
446 # Usage: &isStale($file, $age);
448 my ($file, $age) = @_;
451 &WARN("isStale: age == NULL.");
455 if (!defined $file) {
456 &WARN("isStale: file == NULL.");
460 &DEBUG("!exist $file") if (! -f $file);
462 return 1 unless ( -f $file);
463 if ($file =~ /idx/) {
464 my $age2 = time() - (stat($file))[9];
465 &VERB("stale: $age2. (". &Time2String($age2) .")",2);
467 $age *= 60*60*24 if ($age >= 0 and $age < 30);
469 return 1 if (time() - (stat($file))[9] > $age);
477 # Usage: &makeHostMask($host);
482 if ($host =~ s/^(\S+!\S+\@)//) {
483 &DEBUG("mHM: detected nick!user\@ for host arg; fixing");
488 if ($host =~ /^$mask{ip}$/) {
489 return $nu."$1.$2.$3.*";
492 my @array = split(/\./, $host);
493 return $nu.$host if (scalar @array <= 3);
494 return $nu."*.".join('.',@{array}[1..$#array]);
497 # Usage: &makeRandom(int);
503 if ($max =~ /^\D+$/) {
504 &ERROR("makeRandom: arg ($max) is not integer.");
509 &ERROR("makeRandom: arg ($max) is not positive.");
514 while (scalar keys %done < $max) {
515 my $rand = int(rand $max);
516 next if (exists $done{$rand});
527 return unless (&IsParam("minLengthBeforePrivate"));
528 return if ($force_public_reply);
530 if (length $reply > $param{'minLengthBeforePrivate'}) {
531 &status("Reply: len reply > minLBP ($param{'minLengthBeforePrivate'}); msgType now private.");
532 $msgType = 'private';
540 # Usage: &validExec($string);
544 if ($str =~ /[\'\"\|]/) { # invalid.
551 # Usage: &hasProfanity($string);
558 /dick|dildo/ and last;
559 /shit|turd|crap/ and last;
560 /pussy|[ck]unt/ and last;
561 /wh[0o]re|bitch|slut/ and last;
572 if (&IsChanConf($param) or &IsParam($param)) {
575 ### TODO: specific reason why it failed.
576 &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
582 my ($label, $code) = @_;
586 &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid);
588 if (&IsParam("forking") and $$ == $bot_pid) {
589 return unless &addForked($label);
591 $SIG{CHLD} = 'IGNORE';
592 $pid = eval { fork() };
593 return if $pid; # parent does nothing
595 select(undef, undef, undef, 0.2);
596 # &status("fork starting for '$label', PID == $$.");
597 &status("--- fork starting for '$label', PID == $$ ---");
598 &shmWrite($shm,"SET FORKPID $label $$");
603 ### TODO: use AUTOLOAD
605 if ($label !~ /-/ and !&loadMyModule($myModules{$label})) {
606 &DEBUG("Forker: failed?");
611 $code->(); # weird, hey?
613 &WARN("Forker: code not defined!");
620 return 1 unless (exists $file{PID});
621 return 1 unless ( -f $file{PID});
622 return 1 if (unlink $file{PID});
623 return 0 if ( -f $file{PID});
628 my $salt = join '',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64];
630 return crypt($str, $salt);
634 return unless (&getChanConfList("ircTextCounters"));
636 foreach (keys %cmdstats) {
638 my $i = &sqlSelect("stats", "counter", {
645 $i += $cmdstats{$type};
652 $hash{time} = time() if ($z);
655 &sqlReplace("stats", %hash);
657 &sqlReplace("stats", {
660 -counter => "counter+1",