summaryrefslogtreecommitdiff
path: root/localuserinfo
blob: 8f01e0f1e96667694df95c20dc66277eb9e77746 (plain)
  1. #!/usr/bin/perl -wT
  2. our $ID = q$Id: localuserinfo,v 1.3 2007-02-07 11:33:53 jonas Exp $;
  3. #
  4. # localuserinfo -- List fullname for each user
  5. #
  6. # Written by Jonas Smedegaard <dr@jones.dk>
  7. # Copyright 2003-2007 Jonas Smedegaard <dr@jones.dk>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. #
  23. # TODO: Options to print other info than fullname
  24. #
  25. use strict;
  26. use Getopt::Long;
  27. use User::pwent;
  28. use User::grent;
  29. our $verbose = 1; # should we be verbose?
  30. my $ignore_badname = 0; # should we ignore bad names?
  31. our @names;
  32. # Resolve version number from CVS id
  33. our $version = join (' ', (split (' ', $ID))[1..3]);
  34. $version =~ s/,v\b//;
  35. $version =~ s/(\S+)$/($1)/;
  36. # Parse options, sanity checks
  37. unless (
  38. GetOptions (
  39. "quiet|q" => sub { $verbose = 0 },
  40. "ignore-badname" => \$ignore_badname,
  41. "help|h" => sub { &usage(); exit 0 },
  42. "version|v" => sub { &version(); exit 0 },
  43. "debug" => sub { $verbose = 2 }
  44. )
  45. ) {
  46. &usage();
  47. exit 1;
  48. }
  49. while (defined(my $arg = shift(@ARGV))) {
  50. push (@names, $arg);
  51. }
  52. # TODO: Rewrite to batch-resolve userinfo for all users before using any
  53. while (my $username = shift @names) {
  54. my ($fullname, @addresses) = &getuserinfo($username);
  55. my $localaddress = "$username\@users.kaospilot.no";
  56. print "$fullname\n";
  57. }
  58. sub getuserinfo($) {
  59. my $username = shift;
  60. my $pw = getpwnam($username) || die "Username \"$username\" does not exist.";
  61. my ($fullname, $office, $workphone, $homephone, $other) = split /\s*,\s*/, $pw->gecos;
  62. my @addresshints;
  63. if (defined($other)) {
  64. @addresshints = grep {/^([\.[:alnum:]_-]+|\+)?@([\.[:alnum:]_-]+)?$/} split /\s+/, $other;
  65. }
  66. return ($fullname, @addresshints);
  67. }
  68. sub version {
  69. printf ("localuserinfo version %s\n\n", $version);
  70. print <<'EOF';
  71. List fullname for each user.
  72. Copyright (C) 2003-2007 Jonas Smedegaard <dr@jones.dk>
  73. EOF
  74. print <<'EOF';
  75. This program is free software; you can redistribute it and/or modify
  76. it under the terms of the GNU General Public License as published by
  77. the Free Software Foundation; either version 2 of the License, or (at
  78. your option) any later version.
  79. This program is distributed in the hope that it will be useful, but
  80. WITHOUT ANY WARRANTY; without even the implied warranty of
  81. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  82. General Public License, /usr/share/common-licenses/GPL, for more details.
  83. EOF
  84. }
  85. sub usage {
  86. printf <<"EOF";
  87. localuserinfo USER [USER...]
  88. List fullname for each user
  89. general options:
  90. --quiet | -q don't give process information to stdout
  91. --ingore-badname ignore non-existing usernames instead of failing
  92. --help | -h usage message
  93. --version | -v version number and copyright
  94. EOF
  95. }