diff --git a/modules/core/hm-mailbox.php b/modules/core/hm-mailbox.php index 1b2438283..eddcc6394 100644 --- a/modules/core/hm-mailbox.php +++ b/modules/core/hm-mailbox.php @@ -119,7 +119,11 @@ public function create_folder($folder, $parent = null) { } if ($this->is_imap()) { $new_folder = prep_folder_name($this->connection, $folder, false, $parent); - return $this->connection->create_mailbox($new_folder); + if ($this->connection->create_mailbox($new_folder)) { + return $new_folder; + } else { + return false; + } } else { return $this->connection->create_folder($folder, $parent); } @@ -249,7 +253,7 @@ public function get_special_use_mailboxes($folder = false) { * Get messages in a folder applying filters, sorting and pagination * @return array - [total results found, results for a single page] */ - public function get_messages($folder, $sort, $reverse, $flag_filter, $offset=0, $limit=0, $keyword=false, $trusted_senders=[], $include_preview = false) { + public function get_messages($folder, $sort, $reverse, $flag_filter, $offset=0, $limit=50, $keyword=false, $trusted_senders=[], $include_preview = false) { if (! $this->select_folder($folder)) { return; } diff --git a/modules/imap/functions.php b/modules/imap/functions.php index 3364985bc..5d4146703 100644 --- a/modules/imap/functions.php +++ b/modules/imap/functions.php @@ -1335,8 +1335,11 @@ function snooze_message($mailbox, $msg_id, $folder, $snooze_tag) { $res = false; $snooze_folder = 'Snoozed'; if ($snooze_tag) { - if (!count($mailbox->get_folder_status($snooze_folder))) { - $mailbox->create_folder($snooze_folder); + $status = $mailbox->get_folder_status($snooze_folder); + if (! count($status)) { + $snooze_folder = $mailbox->create_folder($snooze_folder); + } else { + $snooze_folder = $status['id']; } if ($mailbox->store_message($snooze_folder, $msg)) { $deleteResult = $mailbox->message_action($folder, 'DELETE', array($msg_id)); diff --git a/modules/imap/handler_modules.php b/modules/imap/handler_modules.php index b23efc690..4f7d53dbb 100644 --- a/modules/imap/handler_modules.php +++ b/modules/imap/handler_modules.php @@ -1083,9 +1083,11 @@ public function process() { $mailbox = Hm_IMAP_List::get_connected_mailbox($server_id, $this->cache); if ($mailbox && $mailbox->authed()) { $folder = 'Snoozed'; - if (! count($mailbox->get_folder_status($folder))) { + $status = $mailbox->get_folder_status($folder); + if (! count($status)) { continue; } + $folder = $status['id']; $ret = $mailbox->get_messages($folder, 'DATE', false, 'ALL'); foreach ($ret[1] as $msg) { $msg_headers = $mailbox->get_message_headers($folder, $msg['uid']); diff --git a/modules/imap/hm-ews.php b/modules/imap/hm-ews.php index 5ebf0c6ed..9b34fa702 100644 --- a/modules/imap/hm-ews.php +++ b/modules/imap/hm-ews.php @@ -96,9 +96,8 @@ public function get_folders($folder = null, $only_subscribed = false, $unsubscri 'basename' => $name, 'realname' => $name, 'namespace' => '', - // TODO - flags - 'marked' => false, - 'noselect' => false, + 'marked' => false, // doesn't seem to be used anywhere but imap returns it + 'noselect' => false, // all EWS folders are selectable 'can_have_kids' => true, 'has_kids' => $folder->get('childFolderCount') > 0, 'children' => $folder->get('childFolderCount'), @@ -145,11 +144,18 @@ public function get_folder_status($folder) { try { if ($this->is_distinguished_folder($folder)) { $folder = new Type\DistinguishedFolderIdType($folder); - } else { + $result = $this->api->getFolder($folder->toArray(true)); + } elseif (base64_encode(base64_decode($folder, true)) === $folder) { $folder = new Type\FolderIdType($folder); + $result = $this->api->getFolder($folder->toArray(true)); + } else { + $result = $this->api->getFolderByDisplayName($folder, Enumeration\DistinguishedFolderIdNameType::MESSAGE_ROOT); + if (! $result) { + throw new Exception('Folder not found.'); + } } - $result = $this->api->getFolder($folder->toArray(true)); return [ + 'id' => $result->get('folderId')->get('id'), 'name' => $result->get('displayName'), 'messages' => $result->get('totalCount'), 'uidvalidity' => false, @@ -173,9 +179,14 @@ public function create_folder($folder, $parent = null) { $parent = new Type\FolderIdType($parent); } try { - $result = $this->api->createFolders([$folder], $parent); - print_r($result); - exit; + $request = [ + 'Folders' => ['Folder' => [ + 'DisplayName' => $folder + ]], + 'ParentFolderId' => $parent->toArray(true), + ]; + $result = $this->ews->CreateFolder($request); + return $result->get('id'); } catch(\Exception $e) { Hm_Msgs::add('ERR' . $e->getMessage()); return false; @@ -288,9 +299,7 @@ public function store_message($folder, $message, $seen = true, $draft = false) { 'MessageDisposition' => 'SaveOnly', 'SavedItemFolderId' => $folder->toArray(true), ]); - print_r($result); - exit; - return true; + return $result->get('id'); } catch (\Exception $e) { Hm_Msgs::add('ERR' . $e->getMessage()); return false; @@ -386,7 +395,7 @@ public function search($folder, $sort, $reverse, $flag_filter, $offset, $limit, $qs[] = "Subject:($term[1])"; break; default: - // TODO: check for other types of terms + // noop } } } elseif (! empty($keyword)) { diff --git a/modules/imap/hm-imap.php b/modules/imap/hm-imap.php index 87b78ac2b..13e842665 100644 --- a/modules/imap/hm-imap.php +++ b/modules/imap/hm-imap.php @@ -837,6 +837,7 @@ public function get_mailbox_status($mailbox, $args=array('UNSEEN', 'UIDVALIDITY' if ($this->check_response($response, true)) { $attributes = $this->parse_status_response($response); $this->check_mailbox_state_change($attributes); + $attributes['id'] = $mailbox; } return $attributes; }