[24013] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | # ---------------------------------------------------------------------- |
---|
| 4 | # expand-alias - expand an alias from /etc/aliases |
---|
| 5 | # Copyright (C) 2002 darren chamberlain <darren@cpan.org> |
---|
| 6 | # |
---|
| 7 | # This program is free software; you can redistribute it and/or |
---|
| 8 | # modify it under the terms of the GNU General Public License as |
---|
| 9 | # published by the Free Software Foundation; version 2. |
---|
| 10 | # |
---|
| 11 | # This program is distributed in the hope that it will be useful, but |
---|
| 12 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | # General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
| 17 | # along with this program; if not, write to the Free Software |
---|
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
| 19 | # 02111-1307 USA |
---|
| 20 | # ---------------------------------------------------------------------- |
---|
| 21 | |
---|
| 22 | use strict; |
---|
| 23 | use vars qw($VERSION $opt_h $opt_f $opt_t $opt_v |
---|
| 24 | $opt_n $opt_c $opt_s $opt_D |
---|
| 25 | $joint); |
---|
| 26 | |
---|
| 27 | use Getopt::Std; |
---|
| 28 | use Mail::ExpandAliases; |
---|
| 29 | |
---|
| 30 | getopts("Dtcnhf:sv"); |
---|
| 31 | |
---|
| 32 | if ($opt_h) { |
---|
| 33 | exec("perldoc $0"); |
---|
| 34 | exit 0; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | if (($opt_t + $opt_c + $opt_n + $opt_s) > 1) { |
---|
| 38 | die "Please specify only one of -t, -n, -c, or -s."; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | $joint = $opt_t ? "\t" : $opt_c ? ", " : $opt_n ? "\n" : " "; |
---|
| 42 | |
---|
| 43 | $Mail::ExpandAliases::DEBUG =1 if $opt_D; |
---|
| 44 | my $expander = Mail::ExpandAliases->new($opt_f); |
---|
| 45 | |
---|
| 46 | for (@ARGV) { |
---|
| 47 | my $alias = $opt_v ? "$_: " : ""; |
---|
| 48 | if ($opt_s) { |
---|
| 49 | my (@expandos, $last, $comma, $num, $str); |
---|
| 50 | |
---|
| 51 | @expandos = $expander->expand($_); |
---|
| 52 | if (@expandos > 2) { |
---|
| 53 | my $last = pop @expandos; |
---|
| 54 | print $alias, join(", ", @expandos), ", and ", $last; |
---|
| 55 | } elsif (@expandos == 2) { |
---|
| 56 | print $alias, join " and ", @expandos; |
---|
| 57 | } else { |
---|
| 58 | print $alias, @expandos; |
---|
| 59 | } |
---|
| 60 | } else { |
---|
| 61 | print $alias, join $joint, $expander->expand($_); |
---|
| 62 | } |
---|
| 63 | print "\n"; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | exit(0); |
---|
| 67 | |
---|
| 68 | __END__ |
---|
| 69 | |
---|
| 70 | =head1 NAME |
---|
| 71 | |
---|
| 72 | expand-alias - expand mail aliases from /etc/aliases |
---|
| 73 | |
---|
| 74 | =head1 SYNOPSIS |
---|
| 75 | |
---|
| 76 | $ expand-alias MAILER-DAEMON |
---|
| 77 | root |
---|
| 78 | |
---|
| 79 | $ expand-alias -c listname |
---|
| 80 | addr1, addr2, addr3 |
---|
| 81 | |
---|
| 82 | $ expand-alias -n listname |
---|
| 83 | addr1 |
---|
| 84 | addr2 |
---|
| 85 | addr3 |
---|
| 86 | |
---|
| 87 | $ expand-alias -t listname |
---|
| 88 | addr1 addr2 addr3 |
---|
| 89 | |
---|
| 90 | $ expand-alias -f ~/my.aliases friends |
---|
| 91 | friend1@isp.net other.friend@isp2.net |
---|
| 92 | |
---|
| 93 | $ expand-alias -f ~/my.aliases -s friends |
---|
| 94 | friend1@isp.net and other.friend@isp2.net |
---|
| 95 | |
---|
| 96 | =head1 DESCRIPTION |
---|
| 97 | |
---|
| 98 | C<expand-alias> expands aliases from an aliases file, as implemented |
---|
| 99 | by the Mail::ExpandAliases module. |
---|
| 100 | |
---|
| 101 | =head1 USE |
---|
| 102 | |
---|
| 103 | C<expand-alias> takes 0 or more aliases as arguments: |
---|
| 104 | |
---|
| 105 | $ expand-alias postmaster |
---|
| 106 | darren@cpan.org |
---|
| 107 | |
---|
| 108 | $ expand-alias foo |
---|
| 109 | foo |
---|
| 110 | |
---|
| 111 | Note that unknown aliases expand to themselves; that is, they don't |
---|
| 112 | expand. |
---|
| 113 | |
---|
| 114 | C<expand-alias> has several command line swicthes that control the |
---|
| 115 | output: |
---|
| 116 | |
---|
| 117 | =over 4 |
---|
| 118 | |
---|
| 119 | =item -c |
---|
| 120 | |
---|
| 121 | comma-separated output: |
---|
| 122 | |
---|
| 123 | $ expand-alias -c listname |
---|
| 124 | addr1, addr2, addr3 |
---|
| 125 | |
---|
| 126 | =item -t |
---|
| 127 | |
---|
| 128 | tab-separated output |
---|
| 129 | |
---|
| 130 | $ expand-alias -c listname |
---|
| 131 | addr1 addr2 addr3 |
---|
| 132 | |
---|
| 133 | =item -n |
---|
| 134 | |
---|
| 135 | newline separated output |
---|
| 136 | |
---|
| 137 | $ expand-alias -n listname |
---|
| 138 | addr1 |
---|
| 139 | addr2 |
---|
| 140 | addr3 |
---|
| 141 | |
---|
| 142 | =item -s |
---|
| 143 | |
---|
| 144 | "sentence" form (a, b, and c). |
---|
| 145 | |
---|
| 146 | $ expand-alias -s listname |
---|
| 147 | addr1, addr2, and addr3 |
---|
| 148 | |
---|
| 149 | =back |
---|
| 150 | |
---|
| 151 | The default separator is a single space: |
---|
| 152 | |
---|
| 153 | $ expand-alias listname |
---|
| 154 | addr1 addr2 addr3 |
---|
| 155 | |
---|
| 156 | This is useful in shell scripts: |
---|
| 157 | |
---|
| 158 | $ for addr in `expand-alias -f ~/my.lists friends`; do |
---|
| 159 | > mail -s 'For your eyes only!' $addr < secret-file |
---|
| 160 | > done |
---|
| 161 | |
---|
| 162 | C<expand-alias> also takes a C<-f> option, as hinted above, which |
---|
| 163 | indicates the file to be used; see L<Mail::ExpandAliases> for details |
---|
| 164 | about alias files search paths. |
---|
| 165 | |
---|
| 166 | If the C<-v> (verbose) flag is set, alias expansions are prefixed by |
---|
| 167 | the alias itself. This is useful when specifying multiple aliases on |
---|
| 168 | the command line: |
---|
| 169 | |
---|
| 170 | $ expand-alias -vc listone listtwo listthree |
---|
| 171 | listone: addr1, addr2, addr3 |
---|
| 172 | listtwo: addr4, addr3, addr2 |
---|
| 173 | listthree: addr1, addr4, addr3 |
---|
| 174 | |
---|
| 175 | =head1 AUTHOR |
---|
| 176 | |
---|
| 177 | darren chamberlain E<lt>darren@cpan.orgE<gt> |
---|
| 178 | |
---|
| 179 | =head1 SEE ALSO |
---|
| 180 | |
---|
| 181 | L<Perl>, L<Mail::ExpandAliases> |
---|