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 if (!isset($url['user'])) {
34 parent
::__construct($url['host'], $url['port']);
36 parent
::__construct($url['host'], $url['port'], 120, false
);
37 $this->authinfo($url['user'], $url['pass']);
41 /** Return the descript;ion of the current box
43 public function getDescription()
45 $descs = $this->xgtitle(Banana
::$group);
46 if (isset($descs[Banana
::$group])) {
47 return trim(utf8_encode($descs[Banana
::$group]));
52 /** Return the list of the boxes
53 * @param mode Kind of boxes to list
54 * @param since date of last check (for new boxes and new messages)
55 * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages)
57 public function getBoxList($mode = Banana
::BOXES_ALL
, $since = 0, $withstats = false
)
59 if (!is_array($this->boxes
) ||
$this->mode
!= $mode) {
60 $descs = $this->xgtitle();
61 if ($mode == Banana
::BOXES_NEW
&& $since) {
62 $list = $this->newgroups($since);
64 $list = $this->listGroups();
65 if ($mode == Banana
::BOXES_SUB
) {
66 $sub = array_flip(Banana
::$profile['subscribe']);
67 $list = array_intersect_key($list, $sub);
70 $this->boxes
= array();
71 foreach ($list as $group=>&$infos) {
72 if (isset($descs[$group])) {
73 $desc = $descs[$group];
74 if (!is_utf8($desc)) {
75 $desc = utf8_encode($desc);
77 $this->boxes
[$group] = array('desc' => $desc);
79 $this->boxes
[$group] = array('desc' => null
);
85 foreach ($this->boxes
as $group=>&$desc) {
86 list($msgnum, $first, $last, $groupname) = $this->group($group);
87 $this->ingroup
= $group;
88 $new = count($this->newnews($group, $since));
89 $desc['msgnum'] = $msgnum;
90 $desc['unread'] = $new;
97 * @param id Id of the emssage (can be either an Message-id or a message index)
98 * @return A BananaMessage or null if the given id can't be retreived
100 public function &getMessage($id)
103 if (is_numeric($id) && Banana
::$group != $this->ingroup
) {
104 if (is_null(Banana
::$spool)) {
105 $this->group(Banana
::$group);
106 $this->ingroup
= Banana
::$group;
108 $id = array_search($id, Banana
::$spool->ids
);
111 $data = $this->article($id);
112 if ($data !== false
) {
113 $message = new BananaMessage($data);
118 /** Return the sources of the message
120 public function getMessageSource($id)
122 if (is_numeric($id) && Banana
::$group != $this->ingroup
) {
123 if (is_null(Banana
::$spool)) {
124 $this->group(Banana
::$group);
125 $this->ingroup
= Banana
::$group;
127 $id = array_search($id, Banana
::$spool->ids
);
130 $data = $this->article($id);
131 if ($data !== false
) {
132 return implode("\n", $data);
138 /** Return the indexes of the messages presents in the Box
139 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
141 public function getIndexes()
143 list($msgnum, $first, $last, $groupname) = $this->group(Banana
::$group);
144 $this->ingroup
= Banana
::$group;
145 return array($msgnum, $first, $last);
148 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
149 * @return Array(id => array(headername => headervalue))
151 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
154 foreach ($msg_headers as $header) {
155 $headers = $this->xhdr($header, $firstid, $lastid);
156 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
157 $header = strtolower($header);
158 if ($header == 'date') {
159 $headers = array_map('strtotime', $headers);
161 foreach ($headers as $id=>&$value) {
162 if (!isset($messages[$id])) {
163 $messages[$id] = array();
165 $messages[$id][$header] =& $value;
171 /** Add protocole specific data in the spool
173 public function updateSpool(array &$messages)
178 /** Return the indexes of the new messages since the give date
179 * @return Array(MSGNUM of new messages)
181 public function getNewIndexes($since)
183 return $this->newnews(Banana
::$group, $since);
186 /** Return true if can post
188 public function canSend()
190 return $this->isValid();
193 /** Return true if can cancel
195 public function canCancel()
197 return $this->isValid();
200 /** Return the list of requested header for a new post
202 public function requestedHeaders()
204 return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization');
209 public function send(BananaMessage
&$message)
211 $sources = $message->get(true
);
212 return $this->post($sources);
215 /** Cancel the message
217 public function cancel(BananaMessage
&$message)
219 $headers = Array('From' => Banana
::$profile['From'],
220 'Newsgroups' => Banana
::$group,
221 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'),
222 'Control' => 'cancel ' . $message->getHeaderValue('message-id'));
223 $headers = array_merge($headers, Banana
::$msgedit_headers);
224 $body = 'Message canceled with Banana';
225 $msg = BananaMessage
::newMessage($headers, $body);
226 return $this->send($msg);
229 /** Return the protocole name
231 public function name()
236 /** Return the filename for the spool
238 public function filename()
240 $url = parse_url(Banana
::$nntp_host);
242 if (isset($url['host'])) {
243 $file .= $url['host'] . '_';
245 if (isset($url['port'])) {
246 $file .= $url['port'] . '_';
248 $file .= Banana
::$group;
253 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: