X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2FMisc.pl;h=db0c523d66d391103ebeeb35a041f7862f06ab6b;hb=d337f15b80c305f20994c630ac788b337cf2ab60;hp=bf95b602216638c250c00dae377f83a13fe2f12d;hpb=11e57f735626f775a95284a0d8ff284ec3617dc7;p=infobot.git diff --git a/src/Misc.pl b/src/Misc.pl index bf95b60..db0c523 100644 --- a/src/Misc.pl +++ b/src/Misc.pl @@ -13,11 +13,11 @@ use vars qw($msgType $who $bot_pid $nuh $shm $force_public_reply sub help { my $topic = shift; - my $file = $bot_data_dir."/blootbot.help"; + my $file = $bot_data_dir."/infobot.help"; my %help = (); # crude hack for performStrictReply() to work as expected. - $msgType = "private" if ($msgType eq "public"); + $msgType = 'private' if ($msgType eq 'public'); if (!open(FILE, $file)) { &ERROR("Failed reading help file ($file): $!"); @@ -39,12 +39,12 @@ sub help { $val =~ s/__/\037/g; $val =~ s/==/ /; - $help{$key} = "" if (!exists $help{$key}); + $help{$key} = '' if (!exists $help{$key}); $help{$key} .= $val."\n"; } close FILE; - if (!defined $topic or $topic eq "") { + if (!defined $topic or $topic eq '') { &msg($who, $help{'main'}); my $i = 0; @@ -165,7 +165,7 @@ sub formListReply { # Usage: &IJoin(@array); sub IJoin { if (!scalar @_) { - return "NULL"; + return 'NULL'; } elsif (scalar @_ == 1) { return $_[0]; } else { @@ -177,10 +177,10 @@ sub IJoin { # Usage: &Time2String(seconds); sub Time2String { my ($time) = @_; - my $prefix = ""; + my $prefix = ''; my (@s, @t); - return "NULL" if (!defined $time); + return 'NULL' if (!defined $time); return $time if ($time !~ /\d+/); if ($time < 0) { @@ -279,24 +279,24 @@ sub fixPlural { return $str; } - if ($str eq "has") { - $str = "have" if ($int > 1); - } elsif ($str eq "is") { - $str = "are" if ($int > 1); - } elsif ($str eq "was") { - $str = "were" if ($int > 1); - } elsif ($str eq "this") { - $str = "these" if ($int > 1); + if ($str eq 'has') { + $str = 'have' if ($int > 1); + } elsif ($str eq 'is') { + $str = 'are' if ($int > 1); + } elsif ($str eq 'was') { + $str = 'were' if ($int > 1); + } elsif ($str eq 'this') { + $str = 'these' if ($int > 1); } elsif ($str =~ /y$/) { if ($int > 1) { if ($str =~ /ey$/) { - $str .= "s"; # eg: "money" => "moneys". + $str .= 's'; # eg: 'money' => 'moneys'. } else { $str =~ s/y$/ies/; } } } else { - $str .= "s" if ($int != 1); + $str .= 's' if ($int != 1); } return $str; @@ -367,36 +367,28 @@ sub getRandom { return $array[int(rand(scalar @array))]; } -# Usage: &getRandomInt("30-60"); +# Usage: &getRandomInt("30-60"); &getRandomInt(5); +# Desc : Returns a randomn integer between "X-Y" or 1 and the value passed sub getRandomInt { - my $str = $_[0]; + my $str = shift; - if (!defined $str) { - &WARN("gRI: str == NULL."); - return; - } - - srand(); - - if ($str =~ /^(\d+(\.\d+)?)$/) { - my $i = $1; - my $fuzzy = int(rand 5); - if ($i < 10) { - return $i; + if ( !defined $str ) { + &WARN("getRandomInt: str == NULL."); + return undef; } - if (rand > 0.5) { - return ($i - $fuzzy)*60; + + if ( $str =~ /^(\d+(\.\d+)?)$/ ) { + return int( rand $str ) + 1; + } elsif ( $str =~ /^(\d+)-(\d+)$/ ) { + return $1 if $1 == $2; + my $min = $1 < $2 ? $1 : $2; # Swap is backwords + my $max = $2 > $1 ? $2 : $1; + return int( rand( $max - $min + 1 ) ) + $min; } else { - return ($i + $fuzzy)*60; - } - } elsif ($str =~ /^(\d+)-(\d+)$/) { - return ($2 - $1)*int(rand $1)*60; - } else { - return $str; # hope we're safe. - } - &ERROR("getRandomInt: invalid arg '$str'."); - return 1800; + # &ERROR("getRandomInt: invalid arg '$str'."); + return undef; + } } ########## @@ -436,7 +428,7 @@ sub IsHostMatch { $this{'host'} = &makeHostMask(lc $3); } else { &WARN("IHM: thisnuh is invalid '$thisnuh'."); - return 1 if ($thisnuh eq ""); + return 1 if ($thisnuh eq ''); return 0; } @@ -501,7 +493,7 @@ sub isFileUpdated { # Usage: &makeHostMask($host); sub makeHostMask { my ($host) = @_; - my $nu = ""; + my $nu = ''; if ($host =~ s/^(\S+!\S+\@)//) { &DEBUG("mHM: detected nick!user\@ for host arg; fixing"); @@ -548,7 +540,7 @@ sub makeRandom { sub checkMsgType { my ($reply) = @_; - return unless (&IsParam("minLengthBeforePrivate")); + return unless (&IsParam('minLengthBeforePrivate')); return if ($force_public_reply); if (length $reply > $param{'minLengthBeforePrivate'}) { @@ -609,7 +601,7 @@ sub Forker { &shmFlush(); &VERB("double fork detected; not forking.",2) if ($$ != $bot_pid); - if (&IsParam("forking") and $$ == $bot_pid) { + if (&IsParam('forking') and $$ == $bot_pid) { return unless &addForked($label); $SIG{CHLD} = 'IGNORE'; @@ -655,13 +647,13 @@ sub mkcrypt { } sub closeStats { - return unless (&getChanConfList("ircTextCounters")); + return unless (&getChanConfList('ircTextCounters')); foreach (keys %cmdstats) { my $type = $_; - my $i = &sqlSelect("stats", "counter", { + my $i = &sqlSelect('stats', 'counter', { nick => $type, - type => "cmdstats", + type => 'cmdstats', } ); my $z = 0; $z++ unless ($i); @@ -669,9 +661,8 @@ sub closeStats { $i += $cmdstats{$type}; - &sqlReplace("stats", { - nick => $type, - type => "cmdstats", + &sqlSet('stats', {'nick' => $type}, { + type => 'cmdstats', 'time' => time(), counter => $i, } ); @@ -679,3 +670,5 @@ sub closeStats { } 1; + +# vim:ts=4:sw=4:expandtab:tw=80