]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/botmail.pl
* Add vim formatting comments ( # vim:ts=4:sw=4:expandtab:tw=80 )
[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 TimRiker.
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 =~ /^(to|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     my %from = &::sqlSelectColHash('botmail', "srcwho,time", {
50         dstwho => lc $recipient
51     } );
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 = &::sqlSelectRowHash('botmail', '*', {
68         dstwho => lc $recipient
69     } );
70
71     if (scalar (keys %hash) <= 1) {
72         &::msg($recipient, "You have no botmail.");
73     } else {
74         my $date = scalar(gmtime $hash{'time'});
75         my $ago = &::Time2String(time() - $hash{'time'});
76         &::msg($recipient, "From $hash{srcwho} ($hash{srcuh}) on $date ($ago ago):");
77         &::msg($recipient, $hash{'msg'});
78         &::sqlDelete('botmail', { 'dstwho'=>$hash{dstwho}, 'srcwho'=>$hash{srcwho}});
79     }
80 }
81
82 #####
83 # Usage: botmail::add($recipient, $msg)
84 sub add {
85     my($recipient, $msg) = @_;
86     &::DEBUG("botmail::add(@_)");
87
88     # allow optional trailing : ie: botmail for foo[:] hello
89     $recipient =~ s/:$//;
90
91     # only support 1 botmail with unique dstwho/srcwho to have same
92     # functionality as botmail from infobot.
93     # Note: I removed the &::sqlQuote reference. Seems to be working and inserting fine without it here. -- troubled
94     my %hash = &::sqlSelectRowHash('botmail', '*', {
95         srcwho => lc $::who,
96         dstwho => 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     &::sqlInsert('botmail', {
105         'dstwho'        => lc $recipient,
106         'srcwho'        => lc $::who,
107         'srcuh'         => $::nuh,
108         'time'          => time(),
109         'msg'           => $msg,
110     } );
111
112     &::msg($::who, "OK, $::who, I'll let $recipient know.");
113 }
114
115 1;
116
117 # vim:ts=4:sw=4:expandtab:tw=80