diff server.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 d7ab68b71c74
children 0dcdb73cbcbf
line wrap: on
line diff
--- a/server.php	Mon Apr 11 23:17:27 2011 +0200
+++ b/server.php	Tue Apr 12 00:29:41 2011 +0200
@@ -1,6 +1,6 @@
 #!/usr/bin/php
 <?php
-chdir('/home/ivo/projects/pnewss');
+chdir(__DIR__);
 require_once './pdo.php';
 require_once './config.php';
 
@@ -24,7 +24,7 @@
 nntp_writeline(STDOUT, '200 pNewss ready');
 while (TRUE) {
 	$line = nntp_readline(STDIN);
-	if ($line === FALSE || $line === NULL) break;	
+	if ($line === FALSE || $line === NULL) break;
 	$cmd = strtok($line, " \t");
 	switch (strtoupper($cmd)) {
 		case 'LIST':
@@ -116,6 +116,66 @@
 				nntp_writeline(STDOUT, '223 '.$article['number'].' <'.$articlea['messageid'].'> ok');
 			}
 			break;
+		case 'POST':
+			nntp_writeline(STDOUT, '340 Ok, recommended message-ID <'.md5(time().rand()).'@pNews.Core.UCIS.nl>');
+			$lines = nntp_readlines(STDIN);
+			$header = array();
+			$headers = array();
+			while (count($lines)) {
+				$line = array_shift($lines);
+				if (!strlen($line)) break;
+				$parts = explode(': ', $line, 2);
+				$headers[strtoupper($parts[0])] = $parts[1];
+				switch (strtoupper($parts[0])) {
+					case 'PATH':
+						$line = $parts[0].': pNews.Core.UCIS.nl!'.$parts[1];
+						break;
+					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:
+						writelog("Received unknown header $parts[0]");
+				}
+				if ($line !== NULL) $header[] = $line;
+			}
+			if (!isset($headers['NEWSGROUPS'])) {
+				nntp_writeline(STDOUT, '441 Missing required Newsgroups: header');
+				break;
+			}
+			if (!isset($headers['MESSAGE-ID'])) {
+				$headers['MESSAGE-ID'] = '<'.md5(time().rand()).'@pNews.Core.UCIS.nl>';
+				$header[] = 'Message-ID: '.$headers['MESSAGE-ID'];
+			}
+			if (!isset($headers['PATH'])) {
+				$headers['PATH'] = 'pNews.Core.UCIS.nl';
+				$header[] = 'Path: '.$headers['PATH'];
+			}
+			$msgid = $headers['MESSAGE-ID'];
+			if (strlen($msgid) <= 2 || $msgid[0] != '<' || $msgid[strlen($msgid)-1] != '>') {
+				nntp_writeline(STDOUT, '441 435 Bad Message-ID');
+				break;
+			}
+			$msgid = substr($msgid, 1, -1);
+			$article = $db->evalRowAssoc('SELECT * FROM `messages` WHERE `messageid` = ?', $msgid);
+			if ($article !== FALSE) {
+				nntp_writeline(STDOUT, '441 435 Duplicate');
+				return NULL;
+			}
+			$id = $db->insert('INSERT INTO `messages` (`messageid`, `header`, `body`) VALUES (?, ?, ?)', array($msgid, implode("\r\n", $header), implode("\r\n", $lines)));
+			foreach (explode(',', $headers['NEWSGROUPS']) as $groupname) {
+				$group = $db->evalRowAssoc('SELECT * FROM `groups` WHERE `name` = ?', $groupname);
+				if ($group === FALSE) continue;
+				$db->insert('INSERT INTO `groupmessages` (`group`, `message`) VALUES (?, ?)', array($group['id'], $id));
+			}
+			nntp_writeline(STDOUT, '240 Article received <'.$msgid.'>');
+			break;
 		case 'QUIT':
 			nntp_writeline(STDOUT, '205 .');
 			return;