summaryrefslogtreecommitdiff
path: root/localuserinfo
blob: a5dfee6be937e3632a2cecb48c7fcfcafe206125 (plain)
  1. #!/usr/bin/perl -wT
  2. my $ID = q$Id: localuserinfo,v 1.6 2007-02-07 13:10:50 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. my $include_fullname = 1;
  32. my $include_mail = 0;
  33. our $custom_string;
  34. my $use_custom_string = 0;
  35. our @names;
  36. # Resolve version number from CVS id
  37. my $version = join (' ', (split (' ', $ID))[1..3]);
  38. $version =~ s/,v\b//;
  39. $version =~ s/(\S+)$/($1)/;
  40. our $maildomain_path = '/etc/mailname';
  41. our $maildomain = &get_maildomain;
  42. # Parse options, sanity checks
  43. unless (
  44. GetOptions (
  45. "quiet|q" => sub { $verbose = 0 },
  46. "fullname|n" => \$include_fullname,
  47. "mail|m" => \$include_mail,
  48. "custom" => \$use_custom_string,
  49. "ignore-badname" => \$ignore_badname,
  50. "help|h" => sub { &usage(); exit 0 },
  51. "version|v" => sub { &version(); exit 0 },
  52. "debug" => sub { $verbose = 2 }
  53. )
  54. ) {
  55. &usage();
  56. exit 1;
  57. }
  58. while (defined(my $arg = shift(@ARGV))) {
  59. push (@names, $arg);
  60. }
  61. # TODO: Rewrite to batch-resolve userinfo for all users before using any
  62. while (my $username = shift @names) {
  63. my $string;
  64. my ($fullname, @addresses) = &getuserinfo($username);
  65. if (defined($custom_string)) {
  66. die "Use of custom string not yet implemented!";
  67. } else {
  68. my @output;
  69. push (@output, $fullname) if ($include_fullname);
  70. push (@output, "$username\@$maildomain") if ($include_mail);
  71. $string = join(' ', @output);
  72. }
  73. print "$string\n";
  74. }
  75. sub getuserinfo($) {
  76. my $username = shift;
  77. my $pw = getpwnam($username) || die "Username \"$username\" does not exist.";
  78. my ($fullname, $office, $workphone, $homephone, $other) = split /\s*,\s*/, $pw->gecos;
  79. my @addresshints;
  80. if (defined($other)) {
  81. @addresshints = grep {/^([\.[:alnum:]_-]+|\+)?@([\.[:alnum:]_-]+)?$/} split /\s+/, $other;
  82. }
  83. return ($fullname, @addresshints);
  84. }
  85. sub get_maildomain {
  86. open (MAILDOMAINFILE, "<" . $maildomain_path) || die "can’t open $maildomain_path: $!";
  87. my $string = readline(MAILDOMAINFILE) || die "can’t read $maildomain_path: $!";
  88. close (MAILDOMAINFILE);
  89. chomp($string);
  90. # FIXME: Do some sanity check of the string - ensure a single word, FQDN syntax etc.
  91. return $string;
  92. }
  93. sub version {
  94. printf ("localuserinfo version %s\n\n", $version);
  95. print <<'EOF';
  96. List fullname and/or other info for each user.
  97. Copyright (C) 2003-2007 Jonas Smedegaard <dr@jones.dk>
  98. EOF
  99. print <<'EOF';
  100. This program is free software; you can redistribute it and/or modify
  101. it under the terms of the GNU General Public License as published by
  102. the Free Software Foundation; either version 2 of the License, or (at
  103. your option) any later version.
  104. This program is distributed in the hope that it will be useful, but
  105. WITHOUT ANY WARRANTY; without even the implied warranty of
  106. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  107. General Public License, /usr/share/common-licenses/GPL, for more details.
  108. EOF
  109. }
  110. sub usage {
  111. printf <<"EOF";
  112. localuserinfo USER [USER...]
  113. List fullname and/or other info for each user
  114. general options:
  115. --quiet | -q don't give process information to stdout
  116. --ingore-badname ignore non-existing usernames instead of failing
  117. --help | -h usage message
  118. --fullname | -n include fullname (enabled by default)
  119. --mail | -m include email address
  120. --version | -v version number and copyright
  121. EOF
  122. }