Fix parsing of messages containing UUEncoded data.
[banana.git] / banana / mbox.inc.php
1 <?php
2 /********************************************************************************
3 * banana/protocoleinterface.inc.php : interface for box access
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__) . '/protocoleinterface.inc.php';
12 require_once dirname(__FILE__) . '/message.inc.php';
13
14 class BananaMBox implements BananaProtocoleInterface
15 {
16 private $debug = false;
17 private $bt = array();
18
19 private $_lasterrno = 0;
20 private $_lasterror = null;
21
22 public function __construct()
23 {
24 $this->debug = Banana::$debug_mbox;
25 }
26
27 public function isValid()
28 {
29 return true;
30 //!Banana::$group || $this->file;
31 }
32
33 /** Indicate last error n°
34 */
35 public function lastErrNo()
36 {
37 return $this->_lasterrno;;
38 }
39
40 /** Indicate last error text
41 */
42 public function lastError()
43 {
44 return $this->_lasterror;
45 }
46
47 /** Return the description of the current box
48 */
49 public function getDescription()
50 {
51 return null;
52 }
53
54 /** Return the list of the boxes
55 * @param mode Kind of boxes to list
56 * @param since date of last check (for new boxes and new messages)
57 * @param withstats Indicated whether msgnum and unread must be set in the result
58 * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages)
59 */
60 public function getBoxList($mode = Banana::BOXES_ALL, $since = 0, $withstats = false)
61 {
62 return array(Banana::$group => array('desc' => '', 'msgnum' => 0, 'unread' => 0));
63 }
64
65 private function &getRawMessage($id)
66 {
67 $message = null;
68 if (!is_numeric($id)) {
69 if (!Banana::$spool) {
70 return $message;
71 }
72 $id = Banana::$spool->ids[$id]->id;
73 }
74 $options = array ('-m ' . $id);
75 $this->getMBoxPosition($options, $id);
76 return $this->callHelper('-b', $options);
77 }
78
79 /** Return a message
80 * @param id Id of the emssage (can be either an Message-id or a message index)
81 * @return A BananaMessage or null if the given id can't be retreived
82 */
83 public function &getMessage($id)
84 {
85 $messages =& $this->getRawMessage($id);
86 if ($messages) {
87 $messages = new BananaMessage($messages);
88 } else {
89 $messages = null;
90 }
91 return $messages;
92 }
93
94 /** Return the sources of the given message
95 */
96 public function getMessageSource($id)
97 {
98 $message =& $this->getRawMessage($id);
99 if ($message) {
100 $message = implode("\n", $message);
101 }
102 return $message;
103 }
104
105 /** Compute the number of messages of the box
106 */
107 private function getCount()
108 {
109 $options = array();
110 if (@filesize($this->getFileName()) == @Banana::$spool->storage['size']) {
111 return max(array_keys(Banana::$spool->overview)) + 1;
112 }
113 $this->getMBoxPosition($options);
114 $val =& $this->callHelper('-c', $options);
115 if (!$val) {
116 return 0;
117 }
118 return intval(trim($val[0]));
119 }
120
121 /** Return the indexes of the messages presents in the Box
122 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
123 */
124 public function getIndexes()
125 {
126 $count = $this->getCount();
127 return array($count, 0, $count - 1);
128 }
129
130 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
131 * @return Array(id => array(headername => headervalue))
132 */
133 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
134 {
135 $headers = null;
136 $options = array();
137 $options[] = "-m $firstid:$lastid";
138 $this->getMboxPosition($options, $firstid);
139 $lines =& $this->callHelper('-d', $options, $msg_headers);
140 if (!$lines) {
141 return $headers;
142 }
143 $headers = array();
144 $in_message = false;
145 $get_pos = true;
146 $hname = null;
147 foreach ($lines as $key=>&$line) {
148 if (!$in_message) {
149 if (!empty($line)) {
150 $id = intval($line);
151 $in_message = true;
152 $get_pos = true;
153 }
154 } elseif ($get_pos) {
155 $headers[$id] = array('beginning' => intval($line));
156 $get_pos = false;
157 } elseif (empty($line) && empty($hname)) {
158 $in_message = false;
159 } elseif (empty($hname)) {
160 $hname = $line;
161 } elseif ($hname == 'date') {
162 $headers[$id][$hname] = @strtotime($line);
163 $hname = null;
164 } else {
165 BananaMimePart::decodeHeader($line, $hname);
166 $headers[$id][$hname] = $line;
167 $hname = null;
168 }
169 unset($lines[$key]);
170 }
171 return $headers;
172 }
173
174 /** Add storage data in spool overview
175 */
176 public function updateSpool(array &$messages)
177 {
178 foreach ($messages as $id=>&$data) {
179 if (isset(Banana::$spool->overview[$id])) {
180 Banana::$spool->overview[$id]->storage['offset'] = $data['beginning'];
181 }
182 }
183 Banana::$spool->storage['size'] = @filesize($this->getFileName());
184 }
185
186 /** Return the indexes of the new messages since the give date
187 * @return Array(MSGNUM of new messages)
188 */
189 public function getNewIndexes($since)
190 {
191 $this->open();
192 if (is_null($this->file)) {
193 return array();
194 }
195 if (is_null($this->new_messages)) {
196 $this->getCount();
197 }
198 return range($this->count - $this->new_messages, $this->count - 1);
199 }
200
201 /** Return wether or not the protocole can be used to add new messages
202 */
203 public function canSend()
204 {
205 return true;
206 }
207
208 /** Return false because we can't cancel a mail
209 */
210 public function canCancel()
211 {
212 return false;
213 }
214
215 /** Return the list of requested headers
216 * @return Array('header1', 'header2', ...) with the key 'dest' for the destination header
217 * and 'reply' for the reply header, eg:
218 * * for a mail: Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To')
219 * * for a post: Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To')
220 */
221 public function requestedHeaders()
222 {
223 return Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To');
224 }
225
226 /** Send a message
227 * @return true if it was successfull
228 */
229 public function send(BananaMessage &$message)
230 {
231 $headers = $message->getHeaders();
232 $to = $headers['To'];
233 $subject = $headers['Subject'];
234 unset($headers['To']);
235 unset($headers['Subject']);
236 $hdrs = '';
237 foreach ($headers as $key=>$value) {
238 if (!empty($value)) {
239 $hdrs .= "$key: $value\r\n";
240 }
241 }
242 $body = $message->get(false);
243 return mail($to, $subject, $body, $hdrs);
244 }
245
246 /** Cancel a message
247 * @return true if it was successfull
248 */
249 public function cancel(BananaMessage &$message)
250 {
251 return false;
252 }
253
254 /** Return the protocole name
255 */
256 public function name()
257 {
258 return 'MBOX';
259 }
260
261 /** Return the spool filename
262 */
263 public function filename()
264 {
265 @list($mail, $domain) = explode('@', Banana::$group);
266 $file = "";
267 if (isset($domain)) {
268 $file = $domain . '_';
269 }
270 return $file . $mail;
271 }
272
273 /** Return the execution backtrace
274 */
275 public function backtrace()
276 {
277 if ($this->debug) {
278 return $this->bt;
279 } else {
280 return null;
281 }
282 }
283
284 #######
285 # Filesystem functions
286 #######
287
288 protected function getFileName()
289 {
290 if (is_null(Banana::$group)) {
291 return null;
292 }
293 @list($mail, $domain) = explode('@', Banana::$group);
294 return Banana::$mbox_path . '/' . $mail;
295 }
296
297 #######
298 # MBox parser
299 #######
300
301 /** Add the '-p' optioin for callHelper
302 */
303 private function getMBoxPosition(array &$options, $id = null)
304 {
305 if (Banana::$spool && Banana::$spool->overview) {
306 if (!is_null($id) && isset(Banana::$spool->overview[$id])) {
307 $key = $id;
308 } else {
309 $key = max(array_keys(Banana::$spool->overview));
310 if (!is_null($id) && $key >= $id) {
311 return;
312 }
313 }
314 if (isset(Banana::$spool->overview[$key]->storage['offset'])) {
315 $options[] = '-p ' . $key . ':' . Banana::$spool->overview[$key]->storage['offset'];
316 }
317 }
318 }
319
320 private function &callHelper($action, array $options = array(), array $headers = array())
321 {
322 $action .= ' -f ' . $this->getFileName();
323 $cmd = Banana::$mbox_helper . " $action " . implode(' ', $options) . ' ' . implode(' ', $headers);
324 if ($this->debug) {
325 $start = microtime(true);
326 }
327 exec($cmd, $out, $return);
328 if ($this->debug) {
329 $this->bt[] = array('action' => $cmd, 'time' => (microtime(true) - $start),
330 'code' => $return, 'response' => count($out), 'error' => $return ? "Helper failed" : null);
331 }
332 if ($return != 0) {
333 $this->_lasterrorno = 1;
334 $this->_lasterrorcode = "Helper failed";
335 $out = null;
336 }
337 return $out;
338 }
339 }
340
341 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
342 ?>