]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/botmail.pl
* Missed DebianBugs.pl in the previous merge
[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     }
26     elsif ( $what =~ /^stats?$/i ) {
27         &stats();
28
29     }
30     elsif ( $what =~ /^check?$/i ) {
31         &check( $1, 1 );
32
33     }
34     elsif ( $what =~ /^(read|next)$/i ) {
35
36         # TODO: read specific items? nah, will make this too complex.
37         &next($::who);
38
39     }
40 }
41
42 sub stats {
43     my $botmail = &::countKeys('botmail');
44     &::msg( $::who,
45             "I have \002$botmail\002 "
46           . &::fixPlural( 'message', $botmail )
47           . "." );
48 }
49
50 #####
51 # Usage: botmail::check($recipient, [$always])
52 sub check {
53     my ( $recipient, $always ) = @_;
54     $recipient ||= $::who;
55
56     my %from =
57       &::sqlSelectColHash( 'botmail', "srcwho,time",
58         { dstwho => lc $recipient } );
59     my $t = keys %from;
60     my $from = join( ", ", keys %from );
61
62     if ( $t == 0 ) {
63         &::msg( $recipient, "You have no botmail." ) if ($always);
64     }
65     else {
66         &::msg( $recipient,
67             "You have $t messages awaiting, from: $from (botmail read)" );
68     }
69 }
70
71 #####
72 # Usage: botmail::next($recipient)
73 sub next {
74     my ($recipient) = @_;
75
76     my %hash =
77       &::sqlSelectRowHash( 'botmail', '*', { dstwho => lc $recipient } );
78
79     if ( scalar( keys %hash ) <= 1 ) {
80         &::msg( $recipient, "You have no botmail." );
81     }
82     else {
83         my $date = scalar( gmtime $hash{'time'} );
84         my $ago  = &::Time2String( time() - $hash{'time'} );
85         &::msg( $recipient,
86             "From $hash{srcwho} ($hash{srcuh}) on $date ($ago ago):" );
87         &::msg( $recipient, $hash{'msg'} );
88         &::sqlDelete( 'botmail',
89             { 'dstwho' => $hash{dstwho}, 'srcwho' => $hash{srcwho} } );
90     }
91 }
92
93 #####
94 # Usage: botmail::add($recipient, $msg)
95 sub add {
96     my ( $recipient, $msg ) = @_;
97     &::DEBUG("botmail::add(@_)");
98
99     # allow optional trailing : ie: botmail for foo[:] hello
100     $recipient =~ s/:$//;
101
102 # only support 1 botmail with unique dstwho/srcwho to have same
103 # functionality as botmail from infobot.
104 # Note: I removed the &::sqlQuote reference. Seems to be working and inserting fine without it here. -- troubled
105     my %hash = &::sqlSelectRowHash(
106         'botmail',
107         '*',
108         {
109             srcwho => lc $::who,
110             dstwho => lc $recipient
111         }
112     );
113
114     if ( scalar( keys %hash ) > 1 ) {
115         &::msg( $::who, "$recipient already has a message queued from you" );
116         return;
117     }
118
119     &::sqlInsert(
120         'botmail',
121         {
122             'dstwho' => lc $recipient,
123             'srcwho' => lc $::who,
124             'srcuh'  => $::nuh,
125             'time'   => time(),
126             'msg'    => $msg,
127         }
128     );
129
130     &::msg( $::who, "OK, $::who, I'll let $recipient know." );
131 }
132
133 1;
134
135 # vim:ts=4:sw=4:expandtab:tw=80