Files
tyforum/script/spawn_subscriptions.pl

143 lines
4.7 KiB
Perl
Raw Permalink Normal View History

2012-04-08 16:29:38 +02:00
#!/usr/bin/perl
#------------------------------------------------------------------------------
# mwForum - Web-based discussion forum
2015-01-03 11:43:36 +01:00
# Copyright (c) 1999-2015 Markus Wichitill
2012-04-08 16:29:38 +02:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#------------------------------------------------------------------------------
use strict;
use warnings;
no warnings qw(uninitialized);
2012-04-08 16:29:38 +02:00
# Imports
use Getopt::Std ();
2023-10-06 09:58:30 +09:00
use TyfMain;
2012-04-08 16:29:38 +02:00
#------------------------------------------------------------------------------
# Get arguments
my %opts = ();
2023-10-06 08:13:33 +09:00
Getopt::Std::getopts( 'sf:p:', \%opts );
2012-04-09 03:30:40 +02:00
my $spawned = $opts{s};
2012-04-08 16:29:38 +02:00
my $forumId = $opts{f};
2023-10-06 08:13:33 +09:00
my $postId = int( $opts{p} );
2012-04-08 16:29:38 +02:00
# Init
2023-10-16 19:17:15 +09:00
my ( $m, $cfg, $lng )
= TyfMain->newShell( forumId => $forumId, spawned => $spawned );
2012-04-08 16:29:38 +02:00
exit if !$cfg->{subsInstant};
$m->dbBegin();
# Shortcuts
my $baseUrl = $cfg->{baseUrl} . $cfg->{scriptUrlPath};
2012-04-08 16:29:38 +02:00
# Get post
2023-10-06 08:13:33 +09:00
my $post = $m->fetchHash( "
2012-04-08 16:29:38 +02:00
SELECT posts.*, topics.subject
FROM posts AS posts
INNER JOIN topics AS topics
ON topics.id = posts.topicId
2023-10-06 08:13:33 +09:00
WHERE posts.id = ?",
$postId );
2012-04-08 16:29:38 +02:00
$post or $m->error('errPstNotFnd');
my $topicId = $post->{topicId};
my $boardId = $post->{boardId};
2023-10-06 08:13:33 +09:00
$m->dbToEmail( {}, $post );
2012-04-08 16:29:38 +02:00
# Get board
2023-10-06 08:13:33 +09:00
my $board = $m->fetchHash( "
SELECT * FROM boards WHERE id = ?", $boardId );
2012-04-08 16:29:38 +02:00
$board or $m->error('errBrdNotFnd');
2023-10-06 08:13:33 +09:00
my $boardTitleDeesc = $m->deescHtml( $board->{title} );
2012-04-08 16:29:38 +02:00
# Get board subscribers
2023-10-06 08:13:33 +09:00
my $boardSubscribers = $m->fetchAllHash( "
SELECT boardSubscriptions.unsubAuth, users.*
2012-04-08 16:29:38 +02:00
FROM boardSubscriptions AS boardSubscriptions
INNER JOIN users AS users
ON users.id = boardSubscriptions.userId
WHERE boardSubscriptions.boardId = :boardId
AND boardSubscriptions.instant = 1
AND users.email <> ''
AND users.dontEmail = 0",
2023-10-06 08:13:33 +09:00
{ boardId => $boardId } );
2012-04-08 16:29:38 +02:00
2023-10-06 08:13:33 +09:00
# Send to board subscribers if they still have board access
for my $subscriber (@$boardSubscribers) {
next if !$m->boardVisible( $board, $subscriber );
$lng = $m->setLanguage( $subscriber->{language} );
my $subject = "$lng->{subSubjBrdIn}: $boardTitleDeesc";
2023-10-16 19:17:15 +09:00
my $body
= $lng->{subNoReply} . "\n\n"
. "-" x 70 . "\n\n"
. $lng->{subLink}
. "$baseUrl/topic_show$m->{ext}?pid=$post->{id}\n"
. $lng->{subBoard}
. $boardTitleDeesc . "\n"
. $lng->{subTopic}
. $post->{subject} . "\n"
. $lng->{subBy}
. $post->{userNameBak} . "\n"
. $lng->{subOn}
. $m->formatTime( $post->{postTime}, $subscriber->{timezone} )
. "\n\n"
. $post->{body} . "\n\n"
. ( $post->{rawBody} ? $post->{rawBody} . "\n\n" : "" )
. "-" x 70 . "\n\n"
. $lng->{subUnsubBrd} . "\n"
. "$baseUrl/user_unsubscribe$m->{ext}?t=$subscriber->{unsubAuth}\n\n";
2023-10-06 08:13:33 +09:00
$m->sendEmail( user => $subscriber, subject => $subject, body => $body );
2012-04-08 16:29:38 +02:00
}
# Get topic subscribers
2023-10-06 08:13:33 +09:00
my $topicSubscribers = $m->fetchAllHash( "
SELECT topicSubscriptions.unsubAuth, users.*
2012-04-08 16:29:38 +02:00
FROM topicSubscriptions AS topicSubscriptions
INNER JOIN users AS users
ON users.id = topicSubscriptions.userId
WHERE topicSubscriptions.topicId = :topicId
AND topicSubscriptions.instant = 1
AND users.email <> ''
AND users.dontEmail = 0",
2023-10-06 08:13:33 +09:00
{ topicId => $topicId } );
2012-04-08 16:29:38 +02:00
2023-10-06 08:13:33 +09:00
# Send to topic subscribers if they still have board access
for my $subscriber (@$topicSubscribers) {
next if !$m->boardVisible( $board, $subscriber );
$lng = $m->setLanguage( $subscriber->{language} );
my $subject = "$lng->{subSubjTpcIn}: $post->{subject}";
2023-10-16 19:17:15 +09:00
my $body
= $lng->{subNoReply} . "\n\n"
. "-" x 70 . "\n\n"
. $lng->{subLink}
. "$baseUrl/topic_show$m->{ext}?pid=$post->{id}\n"
. $lng->{subBoard}
. $boardTitleDeesc . "\n"
. $lng->{subTopic}
. $post->{subject} . "\n"
. $lng->{subBy}
. $post->{userNameBak} . "\n"
. $lng->{subOn}
. $m->formatTime( $post->{postTime}, $subscriber->{timezone} )
. "\n\n"
. $post->{body} . "\n\n"
. ( $post->{rawBody} ? $post->{rawBody} . "\n\n" : "" )
. "-" x 70 . "\n\n"
. $lng->{subUnsubTpc} . "\n"
. "$baseUrl/user_unsubscribe$m->{ext}?t=$subscriber->{unsubAuth}\n\n";
2023-10-06 08:13:33 +09:00
$m->sendEmail( user => $subscriber, subject => $subject, body => $body );
2012-04-08 16:29:38 +02:00
}
# Log action and commit
2023-10-06 08:13:33 +09:00
$m->logAction( 1, 'spawn', 'subscr' );
2012-04-08 16:29:38 +02:00
$m->dbCommit();