2 /********************************************************************************
3 * include/NetNNTP.inc.php : NNTP subroutines
4 * -------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
11 * implements some basic functions for NNTP protocol
15 /** socket filehandle */
17 /** posting allowed */
19 /** last NNTP error code */
21 /** last NNTP error text */
27 * @param $_host STRING NNTP host
28 * @param $_timeout INTEGER socket timeout
29 * @param $_reader BOOLEAN sends a "MODE READER" at connection if true
32 function nntp($_url, $_timeout=120, $_reader=true
)
35 $url = parse_url($_url);
36 $this->ns
= fsockopen($url['host'], $url['port'], $errno, $errstr, $_timeout);
42 $result = $this->gline();
43 $this->posting
= (substr($result, 0, 3)=="200");
44 if ($_reader && ($result{0}=="2")) {
45 $this->pline("MODE READER\r\n");
46 $result = $this->gline();
47 $this->posting
= ($result{0}=="200");
49 if ($result{0}=="2" && isset($url['user']) && $url['user'] != 'anonymous') {
50 return $this->authinfo($url['user'], $url['pass']);
52 return ($result{0}=="2");
57 /** get a line from server
63 return rtrim(fgets($this->ns
, 1200));
66 /** puts a line on server
67 * @param STRING $_line line to put
70 function pline($_line)
72 return fputs($this->ns
, $_line, strlen($_line));
75 # strict NNTP Functions [RFC 977]
76 # see http://www.faqs.org/rfcs/rfc977.html
79 * @param $_user STRING login
80 * @param $_pass INTEGER password
81 * @return BOOLEAN true if authentication was successful
84 function authinfo($_user, $_pass)
86 $user = preg_replace("/(\r|\n)/", "", $_user);
87 $pass = preg_replace("/(\r|\n)/", "", $_pass);
88 $this->pline("AUTHINFO USER $user\r\n");
90 $this->pline("AUTHINFO PASS $pass\r\n");
91 $result=$this->gline();
92 if ($result{0}!="2") {
93 $this->lasterrorcode
= substr($result, 0, 3);
94 $this->lasterrortext
= substr($result, 4);
100 /** retrieves an article
101 * MSGID is a numeric ID a shown in article's headers. MSGNUM is a
102 * server-dependent ID (see X-Ref on many servers) and retriving
103 * an article by this way will change the current article pointer.
104 * If an error occur, false is returned.
105 * @param $_msgid STRING MSGID or MSGNUM of article
106 * @return ARRAY lines of the article
111 function article($_msgid="")
113 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
114 $this->pline("ARTICLE $msgid\r\n");
115 $result = $this->gline();
116 if ($result{0} != '2') {
117 $this->lasterrorcode
= substr($result, 0, 3);
118 $this->lasterrortext
= substr($result, 4);
121 $result = $this->gline();
122 while ($result != ".") {
124 $result = $this->gline();
130 * if an error occur, false is returned
131 * @param $_message STRING message to post
132 * @return STRING MSGID of article
135 function post($_message)
137 if (is_array($_message)) {
138 $message=join("\n", $_message);
142 $this->pline("POST \r\n");
143 $result=$this->gline();
144 if ($result{0} != '3') {
145 $this->lasterrorcode
= substr($result, 0, 3);
146 $this->lasterrortext
= substr($result, 4);
149 $this->pline($message."\r\n.\r\n");
150 $result = $this->gline();
151 if ($result{0} != '2') {
152 $this->lasterrorcode
= substr($result, 0, 3);
153 $this->lasterrortext
= substr($result, 4);
156 if ($result{0} == '2') {
157 if (preg_match("/(<[^@>]+@[^@>]+>)/", $result, $regs)) {
166 /** fetches the body of an article
167 * params are the same as article
168 * @param $_msgid STRING MSGID or MSGNUM of article
169 * @return ARRAY lines of the article
174 function body($_msgid="")
176 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
177 $this->pline("BODY $msgid\r\n");
178 $result = $this->gline();
179 if ($result{0} != '2') {
180 $this->lasterrorcode
= substr($result, 0, 3);
181 $this->lasterrortext
= substr($result, 4);
185 while (($result = $this->gline()) != ".") {
191 /** fetches the headers of an article
192 * params are the same as article
193 * @param $_msgid STRING MSGID or MSGNUM of article
194 * @return ARRAY lines of the article
199 function head($_msgid="")
201 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
202 $this->pline("HEAD $msgid\r\n");
203 $result = $this->gline();
204 if ($result{0}!="2") {
205 $this->lasterrorcode
= substr($result, 0, 3);
206 $this->lasterrortext
= substr($result, 4);
209 $result = $this->gline();
210 while ($result != ".") {
212 $result = $this->gline();
217 /** set current group
218 * @param $_group STRING
219 * @return ARRAY array : nb of articles in group, MSGNUM of first article, MSGNUM of last article, and group name
222 function group($_group)
224 $group = preg_replace("/(\r|\n)/", "", $_group);
225 $this->pline("GROUP $group\r\n");
226 $line = $this->gline();
228 $this->lasterrorcode
= substr($line, 0, 3);
229 $this->lasterrortext
= substr($line, 4);
232 if (preg_match("/^2\d{2} (\d+) (\d+) (\d+) ([^ ]+)/", $line, $regs)) {
233 return array($regs[1], $regs[2], $regs[3], $regs[4]);
238 /** set the article pointer to the previous article in current group
239 * @return STRING MSGID of article
245 $this->pline("LAST \r\n");
246 $line = $this->gline();
248 $this->lasterrorcode
= substr($result, 0, 3);
249 $this->lasterrortext
= substr($result, 4);
252 if (preg_match("/^2\d{2} \d+ <([^>]+)>/", $line, $regs)) {
253 return "<{$regs[1]}>";
258 /** set the article pointer to the next article in current group
259 * @return STRING MSGID of article
265 $this->pline("NEXT \r\n");
266 $line = $this->gline();
268 $this->lasterrorcode
= substr($result, 0, 3);
269 $this->lasterrortext
= substr($result, 4);
272 if (preg_match("/^2\d{2} \d+ <([^>]+)>/", $line, $regs)) {
273 return "<{$regs[1]}>";
278 /** set the current article pointer
279 * @param $_msgid STRING MSGID or MSGNUM of article
280 * @return BOOLEAN true if authentication was successful, error code otherwise
285 function nntpstat($_msgid)
287 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
288 $this->pline("STAT $msgid\r\n");
289 $line = $this->gline();
291 $this->lasterrorcode
= substr($result, 0, 3);
292 $this->lasterrortext
= substr($result, 4);
295 if (preg_match("/^2\d{2} \d+ <([^>]+)>/", $line, $regs)) {
296 return "<{$regs[1]}>";
301 /** returns true if posting is allowed
302 * @return BOOLEAN true if posting is allowed lines
307 return ($this->posting
);
310 /** retreive the group list
311 * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
312 * @see newgroups, liste
314 function _grouplist()
318 if (substr($this->gline(), 0, 1)!="2") {
321 $result = $this->gline();
323 while ($result != ".") {
324 preg_match("/([^ ]+) (\d+) (\d+) (.)/", $result, $regs);
325 if (!isset($banana->grp_pattern
) ||
preg_match('@'.$banana->grp_pattern
.'@', $regs[1])) {
326 $array[$regs[1]] = array(intval($regs[2]), intval($regs[3]), intval($regs[4]));
328 $result = $this->gline();
333 /** gets information about all active newsgroups
334 * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
340 $this->pline("LIST\r\n");
341 return $this->_grouplist();
344 /** get information about recent newsgroups
345 * same as list, but information are limited to newgroups created after $_since
346 * @param $_since INTEGER unix timestamp
347 * @param $_distributions STRING distributions
348 * @return ARRAY same format as liste
352 function newgroups($_since, $_distributions="")
354 #assume $_since is a unix timestamp
355 $distributions = preg_replace("/(\r|\n)/", "", $_distributions);
356 $this->pline("NEWGROUPS ".gmdate("ymd His", $_since)
357 ." GMT $distributions\r\n");
358 return $this->_grouplist();
361 /** gets a list of new articles
362 * @param $_since INTEGER unix timestamp
363 * @parma $_groups STRING pattern of intersting groups
364 * @return ARRAY MSGID of new articles
367 function newnews($_since, $_groups="*", $_distributions="")
369 $distributions = preg_replace("/(\r|\n)/", "", $_distributions);
370 $groups = preg_replace("/(\r|\n)/", "", $_groups);
372 #assume $since is a unix timestamp
373 $this->pline("NEWNEWS $_groups ".gmdate("ymd His", $_since)." GMT $distributions\r\n");
374 if (substr($this->gline(), 0, 1)!="2") {
377 while (($result = $this->gline()) != ".") {
383 /** Tell the remote server that I am not a user client, but probably another news server
384 * @return BOOLEAN true if sucessful
389 $this->pline("SLAVE \r\n");
390 return (substr($this->gline(), 0, 1)=="2");
393 /** implements IHAVE method
394 * @param $_msgid STRING MSGID of article
395 * @param $_message STRING article
399 function ihave($_msgid, $_message=false
)
401 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
402 if (is_array($message)) {
403 $message = join("\n", $_message);
405 $message = $_message;
407 $this->pline("IHAVE $msgid \r\n");
408 $result = $this->gline();
409 if ($message && ($result{0}=="3")) {
410 $this->pline("$message\r\n.\r\n");
411 $result = $this->gline();
413 return ($result{0}=="2");
416 /** closes connection to server
421 $this->pline("QUIT\r\n");
426 # NNTP Extensions [RFC 2980]
428 /** Returns the date on the remote server
429 * @return INTEGER timestamp
434 $this->pline("DATE \r\n");
435 $result = $this->gline();
436 if (preg_match("/^111 (\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/", $result, $r)) {
437 return gmmktime($r[4], $r[5], $r[6], $r[2], $r[3], $r[1]);
442 /** returns group descriptions
443 * @param $_pattern STRING pattern of intersting groups
444 * @return ARRAY group name => description
447 function xgtitle($_pattern="*")
449 $pattern = preg_replace("/[\r\n]/", "", $_pattern);
450 $this->pline("XGTITLE $pattern \r\n");
451 if (substr($this->gline(), 0, 1)!="2") return false
;
452 $result = $this->gline();
453 while ($result != ".") {
454 preg_match("/([^ \t]+)[ \t]+(.+)$/", $result, $regs);
455 $array[$regs[1]] = $regs[2];
456 $result = $this->gline();
461 /** obtain the header field $hdr for all the messages specified
462 * @param $_hdr STRING name of the header (eg: 'From')
463 * @param $_range STRING range of articles
464 * @return ARRAY MSGNUM => header value
467 function xhdr($_hdr, $_range="")
469 $hdr = preg_replace("/(\r|\n)/", "", $_hdr);
470 $range = preg_replace("/(\r|\n)/", "", $_range);
471 $this->pline("XHDR $hdr $range \r\n");
472 if (substr($this->gline(), 0, 1)!="2") {
477 while (($result = $this->gline()) != '.') {
478 preg_match("/([^ \t]+) (.*)$/", $result, $regs);
479 $array[$regs[1]] = $regs[2];
484 /** obtain the header field $_hdr matching $_pat for all the messages specified
485 * @param $_hdr STRING name of the header (eg: 'From')
486 * @param $_range STRING range of articles
487 * @param $_pat STRING pattern
488 * @return ARRAY MSGNUM => header value
491 function xpat($_hdr, $_range, $_pat)
493 $hdr = preg_replace("/(\r|\n)/", "", $_hdr);
494 $range = preg_replace("/(\r|\n)/", "", $_range);
495 $pat = preg_replace("/(\r|\n)/", "", $_pat);
496 $this->pline("XPAT $hdr $range $pat\r\n");
497 if (substr($this->gline(), 0, 1)!="2") {
500 $result = $this->gline();
501 while ($result != ".") {
502 preg_match("/([^ \t]+) (.*)$/", $result, $regs);
503 $array[$regs[1]] = $regs[2];
504 $result = $this->gline();
510 // vim:set et sw=4 sts=4 ts=4