1 rizwank 1.1 #!/usr/bin/perl -wT
2 #
3 # TWiki Collaboration Platform, http://TWiki.org/
4 #
5 # Copyright (C) 1999-2003 Peter Thoeny, peter@thoeny.com
6 #
7 # For licensing info read license.txt file in the TWiki root.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details, published at
17 # http://www.gnu.org/copyleft/gpl.html
18
19 # Colas (http://colas.nahaboo.net)
20 # modifications from mailnotify script from Dec 2001 release:
21 # - email is now optional, is fetched from the user homepage
22 rizwank 1.1 # - webs not beginning by a capital letter are ignored ( _default, ...)
23 # - no mail is sent to TWikiGuest
24 # - if user is a group, recurses through its members
25
26 BEGIN {
27 # Set library paths in @INC at compile time
28 unshift @INC, '.';
29 require 'setlib.cfg';
30 }
31
32 use TWiki;
33 use TWiki::Net;
34
35 my $debug = ! ( @ARGV && $ARGV[0] eq "-q" );
36
37 &main();
38
39 sub main
40 {
41 &TWiki::basicInitialize();
42
43 rizwank 1.1 $debug && print "TWiki mail notification\n";
44 $debug && print "- to suppress all normal output: mailnotify -q\n";
45
46 my $dataDir = &TWiki::getDataDir();
47 opendir( DIR, "$dataDir" ) or die "could not open $dataDir";
48 @weblist = grep !/^\.\.?$/, readdir DIR;
49 closedir DIR;
50 foreach $web ( @weblist ) {
51 # Only process webs with normal names, i.e. not starting with '_'
52 if( -d "$dataDir/$web" && &TWiki::isWebName($web) ) {
53 processWeb( $web );
54
55 # remove obsolete .lock files
56 &TWiki::Store::removeObsoleteTopicLocks( $web );
57 }
58 }
59 $debug && print "End TWiki mail notification\n";
60 }
61
62 sub processWeb
63 {
64 rizwank 1.1 my( $web) = @_;
65
66 my ( $topic, $webName, $dummy, $userName, $dataDir) =
67 &TWiki::initialize( "/$web", "nobody" );
68 $dummy = ""; # to suppress warning
69
70 $debug && print "Checking TWiki.$webName\n";
71
72 if( ! &TWiki::Store::webExists( $webName ) ) {
73 print STDERR "* ERROR: TWiki mailnotify does not find web $webName\n";
74 return;
75 }
76
77 my @notifylist = TWiki::getEmailNotifyList($webName);
78 unless ( scalar @notifylist ) {
79 $debug && print "<none>\n";
80 return;
81 }
82
83 my $emailbody = "";
84 my $topiclist = "";
85 rizwank 1.1
86 my $skin = TWiki::Prefs::getPreferencesValue( "SKIN" );
87 my $text = TWiki::Store::readTemplate( "changes", $skin );
88 my $changes= &TWiki::Store::readFile( "$dataDir/$webName/.changes" );
89
90 my %exclude;
91
92 $text = &TWiki::handleCommonTags($text, $topic);
93 $text =~ s/\%META{.*?}\%//go; # remove %META{"parent"}%
94
95 if( $TWiki::doRemoveImgInMailnotify ) {
96 # change images to [alt] text if there, else remove image
97 $text =~ s/<img src=.*?alt=\"([^\"]+)[^>]*>/[$1]/goi;
98 $text =~ s/<img src=.*?[^>]>//goi;
99 }
100
101 my $before = "";
102 my $after = "";
103 ( $before, $text, $after) = split( /%REPEAT%/, $text );
104 $emailbody = &TWiki::Render::getRenderedVersion( $before );
105 $after = &TWiki::Render::getRenderedVersion( $after );
106 rizwank 1.1
107 my $prevLastmodify = &TWiki::Store::readFile( "$dataDir/$webName/.mailnotify" ) || "0";
108 my $currLastmodify = "";
109 my $scriptSuffix = $TWiki::scriptSuffix;
110 my $scriptUrlPath = $TWiki::scriptUrlPath;
111 my $scriptUrl = "$TWiki::urlHost$scriptUrlPath";
112 my $frev = "";
113
114 foreach( reverse split( /\n/, $changes ) ) {
115 # Parse lines from .changes:
116 # <topic> <user> <change time> <revision>
117 # WebHome FredBloggs 1014591347 21
118
119 my ($topicName, $userName, $changeTime, $revision) = split( /\t/);
120 $newText = $text; # Repeating text from email template
121
122 if( ( ! %exclude ) || ( ! $exclude{ $topicName } ) ) {
123 next unless TWiki::Store::topicExists( $webName, $topicName );
124
125 if( ! $currLastmodify ) {
126 # newest entry
127 rizwank 1.1 $time = &TWiki::formatTime( $prevLastmodify );
128 if( $prevLastmodify eq $changeTime ) {
129 # newest entry is same as at time of previous notification
130 $debug && print "- Note: No topics changed since $time\n";
131 return;
132 }
133 $currLastmodify = $changeTime;
134 $debug && print "- Changed topics since $time: ";
135 }
136
137 if( $prevLastmodify >= $changeTime ) {
138 #print "Date: found item of last notification\n";
139 # found item of last notification
140 last;
141 }
142 $frev = "";
143 if( $revision ) {
144 if( $revision > 1 ) {
145 $frev = "r1.$revision";
146 } else {
147 $frev = "<b>NEW</b>";
148 rizwank 1.1 }
149 }
150
151 # Create entry in HTML attachment
152 $newText = $text;
153 $newText =~ s/%TOPICNAME%/$topicName/go;
154 $wikiuser = &TWiki::userToWikiName( $userName );
155
156 $newText =~ s/%AUTHOR%/$wikiuser/go;
157 $newText =~ s/%LOCKED%//go;
158 $time = &TWiki::formatTime( $changeTime );
159 $newText =~ s/%TIME%/$time/go;
160 $newText =~ s/%REVISION%/$frev/go;
161 $newText = &TWiki::Render::getRenderedVersion( $newText );
162
163 $head = &TWiki::Store::readFileHead( "$dataDir\/$webName\/$topicName.txt", 16 );
164 $head = &TWiki::makeTopicSummary( $head, $topicName, $webName );
165 $newText =~ s/%TEXTHEAD%/$head/go;
166
167 $emailbody .= $newText;
168 $exclude{ $topicName } = "1";
169 rizwank 1.1
170 $debug && print "$topicName ";
171
172 # URL-encode topic names for use of I18N topic names in plain text
173 $webNameEnc = TWiki::handleUrlEncode( $webName );
174 my $topicNameEnc = TWiki::handleUrlEncode( $topicName );
175
176 # Create entry in plain-text email body
177 $newText = "- $topicName ($wikiuser)\n $scriptUrl/view$scriptSuffix/$webNameEnc/$topicNameEnc\n";
178 $newText =~ s/Main\.//go;
179 $topiclist = "$topiclist$newText";
180 }
181 }
182
183 if( $topiclist eq "" ) {
184 $debug && print "- Note: Topic list is empty\n";
185 return;
186 }
187 $debug && print "\n";
188
189 $emailbody .= $after;
190 rizwank 1.1
191 my $from = &TWiki::Prefs::getPreferencesValue("WIKIWEBMASTER");
192
193 my $notifylist = join ', ', @notifylist;
194
195 $text = &TWiki::Store::readTemplate( "mailnotify", $skin );
196 $text =~ s/%EMAILFROM%/$from/go;
197 $text =~ s/%EMAILTO%/$notifylist/go;
198 $text =~ s/%EMAILBODY%/$emailbody/go;
199 $text =~ s/%TOPICLIST%/$topiclist/go;
200 $text =~ s/%LASTDATE%/&TWiki::formatTime($prevLastmodify)/geo;
201 $text = &TWiki::handleCommonTags( $text, $topic );
202
203 # change absolute addresses to relative ones & do some cleanup
204 $text =~ s/(href=\")$scriptUrlPath/$1..\/../goi;
205 $text =~ s/(action=\")$scriptUrlPath/$1..\/../goi;
206 $text =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois; # remove <nop> and <noautolink> tags
207
208 $debug && print "- Sending mail notification to: $notifylist\n";
209
210 my $error = &TWiki::Net::sendEmail( $text );
211 rizwank 1.1 if( $error ) {
212 print STDERR "* $error\n";
213 $debug && print "- End TWiki.$webName\n";
214
215 } else {
216 &TWiki::Store::saveFile( "$dataDir/$webName/.mailnotify", $currLastmodify );
217
218 $debug && print "- End TWiki.$webName, mail notification sent\n";
219 }
220 }
|