LibIMAP+Mail+test-imap: Let a Promise result type be non-optional

The Empty type was used only when parsing a response failed. The
failures will now go to Promise's error type.
This commit is contained in:
Karol Kosek 2023-08-29 15:15:41 +02:00 committed by Andrew Kaster
parent 1f605407bd
commit 90af21aef4
Notes: sideshowbarker 2024-07-17 00:16:31 +09:00
4 changed files with 71 additions and 72 deletions

View file

@ -137,7 +137,7 @@ ErrorOr<bool> MailWidget::connect_and_login()
m_statusbar->set_text(String::formatted("Connected. Logging in as {}...", username).release_value_but_fixme_should_propagate_errors()); m_statusbar->set_text(String::formatted("Connected. Logging in as {}...", username).release_value_but_fixme_should_propagate_errors());
auto response = TRY(m_imap_client->login(username, password)->await()).release_value(); auto response = TRY(m_imap_client->login(username, password)->await());
if (response.status() != IMAP::ResponseStatus::OK) { if (response.status() != IMAP::ResponseStatus::OK) {
dbgln("Failed to login. The server says: '{}'", response.response_text()); dbgln("Failed to login. The server says: '{}'", response.response_text());
@ -147,7 +147,7 @@ ErrorOr<bool> MailWidget::connect_and_login()
} }
m_statusbar->set_text("Logged in. Loading mailboxes..."_string); m_statusbar->set_text("Logged in. Loading mailboxes..."_string);
response = TRY(m_imap_client->list(""sv, "*"sv)->await()).release_value(); response = TRY(m_imap_client->list(""sv, "*"sv)->await());
if (response.status() != IMAP::ResponseStatus::OK) { if (response.status() != IMAP::ResponseStatus::OK) {
dbgln("Failed to retrieve mailboxes. The server says: '{}'", response.response_text()); dbgln("Failed to retrieve mailboxes. The server says: '{}'", response.response_text());
@ -174,7 +174,7 @@ void MailWidget::on_window_close()
// User closed main window before a connection was established // User closed main window before a connection was established
return; return;
} }
auto response = move(MUST(m_imap_client->send_simple_command(IMAP::CommandType::Logout)->await()).release_value().get<IMAP::SolidResponse>()); auto response = move(MUST(m_imap_client->send_simple_command(IMAP::CommandType::Logout)->await()).get<IMAP::SolidResponse>());
VERIFY(response.status() == IMAP::ResponseStatus::OK); VERIFY(response.status() == IMAP::ResponseStatus::OK);
m_imap_client->close(); m_imap_client->close();
@ -265,7 +265,7 @@ void MailWidget::selected_mailbox()
if (mailbox.flags & (unsigned)IMAP::MailboxFlag::NoSelect) if (mailbox.flags & (unsigned)IMAP::MailboxFlag::NoSelect)
return; return;
auto response = MUST(m_imap_client->select(mailbox.name)->await()).release_value(); auto response = MUST(m_imap_client->select(mailbox.name)->await());
if (response.status() != IMAP::ResponseStatus::OK) { if (response.status() != IMAP::ResponseStatus::OK) {
dbgln("Failed to select mailbox. The server says: '{}'", response.response_text()); dbgln("Failed to select mailbox. The server says: '{}'", response.response_text());
@ -297,7 +297,7 @@ void MailWidget::selected_mailbox()
}, },
}; };
auto fetch_response = MUST(m_imap_client->fetch(fetch_command, false)->await()).release_value(); auto fetch_response = MUST(m_imap_client->fetch(fetch_command, false)->await());
if (fetch_response.status() != IMAP::ResponseStatus::OK) { if (fetch_response.status() != IMAP::ResponseStatus::OK) {
dbgln("Failed to retrieve subject/from for e-mails. The server says: '{}'", response.response_text()); dbgln("Failed to retrieve subject/from for e-mails. The server says: '{}'", response.response_text());
m_statusbar->set_text(String::formatted("[{}]: Failed to fetch messages :^(", mailbox.name).release_value_but_fixme_should_propagate_errors()); m_statusbar->set_text(String::formatted("[{}]: Failed to fetch messages :^(", mailbox.name).release_value_but_fixme_should_propagate_errors());
@ -452,7 +452,7 @@ void MailWidget::selected_email_to_load()
}, },
}; };
auto fetch_response = MUST(m_imap_client->fetch(fetch_command, false)->await()).release_value(); auto fetch_response = MUST(m_imap_client->fetch(fetch_command, false)->await());
if (fetch_response.status() != IMAP::ResponseStatus::OK) { if (fetch_response.status() != IMAP::ResponseStatus::OK) {
dbgln("Failed to retrieve the body structure of the selected e-mail. The server says: '{}'", fetch_response.response_text()); dbgln("Failed to retrieve the body structure of the selected e-mail. The server says: '{}'", fetch_response.response_text());
@ -516,7 +516,7 @@ void MailWidget::selected_email_to_load()
}, },
}; };
fetch_response = MUST(m_imap_client->fetch(fetch_command, false)->await()).release_value(); fetch_response = MUST(m_imap_client->fetch(fetch_command, false)->await());
if (fetch_response.status() != IMAP::ResponseStatus::OK) { if (fetch_response.status() != IMAP::ResponseStatus::OK) {
dbgln("Failed to retrieve the body of the selected e-mail. The server says: '{}'", fetch_response.response_text()); dbgln("Failed to retrieve the body of the selected e-mail. The server says: '{}'", fetch_response.response_text());

View file

@ -171,12 +171,12 @@ ErrorOr<void> Client::send_raw(StringView data)
return {}; return {};
} }
NonnullRefPtr<Promise<Optional<Response>>> Client::send_command(Command&& command) NonnullRefPtr<Promise<Response>> Client::send_command(Command&& command)
{ {
m_command_queue.append(move(command)); m_command_queue.append(move(command));
m_current_command++; m_current_command++;
auto promise = Promise<Optional<Response>>::construct(); auto promise = Promise<Response>::construct();
m_pending_promises.append(promise); m_pending_promises.append(promise);
if (m_pending_promises.size() == 1) { if (m_pending_promises.size() == 1) {
@ -189,22 +189,22 @@ NonnullRefPtr<Promise<Optional<Response>>> Client::send_command(Command&& comman
} }
template<typename T> template<typename T>
NonnullRefPtr<Promise<Optional<T>>> cast_promise(NonnullRefPtr<Promise<Optional<Response>>> promise_variant) NonnullRefPtr<Promise<T>> cast_promise(NonnullRefPtr<Promise<Response>> promise_variant)
{ {
auto new_promise = promise_variant->map<Optional<T>>( auto new_promise = promise_variant->map<T>(
[](Optional<Response>& variant) { [](Response& variant) {
return move(variant->get<T>()); return move(variant.get<T>());
}); });
return new_promise; return new_promise;
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::login(StringView username, StringView password) NonnullRefPtr<Promise<SolidResponse>> Client::login(StringView username, StringView password)
{ {
auto command = Command { CommandType::Login, m_current_command, { serialize_astring(username), serialize_astring(password) } }; auto command = Command { CommandType::Login, m_current_command, { serialize_astring(username), serialize_astring(password) } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::list(StringView reference_name, StringView mailbox) NonnullRefPtr<Promise<SolidResponse>> Client::list(StringView reference_name, StringView mailbox)
{ {
auto command = Command { CommandType::List, m_current_command, auto command = Command { CommandType::List, m_current_command,
{ DeprecatedString::formatted("\"{}\"", reference_name), { DeprecatedString::formatted("\"{}\"", reference_name),
@ -212,7 +212,7 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::list(StringView referenc
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::lsub(StringView reference_name, StringView mailbox) NonnullRefPtr<Promise<SolidResponse>> Client::lsub(StringView reference_name, StringView mailbox)
{ {
auto command = Command { CommandType::ListSub, m_current_command, auto command = Command { CommandType::ListSub, m_current_command,
{ DeprecatedString::formatted("\"{}\"", reference_name), { DeprecatedString::formatted("\"{}\"", reference_name),
@ -220,19 +220,19 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::lsub(StringView referenc
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::fetch(FetchCommand request, bool uid) NonnullRefPtr<Promise<SolidResponse>> Client::fetch(FetchCommand request, bool uid)
{ {
auto command = Command { uid ? CommandType::UIDFetch : CommandType::Fetch, m_current_command, { request.serialize() } }; auto command = Command { uid ? CommandType::UIDFetch : CommandType::Fetch, m_current_command, { request.serialize() } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<Response>>> Client::send_simple_command(CommandType type) NonnullRefPtr<Promise<Response>> Client::send_simple_command(CommandType type)
{ {
auto command = Command { type, m_current_command, {} }; auto command = Command { type, m_current_command, {} };
return send_command(move(command)); return send_command(move(command));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::select(StringView string) NonnullRefPtr<Promise<SolidResponse>> Client::select(StringView string)
{ {
auto command = Command { CommandType::Select, m_current_command, { serialize_astring(string) } }; auto command = Command { CommandType::Select, m_current_command, { serialize_astring(string) } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
@ -255,8 +255,7 @@ ErrorOr<void> Client::handle_parsed_response(ParseStatus&& parse_status)
if (parse_status.response.has_value()) { if (parse_status.response.has_value()) {
m_expecting_response = false; m_expecting_response = false;
should_send_next = parse_status.response->has<SolidResponse>(); should_send_next = parse_status.response->has<SolidResponse>();
m_pending_promises.first()->resolve(move(parse_status.response)); m_pending_promises.take_first()->resolve(parse_status.response.release_value());
m_pending_promises.remove(0);
} }
if (should_send_next && !m_command_queue.is_empty()) { if (should_send_next && !m_command_queue.is_empty()) {
@ -286,25 +285,25 @@ ErrorOr<void> Client::send_next_command()
return {}; return {};
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::examine(StringView string) NonnullRefPtr<Promise<SolidResponse>> Client::examine(StringView string)
{ {
auto command = Command { CommandType::Examine, m_current_command, { serialize_astring(string) } }; auto command = Command { CommandType::Examine, m_current_command, { serialize_astring(string) } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::create_mailbox(StringView name) NonnullRefPtr<Promise<SolidResponse>> Client::create_mailbox(StringView name)
{ {
auto command = Command { CommandType::Create, m_current_command, { serialize_astring(name) } }; auto command = Command { CommandType::Create, m_current_command, { serialize_astring(name) } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::delete_mailbox(StringView name) NonnullRefPtr<Promise<SolidResponse>> Client::delete_mailbox(StringView name)
{ {
auto command = Command { CommandType::Delete, m_current_command, { serialize_astring(name) } }; auto command = Command { CommandType::Delete, m_current_command, { serialize_astring(name) } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::store(StoreMethod method, Sequence sequence_set, bool silent, Vector<DeprecatedString> const& flags, bool uid) NonnullRefPtr<Promise<SolidResponse>> Client::store(StoreMethod method, Sequence sequence_set, bool silent, Vector<DeprecatedString> const& flags, bool uid)
{ {
StringBuilder data_item_name; StringBuilder data_item_name;
switch (method) { switch (method) {
@ -330,7 +329,7 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::store(StoreMethod method
auto command = Command { uid ? CommandType::UIDStore : CommandType::Store, m_current_command, { sequence_set.serialize(), data_item_name.to_deprecated_string(), flags_builder.to_deprecated_string() } }; auto command = Command { uid ? CommandType::UIDStore : CommandType::Store, m_current_command, { sequence_set.serialize(), data_item_name.to_deprecated_string(), flags_builder.to_deprecated_string() } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::search(Optional<DeprecatedString> charset, Vector<SearchKey>&& keys, bool uid) NonnullRefPtr<Promise<SolidResponse>> Client::search(Optional<DeprecatedString> charset, Vector<SearchKey>&& keys, bool uid)
{ {
Vector<DeprecatedString> args; Vector<DeprecatedString> args;
if (charset.has_value()) { if (charset.has_value()) {
@ -344,21 +343,21 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::search(Optional<Deprecat
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<ContinueRequest>>> Client::idle() NonnullRefPtr<Promise<ContinueRequest>> Client::idle()
{ {
auto promise = send_simple_command(CommandType::Idle); auto promise = send_simple_command(CommandType::Idle);
return cast_promise<ContinueRequest>(promise); return cast_promise<ContinueRequest>(promise);
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::finish_idle() NonnullRefPtr<Promise<SolidResponse>> Client::finish_idle()
{ {
auto promise = Promise<Optional<Response>>::construct(); auto promise = Promise<Response>::construct();
m_pending_promises.append(promise); m_pending_promises.append(promise);
MUST(send_raw("DONE"sv)); MUST(send_raw("DONE"sv));
m_expecting_response = true; m_expecting_response = true;
return cast_promise<SolidResponse>(promise); return cast_promise<SolidResponse>(promise);
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::status(StringView mailbox, Vector<StatusItemType> const& types) NonnullRefPtr<Promise<SolidResponse>> Client::status(StringView mailbox, Vector<StatusItemType> const& types)
{ {
Vector<DeprecatedString> args; Vector<DeprecatedString> args;
for (auto type : types) { for (auto type : types) {
@ -388,7 +387,7 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::status(StringView mailbo
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::append(StringView mailbox, Message&& message, Optional<Vector<DeprecatedString>> flags, Optional<Core::DateTime> date_time) NonnullRefPtr<Promise<SolidResponse>> Client::append(StringView mailbox, Message&& message, Optional<Vector<DeprecatedString>> flags, Optional<Core::DateTime> date_time)
{ {
Vector<DeprecatedString> args = { mailbox }; Vector<DeprecatedString> args = { mailbox };
if (flags.has_value()) { if (flags.has_value()) {
@ -405,7 +404,7 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::append(StringView mailbo
auto continue_req = send_command(Command { CommandType::Append, m_current_command, args }); auto continue_req = send_command(Command { CommandType::Append, m_current_command, args });
auto response_promise = Promise<Optional<Response>>::construct(); auto response_promise = Promise<Response>::construct();
m_pending_promises.append(response_promise); m_pending_promises.append(response_promise);
continue_req->on_resolution = [this, message2 { move(message) }](auto&) -> ErrorOr<void> { continue_req->on_resolution = [this, message2 { move(message) }](auto&) -> ErrorOr<void> {
@ -420,27 +419,27 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::append(StringView mailbo
return cast_promise<SolidResponse>(response_promise); return cast_promise<SolidResponse>(response_promise);
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::subscribe(StringView mailbox) NonnullRefPtr<Promise<SolidResponse>> Client::subscribe(StringView mailbox)
{ {
auto command = Command { CommandType::Subscribe, m_current_command, { serialize_astring(mailbox) } }; auto command = Command { CommandType::Subscribe, m_current_command, { serialize_astring(mailbox) } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::unsubscribe(StringView mailbox) NonnullRefPtr<Promise<SolidResponse>> Client::unsubscribe(StringView mailbox)
{ {
auto command = Command { CommandType::Unsubscribe, m_current_command, { serialize_astring(mailbox) } }; auto command = Command { CommandType::Unsubscribe, m_current_command, { serialize_astring(mailbox) } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<Response>>> Client::authenticate(StringView method) NonnullRefPtr<Promise<Response>> Client::authenticate(StringView method)
{ {
auto command = Command { CommandType::Authenticate, m_current_command, { method } }; auto command = Command { CommandType::Authenticate, m_current_command, { method } };
return send_command(move(command)); return send_command(move(command));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::rename(StringView from, StringView to) NonnullRefPtr<Promise<SolidResponse>> Client::rename(StringView from, StringView to)
{ {
auto command = Command { CommandType::Rename, m_current_command, { serialize_astring(from), serialize_astring(to) } }; auto command = Command { CommandType::Rename, m_current_command, { serialize_astring(from), serialize_astring(to) } };
return cast_promise<SolidResponse>(send_command(move(command))); return cast_promise<SolidResponse>(send_command(move(command)));
} }
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::copy(Sequence sequence_set, StringView name, bool uid) NonnullRefPtr<Promise<SolidResponse>> Client::copy(Sequence sequence_set, StringView name, bool uid)
{ {
auto command = Command { auto command = Command {
uid ? CommandType::UIDCopy : CommandType::Copy, m_current_command, { sequence_set.serialize(), serialize_astring(name) } uid ? CommandType::UIDCopy : CommandType::Copy, m_current_command, { sequence_set.serialize(), serialize_astring(name) }

View file

@ -30,28 +30,28 @@ public:
return m_connect_pending; return m_connect_pending;
} }
NonnullRefPtr<Promise<Optional<Response>>> send_command(Command&&); NonnullRefPtr<Promise<Response>> send_command(Command&&);
NonnullRefPtr<Promise<Optional<Response>>> send_simple_command(CommandType); NonnullRefPtr<Promise<Response>> send_simple_command(CommandType);
ErrorOr<void> send_raw(StringView data); ErrorOr<void> send_raw(StringView data);
NonnullRefPtr<Promise<Optional<SolidResponse>>> login(StringView username, StringView password); NonnullRefPtr<Promise<SolidResponse>> login(StringView username, StringView password);
NonnullRefPtr<Promise<Optional<SolidResponse>>> list(StringView reference_name, StringView mailbox_name); NonnullRefPtr<Promise<SolidResponse>> list(StringView reference_name, StringView mailbox_name);
NonnullRefPtr<Promise<Optional<SolidResponse>>> lsub(StringView reference_name, StringView mailbox_name); NonnullRefPtr<Promise<SolidResponse>> lsub(StringView reference_name, StringView mailbox_name);
NonnullRefPtr<Promise<Optional<SolidResponse>>> select(StringView string); NonnullRefPtr<Promise<SolidResponse>> select(StringView string);
NonnullRefPtr<Promise<Optional<SolidResponse>>> examine(StringView string); NonnullRefPtr<Promise<SolidResponse>> examine(StringView string);
NonnullRefPtr<Promise<Optional<SolidResponse>>> search(Optional<DeprecatedString> charset, Vector<SearchKey>&& search_keys, bool uid); NonnullRefPtr<Promise<SolidResponse>> search(Optional<DeprecatedString> charset, Vector<SearchKey>&& search_keys, bool uid);
NonnullRefPtr<Promise<Optional<SolidResponse>>> fetch(FetchCommand request, bool uid); NonnullRefPtr<Promise<SolidResponse>> fetch(FetchCommand request, bool uid);
NonnullRefPtr<Promise<Optional<SolidResponse>>> store(StoreMethod, Sequence, bool silent, Vector<DeprecatedString> const& flags, bool uid); NonnullRefPtr<Promise<SolidResponse>> store(StoreMethod, Sequence, bool silent, Vector<DeprecatedString> const& flags, bool uid);
NonnullRefPtr<Promise<Optional<SolidResponse>>> copy(Sequence sequence_set, StringView name, bool uid); NonnullRefPtr<Promise<SolidResponse>> copy(Sequence sequence_set, StringView name, bool uid);
NonnullRefPtr<Promise<Optional<SolidResponse>>> create_mailbox(StringView name); NonnullRefPtr<Promise<SolidResponse>> create_mailbox(StringView name);
NonnullRefPtr<Promise<Optional<SolidResponse>>> delete_mailbox(StringView name); NonnullRefPtr<Promise<SolidResponse>> delete_mailbox(StringView name);
NonnullRefPtr<Promise<Optional<SolidResponse>>> subscribe(StringView mailbox); NonnullRefPtr<Promise<SolidResponse>> subscribe(StringView mailbox);
NonnullRefPtr<Promise<Optional<SolidResponse>>> unsubscribe(StringView mailbox); NonnullRefPtr<Promise<SolidResponse>> unsubscribe(StringView mailbox);
NonnullRefPtr<Promise<Optional<SolidResponse>>> rename(StringView from, StringView to); NonnullRefPtr<Promise<SolidResponse>> rename(StringView from, StringView to);
NonnullRefPtr<Promise<Optional<Response>>> authenticate(StringView method); NonnullRefPtr<Promise<Response>> authenticate(StringView method);
NonnullRefPtr<Promise<Optional<ContinueRequest>>> idle(); NonnullRefPtr<Promise<ContinueRequest>> idle();
NonnullRefPtr<Promise<Optional<SolidResponse>>> finish_idle(); NonnullRefPtr<Promise<SolidResponse>> finish_idle();
NonnullRefPtr<Promise<Optional<SolidResponse>>> status(StringView mailbox, Vector<StatusItemType> const& types); NonnullRefPtr<Promise<SolidResponse>> status(StringView mailbox, Vector<StatusItemType> const& types);
NonnullRefPtr<Promise<Optional<SolidResponse>>> append(StringView mailbox, Message&& message, Optional<Vector<DeprecatedString>> flags = {}, Optional<Core::DateTime> date_time = {}); NonnullRefPtr<Promise<SolidResponse>> append(StringView mailbox, Message&& message, Optional<Vector<DeprecatedString>> flags = {}, Optional<Core::DateTime> date_time = {});
bool is_open(); bool is_open();
void close(); void close();
@ -77,7 +77,7 @@ private:
int m_current_command = 1; int m_current_command = 1;
// Sent but response not received // Sent but response not received
Vector<RefPtr<Promise<Optional<Response>>>> m_pending_promises; Vector<NonnullRefPtr<Promise<Response>>> m_pending_promises;
// Not yet sent // Not yet sent
Vector<Command> m_command_queue {}; Vector<Command> m_command_queue {};

View file

@ -46,18 +46,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto client = TRY(tls ? IMAP::Client::connect_tls(host, port) : IMAP::Client::connect_plaintext(host, port)); auto client = TRY(tls ? IMAP::Client::connect_tls(host, port) : IMAP::Client::connect_plaintext(host, port));
TRY(client->connection_promise()->await()); TRY(client->connection_promise()->await());
auto response = TRY(client->login(username, password.view())->await()).release_value(); auto response = TRY(client->login(username, password.view())->await());
outln("[LOGIN] Login response: {}", response.response_text()); outln("[LOGIN] Login response: {}", response.response_text());
response = move(TRY(client->send_simple_command(IMAP::CommandType::Capability)->await()).value().get<IMAP::SolidResponse>()); response = move(TRY(client->send_simple_command(IMAP::CommandType::Capability)->await()).get<IMAP::SolidResponse>());
outln("[CAPABILITY] First capability: {}", response.data().capabilities().first()); outln("[CAPABILITY] First capability: {}", response.data().capabilities().first());
bool idle_supported = !response.data().capabilities().find_if([](auto capability) { return capability.equals_ignoring_ascii_case("IDLE"sv); }).is_end(); bool idle_supported = !response.data().capabilities().find_if([](auto capability) { return capability.equals_ignoring_ascii_case("IDLE"sv); }).is_end();
response = TRY(client->list(""sv, "*"sv)->await()).release_value(); response = TRY(client->list(""sv, "*"sv)->await());
outln("[LIST] First mailbox: {}", response.data().list_items().first().name); outln("[LIST] First mailbox: {}", response.data().list_items().first().name);
auto mailbox = "Inbox"sv; auto mailbox = "Inbox"sv;
response = TRY(client->select(mailbox)->await()).release_value(); response = TRY(client->select(mailbox)->await());
outln("[SELECT] Select response: {}", response.response_text()); outln("[SELECT] Select response: {}", response.response_text());
auto message = Message { auto message = Message {
@ -71,7 +71,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
"So, \"Hello\"." "So, \"Hello\"."
}; };
auto promise = client->append("INBOX"sv, move(message)); auto promise = client->append("INBOX"sv, move(message));
response = TRY(promise->await()).release_value(); response = TRY(promise->await());
outln("[APPEND] Response: {}", response.response_text()); outln("[APPEND] Response: {}", response.response_text());
Vector<IMAP::SearchKey> keys; Vector<IMAP::SearchKey> keys;
@ -79,13 +79,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
IMAP::SearchKey::From { "jdoe@machine.example" } }); IMAP::SearchKey::From { "jdoe@machine.example" } });
keys.append(IMAP::SearchKey { keys.append(IMAP::SearchKey {
IMAP::SearchKey::Subject { "Saying Hello" } }); IMAP::SearchKey::Subject { "Saying Hello" } });
response = TRY(client->search({}, move(keys), false)->await()).release_value(); response = TRY(client->search({}, move(keys), false)->await());
Vector<unsigned> search_results = move(response.data().search_results()); Vector<unsigned> search_results = move(response.data().search_results());
auto added_message = search_results.first(); auto added_message = search_results.first();
outln("[SEARCH] Number of results: {}", search_results.size()); outln("[SEARCH] Number of results: {}", search_results.size());
response = TRY(client->status("INBOX"sv, { IMAP::StatusItemType::Recent, IMAP::StatusItemType::Messages })->await()).release_value(); response = TRY(client->status("INBOX"sv, { IMAP::StatusItemType::Recent, IMAP::StatusItemType::Messages })->await());
outln("[STATUS] Recent items: {}", response.data().status_item().get(IMAP::StatusItemType::Recent)); outln("[STATUS] Recent items: {}", response.data().status_item().get(IMAP::StatusItemType::Recent));
for (auto item : search_results) { for (auto item : search_results) {
@ -118,7 +118,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}; };
// clang-format on // clang-format on
auto fetch_response = TRY(client->fetch(fetch_command, false)->await()).release_value(); auto fetch_response = TRY(client->fetch(fetch_command, false)->await());
outln("[FETCH] Subject of search result: {}", outln("[FETCH] Subject of search result: {}",
fetch_response.data() fetch_response.data()
.fetch_data() .fetch_data()
@ -136,22 +136,22 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
// FIXME: There is a discrepancy between IMAP::Sequence wanting signed ints // FIXME: There is a discrepancy between IMAP::Sequence wanting signed ints
// and IMAP search results returning unsigned ones. Find which one is // and IMAP search results returning unsigned ones. Find which one is
// more correct and fix this. // more correct and fix this.
response = TRY(client->store(IMAP::StoreMethod::Add, { static_cast<int>(added_message), static_cast<int>(added_message) }, false, { "\\Deleted" }, false)->await()).release_value(); response = TRY(client->store(IMAP::StoreMethod::Add, { static_cast<int>(added_message), static_cast<int>(added_message) }, false, { "\\Deleted" }, false)->await());
outln("[STORE] Store response: {}", response.response_text()); outln("[STORE] Store response: {}", response.response_text());
response = move(TRY(client->send_simple_command(IMAP::CommandType::Expunge)->await()).release_value().get<IMAP::SolidResponse>()); response = move(TRY(client->send_simple_command(IMAP::CommandType::Expunge)->await()).get<IMAP::SolidResponse>());
outln("[EXPUNGE] Number of expunged entries: {}", response.data().expunged().size()); outln("[EXPUNGE] Number of expunged entries: {}", response.data().expunged().size());
if (idle_supported) { if (idle_supported) {
VERIFY(TRY(client->idle()->await()).has_value()); VERIFY(!client->idle()->await().is_error());
sleep(3); sleep(3);
response = TRY(client->finish_idle()->await()).release_value(); response = TRY(client->finish_idle()->await());
outln("[IDLE] Idle response: {}", response.response_text()); outln("[IDLE] Idle response: {}", response.response_text());
} else { } else {
outln("[IDLE] Skipped. No IDLE support."); outln("[IDLE] Skipped. No IDLE support.");
} }
response = move(TRY(client->send_simple_command(IMAP::CommandType::Logout)->await()).release_value().get<IMAP::SolidResponse>()); response = move(TRY(client->send_simple_command(IMAP::CommandType::Logout)->await()).get<IMAP::SolidResponse>());
outln("[LOGOUT] Bye: {}", response.data().bye_message().value()); outln("[LOGOUT] Bye: {}", response.data().bye_message().value());
client->close(); client->close();