view fetchnews.php @ 14:372f4e195986 draft default tip

Added article blacklist function, switch to binary-safe latin1 character set for database
author Ivo Smits <Ivo@UCIS.nl>
date Thu, 17 Jul 2014 23:24:17 +0200
parents cccd73f72bf6
children
line wrap: on
line source

#!/usr/bin/php
<?php
/* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved.
   Redistribution and use in source and binary forms, with or without modification, are
   permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, this list of
      conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice, this list
      of conditions and the following disclaimer in the documentation and/or other materials
      provided with the distribution.

   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

   The views and conclusions contained in the software and documentation are those of the
   authors and should not be interpreted as representing official policies, either expressed
   or implied, of Ivo Smits.*/

chdir(__DIR__);
require_once './common.php';

foreach ($db->evalAllAssoc('SELECT * FROM `peers`') as $peer) {
	if (!$peer['enabled']) continue;
	$socket = stream_socket_client($peer['address']);
	if ($socket === FALSE) {
		print("Could not connect to peer $peer[address]\n");
		continue;
	}
	$line = nntp_readline($socket);
	$code = strtok($line, " \t");
	if ($code == 200) {
	} else if ($code == 201) {
		$peer['post'] = 0;
	} else die("Error code $code from $peer[address]\n");
	while ($peer['post'] == 2) {
		if ($peer['lastposted'] === NULL) {
			$articles = $db->evalAllAssoc('SELECT * FROM `messages` ORDER BY `id` ASC LIMIT 10');
		} else {
			$articles = $db->evalAllAssoc('SELECT * FROM `messages` WHERE `id` > ? ORDER BY `id` ASC LIMIT 10', $peer['lastposted']);
		}
		if (!count($articles)) break;
		foreach ($articles as $article) {
			nntp_writeline($socket, 'IHAVE <'.$article['messageid'].'>');
			$line = nntp_readline($socket);
			$code = strtok($line, " \t");
			if ($code == 435) { //Duplicate
			} else if ($code == 335) { //Please send
				foreach (explode("\r\n", $article['header']) as $line) nntp_writeline_data($socket, $line);
				nntp_writeline($socket, '');
				foreach (explode("\r\n", $article['body']) as $line) nntp_writeline_data($socket, $line);
				nntp_writeline($socket, '.');
				$line = nntp_readline($socket);
				$code = strtok($line, " \t");
				if ($code != 240 && $code != 235) print("Article $article[messageid] was not accepted ($code)\n");
			} else {
				print("IHAVE rejected by remote server, falling back to POST\n");
				$peer['post'] = 1;
				break;
			}
			if ($article['id'] > $peer['lastposted']) $peer['lastposted'] = $article['id'];
		}
		$db->update('UPDATE `peers` SET `lastposted` = ? WHERE `id` = ?', array($peer['lastposted'], $peer['id']));
	}
	nntp_writeline($socket, 'MODE READER');
	$line = nntp_readline($socket);
	foreach ($db->evalAllAssoc('SELECT * FROM `peergroups` WHERE `peer` = ?', $peer['id']) as $peergroup) {
		$group = $db->evalRowAssoc('SELECT * FROM `groups` WHERE `id` = ?', $peergroup['group']);
		nntp_writeline($socket, 'GROUP '.$group['name']);
		$line = nntp_readline($socket);
		$code = strtok($line, " \t");
		if ($code != 211) die("Error code $code from $peer[address]\n");
		strtok(" \t"); //number of articles
		$low = strtok(" \t");
		$high = strtok(" \t");
		strtok(" \t"); //group name
		if ($low != $peergroup['low'] || $high != $peergroup['high'] || $peergroup['low'] === NULL || $peergroup['high'] === NULL) {
			for ($i = $low; $i <= $high; $i++) {
				if ($i >= $peergroup['low'] && $i <= $peergroup['high'] && $peergroup['low'] !== NULL && $peergroup['high'] !== NULL) continue;
				nntp_writeline($socket, 'STAT '.$i);
				$line = nntp_readline($socket);
				$code = strtok($line, " \t");
				if ($code == 423) {
					print("Gap in article numbering at $i\n");
					continue;
				}
				if ($code != 223) die("Error code $code from $peer[address]\n");
				strtok(" \t"); //article number
				$messageid = strtok(" \t");
				if ($messageid[0] != '<' || $messageid[strlen($messageid)-1] != '>') die("Malformed message ID $messageid\n");
				$messageid = substr($messageid, 1, -1);
				$message = $db->evalRowAssoc('SELECT * FROM `messages` WHERE `messageid` = ?', $messageid);
				if ($message) {
				} else if (!nntp_article_wanted($messageid)) {
					print("Ignoring filtered article $messageid\n");
				} else {
					nntp_writeline($socket, 'ARTICLE '.$i);
					$line = nntp_readline($socket);
					$code = strtok($line, " \t");
					if ($code != 220) die("Error code $code from $peer[address]\n");
					strtok(" \t"); //article number
					$lines = nntp_readlines($socket);
					try {
						nntp_article_store($lines);
					} catch (Exception $ex) {
						writelog($ex->getMessage());
					}
				}
			}
			$db->update('UPDATE `peergroups` SET `low` = ?, `high` = ? WHERE `peer` = ? AND `group` = ?', array($low, $high, $peergroup['peer'], $peergroup['group']));
		}
	}
	while ($peer['post'] == 1) {
		if ($peer['lastposted'] === NULL) {
			$articles = $db->evalAllAssoc('SELECT * FROM `messages` ORDER BY `id` ASC LIMIT 5');
		} else {
			$articles = $db->evalAllAssoc('SELECT * FROM `messages` WHERE `id` > ? ORDER BY `id` ASC LIMIT 5', $peer['lastposted']);
		}
		if (!count($articles)) break;
		foreach ($articles as $article) {
			nntp_writeline($socket, 'STAT <'.$article['messageid'].'>');
			$line = nntp_readline($socket);
			$code = strtok($line, " \t");
			if ($code == 501) { //Argument error
				print("STAT rejected by remote server, skipping message\n");
			} elseif ($code == 223) { //Exists
			} elseif ($code == 430) { //Not found
				nntp_writeline($socket, 'POST');
				$line = nntp_readline($socket);
				$code = strtok($line, " \t");
				if ($code != 340) die("Error code $code from $peer[address]\n");
				foreach (explode("\r\n", $article['header']) as $line) nntp_writeline_data($socket, $line);
				nntp_writeline($socket, '');
				foreach (explode("\r\n", $article['body']) as $line) nntp_writeline_data($socket, $line);
				nntp_writeline($socket, '.');
				$line = nntp_readline($socket);
				$code = strtok($line, " \t");
				if ($code != 240 && $code != 235) print("Article $article[messageid] was not accepted ($code)\n");
			} else {
				die("Error code $code from $peer[address]\n");
			}
			if ($article['id'] > $peer['lastposted']) $peer['lastposted'] = $article['id'];
		}
		$db->update('UPDATE `peers` SET `lastposted` = ? WHERE `id` = ?', array($peer['lastposted'], $peer['id']));
	}
	nntp_writeline($socket, 'QUIT');
	nntp_readline($socket);
	fclose($socket);
}

function writelog($line) {
	print($line."\n");
}