Mercurial > hg > pnewss
comparison 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 |
comparison
equal
deleted
inserted
replaced
1:61fac319ca3e | 2:40e545510a57 |
---|---|
1 <?php | 1 <?php |
2 chdir(__DIR__); | |
2 require_once './pdo.php'; | 3 require_once './pdo.php'; |
3 require_once './config.php'; | 4 require_once './config.php'; |
4 | 5 |
5 foreach ($db->evalAllAssoc('SELECT * FROM `peers`') as $peer) { | 6 foreach ($db->evalAllAssoc('SELECT * FROM `peers`') as $peer) { |
6 $socket = stream_socket_client($peer['address']); | 7 $socket = stream_socket_client($peer['address']); |
7 if ($socket === FALSE) die("Could not connect to peer $peer[address]\n"); | 8 if ($socket === FALSE) die("Could not connect to peer $peer[address]\n"); |
8 $line = nntp_readline($socket); | 9 $line = nntp_readline($socket); |
9 $code = strtok($line, " \t"); | 10 $code = strtok($line, " \t"); |
10 if ($code != 200) die("Error code $code from $peer[address]\n"); | 11 if ($code == 200) { |
12 } else if ($code == 201) { | |
13 $peer['post'] = 0; | |
14 } else die("Error code $code from $peer[address]\n"); | |
11 foreach ($db->evalAllAssoc('SELECT * FROM `peergroups` WHERE `peer` = ?', $peer['id']) as $peergroup) { | 15 foreach ($db->evalAllAssoc('SELECT * FROM `peergroups` WHERE `peer` = ?', $peer['id']) as $peergroup) { |
12 $group = $db->evalRowAssoc('SELECT * FROM `groups` WHERE `id` = ?', $peergroup['group']); | 16 $group = $db->evalRowAssoc('SELECT * FROM `groups` WHERE `id` = ?', $peergroup['group']); |
13 nntp_writeline($socket, 'GROUP '.$group['name']); | 17 nntp_writeline($socket, 'GROUP '.$group['name']); |
14 $line = nntp_readline($socket); | 18 $line = nntp_readline($socket); |
15 $code = strtok($line, " \t"); | 19 $code = strtok($line, " \t"); |
63 } | 67 } |
64 } | 68 } |
65 $db->update('UPDATE `peergroups` SET `low` = ?, `high` = ? WHERE `peer` = ? AND `group` = ?', array($low, $high, $peergroup['peer'], $peergroup['group'])); | 69 $db->update('UPDATE `peergroups` SET `low` = ?, `high` = ? WHERE `peer` = ? AND `group` = ?', array($low, $high, $peergroup['peer'], $peergroup['group'])); |
66 } | 70 } |
67 } | 71 } |
72 while ($peer['post']) { | |
73 if ($peer['lastposted'] === NULL) { | |
74 $articles = $db->evalAllAssoc('SELECT * FROM `messages` LIMIT 10'); | |
75 } else { | |
76 $articles = $db->evalAllAssoc('SELECT * FROM `messages` WHERE `id` > ? LIMIT 10', $peer['lastposted']); | |
77 } | |
78 if (!count($articles)) break; | |
79 foreach ($articles as $article) { | |
80 nntp_writeline($socket, 'POST'); | |
81 $line = nntp_readline($socket); | |
82 $code = strtok($line, " \t"); | |
83 if ($code != 340) die("Error code $code from $peer[address]\n"); | |
84 foreach (explode("\r\n", $article['header']) as $line) { | |
85 $parts = explode(': ', $line, 2); | |
86 switch (strtoupper($parts[0])) { | |
87 case 'PATH': case 'FROM': case 'NEWSGROUPS': case 'SUBJECT': case 'DATE': case 'ORGANIZATION': | |
88 case 'LINES': case 'MESSAGE-ID': case 'MIME-VERSION': case 'CONTENT-TYPE': case 'CONTENT-TRANSFER-ENCODING': | |
89 case 'USER-AGENT': case 'REFERENCES': case 'REPLY-TO': case 'SENDER': case 'FOLLOWUP-TO': | |
90 case 'EXPIRES': case 'CONTROL': case 'DISTRIBUTION': case 'KEYWORDS': case 'SUMMARY': | |
91 case 'IN-REPLY-TO': | |
92 break; | |
93 case 'NNTP-POSTING-HOST': case 'X-TRACE': case 'XREF': case 'X-COMPLAINTS-TO': | |
94 case 'NNTP-POSTING-DATE': | |
95 $line = NULL; | |
96 break; | |
97 default: | |
98 print("Sending unknown header $parts[0]\n"); | |
99 } | |
100 if ($line !== NULL) nntp_writeline($socket, $line); | |
101 } | |
102 nntp_writeline($socket, ''); | |
103 foreach (explode("\r\n", $article['body']) as $line) nntp_writeline($socket, $line); | |
104 nntp_writeline($socket, '.'); | |
105 $line = nntp_readline($socket); | |
106 $code = strtok($line, " \t"); | |
107 if ($code != 240) print("Article $article[messageid] was not accepted ($code)\n"); | |
108 if ($article['id'] > $peer['lastposted']) $peer['lastposted'] = $article['id']; | |
109 } | |
110 $db->update('UPDATE `peers` SET `lastposted` = ? WHERE `id` = ?', array($peer['lastposted'], $peer['id'])); | |
111 } | |
68 nntp_writeline($socket, 'QUIT'); | 112 nntp_writeline($socket, 'QUIT'); |
69 fclose($socket); | 113 fclose($socket); |
70 } | 114 } |
71 | 115 |
72 function nntp_removeheader(&$lines, $header) { | 116 function nntp_removeheader(&$lines, $header) { |