- $pos = $this->messages[$id]['beginning'];
- }
- if (fseek($this->file, $pos) == 0) {
- $this->current_id = $id;
- $this->at_beginning = true;
- return true;
- } else {
- $this->current_id = null;
- $this->_lasterrno = 2;
- $this->_lasterror = _b_('Can\'t find message ') . $id;
- return false;
- }
- } else {
- $max = @max(array_keys(Banana::$spool->overview));
- if (is_null($max)) {
- $max = 0;
- }
- if ($id <= $max && $max != 0) {
- $this->current_id = null;
- $this->_lasterrno = 3;
- $this->_lasterror = _b_('Invalid message index ') . $id;
- return false;
- }
- if (!$this->goTo($max)) {
- return false;
- }
- if (feof($this->file)) {
- $this->current_id = null;
- $this->_lasterrno = 4;
- $this->_lasterror = _b_('Requested index does not exists or file has been truncated');
- return false;
- }
- while ($this->readCurrentMessage(true) && $this->current_id < $id);
- if ($this->current_id == $id) {
- return true;
- }
- $this->current_id = null;
- $this->_lasterrno = 5;
- $this->_lasterror = _b_('Requested index does not exists or file has been truncated');
- return false;
- }
- }
-
- private function countMessages($from = 0)
- {
- $this->messages =& $this->readMessages(array($from), true, true);
- return count($this->messages);
- }
-
- /** Read the current message (identified by current_id)
- * @param needFrom_ BOOLEAN is true if the first line *must* be a From_ line
- * @param alignNext BOOLEAN is true if the buffer must be aligned at the beginning of the next From_ line
- * @return message sources (without storage data)
- */
- private function &readCurrentMessage($stripBody = false, $needFrom_ = true, $alignNext = true)
- {
- $file_cache =& $this->file_cache;
- if ($file_cache && $file_cache != ftell($this->file)) {
- $file_cache = null;
- }
- $msg = array();
- $canFrom_ = false;
- $inBody = false;
- while(!feof($this->file)) {
- // Process file cache
- if ($file_cache) { // this is a From_ line
- $needFrom_ = false;
- $this->at_beginning = false;
- $file_cache = null;
- continue;
- }
-
- // Read a line
- $line = rtrim(fgets($this->file), "\r\n");
-
- // Process From_ line
- if ($needFrom_ || !$msg || $canFrom_) {
- if (substr($line, 0, 5) == 'From ') { // this is a From_ line
- if ($needFrom_) {
- $needFrom = false;
- } elseif (!$msg) {
- continue;
- } else {
- $this->current_id++; // we are finally in the next message
- if ($alignNext) { // align the file pointer at the beginning of the new message
- $this->at_beginning = true;
- $file_cache = ftell($this->file);
- }
- break;
- }
- } elseif ($needFrom_) {
- return $msg;