You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.4KB

  1. #!/usr/bin/perl -t
  2. # Add a user to the system wide wikilist.
  3. # This script can safely be made suid.
  4. use warnings;
  5. use strict;
  6. use English;
  7. my $remove=(@ARGV && $ARGV[0] eq '-r');
  8. my $username=getpwuid($REAL_USER_ID);
  9. if (! defined $username || ! length $username) {
  10. die "unable to determine user name for UID $REAL_USER_ID\n";
  11. }
  12. my $wikilist="/etc/ikiwiki/wikilist";
  13. if (! -e $wikilist) {
  14. die "$wikilist does not exist\n";
  15. }
  16. my $changed=0;
  17. my $seen=0;
  18. my @lines;
  19. open (my $list, "<$wikilist") || die "read $wikilist: $!";
  20. while (<$list>) {
  21. chomp;
  22. if (/^\s*([^\s]+)\s*$/) {
  23. my $user=$1;
  24. if ($user eq $username) {
  25. if (! $remove) {
  26. $seen=1;
  27. push @lines, $_;
  28. }
  29. else {
  30. $changed=1;
  31. }
  32. }
  33. else {
  34. push @lines, $_;
  35. }
  36. }
  37. else {
  38. push @lines, $_;
  39. }
  40. }
  41. if (! $seen && ! $remove) {
  42. push @lines, $username;
  43. $changed=1;
  44. }
  45. if ($changed) {
  46. close $list || die "ikiwiki-update-wikilist: error reading $list: $!\n";
  47. open ($list, ">$wikilist") || die "ikiwiki-update-wikilist: cannot write to $wikilist\n";
  48. foreach (@lines) {
  49. print $list "$_\n";
  50. }
  51. if ($remove) {
  52. print "ikiwiki-update-wikilist: removed user $username from $wikilist\n";
  53. }
  54. else {
  55. print "ikiwiki-update-wikilist: added user $username to $wikilist\n";
  56. }
  57. close $list || die "ikiwiki-update-wikilist: error writing $wikilist: $!\n";
  58. }
  59. else {
  60. print "ikiwiki-update-wikilist: no changes need to be made\n";
  61. }