]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Modules/botmail.pl
take a few more things literally
[infobot.git] / src / Modules / botmail.pl
index 1547f18b37efc149f0ef0368985c94e71aa96fd7..be246d0b9931c2815df1f0d3f0e16e1e4b793083 100644 (file)
@@ -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)
 #
 
@@ -15,95 +15,101 @@ sub parse {
     my($what) = @_;
 
     if (!defined $what or $what =~ /^\s*$/) {
-       &::help("botmail");
+       &::help('botmail');
        return;
     }
 
-    if ($what =~ /^add(\s+(.*))?$/i) {
-       &add( split(/\s+/, $1, 2) );
+    if ($what =~ /^(to|for|add)\s+(.*)$/i) {
+       &add( split(/\s+/, $2, 2) );
 
-    } elsif ($what =~ /^next$/i) {
-       # todo: read specific items? nah, will make this too complex.
-       &read($::who);
+    } elsif ($what =~ /^stats?$/i) {
+       &stats();
+
+    } 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($who)
+# Usage: botmail::check($recipient, [$always])
 sub check {
-    my($w) = @_;
+    my($recipient, $always) = @_;
+    $recipient ||= $::who;
 
-    # todo: simplify this select (use a diff function)
-    my @from = &::dbGet("botmail", "srcwho"
-       "dstwho=".&::dbQuote(lc $w)
-    );
-    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($w, "You have no botmail.");
+       &::msg($recipient, "You have no botmail.") if ($always);
     } else {
-       &::msg($w, "You have $t messages awaiting, from: $from");
+       &::msg($recipient, "You have $t messages awaiting, from: $from (botmail read)");
     }
 }
 
 #####
-# Usage: botmail::read($who)
-sub read {
-    my($w) = @_;
-
-    # todo: simplify this select (use a diff function)
-    my $H = &::dbSelectHashref("*", "botmail", "srcwho",
-       "dstwho=".&::dbQuote(lc $w)
-    );
+# Usage: botmail::next($recipient)
+sub next {
+    my($recipient) = @_;
 
-    my $t = $H->total; # possible?
+    my %hash = &::sqlSelectRowHash('botmail', '*', {
+       dstwho => lc $recipient
+    } );
 
-    if ($t == 0) {
-       &::msg($w, "You have no botmail.");
+    if (scalar (keys %hash) <= 1) {
+       &::msg($recipient, "You have no botmail.");
     } else {
-       my $ago = &::Time2String(time() - $H->{time});
-       &::msg($w, "From $H->{srcwho} ($H->{srcuh}) on $H->{time} [$ago]:");
-       &::msg($w, $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}});
     }
 }
 
 #####
-# Usage: botmail::add($who, $msg)
+# Usage: botmail::add($recipient, $msg)
 sub add {
-    my($w, $msg) = @_;
-
-    # todo: simplify this select (use a diff function)
-    my $H = &::dbSelectHashref("*", "botmail", "srcwho",
-       "srcwho=".&::dbQuote(lc $::who)." AND ".
-       "dstwho=".&::dbQuote(lc $w)
-    );
+    my($recipient, $msg) = @_;
+    &::DEBUG("botmail::add(@_)");
 
-    my $t = $H->total; # possible?
+    # 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.
-    if ($t == 1) {
-       &::msg($::who, "$w already has a message queued from you");
-       return;
-    }
+    # 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 (lc $w == $::who) {
-       &::msg($::who, "well... a botmail to oneself is stupid!");
+    if (scalar (keys %hash) > 1) {
+       &::msg($::who, "$recipient already has a message queued from you");
        return;
     }
 
-    &::dbSetRow("botmail", {
-       dstwho  => lc $w,
-       srcwho  => lc $::who,
-       srcuh   => $::nuh{lc $w},       # will this work?
-       -time   => "NOW()",             # todo: add '-' support.
-                                       # dbUpdate() supports it.
+    &::sqlInsert('botmail', {
+       'dstwho'        => lc $recipient,
+       'srcwho'        => lc $::who,
+       'srcuh'         => $::nuh,
+       'time'          => time(),
+       'msg'           => $msg,
     } );
 
-    &::msg($::who, "OK, $::who, I'll let $w know.");
+    &::msg($::who, "OK, $::who, I'll let $recipient know.");
 }
 
 1;