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