"Fossies" - the Fresh Open Source Software Archive

Member "email-reminder-0.8.1/collect-reminders" (19 Sep 2020, 4038 Bytes) of package /linux/privat/email-reminder-0.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "collect-reminders": 0.8.0_vs_0.8.1.

    1 #!/usr/bin/perl -T
    2 
    3 =head1 NAME
    4 
    5 collect-reminders - collect email reminders to be sent out
    6 
    7 =head1 SYNOPSIS
    8 
    9 Collect emails reminders set by users for special occasions and move
   10 them to the email-reminder spool directory.
   11 
   12 =head1 DESCRIPTION
   13 
   14 Email-reminder allows users to define events that they want to be
   15 reminded of by email.
   16 
   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.
   19 
   20 =head1 OPTIONS
   21 
   22 =over 6
   23 
   24 =item B<--help>
   25 
   26 Displays basic usage message.
   27 
   28 =item B<--verbose>
   29 
   30 Prints out information about what the program is doing, including the
   31 full emails being sent out.
   32 
   33 =item B<--version>
   34 
   35 Displays the version number.
   36 
   37 =back
   38 
   39 =head1 FILES
   40 
   41 F<~/.email-reminders>, F</etc/email-reminder.conf>
   42 
   43 =head1 AUTHOR
   44 
   45 Francois Marier <francois@fmarier.org>
   46 
   47 =head1 SEE ALSO
   48 
   49 email-reminder-editor, send-reminders
   50 
   51 =head1 COPYRIGHT
   52 
   53 Copyright (C) 2004-2014 by Francois Marier
   54 
   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.
   59 
   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.
   64 
   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.
   69 
   70 =cut
   71 
   72 use strict;
   73 use warnings;
   74 
   75 use File::Copy;
   76 use Getopt::Long;
   77 use Pod::Usage;
   78 
   79 use EmailReminder::Utils;
   80 
   81 binmode(STDOUT, ":utf8");
   82 binmode(STDERR, ":utf8");
   83 
   84 # Command-line parameters
   85 my $verbose  = 0;
   86 my $simulate = 0;
   87 my $version  = 0;
   88 my $help  = 0;
   89 GetOptions( "verbose"  => \$verbose,
   90             "version"  => \$version,
   91             "help"     => \$help,
   92     );
   93 
   94 sub copy_reminders
   95 {
   96     my $uid = shift;
   97     my $homedir = shift;
   98 
   99     my $file = "$homedir/" . $EmailReminder::Utils::USER_CONFIG_FILE;
  100     if (-e $file) {
  101         my $destination = $EmailReminder::Utils::SPOOL_DIRECTORY . '/' . $uid;
  102         print "==> Copying $file to $destination\n" if $verbose;
  103 
  104         # Delete existing file for the user if necessary
  105         if (-e $destination) {
  106             if (unlink($destination)) {
  107                 print "A file for uid $uid was already present and has been removed.\n" if $verbose;
  108             } else {
  109                 die "Could not remove $destination\n";
  110             }
  111         }
  112 
  113         # Copy the file to the spool directory
  114         unless (copy($file, $destination)) {
  115             die "Could not copy $file\n";
  116         }
  117 
  118         return 1;
  119     }
  120     else {
  121         print "No reminders in '$homedir'.\n" if $verbose;
  122     }
  123 
  124     return 0;
  125 }
  126 
  127 sub main
  128 {
  129     my $running_uid = $>;
  130     if ($running_uid != 0) {
  131         die "This script must be run as root\n";
  132     }
  133 
  134     # Iterate through all local users
  135     while (my (undef, undef, $uid, undef, undef, undef, undef,
  136                $homedir, $shell, undef) = getpwent) {
  137 
  138         # Untaint uid
  139         if ($uid =~ /^([0-9]+)$/i) {
  140             $uid = $1;
  141         } else {
  142             die "Error: got a non-numeric uid\n";
  143         }
  144 
  145         # Try to skip non-human users
  146         if (($uid < 1000) || ($uid >= 60000) || ($shell eq '/bin/false')) {
  147             print "Skipped non-human uid $uid\n" if $verbose;
  148             next;
  149         }
  150 
  151         # Untaint homedir
  152         if ($homedir =~ /^([A-Za-z0-9_\-\/.]+)$/) {
  153             $homedir = $1;
  154         } else {
  155             die "Error: home directory for uid $uid contains invalid characters\n";
  156         }
  157 
  158         copy_reminders($uid, $homedir);
  159     }
  160     return 1;
  161 }
  162 
  163 if ($help || $version) {
  164     print "collect-reminders $EmailReminder::Utils::VERSION\n";
  165     if ($help) {
  166         print "\n";
  167         pod2usage(1);
  168     } else {
  169         exit(1);
  170     }
  171 } else {
  172     main();
  173 }