X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2FModules%2Fbotmail.pl;h=778252f8e0cdc397cc6baa15fdeefb419c132292;hb=refs%2Fheads%2Fdpkg;hp=094e5f5adb71679ca9802284856e88941ab4196e;hpb=7a714268b563bd76aeabc672d0b64bfad8495c74;p=infobot.git diff --git a/src/Modules/botmail.pl b/src/Modules/botmail.pl index 094e5f5..778252f 100644 --- a/src/Modules/botmail.pl +++ b/src/Modules/botmail.pl @@ -3,7 +3,7 @@ # Author: dms # Version: v0.1 (20021122). # Created: 20021122 -# NOTE: Motivated by BZFlag. +# NOTE: Motivated by TimRiker. # TODO: full-fledged notes services (optional auth, etc) # @@ -12,97 +12,124 @@ package botmail; use strict; sub parse { - my($what) = @_; + my ($what) = @_; - if (!defined $what or $what =~ /^\s*$/) { - &::help("botmail"); - return; + if ( !defined $what or $what =~ /^\s*$/ ) { + &::help('botmail'); + return; } - if ($what =~ /^(for|add)\s+(.*)$/i) { - &add( split(/\s+/, $2, 2) ); + if ( $what =~ /^(to|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 =~ /^(read|next)$/i) { - # TODO: read specific items? nah, will make this too complex. - &next($::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 = &::dbGetCol("botmail", "srcwho", - "dstwho=".&::dbQuote(lc $recipient),2); - my $t = keys %from; - my $from = join(", ", keys %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."); - } else { - &::msg($recipient, "You have $t messages awaiting, from: $from"); + if ( $t == 0 ) { + &::msg( $recipient, "You have no botmail." ) if ($always); + } + else { + &::msg( $recipient, + "You have $t messages awaiting, from: $from (botmail read)" ); } } ##### # Usage: botmail::next($recipient) sub next { - my($recipient) = @_; + my ($recipient) = @_; - my %hash = &::dbGetColNiceHash("botmail", "*", - "dstwho=".&::dbQuote(lc $recipient) - ); + my %hash = + &::sqlSelectRowHash( 'botmail', '*', { dstwho => lc $recipient } ); - if (scalar (keys %hash) <= 1) { - &::msg($recipient, "You have no botmail."); - } else { - my $ago = &::Time2String(time() - $hash{'time'}); - &::msg($recipient, "From $hash{srcwho} ($hash{srcuh}) on $hash{time} [$ago]:"); - &::msg($recipient, $hash{'msg'}); - &::dbDel("botmail", { 'dstwho'=>$hash{dstwho}, 'srcwho'=>$hash{srcwho}}); + if ( scalar( keys %hash ) <= 1 ) { + &::msg( $recipient, "You have no botmail." ); + } + else { + 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} } ); } } ##### # Usage: botmail::add($recipient, $msg) sub add { - my($recipient, $msg) = @_; + 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) + # allow optional trailing : ie: botmail for foo[:] hello + $recipient =~ s/:$//; + +# only support 1 botmail with unique dstwho/srcwho to have same +# functionality as botmail from infobot. +# Note: I removed the &::sqlQuote reference. Seems to be working and inserting fine without it here. -- troubled + my %hash = &::sqlSelectRowHash( + 'botmail', + '*', + { + srcwho => lc $::who, + dstwho => lc $recipient + } ); - if (scalar (keys %hash) > 1) { - &::msg($::who, "$recipient already has a message queued from you"); - return; + if ( scalar( keys %hash ) > 1 ) { + &::msg( $::who, "$recipient already has a message queued from you" ); + return; } - &::dbSet("botmail", { - 'dstwho' => lc $recipient, - 'srcwho' => lc $::who, - }, { - 'srcuh' => $::nuh, # will this work? - 'time' => time(), - 'msg' => $msg, - } ); + &::sqlInsert( + 'botmail', + { + 'dstwho' => lc $recipient, + 'srcwho' => lc $::who, + 'srcuh' => $::nuh, + 'time' => time(), + 'msg' => $msg, + } + ); - &::msg($::who, "OK, $::who, I'll let $recipient know."); + &::msg( $::who, "OK, $::who, I'll let $recipient know." ); } 1; + +# vim:ts=4:sw=4:expandtab:tw=80