X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2FModules%2Fbotmail.pl;h=fcb5e8a695716dc0afb9c9c99d8708dc4c3f003b;hb=1c30b65aad211e585956587f774bc26bce946a15;hp=95acc7e3656817cecf9030402f5bdbc95210c341;hpb=772ceec34fbaa747b443eb2d3794fe580c579d10;p=infobot.git diff --git a/src/Modules/botmail.pl b/src/Modules/botmail.pl index 95acc7e..fcb5e8a 100644 --- a/src/Modules/botmail.pl +++ b/src/Modules/botmail.pl @@ -19,58 +19,63 @@ sub parse { return; } - if ($what =~ /^(add|for)\s+(.*)$/i) { + if ($what =~ /^(for|add)\s+(.*)$/i) { &add( split(/\s+/, $2, 2) ); - } elsif ($what =~ /^check(\s+(.*))?$/i) { - &check( split(/\s+/, $1, 2) ); + } elsif ($what =~ /^stats?$/i) { + &stats(); - } elsif ($what =~ /^next$/i) { - # todo: read specific items? nah, will make this too complex. - &read($::who); + } elsif ($what =~ /^check?$/i) { + &check( $1, 1); + + } elsif ($what =~ /^(read|next)$/i) { + # TODO: read specific items? nah, will make this too complex. + &next($::who); } } +sub stats { + my $botmail = &::countKeys("botmail"); + &::msg($::who, "I have \002$botmail\002 ". &::fixPlural("message", $botmail). "."); +} + ##### -# Usage: botmail::check($recipient) +# Usage: botmail::check($recipient, [$always]) sub check { - my($recipient) = @_; + my($recipient, $always) = @_; $recipient ||= $::who; - # todo: simplify this select (use a diff function) - my @from = &::dbGet("botmail", "srcwho", - "dstwho=".&::dbQuote(lc $recipient) - ); - my $t = scalar @from; - my $from = join(", ", @from); + my %from = &::sqlSelectColHash("botmail", "srcwho,time", { + dstwho => lc $recipient + } ); + my $t = keys %from; + my $from = join(", ", keys %from); if ($t == 0) { - &::msg($recipient, "You have no botmail."); + &::msg($recipient, "You have no botmail.") if ($always); } else { - &::msg($recipient, "You have $t messages awaiting, from: $from"); + &::msg($recipient, "You have $t messages awaiting, from: $from (botmail read)"); } } ##### -# Usage: botmail::read($recipient) -sub read { +# Usage: botmail::next($recipient) +sub next { my($recipient) = @_; - # todo: simplify this select (use a diff function) - my $H = &::dbSelectHashref("*", "botmail", "srcwho", - "dstwho=".&::dbQuote(lc $recipient) - ); - - my $t = $H->total; # possible? + my %hash = &::sqlSelectRowHash("botmail", "*", { + dstwho => lc $recipient + } ); - if ($t == 0) { + if (scalar (keys %hash) <= 1) { &::msg($recipient, "You have no botmail."); } else { - my $ago = &::Time2String(time() - $H->{time}); - &::msg($recipient, "From $H->{srcwho} ($H->{srcuh}) on $H->{time} [$ago]:"); - &::msg($recipient, $H->{message}); - &::dbDel("botmail", "id", $H->{id}); + my $date = scalar(gmtime $hash{'time'}); + my $ago = &::Time2String(time() - $hash{'time'}); + &::msg($recipient, "From $hash{srcwho} ($hash{srcuh}) on $date ($ago ago):"); + &::msg($recipient, $hash{'msg'}); + &::sqlDelete("botmail", { 'dstwho'=>$hash{dstwho}, 'srcwho'=>$hash{srcwho}}); } } @@ -80,30 +85,24 @@ sub add { my($recipient, $msg) = @_; &::DEBUG("botmail::add(@_)"); - if (lc $recipient eq $::who) { - &::msg($::who, "well... a botmail to oneself is stupid!"); - return; - } - # only support 1 botmail with unique dstwho/srcwho to have same # functionality as botmail from infobot. - my %hash = &::dbGetColNiceHash("botmail", "*", - "srcwho=".&::dbQuote(lc $::who)." AND ". - "dstwho=".&::dbQuote(lc $recipient) - ); + my %hash = &::sqlSelectRowHash("botmail", "*", { + srcwho => &::sqlQuote(lc $::who), + dstwho => &::sqlQuote(lc $recipient) + } ); - if (%hash) { + if (scalar (keys %hash) > 1) { &::msg($::who, "$recipient already has a message queued from you"); return; } - &::dbSet("botmail", { + &::sqlReplace("botmail", { 'dstwho' => lc $recipient, 'srcwho' => lc $::who, - }, { - 'srcuh' => $::nuh, # will this work? - 'time' => time(), - 'msg' => $msg, + 'srcuh' => $::nuh, + 'time' => time(), + 'msg' => $msg, } ); &::msg($::who, "OK, $::who, I'll let $recipient know.");