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