#!/usr/bin/perl -w ################################ # This script creates .forward # # and .vacation.msg files # # for e-mail autoreplying # # used by Exim # # # ############################### use strict; use diagnostics; my %users = ( #username leaves on is back on user => {van => [17,7,5], tm => [13,8,5], }, ); my $vacstd = '/home/user/scripts/.vacation.msg'; #standard away message my $fwdstd = '/home/user/scripts/.forward'; #standard Exim filter file -e $vacstd or die("Needed file \"$vacstd\" not found; Quitting."); -e $fwdstd or die("Needed file \"$fwdstd\" not found; Quitting."); foreach(keys %users) { my $crntuser = $_; #current user my @d = localtime(time); #date info it needs for comparing my $sdate = join('-',@{$users{$crntuser}{van}}); #leaving date my $edate = join('-',@{$users{$crntuser}{tm}}); #returning date my $cdate = join('-',$d[3],$d[4]+1,$d[5]-100); #current date my @udata = getpwnam($crntuser); #user login pass uid gid for chown later my $fwdnew = "/home/$_/.forward"; #filename for fresh Exim filter file if($edate eq $cdate) { #Is the returning date today? if(-e $fwdnew) { #Does the fresh Exim filter file exist? unlink $fwdnew; #then delete it print "User Scrntuser : File $fwdnew removed due to reached target end date $edate ($cdate); Quitting."; exit 0; #and we're done for today. } } if($sdate ne $cdate) { print "User $crntuser : Target start date $sdate ($cdate) not yet reached."; #next user if we're not ready yet next; } my $vacnew = "/home/$_/.vacation.msg"; #fresh vacation message file print "Creating .vacation file for user $_\n"; if(-e $vacnew) #Does it already exist? then go on. { print "File \"$vacnew\" already exists? Wut??\n" ; } else { open VACSTD,$vacstd #open standard vacation file for reading or die("Unable to access standard vacation file \"$vacstd\"; Quitting."); open VACNEW,">$vacnew" #open fresh vacation file for writing or die("Unable to access fresh vacation file \"$vacnew\"; Quitting."); while() { s/_DATE_FROM_/$sdate/g; #replace start date s/_DATE_UNTIL_/$edate/g; #replace return date print VACNEW $_; #save to fresh vacation file } close VACSTD; close VACNEW; print "File \"$vacnew\" successfully written.\n"; } die("Unable to create \"$fwdnew\"; Quitting.") if system('cp',$fwdstd,$fwdnew) == -1; #kill off if we can't copy the exim filter file to the fresh copy. die('Error changing file permissions; Quitting.') if chown $udata[2],$udata[3],[$vacnew,$fwdnew] <2; #kill off if we can't set user permissions on fresh filter # and vacation message file. print "File \"$fwdnew\" successfully written.\n"; #Otherwise we'd have die()d so this can be displayed ok :) } #and we're done.