Ignore:
Timestamp:
10/15/09 19:28:59 (3 years ago)
Author:
KennethLavrsen
Message:

Item2260: MailerContrib cannot find email templates based on skin or web
The checkin does the following

  • It makes a tiny little fix in MailerContrib.pm where I pushTopicContext before loading mail template and pop it again afterwards
  • Updates the the documentation to show by example how to tailor the mail message
  • Merge over the code from trunk. This introduces a bug fix from Item1603 which for some reason was only applied to trunk
  • And with this merge also merge over a lot of perl tinying.

It was too difficult to separate the two once I was this far. Look at next checkin in trunk to see only the actual code changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Release01x00/MailerContrib/lib/Foswiki/Contrib/MailerContrib/WebNotify.pm

    r3638 r5269  
    3232 
    3333use strict; 
    34 use locale; # required for matching \w with international characters 
     34use locale;    # required for matching \w with international characters 
    3535 
    3636use Assert; 
     
    6464    Foswiki::Contrib::MailerContrib::initContrib(); 
    6565 
    66     $this->{web} = $web; 
    67     $this->{topic} = $topic || $Foswiki::cfg{NotifyTopicName} || 'WebNotify'; 
    68     $this->{pretext} = ''; 
     66    $this->{web}      = $web; 
     67    $this->{topic}    = $topic || $Foswiki::cfg{NotifyTopicName} || 'WebNotify'; 
     68    $this->{pretext}  = ''; 
    6969    $this->{posttext} = ''; 
    70     $this->{session} = $session; 
     70    $this->{session}  = $session; 
    7171    $this->{noexpandgroups} = $noexpandgroups; 
    7272 
    73     if( Foswiki::Func::topicExists( $web, $topic )) { 
     73    if ( Foswiki::Func::topicExists( $web, $topic ) ) { 
    7474        $this->_load(); 
    7575    } 
     
    8989sub writeWebNotify { 
    9090    my $this = shift; 
    91     Foswiki::Func::saveTopicText( 
    92         $this->{web}, 
    93         $this->{topic}, 
    94         $this->stringify(), 
    95         1, 1); 
     91    Foswiki::Func::saveTopicText( $this->{web}, $this->{topic}, 
     92        $this->stringify(), 1, 1 ); 
    9693} 
    9794 
     
    112109 
    113110    my $subscriber = $this->{subscribers}{$name}; 
    114     unless ( $noAdd || defined( $subscriber )) { 
    115         $subscriber = 
    116           new Foswiki::Contrib::MailerContrib::Subscriber( $name ); 
     111    unless ( $noAdd || defined($subscriber) ) { 
     112        $subscriber = new Foswiki::Contrib::MailerContrib::Subscriber($name); 
    117113        $this->{subscribers}{$name} = $subscriber; 
    118114    } 
     
    128124 
    129125sub getSubscribers { 
    130     my ( $this ) = @_; 
    131  
    132     return keys %{$this->{subscribers}}; 
     126    my ($this) = @_; 
     127 
     128    return keys %{ $this->{subscribers} }; 
    133129} 
    134130 
     
    147143    my ( $this, $name, $topics, $depth, $opts ) = @_; 
    148144 
    149     ASSERT(defined($opts) && $opts =~ /^\d*$/) if DEBUG; 
     145    ASSERT( defined($opts) && $opts =~ /^\d*$/ ) if DEBUG; 
    150146 
    151147    my @names = ($name); 
    152     unless ($this->{noexpandgroups}) { 
    153         if (defined &Foswiki::Func::eachGroupMember) { 
    154             my $it = Foswiki::Func::eachGroupMember( $name ); 
    155             if( $it ) { 
     148    unless ( $this->{noexpandgroups} ) { 
     149        if ( defined &Foswiki::Func::eachGroupMember ) { 
     150            my $it = Foswiki::Func::eachGroupMember($name); 
     151            if ($it) { 
    156152                @names = (); 
    157                 while( $it->hasNext() ) { 
     153                while ( $it->hasNext() ) { 
    158154                    my $member = $it->next(); 
    159155                    push( @names, $member ); 
    160156                } 
    161157            } 
    162         } else { 
    163             my $user = Foswiki::User->new($this->{session}, '', $name); 
    164             if ($user->isGroup) { 
    165                 @names = map {$_->wikiName} @{$user->groupMembers}; 
     158        } 
     159        else { 
     160            my $user = Foswiki::User->new( $this->{session}, '', $name ); 
     161            if ( $user->isGroup ) { 
     162                @names = map { $_->wikiName } @{ $user->groupMembers }; 
    166163            } 
    167164        } 
     
    169166 
    170167    foreach my $n (@names) { 
    171         my $subscriber = $this->getSubscriber( $n ); 
    172         my $sub = new Foswiki::Contrib::MailerContrib::Subscription( 
    173             $topics, $depth, $opts ); 
    174         $subscriber->subscribe( $sub ); 
     168        my $subscriber = $this->getSubscriber($n); 
     169        my $sub = 
     170          new Foswiki::Contrib::MailerContrib::Subscription( $topics, $depth, 
     171            $opts ); 
     172        $subscriber->subscribe($sub); 
    175173    } 
    176174} 
     
    191189    my ( $this, $name, $topics, $depth ) = @_; 
    192190 
    193     my @names = ( $name ); 
    194     unless ($this->{noexpandgroups}) { 
    195         if (defined &Foswiki::Func::eachGroupMember) { 
    196             my $it = Foswiki::Func::eachGroupMember( $name ); 
    197             if( $it ) { 
     191    my @names = ($name); 
     192    unless ( $this->{noexpandgroups} ) { 
     193        if ( defined &Foswiki::Func::eachGroupMember ) { 
     194            my $it = Foswiki::Func::eachGroupMember($name); 
     195            if ($it) { 
    198196                @names = (); 
    199                 while( $it->hasNext() ) { 
     197                while ( $it->hasNext() ) { 
    200198                    my $member = $it->next(); 
    201199                    push( @names, $member ); 
    202200                } 
    203201            } 
    204         } else { 
    205             my $user = Foswiki::User->new($this->{session}, '', $name); 
    206             if ($user->isGroup) { 
    207                 @names = map {$_->wikiName} @{$user->groupMembers}; 
     202        } 
     203        else { 
     204            my $user = Foswiki::User->new( $this->{session}, '', $name ); 
     205            if ( $user->isGroup ) { 
     206                @names = map { $_->wikiName } @{ $user->groupMembers }; 
    208207            } 
    209208        } 
     
    211210 
    212211    foreach my $n (@names) { 
    213         my $subscriber = $this->getSubscriber( $n ); 
    214         my $sub = new Foswiki::Contrib::MailerContrib::Subscription( 
    215             $topics, $depth, 0 ); 
    216         $subscriber->unsubscribe( $sub ); 
     212        my $subscriber = $this->getSubscriber($n); 
     213        my $sub = 
     214          new Foswiki::Contrib::MailerContrib::Subscription( $topics, $depth, 
     215            0 ); 
     216        $subscriber->unsubscribe($sub); 
    217217    } 
    218218} 
     
    235235    my $page = ''; 
    236236 
    237     $page .= $this->{pretext} if (!$subscribersOnly); 
    238     foreach my $name ( sort keys %{$this->{subscribers}} ) { 
     237    $page .= $this->{pretext} if ( !$subscribersOnly ); 
     238    foreach my $name ( sort keys %{ $this->{subscribers} } ) { 
    239239        my $subscriber = $this->{subscribers}{$name}; 
    240240        $page .= $subscriber->stringify() . "\n"; 
    241241    } 
    242     $page .= $this->{posttext} if (!$subscribersOnly); 
     242    $page .= $this->{posttext} if ( !$subscribersOnly ); 
    243243 
    244244    return $page; 
     
    262262    my ( $this, $change, $db, $changeSet, $seenSet, $allSet ) = @_; 
    263263 
    264     my $topic = $change->{TOPIC}; 
    265     my $web = $change->{WEB}; 
    266     my %authors = map { $_ => 1 } 
    267       @{Foswiki::Contrib::MailerContrib::Subscriber::getEmailAddressesForUser( 
    268           $change->{author})}; 
    269  
    270     foreach my $name ( keys %{$this->{subscribers}} ) { 
     264    my $topic   = $change->{TOPIC}; 
     265    my $web     = $change->{WEB}; 
     266    my %authors = map { $_ => 1 } @{ 
     267        Foswiki::Contrib::MailerContrib::Subscriber::getEmailAddressesForUser( 
     268            $change->{author} 
     269        ) 
     270      }; 
     271 
     272    foreach my $name ( keys %{ $this->{subscribers} } ) { 
    271273        my $subscriber = $this->{subscribers}{$name}; 
    272274        my $subs = $subscriber->isSubscribedTo( $topic, $db ); 
    273         if ($subs && !$subscriber->isUnsubscribedFrom( $topic, $db )) { 
    274  
    275             next unless Foswiki::Func::checkAccessPermission( 
    276                 'VIEW', $name, undef, $topic, $this->{web}, undef ); 
     275        if ( $subs && !$subscriber->isUnsubscribedFrom( $topic, $db ) ) { 
     276 
     277            next 
     278              unless Foswiki::Func::checkAccessPermission( 'VIEW', $name, undef, 
     279                $topic, $this->{web}, undef ); 
    277280 
    278281            my $emails = $subscriber->getEmailAddresses(); 
    279             if( $emails && scalar( @$emails )) { 
    280                 foreach my $email ( @$emails ) { 
     282            if ( $emails && scalar(@$emails) ) { 
     283                foreach my $email (@$emails) { 
     284 
    281285                    # Skip this change if the subscriber is the author 
    282286                    # of the change, and we are not always sending 
    283                     next if (!($subs->{options} & $MailerConst::ALWAYS) 
    284                                && $authors{$email}); 
    285  
    286                     if ($subs->{options} & $MailerConst::FULL_TOPIC) { 
    287                         push( @{$allSet->{$topic}}, $email ); 
    288                     } else { 
     287                    next 
     288                      if ( !( $subs->{options} & $MailerConst::ALWAYS ) 
     289                        && $authors{$email} ); 
     290 
     291                    if ( $subs->{options} & $MailerConst::FULL_TOPIC ) { 
     292                        push( @{ $allSet->{$topic} }, $email ); 
     293                    } 
     294                    else { 
    289295                        my $at = $seenSet->{$email}{$topic}; 
    290                         if ( $at ) { 
    291                             $changeSet->{$email}[$at - 1]->merge( $change ); 
    292                         } else { 
     296                        if ($at) { 
     297                            $changeSet->{$email}[ $at - 1 ]->merge($change); 
     298                        } 
     299                        else { 
    293300                            $seenSet->{$email}{$topic} = 
    294                               push( @{$changeSet->{$email}}, $change ); 
     301                              push( @{ $changeSet->{$email} }, $change ); 
    295302                        } 
    296303                    } 
    297304                } 
    298             } else { 
    299                 $this->_emailWarn($subscriber,$name,$web); 
     305            } 
     306            else { 
     307                $this->_emailWarn( $subscriber, $name, $web ); 
    300308            } 
    301309        } 
     
    313321 
    314322sub processCompulsory { 
    315     my ($this, $topic, $db, $allSet) = @_; 
    316  
    317     foreach my $name ( keys %{$this->{subscribers}} ) { 
     323    my ( $this, $topic, $db, $allSet ) = @_; 
     324 
     325    foreach my $name ( keys %{ $this->{subscribers} } ) { 
    318326        my $subscriber = $this->{subscribers}{$name}; 
    319327        my $subs = $subscriber->isSubscribedTo( $topic, $db ); 
    320328        next unless $subs; 
    321         next unless ($subs->{options} & $MailerConst::ALWAYS); 
    322         unless( $subscriber->isUnsubscribedFrom( $topic, $db )) { 
     329        next unless ( $subs->{options} & $MailerConst::ALWAYS ); 
     330        unless ( $subscriber->isUnsubscribedFrom( $topic, $db ) ) { 
    323331            my $emails = $subscriber->getEmailAddresses(); 
    324             if( $emails ) { 
     332            if ($emails) { 
    325333                foreach my $address (@$emails) { 
    326                     push( @{$allSet->{$topic}}, $address ); 
     334                    push( @{ $allSet->{$topic} }, $address ); 
    327335                } 
    328336            } 
     
    340348sub isEmpty { 
    341349    my $this = shift; 
    342     return ( scalar( keys %{$this->{subscribers}} ) == 0 ); 
     350    return ( scalar( keys %{ $this->{subscribers} } ) == 0 ); 
    343351} 
    344352 
     
    347355    my $this = shift; 
    348356 
    349     my ( $meta, $text ) = Foswiki::Func::readTopic( 
    350         $this->{web}, $this->{topic} ); 
     357    my ( $meta, $text ) = 
     358      Foswiki::Func::readTopic( $this->{web}, $this->{topic} ); 
    351359    my $in_pre = 1; 
    352     $this->{pretext} = ''; 
     360    $this->{pretext}  = ''; 
    353361    $this->{posttext} = ''; 
    354     $this->{meta} = $meta; 
     362    $this->{meta}     = $meta; 
     363 
    355364    # join \ terminated lines 
    356365    $text =~ s/\\\r?\n//gs; 
    357366    my $webRE = qr/(?:$Foswiki::cfg{UsersWebName}\.)?/o; 
    358     foreach my $baseline ( split ( /\r?\n/, $text )) { 
    359         my $line = Foswiki::Func::expandCommonVariables( 
    360             $baseline, $this->{topic}, $this->{web}, $meta); 
    361         if( $line =~ /^\s+\*\s$webRE($Foswiki::regex{wikiWordRegex})\s+\-\s+($Foswiki::cfg{MailerContrib}{EmailFilterIn}+)\s*$/o 
    362               && $1 ne $Foswiki::cfg{DefaultUserWikiName}) { 
     367    foreach my $baseline ( split( /\r?\n/, $text ) ) { 
     368        my $line = 
     369          Foswiki::Func::expandCommonVariables( $baseline, $this->{topic}, 
     370            $this->{web}, $meta ); 
     371        if ( $line =~ 
     372/^\s+\*\s$webRE($Foswiki::regex{wikiWordRegex})\s+\-\s+($Foswiki::cfg{MailerContrib}{EmailFilterIn}+)\s*$/o 
     373            && $1 ne $Foswiki::cfg{DefaultUserWikiName} ) 
     374        { 
     375 
    363376            # Main.WikiName - email@domain (legacy format) 
    364377            $this->subscribe( $2, '*', 0, 0 ); 
    365378            $in_pre = 0; 
    366379        } 
    367         elsif ( $line =~ /^\s+\*\s$webRE($Foswiki::regex{wikiWordRegex}|'.*?'|".*?"|$Foswiki::cfg{MailerContrib}{EmailFilterIn})\s*(:.*)?$/o 
    368                   && $1 ne $Foswiki::cfg{DefaultUserWikiName}) { 
     380        elsif ( $line =~ 
     381/^\s+\*\s$webRE($Foswiki::regex{wikiWordRegex}|'.*?'|".*?"|$Foswiki::cfg{MailerContrib}{EmailFilterIn})\s*(:.*)?$/o 
     382            && $1 ne $Foswiki::cfg{DefaultUserWikiName} ) 
     383        { 
    369384            my $subscriber = $1; 
     385 
    370386            # Get the topic list from the last bracket matched. Have to do it 
    371387            # this awkward way because the email filter may contain braces 
    372388            my $topics = $+; 
     389 
    373390            # email addresses can't start with : 
    374             $topics = undef unless ($topics =~ s/^://); 
    375             $subscriber =~ s/^(['"])(.*)\1$/$2/; # remove quotes 
     391            $topics = undef unless ( $topics =~ s/^:// ); 
     392            $subscriber =~ s/^(['"])(.*)\1$/$2/;    # remove quotes 
    376393            if ($topics) { 
    377394                $this->parsePageSubscriptions( $subscriber, $topics ); 
    378             } else { 
    379                 $this->subscribe($subscriber, '*', 0, 0 ); 
     395            } 
     396            else { 
     397                $this->subscribe( $subscriber, '*', 0, 0 ); 
    380398            } 
    381399            $in_pre = 0; 
    382400        } 
    383401        else { 
    384             if( $in_pre ) { 
     402            if ($in_pre) { 
    385403                $this->{pretext} .= "$baseline\n"; 
    386             } else { 
    387                 $this->{posttext} .=  "$baseline\n"; 
     404            } 
     405            else { 
     406                $this->{posttext} .= "$baseline\n"; 
    388407            } 
    389408        } 
     
    395414sub parsePageSubscriptions { 
    396415    my ( $this, $who, $spec, $unsubscribe ) = @_; 
    397      
     416 
    398417    $this->{topicSub} = \&_subscribeTopic; 
    399      
    400     my $ret = Foswiki::Contrib::MailerContrib::parsePageList($this, $who, $spec, $unsubscribe); 
     418 
     419    my $ret = Foswiki::Contrib::MailerContrib::parsePageList( 
     420        $this, $who, $spec, $unsubscribe ); 
    401421    if ( $ret =~ m/\S/ ) { 
    402422        Foswiki::Func::writeWarning( 
    403423            "Badly formatted page list at $who: $spec"); 
    404         return -1; 
     424        return -1; 
    405425    } 
    406426    return; 
     
    409429sub _subscribeTopic { 
    410430    my ( $this, $who, $unsubscribe, $webTopic, $options, $childDepth ) = @_; 
    411      
    412     my ($web, $topic) = Foswiki::Func::normalizeWebTopicName($this->{web}, $webTopic); 
    413      
    414 #print STDERR "_subscribeTopic($topic)\n"; 
     431 
     432    my ( $web, $topic ) = 
     433      Foswiki::Func::normalizeWebTopicName( $this->{web}, $webTopic ); 
     434 
     435    #print STDERR "_subscribeTopic($topic)\n"; 
    415436    my $opts = 0; 
    416437    if ($options) { 
    417         $opts |= $MailerConst::FULL_TOPIC; 
    418         if ($options =~ /!/) { 
    419             $opts |= $MailerConst::ALWAYS; 
    420         } 
     438        $opts |= $MailerConst::FULL_TOPIC; 
     439        if ( $options =~ /!/ ) { 
     440            $opts |= $MailerConst::ALWAYS; 
     441        } 
    421442    } 
    422443    my $kids = $childDepth or 0; 
    423     if ( $unsubscribe && $unsubscribe eq '-') { 
    424         $this->unsubscribe( $who, $topic, $kids ); 
    425     } else { 
    426         $this->subscribe( $who, $topic, $kids, $opts ); 
    427     } 
     444    if ( $unsubscribe && $unsubscribe eq '-' ) { 
     445        $this->unsubscribe( $who, $topic, $kids ); 
     446    } 
     447    else { 
     448        $this->subscribe( $who, $topic, $kids, $opts ); 
     449    } 
     450 
    428451    #TODO: howto find & report errors? 
    429452    return ''; 
     
    433456# for a subscriber. 
    434457sub _emailWarn { 
    435     my ($this, $subscriber, $name, $web) = @_; 
     458    my ( $this, $subscriber, $name, $web ) = @_; 
    436459 
    437460    # Make sure we only warn once. Don't want to see this for every 
    438461    # Topic we are notifying on. 
    439     unless (defined $this->{nomail}{$name}) { 
     462    unless ( defined $this->{nomail}{$name} ) { 
    440463        $this->{nomail}{$name} = 1; 
    441         Foswiki::Func::writeWarning( 
    442             "Failed to find permitted email for '". 
    443               $subscriber->stringify()."' when processing web '$web'"); 
     464        Foswiki::Func::writeWarning( "Failed to find permitted email for '" 
     465              . $subscriber->stringify() 
     466              . "' when processing web '$web'" ); 
    444467    } 
    445468} 
Note: See TracChangeset for help on using the changeset viewer.