diff fetchnews.php @ 2:40e545510a57

Added support for POSTing to the server, added readme and todo, added support for upstream synchronization using POST
author Ivo Smits <Ivo@UCIS.nl>
date Tue, 12 Apr 2011 00:29:41 +0200
parents 61fac319ca3e
children 0dcdb73cbcbf
line wrap: on
line diff
--- a/fetchnews.php	Mon Apr 11 23:17:27 2011 +0200
+++ b/fetchnews.php	Tue Apr 12 00:29:41 2011 +0200
@@ -1,4 +1,5 @@
 <?php
+chdir(__DIR__);
 require_once './pdo.php';
 require_once './config.php';
 
@@ -7,7 +8,10 @@
 	if ($socket === FALSE) die("Could not connect to peer $peer[address]\n");
 	$line = nntp_readline($socket);
 	$code = strtok($line, " \t");
-	if ($code != 200) die("Error code $code from $peer[address]\n");
+	if ($code == 200) {
+	} else if ($code == 201) {
+		$peer['post'] = 0;
+	} else die("Error code $code from $peer[address]\n");
 	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']);
@@ -65,6 +69,46 @@
 			$db->update('UPDATE `peergroups` SET `low` = ?, `high` = ? WHERE `peer` = ? AND `group` = ?', array($low, $high, $peergroup['peer'], $peergroup['group']));
 		}
 	}
+	while ($peer['post']) {
+		if ($peer['lastposted'] === NULL) {
+			$articles = $db->evalAllAssoc('SELECT * FROM `messages` LIMIT 10');
+		} else {
+			$articles = $db->evalAllAssoc('SELECT * FROM `messages` WHERE `id` > ? LIMIT 10', $peer['lastposted']);
+		}
+		if (!count($articles)) break;
+		foreach ($articles as $article) {
+			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) {
+				$parts = explode(': ', $line, 2);
+				switch (strtoupper($parts[0])) {
+					case 'PATH': case 'FROM': case 'NEWSGROUPS': case 'SUBJECT': case 'DATE': case 'ORGANIZATION':
+					case 'LINES': case 'MESSAGE-ID': case 'MIME-VERSION': case 'CONTENT-TYPE': case 'CONTENT-TRANSFER-ENCODING':
+					case 'USER-AGENT': case 'REFERENCES': case 'REPLY-TO': case 'SENDER': case 'FOLLOWUP-TO':
+					case 'EXPIRES': case 'CONTROL': case 'DISTRIBUTION': case 'KEYWORDS': case 'SUMMARY':
+					case 'IN-REPLY-TO':
+						break;
+					case 'NNTP-POSTING-HOST': case 'X-TRACE': case 'XREF': case 'X-COMPLAINTS-TO':
+					case 'NNTP-POSTING-DATE':
+						$line = NULL;
+						break;
+					default:
+						print("Sending unknown header $parts[0]\n");
+				}
+				if ($line !== NULL) nntp_writeline($socket, $line);
+			}
+			nntp_writeline($socket, '');
+			foreach (explode("\r\n", $article['body']) as $line) nntp_writeline($socket, $line);
+			nntp_writeline($socket, '.');
+			$line = nntp_readline($socket);
+			$code = strtok($line, " \t");
+			if ($code != 240) print("Article $article[messageid] was not accepted ($code)\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');
 	fclose($socket);
 }