- #!/usr/bin/perl -wT
- my $ID = q$Id: localuserinfo,v 1.7 2007-02-07 15:08:09 jonas Exp $;
- #
- # localuserinfo -- List fullname for each user
- #
- # Written by Jonas Smedegaard <dr@jones.dk>
- # Copyright 2003-2007 Jonas Smedegaard <dr@jones.dk>
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- #
- # TODO: Options to print other info than fullname
- #
- use strict;
- use Getopt::Long;
- use User::pwent;
- use User::grent;
- our $verbose = 1; # should we be verbose?
- my $ignore_badname = 0; # should we ignore bad names?
- my $include_fullname = 1;
- my $include_mail = 0;
- our $custom_template;
- our @names;
- # Resolve version number from CVS id
- my $version = join (' ', (split (' ', $ID))[1..3]);
- $version =~ s/,v\b//;
- $version =~ s/(\S+)$/($1)/;
- our $maildomain_path = '/etc/mailname';
- our $maildomain = &get_maildomain;
- # Parse options, sanity checks
- unless (
- GetOptions (
- "quiet|q" => sub { $verbose = 0 },
- "fullname|n" => \$include_fullname,
- "mail|m" => \$include_mail,
- "custom=s" => \$custom_template,
- "ignore-badname" => \$ignore_badname,
- "help|h" => sub { &usage(); exit 0 },
- "version|v" => sub { &version(); exit 0 },
- "debug" => sub { $verbose = 2 }
- )
- ) {
- &usage();
- exit 1;
- }
- while (defined(my $arg = shift(@ARGV))) {
- push (@names, $arg);
- }
- # TODO: Support custom ordering and custom delimiter
- my @infochunks;
- push (@infochunks, 'fullname') if ($include_fullname);
- push (@infochunks, 'mailaddress') if ($include_mail);
- my $template = $custom_template ? $custom_template : '%' . join('% %', @infochunks) . '%';
- # TODO: Rewrite to batch-resolve userinfo for all users before using any
- while (my $username = shift @names) {
- my $string;
- my ($fullname, @addresses) = &getuserinfo($username);
- my $mailaddress = "$username\@$maildomain";
- $string = $template;
- $string =~ s/\%username\%/$username/g;
- $string =~ s/\%fullname\%/$fullname/g;
- $string =~ s/\%mailaddress\%/$mailaddress/g;
- print "$string\n";
- }
- sub getuserinfo($) {
- my $username = shift;
- my $pw = getpwnam($username) || die "Username \"$username\" does not exist.";
- my ($fullname, $office, $workphone, $homephone, $other) = split /\s*,\s*/, $pw->gecos;
- my @addresshints;
- if (defined($other)) {
- @addresshints = grep {/^([\.[:alnum:]_-]+|\+)?@([\.[:alnum:]_-]+)?$/} split /\s+/, $other;
- }
- return ($fullname, @addresshints);
- }
- sub get_maildomain {
- open (MAILDOMAINFILE, "<" . $maildomain_path) || die "can’t open $maildomain_path: $!";
- my $string = readline(MAILDOMAINFILE) || die "can’t read $maildomain_path: $!";
- close (MAILDOMAINFILE);
- chomp($string);
- # FIXME: Do some sanity check of the string - ensure a single word, FQDN syntax etc.
- return $string;
- }
- sub version {
- printf ("localuserinfo version %s\n\n", $version);
- print <<'EOF';
- List fullname and/or other info for each user.
- Copyright (C) 2003-2007 Jonas Smedegaard <dr@jones.dk>
- EOF
- print <<'EOF';
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or (at
- your option) any later version.
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License, /usr/share/common-licenses/GPL, for more details.
- EOF
- }
- sub usage {
- print <<"EOF";
- localuserinfo USER [USER...]
- List fullname and/or other info for each user
- general options:
- --quiet | -q don't give process information to stdout
- --ignore-badname ignore non-existing usernames instead of failing
- --help | -h usage message
- --fullname | -n include fullname (enabled by default)
- --mail | -m include email address
- --custom=TEMPLATE custom template, e.g. '%username% (%fullname%)'
- available infochunks:
- username
- fullname
- mailaddress
- (default is '%fullname%' or space-delimited list
- of enabled infochunks)
- --version | -v version number and copyright
- EOF
- }
|