summaryrefslogtreecommitdiff
path: root/IkiWiki/UserInfo.pm
blob: 03d63cc23376722cee7663828e91e69b0fb79030 (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. my $subject=sprintf(gettext("update of %s's %s by %s"),
  113. $config{wikiname}, $pagelist, $user);
  114. my $template=template("notifymail.tmpl");
  115. $template->param(
  116. wikiname => $config{wikiname},
  117. diff => $diff,
  118. user => $user,
  119. message => $message,
  120. );
  121. # Daemonize, in case the mail sending takes a while.
  122. defined(my $pid = fork) or error("Can't fork: $!");
  123. return if $pid;
  124. setsid() or error("Can't start a new session: $!");
  125. eval q{use POSIX 'setsid'};
  126. chdir '/';
  127. open STDIN, '/dev/null';
  128. open STDOUT, '>/dev/null';
  129. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  130. unlockwiki(); # don't need to keep a lock on the wiki
  131. eval q{use Mail::Sendmail};
  132. error($@) if $@;
  133. foreach my $email (@email_recipients) {
  134. sendmail(
  135. To => $email,
  136. From => "$config{wikiname} <$config{adminemail}>",
  137. Subject => $subject,
  138. Message => $template->output,
  139. );
  140. }
  141. exit 0; # daemon process done
  142. }
  143. } #}}}
  144. 1