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 $description = null
;
18 private $ingroup = null
;
21 private $boxes = null
;
23 /** Build a protocole handler plugged on the given box
25 public function __construct()
27 $url = parse_url(Banana
::$nntp_host);
28 if ($url['scheme'] == 'nntps' ||
$url['scheme'] == 'snntp') {
29 $url['host'] = 'ssl://' . $url['host'];
31 if (!isset($url['port'])) {
34 if (!isset($url['user'])) {
35 parent
::__construct($url['host'], $url['port']);
37 parent
::__construct($url['host'], $url['port'], 120, false
);
38 $this->authinfo($url['user'], $url['pass']);
42 /** Return the descript;ion of the current box
44 public function getDescription()
46 if ($this->description
) {
47 return $this->description
;
49 $descs = $this->xgtitle(Banana
::$group);
50 if (isset($descs[Banana
::$group])) {
51 $this->description
= $descs[Banana
::$group];
53 return $this->description
;
56 /** Return the list of the boxes
57 * @param mode Kind of boxes to list
58 * @param since date of last check (for new boxes and new messages)
59 * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages)
61 public function getBoxList($mode = Banana
::BOXES_ALL
, $since = 0, $withstats = false
)
63 if (!is_array($this->boxes
) ||
$this->mode
!= $mode) {
64 $descs = $this->xgtitle();
65 if ($mode == Banana
::BOXES_NEW
&& $since) {
66 $list = $this->newgroups($since);
68 $list = $this->listGroups();
69 if ($mode == Banana
::BOXES_SUB
) {
70 $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 = count($this->newnews($group, $since));
93 $desc['msgnum'] = $msgnum;
94 $desc['unread'] = $new;
101 * @param id Id of the emssage (can be either an Message-id or a message index)
102 * @param msg_headers Headers to process
103 * @param is_msgid If is set, $id is en Message-Id
104 * @return A BananaMessage or null if the given id can't be retreived
106 public function getMessage($id, array $msg_headers = array(), $is_msgid = false
)
108 if (!$is_msgid && Banana
::$group != $this->ingroup
) {
109 if (is_null(Banana
::$spool)) {
110 $this->group(Banana
::$group);
111 $this->ingroup
= Banana
::$group;
113 $id = array_search($id, Banana
::$spool->ids
);
116 $data = $this->article($id);
117 if ($data !== false
) {
118 return new BananaMessage($data);
123 /** Return the indexes of the messages presents in the Box
124 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
126 public function getIndexes()
128 list($msgnum, $first, $last, $groupname) = $this->group(Banana
::$group);
129 $this->ingroup
= Banana
::$group;
130 return array($msgnum, $first, $last);
133 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
134 * @return Array(id => array(headername => headervalue))
136 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
139 foreach ($msg_headers as $header) {
140 $headers = $this->xhdr($header, $firstid, $lastid);
141 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
142 $header = strtolower($header);
143 if ($header == 'date') {
144 $headers = array_map('strtotime', $headers);
146 foreach ($headers as $id=>&$value) {
147 if (!isset($messages[$id])) {
148 $messages[$id] = array();
150 $messages[$id][$header] =& $value;
156 /** Add protocole specific data in the spool
158 public function updateSpool(array &$messages)
163 /** Return the indexes of the new messages since the give date
164 * @return Array(MSGNUM of new messages)
166 public function getNewIndexes($since)
168 return $this->newnews(Banana
::$group, $since);
171 /** Return true if can post
173 public function canSend()
175 return $this->isValid();
178 /** Return true if can cancel
180 public function canCancel()
182 return $this->isValid();
185 /** Return the list of requested header for a new post
187 public function requestedHeaders()
189 return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization');
194 public function send(BananaMessage
&$message)
196 $sources = $message->get(true
);
197 return $this->post($sources);
200 /** Cancel the message
202 public function cancel(BananaMessage
&$message)
204 $headers = Array('From' => Banana
::$profile['From'],
205 'Newsgroups' => Banana
::$group,
206 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'),
207 'Control' => 'cancel ' . $message->getHeaderValue('message-id'));
208 $headers = array_merge($headers, Banana
::$msgedit_headers);
209 $body = 'Message canceled with Banana';
210 $msg = BananaMessage
::newMessage($headers, $body);
211 return $this->send($msg);
214 /** Return the protocole name
216 public function name()
221 /** Return the filename for the spool
223 public function filename()
225 $url = parse_url(Banana
::$nntp_host);
227 if (isset($url['host'])) {
228 $file .= $url['host'] . '_';
230 if (isset($url['port'])) {
231 $file .= $url['port'] . '_';
233 $file .= Banana
::$group;
238 // vim:set et sw=4 sts=4 ts=4: