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