c2f7d4a72940a9ee5d23da102195535db7f46a87
2 /********************************************************************************
3 * banana/protocoleinterface.inc.php : interface for box access
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__
) . '/protocoleinterface.inc.php';
12 require_once dirname(__FILE__
) . '/message.inc.php';
14 class BananaMBox
implements BananaProtocoleInterface
17 private $filesize = null
;
18 private $current_id = null
;
19 private $at_beginning = false
;
20 private $file_cache = null
;
22 private $_lasterrno = 0;
23 private $_lasterror = null
;
25 private $count = null
;
26 private $new_messages = null
;
27 private $messages = null
;
29 /** Build a protocole handler plugged on the given box
31 public function __construct()
33 $filename = $this->getFileName(Banana
::$group);
34 if (is_null($filename)) {
37 $this->filesize
= filesize($filename);
38 $this->file
= @fopen
($filename, 'r');
40 $this->_lasterrno
= 1;
41 $this->_lasterror
= _b_('Can\'t open file');
44 $this->current_id
= 0;
45 $this->at_beginning
= true
;
50 public function __destruct()
57 /** Indicate if the Protocole handler has been succesfully built
59 public function isValid()
61 return !Banana
::$group ||
$this->file
;
64 /** Indicate last error n°
66 public function lastErrNo()
68 return $this->_lasterrno
;;
71 /** Indicate last error text
73 public function lastError()
75 return $this->_lasterror
;
78 /** Return the description of the current box
80 public function getDescription()
85 /** Return the list of the boxes
86 * @param mode Kind of boxes to list
87 * @param since date of last check (for new boxes and new messages)
88 * @param withstats Indicated whether msgnum and unread must be set in the result
89 * @return Array(boxname => array(desc => boxdescripton, msgnum => number of message, unread =>number of unread messages)
91 public function getBoxList($mode = Banana
::BOXES_ALL
, $since = 0, $withstats = false
)
93 return array(Banana
::$group => array('desc' => '', 'msgnum' => 0, 'unread' => 0));
97 * @param id Id of the emssage (can be either an Message-id or a message index)
98 * @param msg_headers Headers to process
99 * @param is_msgid If is set, $id is en Message-Id
100 * @return A BananaMessage or null if the given id can't be retreived
102 public function getMessage($id, array $msg_headers = array(), $is_msgid = false
)
104 if ($is_msgid ||
!is_numeric($id)) {
105 if (is_null(Banana
::$spool)) {
108 $id = Banana
::$spool->ids
[$id];
110 $message = $this->readMessages(array($id));
111 if (empty($message)) {
114 $msg = new BananaMessage($message[$id]['message']);
118 private function getCount()
120 $this->count
= count(Banana
::$spool->overview
);
121 $max = @max
(array_keys(Banana
::$spool->overview
));
122 if ($max && Banana
::$spool->overview
[$max]->storage
['next'] == $this->filesize
) {
123 $this->new_messages
= 0;
125 $this->new_messages
= $this->countMessages($this->count
);
126 $this->count +
= $this->new_messages
;
130 /** Return the indexes of the messages presents in the Box
131 * @return Array(number of messages, MSGNUM of the first message, MSGNUM of the last message)
133 public function getIndexes()
135 if (is_null($this->count
)) {
138 return array($this->count
, 0, $this->count
- 1);
141 /** Return the message headers (in BananaMessage) for messages from firstid to lastid
142 * @return Array(id => array(headername => headervalue))
144 public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
146 $msg_headers = array_map('strtolower', $msg_headers);
147 $messages =& $this->readMessages(range($firstid, $lastid), true
);
148 $msg_headers = array_map('strtolower', $msg_headers);
150 foreach ($msg_headers as $header) {
151 foreach ($messages as $id=>&$message) {
152 if (!isset($headers[$id])) {
153 $headers[$id] = array('beginning' => $message['beginning'], 'end' => $message['end']);
155 if ($header == 'date') {
156 $headers[$id][$header] = strtotime($message['message'][$header]);
158 $headers[$id][$header] = $message['message'][$header];
162 unset($this->messages
);
167 /** Add storage data in spool overview
169 public function updateSpool(array &$messages)
171 foreach ($messages as $id=>&$data) {
172 if (isset(Banana
::$spool->overview
[$id])) {
173 Banana
::$spool->overview
[$id]->storage
['offset'] = $data['beginning'];
174 Banana
::$spool->overview
[$id]->storage
['next'] = $data['end'];
179 /** Return the indexes of the new messages since the give date
180 * @return Array(MSGNUM of new messages)
182 public function getNewIndexes($since)
184 if (is_null($this->new_messages
)) {
187 return range($this->count
- $this->new_messages
, $this->count
- 1);
190 /** Return wether or not the protocole can be used to add new messages
192 public function canSend()
197 /** Return false because we can't cancel a mail
199 public function canCancel()
204 /** Return the list of requested headers
205 * @return Array('header1', 'header2', ...) with the key 'dest' for the destination header
206 * and 'reply' for the reply header, eg:
207 * * for a mail: Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To')
208 * * for a post: Array('From', 'Subject', 'dest' => 'Newsgroups', 'reply' => 'Followup-To')
210 public function requestedHeaders()
212 return Array('From', 'Subject', 'dest' => 'To', 'Cc', 'Bcc', 'reply' => 'Reply-To');
216 * @return true if it was successfull
218 public function send(BananaMessage
&$message)
224 * @return true if it was successfull
226 public function cancel(BananaMessage
&$message)
231 /** Return the protocole name
233 public function name()
239 # Filesystem functions
242 protected function getFileName($box)
247 @list
($mail, $domain) = explode('@', $box);
248 if ($mail == 'staff') {
249 return '/home/x2003bruneau/staff.polytechnique.org_innovation.mbox';
251 return '/var/mail/' . $mail;
259 /** Go to the given message
261 private function goTo($id)
263 if ($this->current_id
== $id && $this->at_beginning
) {
267 fseek($this->file
, 0);
268 $this->current_id
= 0;
269 $this->at_beginning
= true
;
271 } elseif (isset(Banana
::$spool->overview
[$id]) ||
isset($this->messages
[$id])) {
272 if (isset(Banana
::$spool->overview
[$id])) {
273 $pos = Banana
::$spool->overview
[$id]->storage
['offset'];
275 $pos = $this->messages
[$id]['beginning'];
277 if (fseek($this->file
, $pos) == 0) {
278 $this->current_id
= $id;
279 $this->at_beginning
= true
;
282 $this->current_id
= null
;
283 $this->_lasterrno
= 2;
284 $this->_lasterror
= _b_('Can\'t find message ') . $id;
288 $max = @max
(array_keys(Banana
::$spool->overview
));
292 if ($id <= $max && $max != 0) {
293 $this->current_id
= null
;
294 $this->_lasterrno
= 3;
295 $this->_lasterror
= _b_('Invalid message index ') . $id;
298 if (!$this->goTo($max)) {
301 if (feof($this->file
)) {
302 $this->current_id
= null
;
303 $this->_lasterrno
= 4;
304 $this->_lasterror
= _b_('Requested index does not exists or file has been truncated');
307 while ($this->readCurrentMessage(true
) && $this->current_id
< $id);
308 if ($this->current_id
== $id) {
311 $this->current_id
= null
;
312 $this->_lasterrno
= 5;
313 $this->_lasterror
= _b_('Requested index does not exists or file has been truncated');
318 private function countMessages($from = 0)
320 $this->messages
=& $this->readMessages(array($from), true
, true
);
321 return count($this->messages
);
324 /** Read the current message (identified by current_id)
325 * @param needFrom_ BOOLEAN is true if the first line *must* be a From_ line
326 * @param alignNext BOOLEAN is true if the buffer must be aligned at the beginning of the next From_ line
327 * @return message sources (without storage data)
329 private function &readCurrentMessage($stripBody = false
, $needFrom_ = true
, $alignNext = true
)
331 $file_cache =& $this->file_cache
;
332 if ($file_cache && $file_cache != ftell($this->file
)) {
338 while(!feof($this->file
)) {
339 // Process file cache
340 if ($file_cache) { // this is a From_ line
342 $this->at_beginning
= false
;
348 $line = rtrim(fgets($this->file
), "\r\n");
350 // Process From_ line
351 if ($needFrom_ ||
!$msg ||
$canFrom_) {
352 if (substr($line, 0, 5) == 'From ') { // this is a From_ line
358 $this->current_id++
; // we are finally in the next message
359 if ($alignNext) { // align the file pointer at the beginning of the new message
360 $this->at_beginning
= true
;
361 $file_cache = ftell($this->file
);
365 } elseif ($needFrom_) {
370 // Process non-From_ lines
371 if (substr($line, 0, 6) == '>From ') { // remove inline From_ quotation
372 $line = substr($line, 1);
374 if (!$stripBody ||
!$inBody) {
375 $msg[] = $line; // add the line to the message source
377 $canFrom_ = empty($line); // check if next line can be a From_ line
378 if ($canFrom_ && !$inBody && $stripBody) {
381 $this->at_beginning
= false
;
383 if (!feof($this->file
) && !$canFrom_) {
389 /** Read message with the given ids
390 * @param ids ARRAY of ids to look for
391 * @param strip BOOLEAN if true, only headers are retrieved
392 * @param from BOOLEAN if true, process all messages from max(ids) to the end of the mbox
393 * @return Array(Array('message' => message sources (or parsed message headers if $strip is true),
394 * 'beginning' => offset of message beginning,
395 * 'end' => offset of message end))
397 private function &readMessages(array $ids, $strip = false
, $from = false
)
399 if (!is_null($this->messages
)) {
400 return $this->messages
;
404 while ((count($ids) ||
$from) && !feof($this->file
)) {
406 $id = array_shift($ids);
410 if ($id != $this->current_id ||
!$this->at_beginning
) {
411 if (!$this->goTo($id)) {
415 $beginning = ftell($this->file
);
416 $message =& $this->readCurrentMessage($strip, false
);
418 $message =& BananaMimePart
::parseHeaders($message);
420 $end = ftell($this->file
);
421 $messages[$id] = array('message' => $message, 'beginning' => $beginning, 'end' => $end);
427 // vim:set et sw=4 sts=4 ts=4: