]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/botmail.pl
dbDel takes a hash, dbm untested
[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 =~ /^check(\s+(.*))?$/i) {
26         &check( split(/\s+/, $1, 2) );
27
28     } elsif ($what =~ /^(read|next)$/i) {
29         # TODO: read specific items? nah, will make this too complex.
30         &next($::who);
31
32     }
33 }
34
35 #####
36 # Usage: botmail::check($recipient)
37 sub check {
38     my($recipient) = @_;
39     $recipient ||= $::who;
40
41     # todo: simplify this select (use a diff function)
42     my %from = &::dbGetCol("botmail", "srcwho",
43         "dstwho=".&::dbQuote(lc $recipient),2);
44     my $t       = keys %from;
45     my $from    = join(", ", keys %from);
46
47     if ($t == 0) {
48         &::msg($recipient, "You have no botmail.");
49     } else {
50         &::msg($recipient, "You have $t messages awaiting, from: $from");
51     }
52 }
53
54 #####
55 # Usage: botmail::next($recipient)
56 sub next {
57     my($recipient) = @_;
58
59     my %hash = &::dbGetColNiceHash("botmail", "*",
60         "dstwho=".&::dbQuote(lc $recipient)
61     );
62
63     if (scalar (keys %hash) <= 1) {
64         &::msg($recipient, "You have no botmail.");
65     } else {
66         my $ago = &::Time2String(time() - $hash{'time'});
67         &::msg($recipient, "From $hash{srcwho} ($hash{srcuh}) on $hash{time} [$ago]:");
68         &::msg($recipient, $hash{'msg'});
69         &::dbDel("botmail", { 'dstwho'=>$hash{dstwho}, 'srcwho'=>$hash{srcwho}});
70     }
71 }
72
73 #####
74 # Usage: botmail::add($recipient, $msg)
75 sub add {
76     my($recipient, $msg) = @_;
77     &::DEBUG("botmail::add(@_)");
78
79     if (lc $recipient eq $::who) {
80         &::msg($::who, "well... a botmail to oneself is stupid!");
81         return;
82     }
83
84     # only support 1 botmail with unique dstwho/srcwho to have same
85     # functionality as botmail from infobot.
86     my %hash = &::dbGetColNiceHash("botmail", "*",
87         "srcwho=".&::dbQuote(lc $::who)." AND ".
88         "dstwho=".&::dbQuote(lc $recipient)
89     );
90
91     if (scalar (keys %hash) > 1) {
92         &::msg($::who, "$recipient already has a message queued from you");
93         return;
94     }
95
96     &::dbSet("botmail", {
97         'dstwho'        => lc $recipient,
98         'srcwho'        => lc $::who,
99     }, {
100         'srcuh' => $::nuh,      # will this work?
101         'time'  => time(),
102         'msg'   => $msg,
103     } );
104
105     &::msg($::who, "OK, $::who, I'll let $recipient know.");
106 }
107
108 1;