Mercurial > hg > pnewss
view fetchnews.php @ 8:005339a1b2ce
Added copyright notice
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 12 Apr 2011 14:30:56 +0200 |
parents | 01dc7eeaf5df |
children | ae0c67d72670 |
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) { $socket = stream_socket_client($peer['address']); if ($socket === FALSE) die("Could not connect to peer $peer[address]\n"); $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"); 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) { $groupmessage = $db->evalRowAssoc('SELECT * FROM `groupmessages` WHERE `group` = ? AND `message` = ?', array($group['id'], $message['id'])); if (!$groupmessage) { $db->insert('INSERT INTO `groupmessages` (`group`, `message`) VALUES (?, ?)', array($group['id'], $message['id'])); } } 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']) { 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_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) 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); } function writelog($line) { print($line."\n"); }