]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/botmail.pl
remove Berkeley DBM support
[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     # todo: simplify this select (use a diff function)
50     my %from = &::dbGetCol("botmail", "srcwho",
51         "dstwho=".&::dbQuote(lc $recipient),2);
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 = &::dbGetColNiceHash("botmail", "*",
68         "dstwho=".&::dbQuote(lc $recipient)
69     );
70
71     if (scalar (keys %hash) <= 1) {
72         &::msg($recipient, "You have no botmail.");
73     } else {
74         my $ago = &::Time2String(time() - $hash{'time'});
75         &::msg($recipient, "From $hash{srcwho} ($hash{srcuh}) on $hash{time} [$ago]:");
76         &::msg($recipient, $hash{'msg'});
77         &::dbDel("botmail", { 'dstwho'=>$hash{dstwho}, 'srcwho'=>$hash{srcwho}});
78     }
79 }
80
81 #####
82 # Usage: botmail::add($recipient, $msg)
83 sub add {
84     my($recipient, $msg) = @_;
85     &::DEBUG("botmail::add(@_)");
86
87     if (lc $recipient eq $::who) {
88         &::msg($::who, "well... a botmail to oneself is stupid!");
89         return;
90     }
91
92     # only support 1 botmail with unique dstwho/srcwho to have same
93     # functionality as botmail from infobot.
94     my %hash = &::dbGetColNiceHash("botmail", "*",
95         "srcwho=".&::dbQuote(lc $::who)." AND ".
96         "dstwho=".&::dbQuote(lc $recipient)
97     );
98
99     if (scalar (keys %hash) > 1) {
100         &::msg($::who, "$recipient already has a message queued from you");
101         return;
102     }
103
104     &::dbSet("botmail", {
105         'dstwho'        => lc $recipient,
106         'srcwho'        => lc $::who,
107     }, {
108         'srcuh' => $::nuh,      # will this work?
109         'time'  => time(),
110         'msg'   => $msg,
111     } );
112
113     &::msg($::who, "OK, $::who, I'll let $recipient know.");
114 }
115
116 1;