]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/botmail.pl
more botmail
[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 =~ /^add\s+(.*)$/i) {
23         &add( split(/\s+/, $1, 2) );
24
25     } elsif ($what =~ /^next$/i) {
26         # todo: read specific items? nah, will make this too complex.
27         &read($::who);
28
29     }
30 }
31
32 #####
33 # Usage: botmail::check($recipient)
34 sub check {
35     my($recipient) = @_;
36
37     # todo: simplify this select (use a diff function)
38     my @from = &::dbGet("botmail", "srcwho",
39         "dstwho=".&::dbQuote(lc $recipient)
40     );
41     my $t       = scalar @from;
42     my $from    = join(", ", @from);
43
44     if ($t == 0) {
45         &::msg($recipient, "You have no botmail.");
46     } else {
47         &::msg($recipient, "You have $t messages awaiting, from: $from");
48     }
49 }
50
51 #####
52 # Usage: botmail::read($recipient)
53 sub read {
54     my($recipient) = @_;
55
56     # todo: simplify this select (use a diff function)
57     my $H = &::dbSelectHashref("*", "botmail", "srcwho",
58         "dstwho=".&::dbQuote(lc $recipient)
59     );
60
61     my $t = $H->total;  # possible?
62
63     if ($t == 0) {
64         &::msg($recipient, "You have no botmail.");
65     } else {
66         my $ago = &::Time2String(time() - $H->{time});
67         &::msg($recipient, "From $H->{srcwho} ($H->{srcuh}) on $H->{time} [$ago]:");
68         &::msg($recipient, $H->{message});
69         &::dbDel("botmail", "id", $H->{id});
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 (%hash) {
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;