]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/botmail.pl
botmail check working now
[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|for)\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 =~ /^next$/i) {
29         # todo: read specific items? nah, will make this too complex.
30         &read($::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::read($recipient)
57 sub read {
58     my($recipient) = @_;
59
60     # todo: simplify this select (use a diff function)
61     my $H = &::dbSelectHashref("*", "botmail", "srcwho",
62         "dstwho=".&::dbQuote(lc $recipient)
63     );
64
65     my $t = $H->total;  # possible?
66
67     if ($t == 0) {
68         &::msg($recipient, "You have no botmail.");
69     } else {
70         my $ago = &::Time2String(time() - $H->{time});
71         &::msg($recipient, "From $H->{srcwho} ($H->{srcuh}) on $H->{time} [$ago]:");
72         &::msg($recipient, $H->{message});
73         &::dbDel("botmail", "id", $H->{id});
74     }
75 }
76
77 #####
78 # Usage: botmail::add($recipient, $msg)
79 sub add {
80     my($recipient, $msg) = @_;
81     &::DEBUG("botmail::add(@_)");
82
83     if (lc $recipient eq $::who) {
84         &::msg($::who, "well... a botmail to oneself is stupid!");
85         return;
86     }
87
88     # only support 1 botmail with unique dstwho/srcwho to have same
89     # functionality as botmail from infobot.
90     my %hash = &::dbGetColNiceHash("botmail", "*",
91         "srcwho=".&::dbQuote(lc $::who)." AND ".
92         "dstwho=".&::dbQuote(lc $recipient)
93     );
94
95     if (%hash) {
96         &::msg($::who, "$recipient already has a message queued from you");
97         return;
98     }
99
100     &::dbSet("botmail", {
101         'dstwho'        => lc $recipient,
102         'srcwho'        => lc $::who,
103     }, {
104         'srcuh' => $::nuh,      # will this work?
105         'time'  => time(),
106         'msg'   => $msg,
107     } );
108
109     &::msg($::who, "OK, $::who, I'll let $recipient know.");
110 }
111
112 1;