monitor.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2011 Alex Schroeder <alex@gnu.org>
  2. # This program is free software: you can redistribute it and/or modify it under
  3. # the terms of the GNU General Public License as published by the Free Software
  4. # Foundation, either version 3 of the License, or (at your option) any later
  5. # version.
  6. #
  7. # This program is distributed in the hope that it will be useful, but WITHOUT
  8. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License along with
  12. # this program. If not, see <http://www.gnu.org/licenses/>.
  13. use strict;
  14. use v5.10;
  15. our ($q, $Message, $HomePage, $SiteName, @MyInitVariables);
  16. our ($MonitorUser, $MonitorPassword, $MonitorHost,
  17. $MonitorFrom, $MonitorTo, $MonitorRegexp);
  18. AddModuleDescription('monitor.pl');
  19. # example settings:
  20. # $MonitorUser = 'oddmuse.wiki';
  21. # $MonitorPassword = '***secret***';
  22. # $MonitorHost = 'smpt.google.com';
  23. # $MonitorFrom = 'oddmuse.wiki@gmail.com';
  24. # $MonitorTo = 'kensanata@gmail.com';
  25. # $MonitorRegexp = '.';
  26. push(@MyInitVariables, \&MonitorInit);
  27. sub MonitorLog {
  28. $Message .= $q->p(shift);
  29. }
  30. sub MonitorInit {
  31. $MonitorTo = $MonitorFrom unless $MonitorTo;
  32. if (!$MonitorUser or !$MonitorPassword
  33. or !$MonitorHost or !$MonitorFrom) {
  34. $Message .= $q->p('Monitor extension has been installed but not configured.');
  35. }
  36. if ($q->request_method() eq 'POST'
  37. && !$q->param('Preview')
  38. && $q->url =~ /$MonitorRegexp/) {
  39. eval {
  40. MonitorSend();
  41. };
  42. if ($@) {
  43. MonitorLog("monitor error: $@");
  44. }
  45. }
  46. }
  47. sub MonitorSend {
  48. # MonitorLog("monitor send");
  49. require File::Temp;
  50. require MIME::Entity;
  51. # MonitorLog("monitor require");
  52. my $fh = File::Temp->new(SUFFIX => '.html');
  53. my $home = ScriptLink(UrlEncode($HomePage), "$SiteName: $HomePage");
  54. print $fh qq(<p>Monitor mail from $home.</p><hr />)
  55. . $q->Dump();
  56. $fh->close;
  57. # MonitorLog("monitor file");
  58. my $mail = new MIME::Entity->build(To => $MonitorTo,
  59. From => $MonitorFrom,
  60. Subject => "Oddmuse Monitor",
  61. Path => $fh,
  62. Type=> "text/html");
  63. # MonitorLog("monitor mail");
  64. eval {
  65. require Net::SMTP::TLS;
  66. my $smtp = Net::SMTP::TLS->new($MonitorHost,
  67. User => $MonitorUser,
  68. Password => $MonitorPassword);
  69. $smtp->mail($MonitorFrom);
  70. $smtp->to($MonitorTo);
  71. $smtp->data;
  72. $smtp->datasend($mail->stringify);
  73. $smtp->dataend;
  74. $smtp->quit;
  75. };
  76. MonitorLog("monitor TSL error: $@") if $@;
  77. if ($@) {
  78. require Net::SMTP::SSL;
  79. my $smtp = Net::SMTP::SSL->new($MonitorHost, Port => 465);
  80. $smtp->auth($MonitorUser, $MonitorPassword);
  81. $smtp->mail($MonitorFrom);
  82. $smtp->to($MonitorTo);
  83. $smtp->data;
  84. $smtp->datasend($mail->stringify);
  85. $smtp->dataend;
  86. $smtp->quit;
  87. }
  88. MonitorLog("monitor SSL error: $@") if $@;
  89. }