Changeset 5269 for branches/Release01x00/MailerContrib/lib/Foswiki/Contrib/MailerContrib/WebNotify.pm
- Timestamp:
- 10/15/09 19:28:59 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Release01x00/MailerContrib/lib/Foswiki/Contrib/MailerContrib/WebNotify.pm
r3638 r5269 32 32 33 33 use strict; 34 use locale; # required for matching \w with international characters34 use locale; # required for matching \w with international characters 35 35 36 36 use Assert; … … 64 64 Foswiki::Contrib::MailerContrib::initContrib(); 65 65 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} = ''; 69 69 $this->{posttext} = ''; 70 $this->{session} = $session;70 $this->{session} = $session; 71 71 $this->{noexpandgroups} = $noexpandgroups; 72 72 73 if ( Foswiki::Func::topicExists( $web, $topic )) {73 if ( Foswiki::Func::topicExists( $web, $topic ) ) { 74 74 $this->_load(); 75 75 } … … 89 89 sub writeWebNotify { 90 90 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 ); 96 93 } 97 94 … … 112 109 113 110 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); 117 113 $this->{subscribers}{$name} = $subscriber; 118 114 } … … 128 124 129 125 sub getSubscribers { 130 my ( $this) = @_;131 132 return keys %{ $this->{subscribers}};126 my ($this) = @_; 127 128 return keys %{ $this->{subscribers} }; 133 129 } 134 130 … … 147 143 my ( $this, $name, $topics, $depth, $opts ) = @_; 148 144 149 ASSERT( defined($opts) && $opts =~ /^\d*$/) if DEBUG;145 ASSERT( defined($opts) && $opts =~ /^\d*$/ ) if DEBUG; 150 146 151 147 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) { 156 152 @names = (); 157 while ( $it->hasNext() ) {153 while ( $it->hasNext() ) { 158 154 my $member = $it->next(); 159 155 push( @names, $member ); 160 156 } 161 157 } 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 }; 166 163 } 167 164 } … … 169 166 170 167 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); 175 173 } 176 174 } … … 191 189 my ( $this, $name, $topics, $depth ) = @_; 192 190 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) { 198 196 @names = (); 199 while ( $it->hasNext() ) {197 while ( $it->hasNext() ) { 200 198 my $member = $it->next(); 201 199 push( @names, $member ); 202 200 } 203 201 } 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 }; 208 207 } 209 208 } … … 211 210 212 211 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); 217 217 } 218 218 } … … 235 235 my $page = ''; 236 236 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} } ) { 239 239 my $subscriber = $this->{subscribers}{$name}; 240 240 $page .= $subscriber->stringify() . "\n"; 241 241 } 242 $page .= $this->{posttext} if ( !$subscribersOnly);242 $page .= $this->{posttext} if ( !$subscribersOnly ); 243 243 244 244 return $page; … … 262 262 my ( $this, $change, $db, $changeSet, $seenSet, $allSet ) = @_; 263 263 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} } ) { 271 273 my $subscriber = $this->{subscribers}{$name}; 272 274 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 ); 277 280 278 281 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 281 285 # Skip this change if the subscriber is the author 282 286 # 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 { 289 295 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 { 293 300 $seenSet->{$email}{$topic} = 294 push( @{ $changeSet->{$email}}, $change );301 push( @{ $changeSet->{$email} }, $change ); 295 302 } 296 303 } 297 304 } 298 } else { 299 $this->_emailWarn($subscriber,$name,$web); 305 } 306 else { 307 $this->_emailWarn( $subscriber, $name, $web ); 300 308 } 301 309 } … … 313 321 314 322 sub 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} } ) { 318 326 my $subscriber = $this->{subscribers}{$name}; 319 327 my $subs = $subscriber->isSubscribedTo( $topic, $db ); 320 328 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 ) ) { 323 331 my $emails = $subscriber->getEmailAddresses(); 324 if ( $emails) {332 if ($emails) { 325 333 foreach my $address (@$emails) { 326 push( @{ $allSet->{$topic}}, $address );334 push( @{ $allSet->{$topic} }, $address ); 327 335 } 328 336 } … … 340 348 sub isEmpty { 341 349 my $this = shift; 342 return ( scalar( keys %{ $this->{subscribers}} ) == 0 );350 return ( scalar( keys %{ $this->{subscribers} } ) == 0 ); 343 351 } 344 352 … … 347 355 my $this = shift; 348 356 349 my ( $meta, $text ) = Foswiki::Func::readTopic(350 $this->{web}, $this->{topic} );357 my ( $meta, $text ) = 358 Foswiki::Func::readTopic( $this->{web}, $this->{topic} ); 351 359 my $in_pre = 1; 352 $this->{pretext} = '';360 $this->{pretext} = ''; 353 361 $this->{posttext} = ''; 354 $this->{meta} = $meta; 362 $this->{meta} = $meta; 363 355 364 # join \ terminated lines 356 365 $text =~ s/\\\r?\n//gs; 357 366 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 363 376 # Main.WikiName - email@domain (legacy format) 364 377 $this->subscribe( $2, '*', 0, 0 ); 365 378 $in_pre = 0; 366 379 } 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 { 369 384 my $subscriber = $1; 385 370 386 # Get the topic list from the last bracket matched. Have to do it 371 387 # this awkward way because the email filter may contain braces 372 388 my $topics = $+; 389 373 390 # email addresses can't start with : 374 $topics = undef unless ( $topics =~ s/^://);375 $subscriber =~ s/^(['"])(.*)\1$/$2/; # remove quotes391 $topics = undef unless ( $topics =~ s/^:// ); 392 $subscriber =~ s/^(['"])(.*)\1$/$2/; # remove quotes 376 393 if ($topics) { 377 394 $this->parsePageSubscriptions( $subscriber, $topics ); 378 } else { 379 $this->subscribe($subscriber, '*', 0, 0 ); 395 } 396 else { 397 $this->subscribe( $subscriber, '*', 0, 0 ); 380 398 } 381 399 $in_pre = 0; 382 400 } 383 401 else { 384 if ( $in_pre) {402 if ($in_pre) { 385 403 $this->{pretext} .= "$baseline\n"; 386 } else { 387 $this->{posttext} .= "$baseline\n"; 404 } 405 else { 406 $this->{posttext} .= "$baseline\n"; 388 407 } 389 408 } … … 395 414 sub parsePageSubscriptions { 396 415 my ( $this, $who, $spec, $unsubscribe ) = @_; 397 416 398 417 $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 ); 401 421 if ( $ret =~ m/\S/ ) { 402 422 Foswiki::Func::writeWarning( 403 423 "Badly formatted page list at $who: $spec"); 404 return -1;424 return -1; 405 425 } 406 426 return; … … 409 429 sub _subscribeTopic { 410 430 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"; 415 436 my $opts = 0; 416 437 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 } 421 442 } 422 443 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 428 451 #TODO: howto find & report errors? 429 452 return ''; … … 433 456 # for a subscriber. 434 457 sub _emailWarn { 435 my ( $this, $subscriber, $name, $web) = @_;458 my ( $this, $subscriber, $name, $web ) = @_; 436 459 437 460 # Make sure we only warn once. Don't want to see this for every 438 461 # Topic we are notifying on. 439 unless ( defined $this->{nomail}{$name}) {462 unless ( defined $this->{nomail}{$name} ) { 440 463 $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'" ); 444 467 } 445 468 }
Note: See TracChangeset
for help on using the changeset viewer.
