source: trunk/debathena/debathena/libmail-expandaliases-perl/expand-alias @ 24013

Revision 24013, 4.0 KB checked in by broder, 15 years ago (diff)
Create a package for the Mail::ExpandAliases Perl module. This will be used for an updated version of the debathena-msmtp-mta which can understand /etc/aliases files.
  • Property svn:executable set to *
RevLine 
[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
22use strict;
23use vars qw($VERSION $opt_h $opt_f $opt_t $opt_v
24                     $opt_n $opt_c $opt_s $opt_D
25                     $joint);
26
27use Getopt::Std;
28use Mail::ExpandAliases;
29
30getopts("Dtcnhf:sv");
31
32if ($opt_h) {
33    exec("perldoc $0");
34    exit 0;
35}
36
37if (($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;
44my $expander = Mail::ExpandAliases->new($opt_f);
45
46for (@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
66exit(0);
67
68__END__
69
70=head1 NAME
71
72expand-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
98C<expand-alias> expands aliases from an aliases file, as implemented
99by the Mail::ExpandAliases module.
100
101=head1 USE
102
103C<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
111Note that unknown aliases expand to themselves; that is, they don't
112expand.
113
114C<expand-alias> has several command line swicthes that control the
115output:
116
117=over 4
118
119=item -c
120
121comma-separated output:
122
123    $ expand-alias -c listname
124    addr1, addr2, addr3
125
126=item -t
127
128tab-separated output
129
130    $ expand-alias -c listname
131    addr1       addr2   addr3
132
133=item -n
134
135newline 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
151The default separator is a single space:
152
153    $ expand-alias listname
154    addr1 addr2 addr3
155
156This 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
162C<expand-alias> also takes a C<-f> option, as hinted above, which
163indicates the file to be used; see L<Mail::ExpandAliases> for details
164about alias files search paths.
165
166If the C<-v> (verbose) flag is set, alias expansions are prefixed by
167the alias itself.  This is useful when specifying multiple aliases on
168the 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
177darren chamberlain E<lt>darren@cpan.orgE<gt>
178
179=head1 SEE ALSO
180
181L<Perl>, L<Mail::ExpandAliases>
Note: See TracBrowser for help on using the repository browser.