a880dd0ce4a05bf4d9bffaae2b83d825de3ea80d
[banana.git] / banana / nntp.inc.php
1 <?php
2 /********************************************************************************
3 * banana/nntp.inc.php : NNTP protocole handler
4 * ------------------------
5 *
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
9
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';
14
15 class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
16 {
17 private $ingroup = null;
18
19 private $mode = null;
20 private $boxes = null;
21
22 /** Build a protocole handler plugged on the given box
23 */
24 public function __construct()
25 {
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'])) {
30 $url['port'] = 563;
31 }
32 } else if (!isset($url['port'])) {
33 $url['port'] = 119;
34 }
35 parent::__construct($url['host'], $url['port']);
36 if (isset($url['user'])) {
37 $this->authinfo($url['user'], $url['pass']);
38 }
39 }
40
41 /** Return the descript;ion of the current box
42 */
43 public function getDescription()
44 {
45 $descs = $this->xgtitle(Banana::$group);
46 if (isset($descs[Banana::$group])) {
47 return trim(utf8_encode($descs[Banana::$group]));
48 }
49 return null;
50 }
51
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)
56 */
57 public function getBoxList($mode = Banana::BOXES_ALL, $since = 0, $withstats = false)
58 {
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);
63 } else {
64 $list = $this->listGroups();
65 if ($mode == Banana::BOXES_SUB) {
66 if (is_array(Banana::$profile['subscribe'])) {
67 $sub = array_flip(Banana::$profile['subscribe']);
68 } else {
69 $sub = array();
70 }
71 $list = array_intersect_key($list, $sub);
72 }
73 }
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);
80 }
81 $this->boxes[$group] = array('desc' => $desc);
82 } else {
83 $this->boxes[$group] = array('desc' => null);
84 }
85 }
86 ksort($this->boxes);
87 }
88 if ($withstats) {
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;
95 }
96 }
97 return $this->boxes;
98 }
99
100 /** Return a message
101 * @param id Id of the emssage (can be either an Message-id or a message index)
102 * @return A BananaMessage or null if the given id can't be retreived
103 */
104 public function &getMessage($id)
105 {
106 $message = null;
107 if (is_numeric($id) && Banana::$group != $this->ingroup) {
108 if (is_null(Banana::$spool)) {
109 $this->group(Banana::$group);
110 $this->ingroup = Banana::$group;
111 } else {
112 $id = array_search($id, Banana::$spool->ids);
113 }
114 }
115 $data = $this->article($id);
116 if ($data !== false) {
117 $message = new BananaMessage($data);
118 }
119 return $message;
120 }
121
122 /** Return the sources of the message
123 */
124 public function getMessageSource($id)
125 {
126 if (is_numeric($id) && Banana::$group != $this->ingroup) {
127 if (is_null(Banana::$spool)) {
128 $this->group(Banana::$group);
129 $this->ingroup = Banana::$group;
130 } else {
131 $id = array_search($id, Banana::$spool->ids);
132 }
133 }
134 $data = $this->article($id);
135 if ($data !== false) {
136 return implode("\n", $data);
137 }
138 $data = null;
139 return $data;
140 }
141
142 /** Return the indexes of the messages presents in the Box
143 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
144 */
145 public function getIndexes()
146 {
147 list($msgnum, $first, $last, $groupname) = $this->group(Banana::$group);
148 $this->ingroup = Banana::$group;
149 return array($msgnum, $first, $last);
150 }
151
152 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
153 * @return Array(id => array(headername => headervalue))
154 */
155 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
156 {
157 $messages = array();
158 foreach ($msg_headers as $header) {
159 $headers = $this->xhdr($header, $firstid, $lastid);
160 $header = strtolower($header);
161 if ($header == 'date') {
162 $headers = array_map('strtotime', $headers);
163 } else {
164 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
165 }
166 foreach ($headers as $id=>&$value) {
167 if (!isset($messages[$id])) {
168 $messages[$id] = array();
169 }
170 $messages[$id][$header] =& $value;
171 }
172 }
173 return $messages;
174 }
175
176 /** Add protocole specific data in the spool
177 */
178 public function updateSpool(array &$messages)
179 {
180 return true;
181 }
182
183 /** Return the indexes of the new messages since the give date
184 * @return Array(MSGNUM of new messages)
185 */
186 public function getNewIndexes($since)
187 {
188 return $this->newnews(Banana::$group, $since);
189 }
190
191 /** Return true if can post
192 */
193 public function canSend()
194 {
195 return $this->isValid();
196 }
197
198 /** Return true if can cancel
199 */
200 public function canCancel()
201 {
202 return $this->isValid();
203 }
204
205 /** Return the list of requested header for a new post
206 */
207 public function requestedHeaders()
208 {
209 return Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To', 'Organization');
210 }
211
212 /** Send the message
213 */
214 public function send(BananaMessage $message)
215 {
216 $sources = $message->get(true);
217 return $this->post($sources);
218 }
219
220 /** Cancel the message
221 */
222 public function cancel(BananaMessage $message)
223 {
224 $headers = Array('From' => Banana::$profile['From'],
225 'Newsgroups' => Banana::$group,
226 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'),
227 'Control' => 'cancel ' . $message->getHeaderValue('message-id'));
228 $headers = array_merge($headers, Banana::$msgedit_headers);
229 $body = 'Message canceled with Banana';
230 $msg = BananaMessage::newMessage($headers, $body);
231 return $this->send($msg);
232 }
233
234 /** Return the protocole name
235 */
236 public function name()
237 {
238 return 'NNTP';
239 }
240
241 /** Return the filename for the spool
242 */
243 public function filename()
244 {
245 $url = parse_url(Banana::$nntp_host);
246 $file = '';
247 if (isset($url['host'])) {
248 $file .= $url['host'] . '_';
249 }
250 if (isset($url['port'])) {
251 $file .= $url['port'] . '_';
252 }
253 $file .= Banana::$group;
254 return $file;
255 }
256 }
257
258 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
259 ?>