summaryrefslogtreecommitdiff
path: root/localuserinfo
blob: 3a4e63aab5921af2fa70e7126799e85f5655e4e5 (plain)
  1. #!/usr/bin/perl -wT
  2. my $ID = q$Id: localuserinfo,v 1.7 2007-02-07 15:08:09 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 suppress infochunks
  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_username = 1;
  32. my $include_fullname = 1;
  33. my $include_office = 1;
  34. my $include_workphone = 1;
  35. my $include_homephone = 1;
  36. my $include_other = 0;
  37. my $include_addresshints = 1;
  38. my $include_mail = 0;
  39. our $custom_template;
  40. our @names;
  41. # Resolve version number from CVS id
  42. my $version = join (' ', (split (' ', $ID))[1..3]);
  43. $version =~ s/,v\b//;
  44. $version =~ s/(\S+)$/($1)/;
  45. our $maildomain_path = '/etc/mailname';
  46. our $maildomain = &get_maildomain;
  47. # Parse options, sanity checks
  48. unless (
  49. GetOptions (
  50. "quiet|q" => sub { $verbose = 0 },
  51. "username" => \$include_username,
  52. "fullname|n" => \$include_fullname,
  53. "office" => \$include_office,
  54. "workphone" => \$include_workphone,
  55. "homephone" => \$include_homephone,
  56. "other" => \$include_other,
  57. "addresshints" => \$include_addresshints,
  58. "mail|m" => \$include_mail,
  59. "custom=s" => \$custom_template,
  60. "ignore-badname" => \$ignore_badname,
  61. "help|h" => sub { &usage(); exit 0 },
  62. "version|v" => sub { &version(); exit 0 },
  63. "debug" => sub { $verbose = 2 }
  64. )
  65. ) {
  66. &usage();
  67. exit 1;
  68. }
  69. while (defined(my $arg = shift(@ARGV))) {
  70. push (@names, $arg);
  71. }
  72. # TODO: Support custom ordering and custom delimiter
  73. my @infochunks;
  74. push (@infochunks, 'username') if ($include_username);
  75. push (@infochunks, 'fullname') if ($include_fullname);
  76. push (@infochunks, 'office') if ($include_office);
  77. push (@infochunks, 'workphone') if ($include_workphone);
  78. push (@infochunks, 'homephone') if ($include_homephone);
  79. push (@infochunks, 'other') if ($include_other);
  80. push (@infochunks, 'addresshints') if ($include_addresshints);
  81. push (@infochunks, 'mailaddress') if ($include_mail);
  82. my $template = $custom_template ? $custom_template : '%' . join('% %', @infochunks) . '%';
  83. # TODO: Rewrite to batch-resolve userinfo for all users before using any
  84. while (my $username = shift @names) {
  85. my $string;
  86. my ($fullname, $office, $workphone, $homephone, $other, @addresshints) = &getuserinfo($username);
  87. my $mailaddress = "$username\@$maildomain";
  88. $string = $template;
  89. $string =~ s/\%username\%/$username/g;
  90. $string =~ s/\%fullname\%/$fullname/g;
  91. $string =~ s/\%office\%/$office/g;
  92. $string =~ s/\%workphone\%/$workphone/g;
  93. $string =~ s/\%homephone\%/$homephone/g;
  94. $string =~ s/\%other\%/$other/g;
  95. $string =~ s/\%addresshints\%/@addresshints/g;
  96. $string =~ s/\%mailaddress\%/$mailaddress/g;
  97. print "$string\n";
  98. }
  99. sub getuserinfo($) {
  100. my $username = shift;
  101. my $pw = getpwnam($username) || die "Username \"$username\" does not exist.";
  102. my ($fullname, $office, $workphone, $homephone, $other) = split /\s*,\s*/, $pw->gecos;
  103. my @addresshints;
  104. if (defined($other)) {
  105. @addresshints = grep {/^([\.[:alnum:]_-]+|\+)?@([\.[:alnum:]_-]+)?$/} split /\s+/, $other;
  106. } else {
  107. $other = "";
  108. }
  109. return ($fullname, $office, $workphone, $homephone, $other, @addresshints);
  110. }
  111. sub get_maildomain {
  112. open (MAILDOMAINFILE, "<" . $maildomain_path) || die "can’t open $maildomain_path: $!";
  113. my $string = readline(MAILDOMAINFILE) || die "can’t read $maildomain_path: $!";
  114. close (MAILDOMAINFILE);
  115. chomp($string);
  116. # FIXME: Do some sanity check of the string - ensure a single word, FQDN syntax etc.
  117. return $string;
  118. }
  119. sub version {
  120. printf ("localuserinfo version %s\n\n", $version);
  121. print <<'EOF';
  122. List fullname and/or other info for each user.
  123. Copyright (C) 2003-2007 Jonas Smedegaard <dr@jones.dk>
  124. EOF
  125. print <<'EOF';
  126. This program is free software; you can redistribute it and/or modify
  127. it under the terms of the GNU General Public License as published by
  128. the Free Software Foundation; either version 2 of the License, or (at
  129. your option) any later version.
  130. This program is distributed in the hope that it will be useful, but
  131. WITHOUT ANY WARRANTY; without even the implied warranty of
  132. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  133. General Public License, /usr/share/common-licenses/GPL, for more details.
  134. EOF
  135. }
  136. sub usage {
  137. print <<"EOF";
  138. localuserinfo USER [USER...]
  139. List fullname and/or other info for each user
  140. general options:
  141. --quiet | -q don't give process information to stdout
  142. --ignore-badname ignore non-existing usernames instead of failing
  143. --help | -h usage message
  144. --username include username
  145. --fullname | -n include fullname
  146. --office include office
  147. --workphone include work phone
  148. --homephone include home phone
  149. --other include full "other" field
  150. --addresshints include address hints: words in "other" field
  151. containing "@"
  152. --mail | -m include email address: USERNAME\@MAILDOMAIN
  153. --custom=TEMPLATE custom template, e.g. '%username% (%fullname%)'
  154. available infochunks:
  155. * username
  156. * fullname
  157. * office
  158. * workphone
  159. * homephone
  160. other
  161. * addresshints
  162. * mailaddress
  163. (all marked infosnippets are included by default,
  164. in the order listed)
  165. --version | -v version number and copyright
  166. EOF
  167. }