summaryrefslogtreecommitdiff
path: root/IkiWiki/UserInfo.pm
blob: 34f05203a10f2367f7f583ce7d6365da1cd8c6d0 (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=shift;
  96. my @email_recipients=commit_notify_list($user, @changed_pages);
  97. if (@email_recipients) {
  98. # TODO: if a commit spans multiple pages, this will send
  99. # subscribers a diff that might contain pages they did not
  100. # sign up for. Should separate the diff per page and
  101. # reassemble into one mail with just the pages subscribed to.
  102. my $diff=$diffsub->();
  103. my $message=$messagesub->();
  104. my $subject="update of $config{wikiname}'s ";
  105. if (@changed_pages > 2) {
  106. $subject.="$changed_pages[0] $changed_pages[1] etc";
  107. }
  108. else {
  109. $subject.=join(" ", @changed_pages);
  110. }
  111. $subject.=" by $user";
  112. my $template=template("notifymail.tmpl");
  113. $template->param(
  114. wikiname => $config{wikiname},
  115. diff => $diff,
  116. user => $user,
  117. message => $message,
  118. );
  119. # Daemonize, in case the mail sending takes a while.
  120. eval q{use POSIX ’setsid’};
  121. chdir '/';
  122. open STDIN, '/dev/null';
  123. open STDOUT, '>/dev/null';
  124. defined(my $pid = fork) or error("Can't fork: $!");
  125. return if $pid;
  126. setsid() or error("Can't start a new session: $!");
  127. open STDERR, '>&STDOUT' or error("Can’t dup stdout: $!");
  128. eval q{use Mail::Sendmail};
  129. error($@) if $@;
  130. foreach my $email (@email_recipients) {
  131. sendmail(
  132. To => $email,
  133. From => "$config{wikiname} <$config{adminemail}>",
  134. Subject => $subject,
  135. Message => $template->output,
  136. ) or error("Failed to send update notification mail");
  137. }
  138. }
  139. } #}}}
  140. 1