summaryrefslogtreecommitdiff
path: root/IkiWiki/UserInfo.pm
blob: 267f5d9cd980d41b21750072bd92652564e2479a (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Storable;
  5. use IkiWiki;
  6. package IkiWiki;
  7. sub userinfo_retrieve () { #{{{
  8. my $userinfo=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  9. return $userinfo;
  10. } #}}}
  11. sub userinfo_store ($) { #{{{
  12. my $userinfo=shift;
  13. my $oldmask=umask(077);
  14. my $ret=Storable::lock_store($userinfo, "$config{wikistatedir}/userdb");
  15. umask($oldmask);
  16. return $ret;
  17. } #}}}
  18. sub userinfo_get ($$) { #{{{
  19. my $user=shift;
  20. my $field=shift;
  21. my $userinfo=userinfo_retrieve();
  22. if (! defined $userinfo ||
  23. ! exists $userinfo->{$user} || ! ref $userinfo->{$user} ||
  24. ! exists $userinfo->{$user}->{$field}) {
  25. return "";
  26. }
  27. return $userinfo->{$user}->{$field};
  28. } #}}}
  29. sub userinfo_set ($$$) { #{{{
  30. my $user=shift;
  31. my $field=shift;
  32. my $value=shift;
  33. my $userinfo=userinfo_retrieve();
  34. if (! defined $userinfo ||
  35. ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
  36. return "";
  37. }
  38. $userinfo->{$user}->{$field}=$value;
  39. return userinfo_store($userinfo);
  40. } #}}}
  41. sub userinfo_setall ($$) { #{{{
  42. my $user=shift;
  43. my $info=shift;
  44. my $userinfo=userinfo_retrieve();
  45. if (! defined $userinfo) {
  46. $userinfo={};
  47. }
  48. $userinfo->{$user}=$info;
  49. return userinfo_store($userinfo);
  50. } #}}}
  51. sub is_admin ($) { #{{{
  52. my $user_name=shift;
  53. return grep { $_ eq $user_name } @{$config{adminuser}};
  54. } #}}}
  55. sub get_banned_users () { #{{{
  56. my @ret;
  57. my $userinfo=userinfo_retrieve();
  58. foreach my $user (keys %{$userinfo}) {
  59. push @ret, $user if $userinfo->{$user}->{banned};
  60. }
  61. return @ret;
  62. } #}}}
  63. sub set_banned_users (@) { #{{{
  64. my %banned=map { $_ => 1 } @_;
  65. my $userinfo=userinfo_retrieve();
  66. foreach my $user (keys %{$userinfo}) {
  67. $userinfo->{$user}->{banned} = $banned{$user};
  68. }
  69. return userinfo_store($userinfo);
  70. } #}}}
  71. sub commit_notify_list ($@) { #{{{
  72. my $committer=shift;
  73. my @pages;
  74. foreach my $file (@_) {
  75. push @pages, grep { $pagesources{$_} eq $file } keys %pagesources;
  76. }
  77. my @ret;
  78. my $userinfo=userinfo_retrieve();
  79. foreach my $user (keys %{$userinfo}) {
  80. next if $user eq $committer;
  81. if (exists $userinfo->{$user}->{subscriptions} &&
  82. length $userinfo->{$user}->{subscriptions} &&
  83. exists $userinfo->{$user}->{email} &&
  84. length $userinfo->{$user}->{email} &&
  85. grep { pagespec_match($_, $userinfo->{$user}->{subscriptions}) } @pages) {
  86. push @ret, $userinfo->{$user}->{email};
  87. }
  88. }
  89. return @ret;
  90. } #}}}
  91. sub send_commit_mails ($$$@) { #{{{
  92. my $messagesub=shift;
  93. my $diffsub=shift;
  94. my $user=shift;
  95. my @changed_pages=@_;
  96. return unless @changed_pages;
  97. my @email_recipients=commit_notify_list($user, @changed_pages);
  98. if (@email_recipients) {
  99. # TODO: if a commit spans multiple pages, this will send
  100. # subscribers a diff that might contain pages they did not
  101. # sign up for. Should separate the diff per page and
  102. # reassemble into one mail with just the pages subscribed to.
  103. my $diff=$diffsub->();
  104. my $message=$messagesub->();
  105. my $pagelist;
  106. if (@changed_pages > 2) {
  107. $pagelist="$changed_pages[0] $changed_pages[1] ...";
  108. }
  109. else {
  110. $pagelist.=join(" ", @changed_pages);
  111. }
  112. #translators: The three variables are the name of the wiki,
  113. #translators: A list of one or more pages that were changed,
  114. #translators: And the name of the user making the change.
  115. #translators: This is used as the subject of a commit email.
  116. my $subject=sprintf(gettext("update of %s's %s by %s"),
  117. $config{wikiname}, $pagelist, $user);
  118. my $template=template("notifymail.tmpl");
  119. $template->param(
  120. wikiname => $config{wikiname},
  121. diff => $diff,
  122. user => $user,
  123. message => $message,
  124. );
  125. # Daemonize, in case the mail sending takes a while.
  126. defined(my $pid = fork) or error("Can't fork: $!");
  127. return if $pid;
  128. setsid() or error("Can't start a new session: $!");
  129. eval q{use POSIX 'setsid'};
  130. chdir '/';
  131. open STDIN, '/dev/null';
  132. open STDOUT, '>/dev/null';
  133. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  134. unlockwiki(); # don't need to keep a lock on the wiki
  135. eval q{use Mail::Sendmail};
  136. error($@) if $@;
  137. foreach my $email (@email_recipients) {
  138. sendmail(
  139. To => $email,
  140. From => "$config{wikiname} <$config{adminemail}>",
  141. Subject => $subject,
  142. Message => $template->output,
  143. );
  144. }
  145. exit 0; # daemon process done
  146. }
  147. } #}}}
  148. 1