Bump the copyright year to 2009
[email-reminder.git] / send-reminders
blob23c4308d39569b4daa8dae88e3eb6c370bc5a654
1 #!/usr/bin/perl -T
3 =head1 NAME
5 Send-reminders - send email reminders for special occasions
7 =head1 SYNOPSIS
9 Send emails reminders set by users for special occasions.
11 =head1 DESCRIPTION
13 Email-reminder allows users to define events that they want to be
14 reminded of by email. Possible events include birthdays,
15 anniversaries and yearly events. Reminders can be sent on the day of
16 the event and a few days beforehand.
18 This script is meant to be invoked everyday by a cron job. It mails
19 the actual reminders out.
21 When run by the root user, it processes all of the spooled reminders.
22 When run by a specific user, it only processes reminders set by that
23 user.
25 =head1 OPTIONS
27 =over 6
29 =item B<--help>
31 Displays basic usage message.
33 =item B<--simulate>
35 Does not actually send any emails out.
37 =item B<--verbose>
39 Prints out information about what the program is doing, including the
40 full emails being sent out.
42 =item B<--version>
44 Displays the version number.
46 =back
48 =head1 FILES
50 F<~/.email-reminders>, F</etc/email-reminder.conf>
52 =head1 AUTHOR
54 Francois Marier <francois@debian.org>
56 =head1 SEE ALSO
58 email-reminder-editor, collect-reminders
60 =head1 COPYRIGHT
62 Copyright (C) 2004-2009 by Francois Marier
64 Email-Reminder is free software; you can redistribute it and/or
65 modify it under the terms of the GNU General Public License as
66 published by the Free Software Foundation; either version 3 of the
67 License, or (at your option) any later version.
69 Email-Reminder is distributed in the hope that it will be useful,
70 but WITHOUT ANY WARRANTY; without even the implied warranty of
71 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
72 General Public License for more details.
74 You should have received a copy of the GNU General Public License
75 along with Email-Reminder; if not, write to the Free Software
76 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
77 02110-1301, USA.
79 =cut
81 use strict;
82 use warnings;
84 use Encode;
85 use Getopt::Long;
86 use MIME::Base64;
87 use MIME::QuotedPrint;
88 use Pod::Usage;
90 use EmailReminder::EventList;
91 use EmailReminder::Utils;
92 use Date::Manip qw(ParseDate UnixDate);
94 # Default preferences
95 my $PREFERENCE_FILE = '/etc/email-reminder.conf';
96 my %preferences;
97 $preferences{"send_reminders"} = 1;
98 $preferences{"smtp_server"} = 'localhost';
99 $preferences{"smtp_ssl"} = 0;
100 $preferences{"smtp_username"} = '';
101 $preferences{"smtp_password"} = '';
102 $preferences{"mail_from"} = 'root@localhost';
103 read_config();
105 # Global variables
106 my $user_fname;
107 my $user_lname;
109 # Command-line parameters
110 my $verbose = 0;
111 my $simulate = 0;
112 my $version = 0;
113 my $help = 0;
114 GetOptions( "verbose" => \$verbose,
115 "simulate" => \$simulate,
116 "version" => \$version,
117 "help" => \$help,
120 # Override preferences with system values
121 sub read_config
123 if (open CONFIG, '<', $PREFERENCE_FILE)
125 print "Reading preferences from '$PREFERENCE_FILE'\n" if $verbose;
127 # Stolen off of the Cookbook (section 8.16)
128 while (<CONFIG>) {
129 chomp; # no newline
130 s/#.*//; # no comments
131 s/^\s+//; # no leading white
132 s/\s+$//; # no trailing white
133 next unless length; # anything left?
134 my ($var, $value) = split(/\s*=\s*/, $_, 2);
135 $preferences{$var} = $value;
138 close CONFIG;
140 else {
141 print "Warning: cannot read configuration file at $PREFERENCE_FILE.\nMake sure that the user running $0 has read permissions on that configuration file.\n";
145 sub send_email
147 my $message = shift;
148 my $subject = shift;
149 my $user_name = shift;
150 my $user_email = shift;
152 unless ($user_email) {
153 return 0;
156 my $to = $user_email;
157 $to = "$user_name <$user_email>" if ($user_name);
159 print "--> Emailing '$to':\n".encode("UTF-8", $subject."\n\n".$message) if $verbose;
161 unless ($simulate) {
162 my $smtp_server = '';
163 if ($preferences{"smtp_server"} =~ /^([A-Za-z_0-9\-\/.]+)$/) {
164 $smtp_server = $1;
166 my $smtp = undef;
167 if ($preferences{"smtp_ssl"}) {
168 use Net::SMTP::SSL;
169 $smtp = Net::SMTP::SSL->new($smtp_server, Port => 465, Debug => 0);
171 else {
172 use Net::SMTP;
173 $smtp = Net::SMTP->new($smtp_server, Debug => 0);
175 die "Error: couldn't connect to server '$smtp_server'" unless $smtp;
177 # SMTP SASL authentication (if necessary)
178 if ($preferences{"smtp_username"} and $preferences{"smtp_password"}) {
179 unless ($smtp->auth($preferences{"smtp_username"}, $preferences{"smtp_password"})) {
180 die "Error: authentication with the SMTP server failed with error code ".$smtp->status;
184 unless ($smtp->mail($preferences{"mail_from"})) {
185 die "Error: the sending address was not accepted. Try setting the 'mail_from' variable to a valid email address in the configuration file";
188 my $ok = 1;
189 $ok = $ok && $smtp->to($to);
190 $ok = $ok && $smtp->data();
191 $ok = $ok && $smtp->datasend("From: Email-Reminder <" . $preferences{"mail_from"} . ">\n");
193 # Create an RFC822 compliant date (current time)
194 my $rfc822_format = "%a, %d %b %Y %H:%M %z";
195 my $today = ParseDate("Now");
196 my $rfc822_date = UnixDate($today,$rfc822_format);
197 $ok = $ok && $smtp->datasend("Date: $rfc822_date\n");
199 $ok = $ok && $smtp->datasend("To: $to\n");
200 $ok = $ok && $smtp->datasend("Subject: =?utf-8?B?".encode_base64(encode("UTF-8", $subject), '')."?=\n");
201 $ok = $ok && $smtp->datasend("Mime-Version: 1.0\n");
202 $ok = $ok && $smtp->datasend("Content-Type: text/plain; charset=utf-8\n");
203 $ok = $ok && $smtp->datasend("Content-Disposition: inline\n");
204 $ok = $ok && $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n");
205 $ok = $ok && $smtp->datasend("\n");
206 $ok = $ok && $smtp->datasend(encode_qp(encode("UTF-8", $message)));
207 $ok = $ok && $smtp->dataend();
209 $smtp->quit();
211 die "Error: could not mail the reminder out" unless $ok;
214 return 1;
217 sub process_file
219 my $file = shift;
221 print "==> Processing $file\n" if $verbose;
223 my $list = EmailReminder::EventList->new($file);
225 my @fullname = $list->get_user_name();
226 my $user_fname = $fullname[0];
227 my $user_lname = $fullname[1];
228 my $user_name = $user_fname;
229 $user_name .= " " . $user_lname if defined($user_lname);
230 my $user_email = $list->get_user_email();
232 foreach my $event ($list->get_events()) {
233 print '--> Processing event '.$event->get_name()."\n" if $verbose;
235 if ($event->is_occurring()) {
236 print '--> Event '.$event->get_name()." is occurring\n" if $verbose;
238 my $subject = $event->get_subject();
240 my @recipients = @{$event->get_recipients()};
241 if ($#recipients > -1) {
242 foreach my $recipient (@recipients) {
243 my $recipient_email = shift @{$recipient};
244 my $recipient_fname = shift @{$recipient};
245 my $recipient_lname = shift @{$recipient};
247 my $recipient_name = $recipient_fname;
248 $recipient_name .= " $recipient_lname" if defined($recipient_lname);
250 my $msg = $event->get_message($recipient_fname);
251 my $success = send_email($msg, $subject, $recipient_name, $recipient_email) if $msg;
252 return 0 if !$success;
254 } else {
255 my $msg = $event->get_message($user_fname);
256 my $success = send_email($msg, $subject, $user_name, $user_email) if $msg;
257 return 0 if !$success;
262 return 1;
265 sub main
267 my $running_uid = $>;
268 if (0 == $running_uid) {
269 print STDERR "Warning: for security reasons, this script should not be not as root.\n";
272 my $spool_dir = $EmailReminder::Utils::SPOOL_DIRECTORY;
273 if (-w $spool_dir) {
274 # Iterate through all spooled files
275 while (defined(my $file = <$spool_dir/*>)) {
276 # Untaint filename
277 if ($file =~ /^([A-Za-z_0-9\-\/]+)$/) {
278 $file = $1;
279 } else {
280 print STDERR "Skipped unclean filename" if $verbose;
281 next;
284 unless (process_file($file, 0, 1)) {
285 return 0;
288 # Delete the file once we're done with it
289 unless (unlink($file)) {
290 print STDERR "Could not remove $file.\n" if $verbose;
293 return 1;
294 } else {
295 # Normal users only get to test their own reminders
296 my @pwinfo = getpwuid($>);
297 my $homedir = $pwinfo[7];
298 my $file = "$homedir/" . $EmailReminder::Utils::USER_CONFIG_FILE;
300 if (-e $file) {
301 return process_file($file, 0, 1);
302 } else {
303 print STDERR "Warning: could not find your .email-reminders file.\n";
304 return 0;
309 if ($help || $version) {
310 print "send-reminders $EmailReminder::Utils::VERSION\n";
311 if ($help) {
312 print "\n";
313 pod2usage(1);
315 } elsif ($preferences{"send_reminders"}) {
316 unless (main()) {
317 print STDERR "Could not send reminders.\n";