]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/botmail.pl
botmail check
[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 = &::dbGet("botmail", "srcwho",
43         "dstwho=".&::dbQuote(lc $recipient)
44     );
45     my $t       = scalar @from;
46     my $from    = join(", ", @from);
47
48     if ($t == 0) {
49         &::msg($recipient, "You have no botmail.");
50     } else {
51         &::msg($recipient, "You have $t messages awaiting, from: $from");
52     }
53 }
54
55 #####
56 # Usage: botmail::next($recipient)
57 sub next {
58     my($recipient) = @_;
59
60     my %hash = &::dbGetColNiceHash("botmail", "*",
61         "dstwho=".&::dbQuote(lc $recipient)
62     );
63
64     if (scalar (keys %hash) <= 1) {
65         &::msg($recipient, "You have no botmail.");
66     } else {
67         my $ago = &::Time2String(time() - $hash{'time'});
68         &::msg($recipient, "From $hash{srcwho} ($hash{srcuh}) on $hash{time} [$ago]:");
69         &::msg($recipient, $hash{'msg'});
70         #&::dbDel("botmail", "id", $hash{id});
71     }
72 }
73
74 #####
75 # Usage: botmail::add($recipient, $msg)
76 sub add {
77     my($recipient, $msg) = @_;
78     &::DEBUG("botmail::add(@_)");
79
80     if (lc $recipient eq $::who) {
81         &::msg($::who, "well... a botmail to oneself is stupid!");
82         return;
83     }
84
85     # only support 1 botmail with unique dstwho/srcwho to have same
86     # functionality as botmail from infobot.
87     my %hash = &::dbGetColNiceHash("botmail", "*",
88         "srcwho=".&::dbQuote(lc $::who)." AND ".
89         "dstwho=".&::dbQuote(lc $recipient)
90     );
91
92     if (scalar (keys %hash) <= 1) {
93         &::msg($::who, "$recipient already has a message queued from you");
94         return;
95     }
96
97     &::dbSet("botmail", {
98         'dstwho'        => lc $recipient,
99         'srcwho'        => lc $::who,
100     }, {
101         'srcuh' => $::nuh,      # will this work?
102         'time'  => time(),
103         'msg'   => $msg,
104     } );
105
106     &::msg($::who, "OK, $::who, I'll let $recipient know.");
107 }
108
109 1;