Update homepage URL and author email address
[email-reminder.git] / collect-reminders
blob1194b6893ee3c07204b455fb155a559908280111
1 #!/usr/bin/perl -T
3 =head1 NAME
5 collect-reminders - collect email reminders to be sent out
7 =head1 SYNOPSIS
9 Collect emails reminders set by users for special occasions and move
10 them to the email-reminder spool directory.
12 =head1 DESCRIPTION
14 Email-reminder allows users to define events that they want to be
15 reminded of by email.
17 This script is meant to be invoked everyday by a cron job or as the
18 root user. It collects the reminder files from each user.
20 =head1 OPTIONS
22 =over 6
24 =item B<--help>
26 Displays basic usage message.
28 =item B<--verbose>
30 Prints out information about what the program is doing, including the
31 full emails being sent out.
33 =item B<--version>
35 Displays the version number.
37 =back
39 =head1 FILES
41 F<~/.email-reminders>, F</etc/email-reminder.conf>
43 =head1 AUTHOR
45 Francois Marier <francois@fmarier.org>
47 =head1 SEE ALSO
49 email-reminder-editor, send-reminders
51 =head1 COPYRIGHT
53 Copyright (C) 2004-2010 by Francois Marier
55 Email-Reminder is free software; you can redistribute it and/or
56 modify it under the terms of the GNU General Public License as
57 published by the Free Software Foundation; either version 3 of the
58 License, or (at your option) any later version.
60 Email-Reminder is distributed in the hope that it will be useful,
61 but WITHOUT ANY WARRANTY; without even the implied warranty of
62 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
63 General Public License for more details.
65 You should have received a copy of the GNU General Public License
66 along with Email-Reminder; if not, write to the Free Software
67 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
68 02110-1301, USA.
70 =cut
72 use strict;
73 use warnings;
75 use File::Copy;
76 use Getopt::Long;
77 use Pod::Usage;
79 use EmailReminder::Utils;
81 # Command-line parameters
82 my $verbose = 0;
83 my $simulate = 0;
84 my $version = 0;
85 my $help = 0;
86 GetOptions( "verbose" => \$verbose,
87 "version" => \$version,
88 "help" => \$help,
91 sub copy_reminders
93 my $uid = shift;
94 my $homedir = shift;
96 my $file = "$homedir/" . $EmailReminder::Utils::USER_CONFIG_FILE;
97 if (-e $file) {
98 my $destination = $EmailReminder::Utils::SPOOL_DIRECTORY . '/' . $uid;
99 print "==> Copying $file to $destination\n" if $verbose;
101 # Delete existing file for the user if necessary
102 if (-e $destination) {
103 if (unlink($destination)) {
104 print "A file for uid $uid was already present and has been removed.\n" if $verbose;
105 } else {
106 die "Could not remove $destination\n";
110 # Copy the file to the spool directory
111 unless (copy($file, $destination)) {
112 die "Could not copy $file\n";
115 return 1;
117 else {
118 print "No reminders in '$homedir'.\n" if $verbose;
121 return 0;
124 sub main
126 my $running_uid = $>;
127 if ($running_uid != 0) {
128 die "This script must be run as root\n";
131 # Iterate through all local users
132 while (my (undef, undef, $uid, undef, undef, undef, undef,
133 $homedir, $shell, undef) = getpwent) {
135 # Untaint uid
136 if ($uid =~ /^([0-9]+)$/i) {
137 $uid = $1;
138 } else {
139 die "Error: got a non-numeric uid\n";
142 # Try to skip non-human users
143 if (($uid < 1000) || ($uid >= 60000) || ($shell eq '/bin/false')) {
144 print "Skipped non-human uid $uid\n" if $verbose;
145 next;
148 # Untaint homedir
149 if ($homedir =~ /^([A-Za-z0-9_\-\/.]+)$/) {
150 $homedir = $1;
151 } else {
152 die "Error: home directory for uid $uid contains invalid characters\n";
155 copy_reminders($uid, $homedir);
157 return 1;
160 if ($help || $version) {
161 print "collect-reminders $EmailReminder::Utils::VERSION\n";
162 if ($help) {
163 print "\n";
164 pod2usage(1);
165 } else {
166 exit(1);
168 } else {
169 main();