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