- #!/usr/bin/perl -wT
- our $ID = q$Id: localuserinfo,v 1.3 2007-02-07 11:33:53 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?
- our @names;
- # Resolve version number from CVS id
- our $version = join (' ', (split (' ', $ID))[1..3]);
- $version =~ s/,v\b//;
- $version =~ s/(\S+)$/($1)/;
- # Parse options, sanity checks
- unless (
- GetOptions (
- "quiet|q" => sub { $verbose = 0 },
- "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: Rewrite to batch-resolve userinfo for all users before using any
- while (my $username = shift @names) {
- my ($fullname, @addresses) = &getuserinfo($username);
- my $localaddress = "$username\@users.kaospilot.no";
- print "$fullname\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 version {
- printf ("localuserinfo version %s\n\n", $version);
- print <<'EOF';
- List fullname 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 {
- printf <<"EOF";
- localuserinfo USER [USER...]
- List fullname for each user
- general options:
- --quiet | -q don't give process information to stdout
- --ingore-badname ignore non-existing usernames instead of failing
- --help | -h usage message
- --version | -v version number and copyright
- EOF
- }
|