home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
FAQ
/
cgi-bin
/
discus4_00
/
source
/
notify.pl
< prev
next >
Wrap
Text File
|
2009-11-06
|
10KB
|
259 lines
# FILE: notify.pl
# DESCRIPTION: E-mail notification of posts
#-------------------------------------------------------------------------------
# DISCUS COPYRIGHT NOTICE
#
# Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
# http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------
use strict;
use vars qw($GLOBAL_OPTIONS $PARAMS $DCONF);
###
### notify_handler
###
### Called from posting mechanism, this calculates who should receive the
### e-mail notification, decides what to send, and sends it
###
sub notify_handler {
my ($post_id, $page_number, $postargs, $pagedata, $args) = @_;
return undef if $GLOBAL_OPTIONS->{'email'} != 1;
my $passed = {};
$passed->{topic_number} = $postargs->{topic_number};
$passed->{page_number} = $page_number;
$passed->{post_id} = $post_id;
$passed->{real_author} = $postargs->{real_author};
$passed->{group} = $postargs->{group};
$passed->{pagedata} = defined $pagedata ? $pagedata : GetPage($passed->{topic_number}, $page_number);
$passed->{modonly} = 1 if ($args->{queue} || $GLOBAL_OPTIONS->{send_mail_only_admins});
$passed->{mypost} = (grep($_->{number} == $post_id, @{ $pagedata->{messages} }))[0];
$passed->{mypost} = $postargs if ref $passed->{mypost} eq "";
$passed->{new_conversation} = 0 + $postargs->{new_conversation};
$passed->{message_is_queued} = 0 + $args->{queue};
dreq("mailer");
$passed->{mailcfg} = email_configuration_read();
my @postlist = map { $_->{number} } @{ $pagedata->{messages} };
$passed->{postlist} = \@postlist;
$passed->{first_level} = $passed->{pagedata}->{head}->{levels}->[1]->{level_number};
my $recipients = notify_calculate_recipients($passed);
if ($DCONF->{debug_email_notification} == 1) {
open (FILE, ">> $DCONF->{admin_dir}/data/debug-email-notification.out");
print FILE "---------------------------------------------------------\n";
print FILE "E-mail notification for topic $passed->{topic_number}, page $passed->{page_number}\n";
print FILE "Post # $post_id by $passed->{real_author}:$passed->{group}\n";
print FILE "Message starts a new conversation: ", ("no","yes")[$passed->{new_conversation}], "\n";
print FILE "Message is queued: ", ("no","yes")[$passed->{message_is_queued}], "\n";
print FILE "E-mail notifications sent to these recipients at ", scalar localtime(time), "\n";
foreach my $z (keys %{$recipients}) {
print FILE "\t$recipients->{$z}->{'user'} ($z) HTML: ", ("off","on")[$recipients->{$z}->{html}], "\n";
}
close (FILE);
}
return undef if scalar(keys(%{ $recipients })) == 0;
$passed->{queue} = 1 if $args->{queue};
my $messages = notify_generate_messages($recipients, $passed);
notify_send_messages($messages, $passed);
}
###
### notify_calculate_recipients
###
### Decides who will receive e-mail notification of a particular post
###
sub notify_calculate_recipients {
my ($passed) = @_;
dreq("authpass");
if ($DCONF->{pro} && $GLOBAL_OPTIONS->{database} == 1 && ! $passed->{message_is_queued}) {
dreq("notifypr-PRO");
return notifypr_sql_calculate_recipients($passed);
}
my $recipients = {};
$passed->{modonly} = 1 if $passed->{message_is_queued};
my $db = $passed->{modonly} ? "passwd" : "users,passwd";
my $rdata = read_account_file($db, undef, { returnformat => 'single_array' });
my $topic = $passed->{topic_number};
my $poster = undef;
if ($DCONF->{pro} && ! $passed->{new_conversation} && ! $passed->{message_is_queued}) {
dreq("notifypr-PRO");
$poster = notifypr_calculate_reply($passed);
}
my $priv = undef;
if ($passed->{message_is_queued}) {
dreq("fcn-priv");
$priv = read_privilege_file();
}
foreach my $acct (@{ $rdata }) {
next if $acct->{email} !~ /^([\w\+\-\.]+)\@([\w\+\-\.]+)$/;
next if $acct->{pass} =~ /^\*/;
my $d = join(":", $acct->{user}, $acct->{database} eq "passwd" ? "MODERATOR" : "users");
my $h = _notify_create_hash($acct->{notify});
if ($passed->{message_is_queued}) {
next if $acct->{'database'} ne "passwd";
next if ! check_topic_authorization($acct->{'user'}, $topic, $priv);
}
if ($acct->{user} eq $passed->{real_author} && (($acct->{database} eq "users" && $passed->{group} ne "MODERATOR") || ($acct->{database} eq "passwd" && $passed->{group} eq "MODERATOR"))) {
if ($h->{'0'}) {
$recipients->{ _notify_email_prepare($acct->{email}) } = _notify_create_record($acct, $h);
}
} else {
if ($h->{'00'} && $poster->{$d}) {
$recipients->{ _notify_email_prepare($acct->{email}) } = _notify_create_record($acct, $h);
}
if ($h->{$topic}->{'*'} || ($passed->{first_level} && $h->{$topic}->{$passed->{first_level}})) {
$recipients->{ _notify_email_prepare($acct->{email}) } = _notify_create_record($acct, $h);
}
}
}
return $recipients;
}
sub _notify_create_record {
my ($acct, $h) = @_;
my $r = {};
$r->{'user'} = $acct->{'user'};
$r->{'pass'} = $acct->{'pass'};
$r->{'html'} = $h->{'000'} ? 1 : 0;
$r->{'html'} = 0 if $GLOBAL_OPTIONS->{disable_html_mail} == 1;
$r->{'name'} = $acct->{'fullname'};
$r->{'name'} = "\u$acct->{user}" if ($acct->{fullname} eq "" || $acct->{fullname} eq "fullname");
return $r;
}
sub _notify_create_hash {
my ($notify) = @_;
my $out = {};
foreach my $t (split(/,/, $notify)) {
if ($t =~ m|^(\d+)/|) {
my $topic = $1;
foreach my $num ( split(/&/, $') ) {
$out->{$topic}->{$num} = 1;
}
} else {
$out->{$t}->{'*'} = 1;
}
}
return $out;
}
sub _notify_email_prepare {
my $email_in = case_lower($_[0]);
return $email_in;
}
###
### notify_generate_messages
###
### Generates all desired e-mail notification messages for recipients
###
sub notify_generate_messages {
my ($recipients, $passed) = @_;
my @msg_out = ();
my $tfile = fetch_template_file("notify");
my $pg = $passed->{pagedata};
my @m = ( $passed->{mypost} );
$pg->{head}->{param} = "Messages";
$pg->{messages} = \@m;
$pg->{general}->{messages_raw} = 0;
$pg->{general}->{skip_headers} = 1;
$pg->{general}->{secure} = 1 if ! -e "$DCONF->{message_dir}/$passed->{topic_number}";
$pg->{general}->{skip_bottom} = 1;
$pg->{general}->{no_icons_shown} = 1;
$pg->{general}->{no_rating_shown} = 1;
$pg->{general}->{no_message_icons_shown} = 1;
$pg->{levels} = $passed->{pagedata}->{head}->{levels};
$pg->{general}->{is_email_notification} = 1;
my $text = templ_int("*$passed->{topic_number}page", $pg);
my $poster_email = $passed->{'mypost'}->{email};
$poster_email = $1 if $poster_email =~ m|^mailto:(.*)|i;
foreach my $key (keys(%{ $recipients })) {
my $subst = {};
$subst->{general}->{wants_html} = $recipients->{$key}->{html};
$subst->{general}->{wants_html} = 0 if $GLOBAL_OPTIONS->{disable_html_mail};
$subst->{general}->{wants_html} = 0 if $passed->{'mailcfg'}->{'1message'} == 1;
$subst->{general}->{text_html} = $text;
$subst->{general}->{is_queued} = 1 if $passed->{queue};
$subst->{postdata} = $passed->{mypost};
$subst->{general}->{topic} = $passed->{topic_number};
$subst->{general}->{page} = $passed->{page_number};
$subst->{general}->{post} = $passed->{post_id};
$subst->{general}->{user} = $recipients->{$key}->{'user'};
$subst->{general}->{pass} = $recipients->{$key}->{'pass'};
$subst->{general}->{name} = $recipients->{$key}->{'name'};
$subst->{general}->{encpass} = crypt($recipients->{$key}->{'pass'}, "cookie");
$subst->{general}->{email} = $key;
$subst->{pagedata} = $pg;
$subst->{levels} = $pg->{levels};
$subst->{general}->{poster_email} = $poster_email;
$subst->{general}->{reply_disabled} = 1 if $passed->{mailcfg}->{'1message'} == 1;
$subst->{general}->{new_conversation} = $passed->{new_conversation};
push @msg_out, notify_format_message($subst, $tfile, $key, $recipients);
}
return \@msg_out;
}
###
### notify_format_message
###
### Uses the e-mail notification template to create the formatted message
###
sub notify_format_message {
my ($subst, $tfile, $key, $recipients) = @_;
$subst->{general}->{wants_html} = 0 if $subst->{general}->{is_queued};
$subst->{general}->{wants_html} = 0 if $GLOBAL_OPTIONS->{disable_html_mail} == 1;
my $p = {};
$p->{text} = templ_int("notify", $subst, $tfile);
$p->{text} =~ s/\+\+\+\s\w+\s\+\+\+\s\d+\s\+\+\+\s(.*?)\s\+\+\+/\[$1\]/g;
$p->{text} = char_convert($p->{text}, 1, { JSP => 1, convert_html => 2 }) if ! $subst->{general}->{wants_html};
$p->{text} =~ s/(\n\n+)/"<br>" x length($1)/ge if $subst->{general}->{wants_html};
# This doesn't truly correspond to charconvert.conf, but trust us, it looks better
$p->{text} =~ s/\\\\/\\/g;
$p->{text} =~ s/\\\{/\{/g;
$p->{text} =~ s/\\\}/\}/g;
# Remove images and embeds from HTML-formatted e-mail messages
if (! $GLOBAL_OPTIONS->{preserve_images_in_email}) {
$p->{text} =~ s/<img.*?>//gi;
$p->{text} =~ s/<embed.*?>//gi;
}
# Want to make it so there is no dot on a line by itself
$p->{text} =~ s/\s+\.\s+/ \. /g;
$p->{to} = $key;
$p->{subject} = $GLOBAL_OPTIONS->{subject_line} == 0 ? $subst->{pagedata}->{head}->{me_name} :
$GLOBAL_OPTIONS->{subject_line} == 1 && $subst->{pagedata}->{head}->{me_number} != $subst->{pagedata}->{head}->{topic_number} ? join(": ", $subst->{pagedata}->{head}->{topic_name}, $subst->{pagedata}->{head}->{me_name}) :
$GLOBAL_OPTIONS->{subject_line} == 1 ? $subst->{pagedata}->{head}->{topic_name} : read_language()->{EMAIL_NOTIFICATION_STATIC_SUBJECT};
$p->{subject} = char_convert($p->{subject}, 1, { JSP => 1, convert_html => 2 });
$p->{subject} = join(" ", read_language()->{EMAIL_TO_QUEUE_SUBJ}, $p->{subject}) if defined $subst->{general}->{is_queued};
$p->{html} = $subst->{general}->{wants_html};
return $p;
}
###
### notify_send_messages
###
### Uses mailer.pl and routines defined there to send the e-mail messages
###
sub notify_send_messages {
my ($messages, $passed) = @_;
my $par = $passed->{mailcfg};
dreq("mailer");
foreach my $msg (@{ $messages }) {
send_email_message({ to => $msg->{to}, subject => $msg->{subject}, html => $msg->{html} }, $msg->{text}, $par);
}
}
1;