]>
iEval git - app-fonbot-daemon.git/blob - Email.pm
5aeea1cf91353ce771b370524abb981ecc1e6646
1 package App
:: FonBot
:: Plugin
:: Email
;
3 our $VERSION = '0.001' ;
9 use Apache2
:: Authen
:: Passphrase qw
/pwcheck/ ;
10 use Email
:: Sender
:: Simple qw
/sendmail/ ;
13 use Linux
:: Inotify2 qw
/IN_MOVED_TO/ ;
14 use File
:: Slurp qw
/read_file/ ;
16 use Log
:: Log4perl qw
// ;
18 use File
:: Glob qw
/bsd_glob GLOB_NOSORT/ ;
19 use Text
:: ParseWords qw
/shellwords/ ;
21 use App
:: FonBot
:: Plugin
:: Common
;
22 use App
:: FonBot
:: Plugin
:: Config qw
/$email_batch_seconds $email_from $email_subject/ ;
24 ##################################################
26 my $log = Log
:: Log4perl
-> get_logger ( __PACKAGE__
);
34 return unless $email_from && $email_subject ;
35 $log -> info ( 'initializing ' . __PACKAGE__
);
36 $session = POE
:: Session
-> create (
38 _start
=> \
& email_start
,
39 send_message
=> \
& email_send_message
,
40 flush_queue
=> \
& email_flush_queue
,
41 inotify_readable
=> sub {
42 $_ [ HEAP
]{ inotify
}-> poll
45 $_ [ KERNEL
]-> select_read ( $inotify )
52 $log -> info ( 'finishing ' . __PACKAGE__
);
53 POE
:: Kernel
-> post ( $session , 'shutdown' )
58 for my $file ( bsd_glob
'Maildir/new/*' , GLOB_NOSORT
) {
59 my $email = Email
:: MIME
-> new ( scalar read_file
$file );
66 return unless defined $email ;
68 my $replyto = $email -> header ( 'From' );
69 return unless defined $replyto ;
71 my ( $user , $password )= split ' ' , $email -> header ( 'Subject' ), 2 ;
74 $log -> debug ( "Processing email from $user " );
76 eval { pwcheck
$user , $password };
78 $log -> debug ( "Incorrect credentials in email subject from user $user . Exception: $@" );
79 POE
:: Kernel
-> yield ( send_message
=> $replyto , "Incorrect credentials" );
83 $ok_user_addresses { " $user EMAIL $replyto " }= 1 ;
85 my $process_email_part = sub {
86 local * __ANON__
= "process_email_part" ; #Name this sub. See http://www.perlmonks.org/?node_id=304883
89 return unless $part -> content_type =~ /text\/plai n
/;
91 my @lines = split ' \n ' , $part -> body ;
93 for my $line ( @lines ) {
94 last if $line =~ /^--/ ;
95 $log -> debug ( "Command received via email from $user : $line " );
96 sendmsg
$user , undef , "EMAIL ' $replyto '" , shellwords
$line
100 $email -> walk_parts ( $process_email_part );
105 $_ [ KERNEL
]-> alias_set ( 'EMAIL' );
106 $_ [ HEAP
]{ inotify
} = Linux
:: Inotify2
-> new ;
107 $_ [ HEAP
]{ inotify
}-> watch ( 'Maildir/new/' , IN_MOVED_TO
, \
& email_handle_new
);
109 open $inotify , '<&=' , $_ [ HEAP
]{ inotify
}-> fileno ;
110 $_ [ KERNEL
]-> select_read ( $inotify , 'inotify_readable' );
114 sub email_send_message
{
115 my ( $address , $message ) = @_ [ ARG0
, ARG1
];
117 $queues { $address }.= $message . " \n " ;
118 if ( defined $queue_alarms { $address }) {
119 $_ [ KERNEL
]-> delay_adjust ( $queue_alarms { $address }, $email_batch_seconds )
121 $queue_alarms { $address }= $_ [ KERNEL
]-> delay_set ( flush_queue
=> $email_batch_seconds , $address )
125 sub email_flush_queue
{
126 my ( $queue ) = $_ [ ARG0
];
127 return unless exists $queues { $queue };
129 my $email = Email
:: Simple
-> create (
133 Subject
=> $email_subject ,
135 body
=> $queues { $queue }
138 delete $queues { $queue };
139 delete $queue_alarms { $queue };
141 $log -> debug ( "Sending email to $queue " );
145 } or $log -> error ( "Could not send email: " . $ @
-> message );
155 App::FonBot::Plugin::Email - FonBot plugin for receiving commands and sending messages through emails
159 use App::FonBot::Plugin::Email;
160 App::FonBot::Plugin::Email->init;
162 App::FonBot::Plugin::Email->fini;
166 This FonBot plugin provides email receiving/sending features to B<fonbotd>. Emails are read from F<Maildir/> and are sent through C<Email::Sender::Simple>.
168 =head1 CONFIGURATION VARIABLES
170 These are the L<App::FonBot::Plugin::Config> configuration variables used in this module
174 =item C<$email_batch_seconds>
176 When receiving an email send request, C<App::FonBot::Plugin::Email> waits this many seconds for further email send requests to the same email address. The timer is reset for each email send request. When the timer expires, all pending send requests are batched and sent as one email.
180 C<From:> header of all emails sent by this plugin. If not set the email plugin is disabled.
182 =item C<$email_subject>
184 C<Subject:> header of all emails sent by this plugin. If not set the email plugin is disabled.
190 Marius Gavrilescu C<< marius@ieval.ro >>
192 =head1 COPYRIGHT AND LICENSE
194 Copyright 2013-2015 Marius Gavrilescu
196 This file is part of fonbotd.
198 fonbotd is free software: you can redistribute it and/or modify
199 it under the terms of the GNU Affero General Public License as published by
200 the Free Software Foundation, either version 3 of the License, or
201 (at your option) any later version.
203 fonbotd is distributed in the hope that it will be useful,
204 but WITHOUT ANY WARRANTY; without even the implied warranty of
205 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
206 GNU Affero General Public License for more details.
208 You should have received a copy of the GNU Affero General Public License
209 along with fonbotd. If not, see <http://www.gnu.org/licenses/>
This page took 0.060192 seconds and 3 git commands to generate.