File: collect-reminders

package info (click to toggle)
email-reminder 0.7.8-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 744 kB
  • ctags: 154
  • sloc: perl: 2,302; xml: 112; sh: 75; makefile: 11
file content (170 lines) | stat: -rwxr-xr-x 3,985 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/perl -T

=head1 NAME

collect-reminders - collect email reminders to be sent out

=head1 SYNOPSIS

Collect emails reminders set by users for special occasions and move
them to the email-reminder spool directory.

=head1 DESCRIPTION

Email-reminder allows users to define events that they want to be
reminded of by email.

This script is meant to be invoked everyday by a cron job or as the
root user.  It collects the reminder files from each user.

=head1 OPTIONS

=over 6

=item B<--help>

Displays basic usage message.

=item B<--verbose>

Prints out information about what the program is doing, including the
full emails being sent out.

=item B<--version>

Displays the version number.

=back

=head1 FILES

F<~/.email-reminders>, F</etc/email-reminder.conf>

=head1 AUTHOR

Francois Marier <francois@fmarier.org>

=head1 SEE ALSO

email-reminder-editor, send-reminders

=head1 COPYRIGHT

Copyright (C) 2004-2014 by Francois Marier

Email-Reminder is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the
License, or (at your option) any later version.

Email-Reminder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with Email-Reminder; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

=cut

use strict;
use warnings;

use File::Copy;
use Getopt::Long;
use Pod::Usage;

use EmailReminder::Utils;

# Command-line parameters
my $verbose  = 0;
my $simulate = 0;
my $version  = 0;
my $help  = 0;
GetOptions( "verbose"  => \$verbose,
            "version"  => \$version,
            "help"     => \$help,
    );

sub copy_reminders
{
    my $uid = shift;
    my $homedir = shift;

    my $file = "$homedir/" . $EmailReminder::Utils::USER_CONFIG_FILE;
    if (-e $file) {
        my $destination = $EmailReminder::Utils::SPOOL_DIRECTORY . '/' . $uid;
        print "==> Copying $file to $destination\n" if $verbose;

        # Delete existing file for the user if necessary
        if (-e $destination) {
            if (unlink($destination)) {
                print "A file for uid $uid was already present and has been removed.\n" if $verbose;
            } else {
                die "Could not remove $destination\n";
            }
        }

        # Copy the file to the spool directory
        unless (copy($file, $destination)) {
            die "Could not copy $file\n";
        }

        return 1;
    }
    else {
        print "No reminders in '$homedir'.\n" if $verbose;
    }

    return 0;
}

sub main
{
    my $running_uid = $>;
    if ($running_uid != 0) {
        die "This script must be run as root\n";
    }

    # Iterate through all local users
    while (my (undef, undef, $uid, undef, undef, undef, undef,
               $homedir, $shell, undef) = getpwent) {

        # Untaint uid
        if ($uid =~ /^([0-9]+)$/i) {
            $uid = $1;
        } else {
            die "Error: got a non-numeric uid\n";
        }

        # Try to skip non-human users
        if (($uid < 1000) || ($uid >= 60000) || ($shell eq '/bin/false')) {
            print "Skipped non-human uid $uid\n" if $verbose;
            next;
        }

        # Untaint homedir
        if ($homedir =~ /^([A-Za-z0-9_\-\/.]+)$/) {
            $homedir = $1;
        } else {
            die "Error: home directory for uid $uid contains invalid characters\n";
        }

        copy_reminders($uid, $homedir);
    }
    return 1;
}

if ($help || $version) {
    print "collect-reminders $EmailReminder::Utils::VERSION\n";
    if ($help) {
        print "\n";
        pod2usage(1);
    } else {
        exit(1);
    }
} else {
    main();
}