# Immediate Notify Plugin for TWiki Collaboration Platform, http://TWiki.org/ # # Copyright (C) 2003 Walter Mundt, emage@spamcop.net # Copyright (C) 2003 Akkaya Consulting GmbH, jpabel@akkaya.de # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html # # ========================= # # This plugin supports immediate notification of topic saves. # # Test plugin for evaluating use of Mailer Contrib for Immediate notifications # Darren Elkerton, Oct 21 2006 # ========================= package TWiki::Plugins::ImmediateNotifyPlugin; # ========================= use vars qw( $web $topic $user $installWeb $VERSION $RELEASE $pluginName $debug %methodHandlers ); use TWiki; use TWiki::Net; use TWiki::Contrib::Mailer; # This should always be $Rev: 11200 $ so that TWiki can determine the checked-in # status of the plugin. It is used by the build automation tools, so # you should leave it alone. $VERSION = '$Rev: 1 $'; # This is a free-form string you can use to "name" your own plugin version. # It is *not* used by the build automation tools, but is reported as part # of the version number in PLUGINDESCRIPTIONS. $RELEASE = 'Dakar'; $pluginName = 'ImmediateNotifyPlugin'; # Name of this Plugin sub debug { TWiki::Func::writeDebug(@_) if $debug; } sub warning { TWiki::Func::writeWarning(@_); debug( "WARNING" . $_[0], @_[ 1 .. $#_ ] ); } # ========================= sub initPlugin { ( $topic, $web, $user, $installWeb ) = @_; # check for Plugins.pm versions if ( $TWiki::Plugins::VERSION < 1.011 ) { warning("Version mismatch between $pluginName and Plugins.pm"); return 0; } my $prefPrefix = "\U$pluginName\E_"; # Get plugin debug flag $debug = TWiki::Func::getPreferencesFlag( $prefPrefix."DEBUG" ); # Plugin correctly initialized debug("- TWiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK"); return 1; } # ========================= sub afterSaveHandler { my ( $text, $topic, $web, $error ) = @_; # This handler is called by TWiki::Store::saveTopic just after the save action. debug("- ${pluginName}::afterSaveHandler( $_[2].$_[1] )"); if ($error) { debug("- $pluginName: Unsuccessful save, not notifying..."); return; } # Read in the web notify topic for the current web to create the subscriber list my $notify = new TWiki::Contrib::MailerContrib::WebNotify( $TWiki::Plugins::SESSION, $web, $TWiki::cfg{NotifyTopicName} ); if ( $notify->isEmpty() ) { debug(" - $pluginName: $web has no subscribers"); return; # nothing to do! } # create a list of all subscribers that are subscribed to this topic # AND the mode is set to immediate mode ('#') foreach my $name ( sort keys %{$notify->{subscribers}} ) { my $subscriber = $notify->{subscribers}{$name}; my $sub = $subscriber->isSubscribedTo($topic); next unless $sub; my $mode = $sub->getMode(); next if (!defined($mode) || $mode ne '#'); unless ( $subscriber->isUnsubscribedFrom($topic)) { my $emails = $subscriber->getEmailAddresses(); if ($emails) { _sendemails($topic, $web, $emails); } } } } sub _sendemails { my ($topic, $web, $emails) = @_; my $skin = TWiki::Func::getPreferencesValue("SKIN"); my $template = TWiki::Func::readTemplate('immediatenotify',$skin); $template = TWiki::Func::expandCommonVariables($template, $topic, $web); $template =~ s/%TOPICNAME%/$topic/go; my $to_addr = ''; foreach my $address (@$emails) { $to_addr = $to_addr . $address . ", "; } $template =~ s/%EMAILTO%/$to_addr/go; debug("sending email to: $to_addr"); my $error = $TWiki::Plugins::SESSION->{net}->sendEmail($template,4); debug("Error sending email") if ($error); } 1;