*** /nfs/home/cnahaboo/t/TWiki20020803beta/bin/mailnotify Sun Aug 4 04:13:55 2002 --- mailnotify2.beta Tue Sep 10 15:42:04 2002 *************** *** 15,20 **** --- 15,27 ---- # GNU General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html + # Colas (http://colas.nahaboo.net) + # modifications from mailnotify script from Dec 2001 release: + # - email is now optional, is fetched from the user homepage + # - webs not beginning by a capital letter are ignored ( _default, ...) + # - no mail is sent to TWikiGuest + # - if user is a group, recurses through its members + # Set library paths in @INC, at compile time BEGIN { require './setlib.cfg'; } *************** *** 35,41 **** @weblist = grep !/^\.\.?$/, readdir DIR; closedir DIR; foreach $web ( @weblist ) { ! if( -d "$dataDir/$web" ) { processWeb( $web ); # remove obsolete .lock files --- 42,48 ---- @weblist = grep !/^\.\.?$/, readdir DIR; closedir DIR; foreach $web ( @weblist ) { ! if( -d "$dataDir/$web" && "$web" =~ /^[A-Z].*/ ) { processWeb( $web ); # remove obsolete .lock files *************** *** 60,77 **** return; } ! my @notifylist = &TWiki::getEmailNotifyList($webName); unless ( scalar @notifylist ) { ! $debug && print "- Note: Notification list is empty\n"; return; } my $emailbody = ""; my $topiclist = ""; ! my $skin = TWiki::Prefs::getPreferencesValue( "SKIN" ); ! my $text = TWiki::Store::readTemplate( "changes", $skin ); ! my $changes= TWiki::Store::readFile( "$dataDir/$webName/.changes" ); my %exclude; --- 67,83 ---- return; } ! my @notifylist = TWiki_getEmailNotifyList($webName); unless ( scalar @notifylist ) { ! $debug && print "\n"; return; } my $emailbody = ""; my $topiclist = ""; ! my $text = &TWiki::Store::readTemplate( "changes" ); ! my $changes= &TWiki::Store::readFile( "$dataDir/$webName/.changes" ); my %exclude; *************** *** 168,174 **** my $notifylist = join ', ', @notifylist; ! $text = &TWiki::Store::readTemplate( "mailnotify", $skin ); $text =~ s/%EMAILFROM%/$from/go; $text =~ s/%EMAILTO%/$notifylist/go; $text =~ s/%EMAILBODY%/$emailbody/go; --- 174,180 ---- my $notifylist = join ', ', @notifylist; ! $text = &TWiki::Store::readTemplate( "mailnotify" ); $text =~ s/%EMAILFROM%/$from/go; $text =~ s/%EMAILTO%/$notifylist/go; $text =~ s/%EMAILBODY%/$emailbody/go; *************** *** 194,196 **** --- 200,281 ---- $debug && print "- End Twiki.$webName, mail notification sent\n"; } } + + # ===================================================================== + # COLAS: this should go in TWiki.pm once stable + # Some names are changed to avoid name clashes with mod_perl + # remove the following line for TWiki.pm + + # this version handles also names of the form + # * Main.UserName + # * UserName + # and will fetch the email(s) from the User Topic + # ========================= + sub TWiki_getEmailNotifyList + { + my( $web, $topicname ) = @_; + + $topicname = $TWiki::notifyTopicname unless $topicname; + return() unless &TWiki::Store::topicExists( $web, $topicname ); + + my @list = (); + foreach (split( /\n/, &TWiki::Store::readWebTopic($web, $topicname))) { + if (/^\s\*\s[A-Za-z0-9\.]+\s+\-\s+/) { # full form: * name - email + if (!/^\s\*\s$TWiki::mainWebname[.]TWikiGuest\s/) { + push @list, $1 if (/([\w\-\.\+]+\@[\w\-\.\+]+)/); + } + } elsif (/^\s\*\s($TWiki::mainWebname[.])?([A-Z][A-Za-z0-9]+)/) { # short form: * name + my $userWikiName = $2; + foreach (TWiki_getEmailOfUser($userWikiName)) { + push @list, $_; + } + } + } + # avoid having twice the same email in the list + my %seen = (); + my @uniq = (); + foreach $item (@list) { + push (@uniq, $item) unless $seen{$item}++; + } + $debug && print "list of emails: @uniq\n"; + return( @uniq ); + } + + sub TWiki_getEmailOfUser + { + my ($userWikiName) = @_; + my @list = (); + if ( $userWikiName ne "TWikiGuest" && + &TWiki::Store::topicExists($TWiki::mainWebname, $userWikiName)) { + if ( $userWikiName =~ /Group$/ ) { + # group page, recurse through users + $debug && print "using group: $TWiki::mainWebname . $userWikiName\n"; + foreach (split( /\n/, &TWiki::Store::readWebTopic($TWiki::mainWebname, $userWikiName))) { + if (/\s\*\sSet\s+GROUP\s+=\s+([A-Za-z0-9. \t\r]+)$/) { + foreach (split( /\s+/, $1)) { + $_ =~ s/$TWiki::mainWebname\.//; + foreach (TWiki_getEmailOfUser($_)) { + push @list, $_; + } + } + } + } + } else { + $debug && print "reading homepage: $TWiki::mainWebname . $userWikiName\n"; + foreach (split( /\n/, &TWiki::Store::readWebTopic($TWiki::mainWebname, $userWikiName))) { + if (/^\s\*\sEmail:\s+([\w\-\.\+]+\@[\w\-\.\+]+)/) { # Email field + push @list, $1; + } + } + } + } + return( @list ); + } + + # END of TWiki.pm code + #====================================================================== + + # todo for definitive inclusion into the main distrib: + # * move the above into lib/TWiki.pm + # * change in the above TWiki_ prefixes to nothing + # * in the rest of this script, replace TWiki_ by TWiki::