Skip to content

Commit

Permalink
Support flags and date arguments in APPEND command
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpl committed Oct 11, 2012
1 parent 485f23b commit 00891e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
6 changes: 4 additions & 2 deletions program/include/rcube_imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2232,13 +2232,15 @@ public function save_message($folder, &$message, $headers='', $is_file=false)
return false;
}

$flags = array('SEEN');

// make sure folder exists
if ($this->folder_exists($folder)) {
if ($is_file) {
$saved = $this->conn->appendFromFile($folder, $message, $headers);
$saved = $this->conn->appendFromFile($folder, $message, $headers, $flags);
}
else {
$saved = $this->conn->append($folder, $message);
$saved = $this->conn->append($folder, $message, $flags);
}
}

Expand Down
47 changes: 39 additions & 8 deletions program/include/rcube_imap_generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2540,10 +2540,12 @@ function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=NULL,
*
* @param string $mailbox Mailbox name
* @param string $message Message content
* @param array $flags Message flags
* @param string $date Message internal date
*
* @return string|bool On success APPENDUID response (if available) or True, False on failure
*/
function append($mailbox, &$message)
function append($mailbox, &$message, $flags = array(), $date = null)
{
unset($this->data['APPENDUID']);

Expand All @@ -2559,12 +2561,17 @@ function append($mailbox, &$message)
return false;
}

// build APPEND command
$key = $this->nextTag();
$request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox),
$len, ($this->prefs['literal+'] ? '+' : ''));
$request = "$key APPEND " . $this->escape($mailbox) . ' (' . $this->flagsToStr($flags) . ')';
if (!empty($date)) {
$request .= ' ' . $this->escape($date);
}
$request .= ' {' . $len . ($this->prefs['literal+'] ? '+' : '') . '}';

// send APPEND command
if ($this->putLine($request)) {
// Don't wait when LITERAL+ is supported
// Do not wait when LITERAL+ is supported
if (!$this->prefs['literal+']) {
$line = $this->readReply();

Expand Down Expand Up @@ -2605,10 +2612,12 @@ function append($mailbox, &$message)
* @param string $mailbox Mailbox name
* @param string $path Path to the file with message body
* @param string $headers Message headers
* @param array $flags Message flags
* @param string $date Message internal date
*
* @return string|bool On success APPENDUID response (if available) or True, False on failure
*/
function appendFromFile($mailbox, $path, $headers=null)
function appendFromFile($mailbox, $path, $headers=null, $flags = array(), $date = null)
{
unset($this->data['APPENDUID']);

Expand Down Expand Up @@ -2639,11 +2648,15 @@ function appendFromFile($mailbox, $path, $headers=null)
$len += strlen($headers) + strlen($body_separator);
}

// send APPEND command
// build APPEND command
$key = $this->nextTag();
$request = sprintf("$key APPEND %s (\\Seen) {%d%s}", $this->escape($mailbox),
$len, ($this->prefs['literal+'] ? '+' : ''));
$request = "$key APPEND " . $this->escape($mailbox) . ' (' . $this->flagsToStr($flags) . ')';
if (!empty($date)) {
$request .= ' ' . $this->escape($date);
}
$request .= ' {' . $len . ($this->prefs['literal+'] ? '+' : '') . '}';

// send APPEND command
if ($this->putLine($request)) {
// Don't wait when LITERAL+ is supported
if (!$this->prefs['literal+']) {
Expand Down Expand Up @@ -3545,6 +3558,24 @@ private function _xor($string, $string2)
return $result;
}

/**
* Converts flags array into string for inclusion in IMAP command
*
* @param array $flags Flags (see self::flags)
*
* @return string Space-separated list of flags
*/
private function flagsToStr($flags)
{
foreach ((array)$flags as $idx => $flag) {
if ($flag = $this->flags[strtoupper($flag)]) {
$flags[$idx] = $flag;
}
}

return implode(' ', (array)$flags);
}

/**
* Converts datetime string into unix timestamp
*
Expand Down

0 comments on commit 00891e6

Please sign in to comment.