]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/botmail.pl
c0ecab5e49e1b960bc6b9e60684ec45e0c168d53
[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 ($::param{'DBType'} =~ /^dbm/i) {
18         # FIXME multi field indexed tables not supported under dbm
19         &::msg($::who, "botmail disabled for $::param{'DBType'}");
20         return;
21     }   
22     if (!defined $what or $what =~ /^\s*$/) {
23         &::help("botmail");
24         return;
25     }
26
27     if ($what =~ /^(for|add)\s+(.*)$/i) {
28         &add( split(/\s+/, $2, 2) );
29
30     } elsif ($what =~ /^stats?$/i) {
31         &stats();
32
33     } elsif ($what =~ /^check?$/i) {
34         &check( $1, 1);
35
36     } elsif ($what =~ /^(read|next)$/i) {
37         # TODO: read specific items? nah, will make this too complex.
38         &next($::who);
39
40     }
41 }
42
43 sub stats {
44     my $botmail = &::countKeys("botmail");
45     &::msg($::who, "I have \002$botmail\002 ". &::fixPlural("message", $botmail). ".");
46 }
47
48 #####
49 # Usage: botmail::check($recipient, [$always])
50 sub check {
51     my($recipient, $always) = @_;
52     $recipient ||= $::who;
53
54     # todo: simplify this select (use a diff function)
55     my %from = &::dbGetCol("botmail", "srcwho",
56         "dstwho=".&::dbQuote(lc $recipient),2);
57     my $t       = keys %from;
58     my $from    = join(", ", keys %from);
59
60     if ($t == 0) {
61         &::msg($recipient, "You have no botmail.") if ($always);
62     } else {
63         &::msg($recipient, "You have $t messages awaiting, from: $from (botmail read)");
64     }
65 }
66
67 #####
68 # Usage: botmail::next($recipient)
69 sub next {
70     my($recipient) = @_;
71
72     my %hash = &::dbGetColNiceHash("botmail", "*",
73         "dstwho=".&::dbQuote(lc $recipient)
74     );
75
76     if (scalar (keys %hash) <= 1) {
77         &::msg($recipient, "You have no botmail.");
78     } else {
79         my $ago = &::Time2String(time() - $hash{'time'});
80         &::msg($recipient, "From $hash{srcwho} ($hash{srcuh}) on $hash{time} [$ago]:");
81         &::msg($recipient, $hash{'msg'});
82         &::dbDel("botmail", { 'dstwho'=>$hash{dstwho}, 'srcwho'=>$hash{srcwho}});
83     }
84 }
85
86 #####
87 # Usage: botmail::add($recipient, $msg)
88 sub add {
89     my($recipient, $msg) = @_;
90     &::DEBUG("botmail::add(@_)");
91
92     if (lc $recipient eq $::who) {
93         &::msg($::who, "well... a botmail to oneself is stupid!");
94         return;
95     }
96
97     # only support 1 botmail with unique dstwho/srcwho to have same
98     # functionality as botmail from infobot.
99     my %hash = &::dbGetColNiceHash("botmail", "*",
100         "srcwho=".&::dbQuote(lc $::who)." AND ".
101         "dstwho=".&::dbQuote(lc $recipient)
102     );
103
104     if (scalar (keys %hash) > 1) {
105         &::msg($::who, "$recipient already has a message queued from you");
106         return;
107     }
108
109     &::dbSet("botmail", {
110         'dstwho'        => lc $recipient,
111         'srcwho'        => lc $::who,
112     }, {
113         'srcuh' => $::nuh,      # will this work?
114         'time'  => time(),
115         'msg'   => $msg,
116     } );
117
118     &::msg($::who, "OK, $::who, I'll let $recipient know.");
119 }
120
121 1;