Gossamer Forum
Home : Products : Gossamer Forum : Discussion :

Re: [Andy] Confusing problem with cronjob and subscriptions

Quote Reply
Re: [Andy] Confusing problem with cronjob and subscriptions In reply to
subscribe.pl:

Code:

use lib '/forum/admin';
use GForum qw/$CFG/;
use GForum::Subscribe;
use Getopt::Long;
GForum::init('/forum/admin');
my %opt;
if ($ENV{REQUEST_METHOD}) {
print "Content-type: text/html\n\n";
print "This script can only be run from shell/command prompt.";
}
else {
GetOptions(
'help' => \$opt{help},
'send' => \$opt{send},
'debug' => \$opt{debug},
'file-debug=s' => \$opt{debug_file}
);
if ($opt{send}) {
GForum::Subscribe::send_all(@opt{'debug', 'debug_file'});
}
else {
usage();
}
}
...


Subscribe.pm:
Code:

package GForum::Subscribe;
use strict;
use vars qw/%TPL_CACHE $DEBUG $DEBUG_FILE $DEBUG_FH/;
use GForum qw/:user $CFG $DB/;
use GForum::Authenticate;
use GForum::Convert;
use GForum::Post;
use GForum::Forum;
use GForum::Template;
use GT::Mail;
use GT::Mail::Editor;
sub send_all {
%TPL_CACHE = ();
($DEBUG, $DEBUG_FILE) = @_;
my $sth = $DB->table('ForumSubscriber', 'User', 'Forum', 'Category')->select;
my $PostUser = $DB->table('Post' => 'User');
while (my $row = $sth->fetchrow_hashref) { # Loops through each forum for each forum subscriber
debug("Checking forum $row->{forum_id} ($row->{forum_name}) for user $row->{user_id} ($row->{user_username})") if $DEBUG;
unless (GForum::Authenticate::auth(enabled_user => $row)) {
debug("Skipping: User not enabled/validated") if $DEBUG;
next;
}
GForum::Forum::normalize($row);
$GForum::TEMPLATE_SET = $row->{user_template} || $CFG->{default_template_set} || 'default'; # GForum::Template needs this
my $pc = GT::SQL::Condition->new(
forum_id_fk => '=' => $row->{forum_id},
post_moved => IS => undef,
post_time => '>' => ($row->{subsc_last} || $row->{subsc_time})
);
my $num_posts = $DB->table('Post')->count($pc);
debug("Found $num_posts posts in this forum") if $DEBUG;
$num_posts or next;
$PostUser->select_options("ORDER BY post_time DESC");
$PostUser->select_options("LIMIT $CFG->{subscribe_email_max_posts}") if $CFG->{subscribe_email_max_posts};
my $psth = $PostUser->select(left_join => $pc);
my %info = %$row;
$info{more_posts} = $num_posts > $CFG->{subscribe_email_max_posts} if $CFG->{subscribe_email_max_posts};
$info{subscribe_email_max_posts} = $CFG->{subscribe_email_max_posts};
my $posts_shown = 0;
while (my $prow = $psth->fetchrow_hashref) {
$prow->{post_message_text} = $prow->{post_message};
GForum::Post::plain_text(\$prow->{post_message_text}, $prow);
push @{$info{post_loop}}, $prow;
last if $CFG->{subscribe_email_max_posts} and ++$posts_shown >= $CFG->{subscribe_email_max_posts};
}
GForum::Post::normalize($info{post_loop});
debug("Displaying $posts_shown" . (($num_posts > $posts_shown and $CFG->{subscribe_email_max_posts}) ? " (subscribe_email_max_posts set to $CFG->{subscribe_email_max_posts})" : "")) if $DEBUG;
my $email = _get_tpl($GForum::TEMPLATE_SET);
my $headers = $email->headers;
my %head;
while (my ($k, $v) = each %$headers) {
my $val = $v; # Copy it
$val = GForum::Template->parse("string", \%info, { string => $val });
$head{$k} = $val;
}
my $body = $email->body;
$body = GForum::Template->parse("string", \%info, { string => $body });
$CFG->{smtp_server} or $CFG->{mail_path} or die 'No mail path or SMTP server set!';
$head{To} ||= $row->{user_email};
$head{From} ||= $CFG->{admin_email};
my $mailer = GT::Mail->new(
%head,
msg => $body,
($CFG->{smtp_server} ? (smtp => $CFG->{smtp_server}) : (sendmail => $CFG->{mail_path}))
);
debug("Sending to $row->{user_email}") if $DEBUG;
local $@;

if($row->{user_username} eq "schubert"){
if (eval { $mailer->send }) {
debug("Archive of $row->{cat_name}: $row->{forum_name} successfully sent to $row->{user_username}") if $DEBUG;
$DB->table('ForumSubscriber')->update({ subsc_last => time }, { user_id_fk => $info{user_id}, forum_id_fk => $info{forum_id} });
}
else {
debug("Unable to send to $row->{user_email}: " . ($@ || $mailer->error)); # No 'if $DEBUG' - always show this
}
}
}
}
...
Subject Author Views Date
Thread Confusing problem with cronjob and subscriptions schubiman 3349 Dec 28, 2004, 4:06 AM
Thread Re: [schubiman] Confusing problem with cronjob and subscriptions
Andy 3317 Dec 28, 2004, 6:04 AM
Thread Re: [Andy] Confusing problem with cronjob and subscriptions
schubiman 3274 Dec 28, 2004, 6:08 AM
Thread Re: [schubiman] Confusing problem with cronjob and subscriptions
Andy 3304 Dec 28, 2004, 6:13 AM
Thread Re: [Andy] Confusing problem with cronjob and subscriptions
schubiman 3279 Dec 28, 2004, 6:31 AM
Thread Re: [schubiman] Confusing problem with cronjob and subscriptions
Andy 3331 Dec 28, 2004, 7:10 AM
Post Re: [Andy] Confusing problem with cronjob and subscriptions
schubiman 3265 Dec 30, 2004, 2:23 AM
Thread Re: [schubiman] Confusing problem with cronjob and subscriptions
Alex 3243 Jan 4, 2005, 8:46 AM
Post Re: [Alex] Confusing problem with cronjob and subscriptions
schubiman 3252 Jan 5, 2005, 3:32 AM