f352292d9bee12314b3caffd2dafd63bbfd6264f
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 if (is_array(Banana
::$profile['subscribe'])) {
65 $sub = array_flip(Banana
::$profile['subscribe']);
69 $list = array_intersect_key($list, $sub);
72 $this->boxes
= array();
73 foreach ($list as $group=>&$infos) {
74 if (isset($descs[$group])) {
75 $desc = $descs[$group];
76 if (!is_utf8($desc)) {
77 $desc = utf8_encode($desc);
79 $this->boxes
[$group] = array('desc' => $desc);
81 $this->boxes
[$group] = array('desc' => null
);
87 foreach ($this->boxes
as $group=>&$desc) {
88 list($msgnum, $first, $last, $groupname) = $this->group($group);
89 $this->ingroup
= $group;
90 $new = count($this->newnews($group, $since));
91 $desc['msgnum'] = $msgnum;
92 $desc['unread'] = $new;
99 * @param id Id of the emssage (can be either an Message-id or a message index)
100 * @return A BananaMessage or null if the given id can't be retreived
102 public function &getMessage($id)
105 if (is_numeric($id) && Banana
::$group != $this->ingroup
) {
106 if (is_null(Banana
::$spool)) {
107 $this->group(Banana
::$group);
108 $this->ingroup
= Banana
::$group;
110 $id = array_search($id, Banana
::$spool->ids
);
113 $data = $this->article($id);
114 if ($data !== false
) {
115 $message = new BananaMessage($data);
120 /** Return the sources of the message
122 public function getMessageSource($id)
124 if (is_numeric($id) && Banana
::$group != $this->ingroup
) {
125 if (is_null(Banana
::$spool)) {
126 $this->group(Banana
::$group);
127 $this->ingroup
= Banana
::$group;
129 $id = array_search($id, Banana
::$spool->ids
);
132 $data = $this->article($id);
133 if ($data !== false
) {
134 return implode("\n", $data);
140 /** Return the indexes of the messages presents in the Box
141 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
143 public function getIndexes()
145 list($msgnum, $first, $last, $groupname) = $this->group(Banana
::$group);
146 $this->ingroup
= Banana
::$group;
147 return array($msgnum, $first, $last);
150 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
151 * @return Array(id => array(headername => headervalue))
153 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
156 foreach ($msg_headers as $header) {
157 $headers = $this->xhdr($header, $firstid, $lastid);
158 $header = strtolower($header);
159 if ($header == 'date') {
160 $headers = array_map('strtotime', $headers);
162 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
164 foreach ($headers as $id=>&$value) {
165 if (!isset($messages[$id])) {
166 $messages[$id] = array();
168 $messages[$id][$header] =& $value;
174 /** Add protocole specific data in the spool
176 public function updateSpool(array &$messages)
181 /** Return the indexes of the new messages since the give date
182 * @return Array(MSGNUM of new messages)
184 public function getNewIndexes($since)
186 return $this->newnews(Banana
::$group, $since);
189 /** Return true if can post
191 public function canSend()
193 return $this->isValid();
196 /** Return true if can cancel
198 public function canCancel()
200 return $this->isValid();
203 /** Return the list of requested header for a new post
205 public function requestedHeaders()
207 return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization');
212 public function send(BananaMessage
$message)
214 $sources = $message->get(true
);
215 return $this->post($sources);
218 /** Cancel the message
220 public function cancel(BananaMessage
$message)
222 $headers = Array('From' => Banana
::$profile['From'],
223 'Newsgroups' => Banana
::$group,
224 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'),
225 'Control' => 'cancel ' . $message->getHeaderValue('message-id'));
226 $headers = array_merge($headers, Banana
::$msgedit_headers);
227 $body = 'Message canceled with Banana';
228 $msg = BananaMessage
::newMessage($headers, $body);
229 return $this->send($msg);
232 /** Return the protocole name
234 public function name()
239 /** Return the filename for the spool
241 public function filename()
243 $url = parse_url(Banana
::$nntp_host);
245 if (isset($url['host'])) {
246 $file .= $url['host'] . '_';
248 if (isset($url['port'])) {
249 $file .= $url['port'] . '_';
251 $file .= Banana
::$group;
256 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: