protected function _parseEnvelope(Horde_Imap_Client_Tokenize $data)
{
// 'route', the 2nd element, is deprecated by RFC 2822.
$addr_structure = array(0 => 'personal', 2 => 'mailbox', 3 => 'host');
$env_data = array(0 => 'date', 1 => 'subject', 2 => 'from', 3 => 'sender', 4 => 'reply_to', 5 => 'to', 6 => 'cc', 7 => 'bcc', 8 => 'in_reply_to', 9 => 'message_id');
$addr_ob = new Horde_Mail_Rfc822_Address();
$env_addrs = $this->getParam('envelope_addrs');
$env_str = $this->getParam('envelope_string');
$key = 0;
$ret = new Horde_Imap_Client_Data_Envelope();
while (($val = $data->next()) !== false) {
if (!isset($env_data[$key]) || is_null($val)) {
++$key;
continue;
}
if (is_string($val)) {
// These entries are text fields.
$ret->{$env_data[$key]} = substr($val, 0, $env_str);
} else {
// These entries are address structures.
$group = null;
$key2 = 0;
$tmp = new Horde_Mail_Rfc822_List();
while ($data->next() !== false) {
$a_val = $data->flushIterator();
// RFC 3501 [7.4.2]: Group entry when host is NIL.
// Group end when mailbox is NIL; otherwise, this is
// mailbox name.
if (is_null($a_val[3])) {
if (is_null($a_val[2])) {
$group = null;
} else {
$group = new Horde_Mail_Rfc822_Group($a_val[2]);
$tmp->add($group);
}
} else {
$addr = clone $addr_ob;
foreach ($addr_structure as $add_key => $add_val) {
if (!is_null($a_val[$add_key])) {
$addr->{$add_val} = $a_val[$add_key];
}
}
if ($group) {
$group->addresses->add($addr);
} else {
$tmp->add($addr);
}
}
if (++$key2 >= $env_addrs) {
$data->flushIterator(false);
break;
}
}
$ret->{$env_data[$key]} = $tmp;
}
++$key;
}
return $ret;
}