]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/botmail.pl
dbm -> sql
[infobot.git] / src / Modules / botmail.pl
1 #
2 #  botmail.pl: Botmail (ala in infobot)
3 #      Author: dms
4 #     Version: v0.1 (20021122).
5 #     Created: 20021122
6 #        NOTE: Motivated by BZFlag.
7 #        TODO: full-fledged notes services (optional auth, etc)
8 #
9
10 package botmail;
11
12 use strict;
13
14 sub parse {
15     my($what) = @_;
16
17     if (!defined $what or $what =~ /^\s*$/) {
18         &::help("botmail");
19         return;
20     }
21
22     if ($what =~ /^(for|add)\s+(.*)$/i) {
23         &add( split(/\s+/, $2, 2) );
24
25     } elsif ($what =~ /^stats?$/i) {
26         &stats();
27
28     } elsif ($what =~ /^check?$/i) {
29         &check( $1, 1);
30
31     } elsif ($what =~ /^(read|next)$/i) {
32         # TODO: read specific items? nah, will make this too complex.
33         &next($::who);
34
35     }
36 }
37
38 sub stats {
39     my $botmail = &::countKeys("botmail");
40     &::msg($::who, "I have \002$botmail\002 ". &::fixPlural("message", $botmail). ".");
41 }
42
43 #####
44 # Usage: botmail::check($recipient, [$always])
45 sub check {
46     my($recipient, $always) = @_;
47     $recipient ||= $::who;
48
49     my %from = &::sqlSelectColHash("botmail", "srcwho,time", {
50         dstwho => lc $recipient
51     } );
52     my $t       = keys %from;
53     my $from    = join(", ", keys %from);
54
55     if ($t == 0) {
56         &::msg($recipient, "You have no botmail.") if ($always);
57     } else {
58         &::msg($recipient, "You have $t messages awaiting, from: $from (botmail read)");
59     }
60 }
61
62 #####
63 # Usage: botmail::next($recipient)
64 sub next {
65     my($recipient) = @_;
66
67     my %hash = &::sqlSelectRowHash("botmail", "*", {
68         dstwho => lc $recipient
69     } );
70
71     if (scalar (keys %hash) <= 1) {
72         &::msg($recipient, "You have no botmail.");
73     } else {
74         my $date = scalar(gmtime $hash{'time'});
75         my $ago = &::Time2String(time() - $hash{'time'});
76         &::msg($recipient, "From $hash{srcwho} ($hash{srcuh}) on $date ($ago ago):");
77         &::msg($recipient, $hash{'msg'});
78         &::sqlDelete("botmail", { 'dstwho'=>$hash{dstwho}, 'srcwho'=>$hash{srcwho}});
79     }
80 }
81
82 #####
83 # Usage: botmail::add($recipient, $msg)
84 sub add {
85     my($recipient, $msg) = @_;
86     &::DEBUG("botmail::add(@_)");
87
88     # only support 1 botmail with unique dstwho/srcwho to have same
89     # functionality as botmail from infobot.
90     my %hash = &::sqlSelectRowHash("botmail", "*", {
91         srcwho => &::sqlQuote(lc $::who),
92         dstwho => &::sqlQuote(lc $recipient)
93     } );
94
95     if (scalar (keys %hash) > 1) {
96         &::msg($::who, "$recipient already has a message queued from you");
97         return;
98     }
99
100     &::sqlReplace("botmail", {
101         'dstwho'        => lc $recipient,
102         'srcwho'        => lc $::who,
103         'srcuh'         => $::nuh,
104         'time'          => time(),
105         'msg'           => $msg,
106     } );
107
108     &::msg($::who, "OK, $::who, I'll let $recipient know.");
109 }
110
111 1;