3ac4670c125271ec81b70db0bb27af1da710e6cd
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'];
29 if (!isset($url['port'])) {
32 } else if (!isset($url['port'])) {
35 parent
::__construct($url['host'], $url['port']);
36 if (isset($url['user'])) {
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 if (is_array(Banana
::$profile['subscribe'])) {
67 $sub = array_flip(Banana
::$profile['subscribe']);
71 $list = array_intersect_key($list, $sub);
74 $this->boxes
= array();
75 foreach ($list as $group=>&$infos) {
76 if (isset($descs[$group])) {
77 $desc = $descs[$group];
78 if (!is_utf8($desc)) {
79 $desc = utf8_encode($desc);
81 $this->boxes
[$group] = array('desc' => $desc);
83 $this->boxes
[$group] = array('desc' => null
);
89 foreach ($this->boxes
as $group=>&$desc) {
90 list($msgnum, $first, $last, $groupname) = $this->group($group);
91 $this->ingroup
= $group;
92 $new = $this->newnews($group, $since);
93 if (!is_array($new)) {
97 if ($c > 0 && function_exists('hook_listReadMessages')) {
98 $msgs = hook_listReadMessages($group);
99 if (is_array($msgs)) {
100 foreach ($msgs as $msg) {
101 if (is_numeric($msg)) {
103 } else if (in_array($msg, $new)) {
111 $desc['msgnum'] = $msgnum;
112 $desc['unread'] = $new;
119 * @param id Id of the emssage (can be either an Message-id or a message index)
120 * @return A BananaMessage or null if the given id can't be retreived
122 public function &getMessage($id)
125 if (is_numeric($id) && Banana
::$group != $this->ingroup
) {
126 if (is_null(Banana
::$spool)) {
127 $this->group(Banana
::$group);
128 $this->ingroup
= Banana
::$group;
130 $id = array_search($id, Banana
::$spool->ids
);
133 $data = $this->article($id);
134 if ($data !== false
) {
135 $message = new BananaMessage($data);
140 /** Return the sources of the message
142 public function getMessageSource($id)
144 if (is_numeric($id) && Banana
::$group != $this->ingroup
) {
145 if (is_null(Banana
::$spool)) {
146 $this->group(Banana
::$group);
147 $this->ingroup
= Banana
::$group;
149 $id = array_search($id, Banana
::$spool->ids
);
152 $data = $this->article($id);
153 if ($data !== false
) {
154 return implode("\n", $data);
160 /** Return the indexes of the messages presents in the Box
161 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
163 public function getIndexes()
165 list($msgnum, $first, $last, $groupname) = $this->group(Banana
::$group);
166 $this->ingroup
= Banana
::$group;
167 return array($msgnum, $first, $last);
170 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
171 * @return Array(id => array(headername => headervalue))
173 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
176 foreach ($msg_headers as $header) {
177 $headers = $this->xhdr($header, $firstid, $lastid);
178 $header = strtolower($header);
179 if ($header == 'date') {
180 $headers = array_map('strtotime', $headers);
182 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
184 foreach ($headers as $id=>&$value) {
185 if (!isset($messages[$id])) {
186 $messages[$id] = array();
188 $messages[$id][$header] =& $value;
194 /** Add protocole specific data in the spool
196 public function updateSpool(array &$messages)
201 /** Return the indexes of the new messages since the give date
202 * @return Array(MSGNUM of new messages)
204 public function getNewIndexes($since)
206 return $this->newnews(Banana
::$group, $since);
209 /** Return true if can post
211 public function canSend()
213 return $this->isValid();
216 /** Return true if can cancel
218 public function canCancel()
220 return $this->isValid();
223 /** Return the list of requested header for a new post
225 public function requestedHeaders()
227 return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization');
232 public function send(BananaMessage
$message)
234 $sources = $message->get(true
);
235 return $this->post($sources);
238 /** Cancel the message
240 public function cancel(BananaMessage
$message)
242 $headers = Array('From' => Banana
::$profile['From'],
243 'Newsgroups' => Banana
::$group,
244 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'),
245 'Control' => 'cancel ' . $message->getHeaderValue('message-id'));
246 $headers = array_merge($headers, Banana
::$msgedit_headers);
247 $body = 'Message canceled with Banana';
248 $msg = BananaMessage
::newMessage($headers, $body);
249 return $this->send($msg);
252 /** Return the protocole name
254 public function name()
259 /** Return the filename for the spool
261 public function filename()
263 $url = parse_url(Banana
::$nntp_host);
265 if (isset($url['host'])) {
266 $file .= $url['host'] . '_';
268 if (isset($url['port'])) {
269 $file .= $url['port'] . '_';
271 $file .= Banana
::$group;
276 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: