| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# send e-mail to all registered F*EX users |
| 4 |
# |
| 5 |
# Author: Ulli Horlacher <[email protected]> |
| 6 |
# |
| 7 |
|
| 8 |
use Getopt::Std; |
| 9 |
use File::Basename; |
| 10 |
use Cwd 'abs_path'; |
| 11 |
|
| 12 |
# do not run as CGI! |
| 13 |
exit if $ENV{SCRIPT_NAME}; |
| 14 |
|
| 15 |
unless ($ENV{FEXLIB}) { |
| 16 |
if ($ENV{FEXHOME}) { |
| 17 |
$ENV{FEXLIB} = $ENV{FEXHOME}.'/lib'; |
| 18 |
} else { |
| 19 |
$ENV{FEXLIB} = dirname(dirname(abs_path($0))).'/lib'; |
| 20 |
} |
| 21 |
} |
| 22 |
$FEXLIB = $ENV{FEXLIB}; |
| 23 |
die "$0: no FEXLIB\n" unless -f "$FEXLIB/fex.pp"; |
| 24 |
|
| 25 |
# program name |
| 26 |
$0 =~ s:.*/::; |
| 27 |
|
| 28 |
# become effective user fex |
| 29 |
unless ($<) { |
| 30 |
if (my @pw = getpwnam('fex')) { |
| 31 |
$) = $pw[3]; |
| 32 |
$> = $pw[2]; |
| 33 |
$ENV{HOME} = $pw[7]; |
| 34 |
} else { |
| 35 |
die "$0: no such user 'fex'\n"; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
# import from fex.pp |
| 40 |
our ($FEXHOME,$hostname,$sendmail,$spooldir,$admin,$bcc); |
| 41 |
|
| 42 |
# load common code, local config : $HOME/lib/fex.ph |
| 43 |
require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n"; |
| 44 |
|
| 45 |
die "$0: \$admin not configured in $FEXLIB/fex.ph\n" |
| 46 |
if not $admin or $admin =~ /example.org/; |
| 47 |
|
| 48 |
$opt_h = $opt_f = $opt_n = 0; |
| 49 |
|
| 50 |
getopts('hfn') or usage(2); |
| 51 |
usage(0) if $opt_h; |
| 52 |
|
| 53 |
$subject = "@ARGV"; |
| 54 |
die usage(1) unless $subject; |
| 55 |
|
| 56 |
local $/; |
| 57 |
$text = <STDIN>; |
| 58 |
die usage(1) unless $text; |
| 59 |
|
| 60 |
if (open my $sig,$ENV{HOME}.'/.signature') { |
| 61 |
$text .= "\n-- \n" . <$sig>; |
| 62 |
close $sig; |
| 63 |
} |
| 64 |
|
| 65 |
local $/ = "\n"; |
| 66 |
|
| 67 |
chdir $spooldir or die "$0: $spooldir - $!\n"; |
| 68 |
|
| 69 |
# @users = grep { chomp;s:/@:: } glob("*/@"); |
| 70 |
foreach $user (glob("*@*")) { |
| 71 |
if (-f "$user/@" |
| 72 |
and (readlink "$user/\@NOTIFICATION"||'') !~ /no/i |
| 73 |
and not ($opt_f and -f "$user/\@ALLOWED_RECIPIENTS")) { |
| 74 |
push @users,$user; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
unless ($opt_f) { |
| 79 |
|
| 80 |
foreach $group (glob "*/\@GROUP/*") { |
| 81 |
if (open $group,$group) { |
| 82 |
while (<$group>) { |
| 83 |
s/#.*//; |
| 84 |
s/:.*\n//; |
| 85 |
push @users,$_ if /@/; |
| 86 |
} |
| 87 |
close $group; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
foreach $subuser (glob "*/\@SUBUSER") { |
| 92 |
if (open $subuser,$subuser) { |
| 93 |
while (<$subuser>) { |
| 94 |
s/#.*//; |
| 95 |
s/:.*\n//; |
| 96 |
push @users,$_ if /@/; |
| 97 |
} |
| 98 |
close $subuser; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
# @users = qw'framstag@fex'; |
| 105 |
die "$0: no users found\n" unless @users or grep /@/,@users; |
| 106 |
push @users,$bcc; |
| 107 |
@users = uniq(@users); |
| 108 |
|
| 109 |
if ($opt_n) { |
| 110 |
print "<|$sendmail\n\t",join("\n\t",@users),"\n\n"; |
| 111 |
$sendmail = *STDOUT; |
| 112 |
} else { |
| 113 |
open $sendmail,'|-',$sendmail,@users or die "$0: $sendmail - $!\n"; |
| 114 |
} |
| 115 |
|
| 116 |
print {$sendmail} |
| 117 |
"From: $admin\n", |
| 118 |
"To: fexusers\@$hostname\n", |
| 119 |
"Subject: $subject\n", |
| 120 |
"\n", |
| 121 |
$text; |
| 122 |
|
| 123 |
unless ($opt_n) { |
| 124 |
close $sendmail or die "$0: $sendmail - $!\n"; |
| 125 |
print "mail sent to:\n",map { "$_\n" } @users; |
| 126 |
} |
| 127 |
exit; |
| 128 |
|
| 129 |
sub uniq { |
| 130 |
my %x; |
| 131 |
grep !$x{$_}++,@_; |
| 132 |
} |
| 133 |
|
| 134 |
sub usage { |
| 135 |
print "usage: $0 [-f] [-n] \"SUBJECT\" < mail.text\n"; |
| 136 |
print "options: -f full users only\n"; |
| 137 |
print " -n do not send mail, just show it with recipients\n"; |
| 138 |
exit shift||0; |
| 139 |
} |