125 lines
4.9 KiB
PHP
125 lines
4.9 KiB
PHP
<?php
|
|
/*
|
|
* spoolnews NNTP news spool creator
|
|
* Download: https://news.novabbs.com/getrslight
|
|
*
|
|
* E-Mail: retroguy@novabbs.com
|
|
* Web: https://news.novabbs.com
|
|
*
|
|
* 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 2 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.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
*/
|
|
set_time_limit(900);
|
|
|
|
include "config.inc.php";
|
|
include("$file_newsportal");
|
|
|
|
if ($CONFIG['remote_server'] == '') {
|
|
exit();
|
|
}
|
|
$logfile = $logdir . '/send.log';
|
|
|
|
@mkdir($spooldir . "/" . $config_name, 0755, 'recursive');
|
|
|
|
$lockfile = $lockdir . '/rslight-send.lock';
|
|
$pid = file_get_contents($lockfile);
|
|
if (posix_getsid($pid) === false || ! is_file($lockfile)) {
|
|
print "Starting Send...\n";
|
|
file_put_contents($lockfile, getmypid()); // create lockfile
|
|
} else {
|
|
print "Send currently running\n";
|
|
exit();
|
|
}
|
|
$ns = nntp2_open($CONFIG['remote_server'], $CONFIG['remote_port']);
|
|
if ($ns == false) {
|
|
file_put_contents($logfile, "\n" . format_log_date() . " " . $config_name . " Failed to connect to " . $CONFIG['remote_server'] . ":" . $CONFIG['remote_port'], FILE_APPEND);
|
|
exit();
|
|
}
|
|
echo "\nPosting articles\r\n";
|
|
post_articles($ns, $spooldir);
|
|
nntp_close($ns);
|
|
unlink($lockfile);
|
|
echo "\nSend Done\r\n";
|
|
|
|
function post_articles($ns, $spooldir)
|
|
{
|
|
global $CONFIG, $OVERRIDES, $logfile, $debug_log, $config_name;
|
|
$outgoing_dir = $spooldir . "/" . $config_name . "/outgoing/";
|
|
$fail_dir = $outgoing_dir . '/failed/';
|
|
|
|
if (!is_dir($fail_dir)) {
|
|
mkdir($fail_dir);
|
|
}
|
|
if (! is_dir($outgoing_dir)) {
|
|
mkdir($outgoing_dir);
|
|
return "No messages to send\r\n";
|
|
}
|
|
|
|
// Check overrides.inc.php if this section is disabled for remote push
|
|
if (isset($OVERRIDES['disable_remote_push'])) {
|
|
foreach ($OVERRIDES['disable_remote_push'] as $disable) {
|
|
if ($config_name == $disable) {
|
|
file_put_contents($debug_log, "\n" . format_log_date() . " Remote push disabled for " . $config_name . " by override", FILE_APPEND);
|
|
return "Remote push disabled for this section: " . $config_name;
|
|
}
|
|
}
|
|
}
|
|
|
|
$messages = scandir($outgoing_dir);
|
|
foreach ($messages as $message) {
|
|
if (! is_file($outgoing_dir . $message)) {
|
|
continue;
|
|
}
|
|
if (filemtime($outgoing_dir . $message) < (time() - 14400)) { // Stop trying to send article of over 4 hours old
|
|
file_put_contents($logfile, "\n" . format_log_date() . " " . $config_name . " POST Failed: Too many retries, giving up for: " . $message, FILE_APPEND);
|
|
rename($outgoing_dir . $message, $fail_dir . $message);
|
|
continue;
|
|
}
|
|
echo "Sending: " . $outgoing_dir . $message . "\r\n";
|
|
fputs($ns, "MODE READER\r\n");
|
|
$response = line_read($ns);
|
|
if (strcmp(substr($response, 0, 3), "200") != 0) {
|
|
file_put_contents($logfile, "\n" . format_log_date() . " " . $config_name . " Unexpected response to MODE command: " . $response, FILE_APPEND);
|
|
return $response;
|
|
}
|
|
fputs($ns, "POST\r\n");
|
|
$response = line_read($ns);
|
|
if (strcmp(substr($response, 0, 3), "340") != 0) {
|
|
file_put_contents($logfile, "\n" . format_log_date() . " " . $config_name . " Unexpected response to POST command: " . $response, FILE_APPEND);
|
|
return $response;
|
|
}
|
|
$message_fp = fopen($outgoing_dir . $message, "rb");
|
|
while (($msgline = fgets($message_fp, 4096)) !== false) {
|
|
fputs($ns, $msgline);
|
|
}
|
|
fputs($ns, ".\r\n");
|
|
fclose($message_fp);
|
|
$response = line_read($ns);
|
|
if (strcmp(substr($response, 0, 3), "441") == 0) { // Posting failed
|
|
if (strcmp(substr($response, 0, 6), "441 43") == 0) { // Article specifically rejected. Move to 'failed'
|
|
rename($outgoing_dir . $message, $fail_dir . $message);
|
|
}
|
|
}
|
|
if (strcmp(substr($response, 0, 3), "240") == 0) {
|
|
$removed = unlink($outgoing_dir . $message);
|
|
file_put_contents($logfile, "\n" . format_log_date() . " " . $config_name . " POSTED: " . $message . ": " . $response, FILE_APPEND);
|
|
} else {
|
|
file_put_contents($logfile, "\n" . format_log_date() . " " . $config_name . " POST Failed: " . $message . ": " . $response, FILE_APPEND);
|
|
continue;
|
|
}
|
|
}
|
|
prune_dir_by_days($fail_dir, 7);
|
|
return "Messages sent\r\n";
|
|
}
|