a7f2e5c7d7ff7ae1fac8830566c516e2becc779b
2 /********************************************************************************
3 * banana/nntp.inc.php : NNTP protocole handler
4 * ------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 require_once dirname(__FILE__
) . '/banana.inc.php';
11 require_once dirname(__FILE__
) . '/message.inc.php';
12 require_once dirname(__FILE__
) . '/nntpcore.inc.php';
13 require_once dirname(__FILE__
) . '/protocoleinterface.inc.php';
15 class BananaNNTP
extends BananaNNTPCore
implements BananaProtocoleInterface
17 private $ingroup = null
;
20 private $boxes = null
;
22 /** Build a protocole handler plugged on the given box
24 public function __construct()
26 $url = parse_url(Banana
::$nntp_host);
27 if ($url['scheme'] == 'nntps' ||
$url['scheme'] == 'snntp') {
28 $url['host'] = 'ssl://' . $url['host'];
30 if (!isset($url['port'])) {
33 parent
::__construct($url['host'], $url['port']);
34 if (isset($url['user'])) {
35 $this->authinfo($url['user'], $url['pass']);
39 /** Return the descript;ion of the current box
41 public function getDescription()
43 $descs = $this->xgtitle(Banana
::$group);
44 if (isset($descs[Banana
::$group])) {
45 return trim(utf8_encode($descs[Banana
::$group]));
50 /** Return the list of the boxes
51 * @param mode Kind of boxes to list
52 * @param since date of last check (for new boxes and new messages)
53 * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages)
55 public function getBoxList($mode = Banana
::BOXES_ALL
, $since = 0, $withstats = false
)
57 if (!is_array($this->boxes
) ||
$this->mode
!= $mode) {
58 $descs = $this->xgtitle();
59 if ($mode == Banana
::BOXES_NEW
&& $since) {
60 $list = $this->newgroups($since);
62 $list = $this->listGroups();
63 if ($mode == Banana
::BOXES_SUB
) {
64 $sub = array_flip(Banana
::$profile['subscribe']);
65 $list = array_intersect_key($list, $sub);
68 $this->boxes
= array();
69 foreach ($list as $group=>&$infos) {
70 if (isset($descs[$group])) {
71 $desc = $descs[$group];
72 if (!is_utf8($desc)) {
73 $desc = utf8_encode($desc);
75 $this->boxes
[$group] = array('desc' => $desc);
77 $this->boxes
[$group] = array('desc' => null
);
83 foreach ($this->boxes
as $group=>&$desc) {
84 list($msgnum, $first, $last, $groupname) = $this->group($group);
85 $this->ingroup
= $group;
86 $new = count($this->newnews($group, $since));
87 $desc['msgnum'] = $msgnum;
88 $desc['unread'] = $new;
95 * @param id Id of the emssage (can be either an Message-id or a message index)
96 * @return A BananaMessage or null if the given id can't be retreived
98 public function &getMessage($id)
101 if (is_numeric($id) && Banana
::$group != $this->ingroup
) {
102 if (is_null(Banana
::$spool)) {
103 $this->group(Banana
::$group);
104 $this->ingroup
= Banana
::$group;
106 $id = array_search($id, Banana
::$spool->ids
);
109 $data = $this->article($id);
110 if ($data !== false
) {
111 $message = new BananaMessage($data);
116 /** Return the sources of the message
118 public function getMessageSource($id)
120 if (is_numeric($id) && Banana
::$group != $this->ingroup
) {
121 if (is_null(Banana
::$spool)) {
122 $this->group(Banana
::$group);
123 $this->ingroup
= Banana
::$group;
125 $id = array_search($id, Banana
::$spool->ids
);
128 $data = $this->article($id);
129 if ($data !== false
) {
130 return implode("\n", $data);
136 /** Return the indexes of the messages presents in the Box
137 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
139 public function getIndexes()
141 list($msgnum, $first, $last, $groupname) = $this->group(Banana
::$group);
142 $this->ingroup
= Banana
::$group;
143 return array($msgnum, $first, $last);
146 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
147 * @return Array(id => array(headername => headervalue))
149 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
152 foreach ($msg_headers as $header) {
153 $headers = $this->xhdr($header, $firstid, $lastid);
154 $header = strtolower($header);
155 if ($header == 'date') {
156 $headers = array_map('strtotime', $headers);
158 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
160 foreach ($headers as $id=>&$value) {
161 if (!isset($messages[$id])) {
162 $messages[$id] = array();
164 $messages[$id][$header] =& $value;
170 /** Add protocole specific data in the spool
172 public function updateSpool(array &$messages)
177 /** Return the indexes of the new messages since the give date
178 * @return Array(MSGNUM of new messages)
180 public function getNewIndexes($since)
182 return $this->newnews(Banana
::$group, $since);
185 /** Return true if can post
187 public function canSend()
189 return $this->isValid();
192 /** Return true if can cancel
194 public function canCancel()
196 return $this->isValid();
199 /** Return the list of requested header for a new post
201 public function requestedHeaders()
203 return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization');
208 public function send(BananaMessage
&$message)
210 $sources = $message->get(true
);
211 return $this->post($sources);
214 /** Cancel the message
216 public function cancel(BananaMessage
&$message)
218 $headers = Array('From' => Banana
::$profile['From'],
219 'Newsgroups' => Banana
::$group,
220 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'),
221 'Control' => 'cancel ' . $message->getHeaderValue('message-id'));
222 $headers = array_merge($headers, Banana
::$msgedit_headers);
223 $body = 'Message canceled with Banana';
224 $msg = BananaMessage
::newMessage($headers, $body);
225 return $this->send($msg);
228 /** Return the protocole name
230 public function name()
235 /** Return the filename for the spool
237 public function filename()
239 $url = parse_url(Banana
::$nntp_host);
241 if (isset($url['host'])) {
242 $file .= $url['host'] . '_';
244 if (isset($url['port'])) {
245 $file .= $url['port'] . '_';
247 $file .= Banana
::$group;
252 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: