2021-06-01 13:21:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-01-02 14:15:14 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2021-06-01 13:21:01 +00:00
|
|
|
#include <LibIMAP/Client.h>
|
|
|
|
|
|
|
|
namespace IMAP {
|
2021-12-18 12:38:44 +00:00
|
|
|
|
2023-02-08 22:05:44 +00:00
|
|
|
Client::Client(StringView host, u16 port, NonnullOwnPtr<Core::Socket> socket)
|
2021-12-18 12:38:44 +00:00
|
|
|
: m_host(host)
|
|
|
|
, m_port(port)
|
|
|
|
, m_socket(move(socket))
|
|
|
|
, m_connect_pending(Promise<Empty>::construct())
|
|
|
|
{
|
|
|
|
setup_callbacks();
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 12:38:44 +00:00
|
|
|
Client::Client(Client&& other)
|
|
|
|
: m_host(other.m_host)
|
|
|
|
, m_port(other.m_port)
|
|
|
|
, m_socket(move(other.m_socket))
|
|
|
|
, m_connect_pending(move(other.m_connect_pending))
|
|
|
|
{
|
|
|
|
setup_callbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::setup_callbacks()
|
2021-06-01 13:21:01 +00:00
|
|
|
{
|
2022-02-02 15:51:55 +00:00
|
|
|
m_socket->on_ready_to_read = [&] {
|
|
|
|
auto maybe_error = on_ready_to_receive();
|
|
|
|
if (maybe_error.is_error()) {
|
|
|
|
dbgln("Error receiving from the socket: {}", maybe_error.error());
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
};
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 12:38:44 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<Client>> Client::connect_tls(StringView host, u16 port)
|
2021-06-01 13:21:01 +00:00
|
|
|
{
|
2022-02-02 15:51:55 +00:00
|
|
|
auto tls_socket = TRY(TLS::TLSv12::connect(host, port));
|
|
|
|
dbgln("connecting to {}:{}", host, port);
|
2021-12-18 12:38:44 +00:00
|
|
|
|
2022-02-02 15:51:55 +00:00
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) Client(host, port, move(tls_socket)));
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 12:38:44 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<Client>> Client::connect_plaintext(StringView host, u16 port)
|
2021-06-01 13:21:01 +00:00
|
|
|
{
|
2023-02-08 22:05:44 +00:00
|
|
|
auto socket = TRY(Core::TCPSocket::connect(host, port));
|
2021-12-18 12:38:44 +00:00
|
|
|
dbgln("Connected to {}:{}", host, port);
|
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) Client(host, port, move(socket)));
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 12:38:44 +00:00
|
|
|
ErrorOr<void> Client::on_ready_to_receive()
|
2021-06-01 13:21:01 +00:00
|
|
|
{
|
2021-12-18 12:38:44 +00:00
|
|
|
if (!TRY(m_socket->can_read_without_blocking()))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto pending_bytes = TRY(m_socket->pending_bytes());
|
|
|
|
auto receive_buffer = TRY(m_buffer.get_bytes_for_writing(pending_bytes));
|
2023-03-01 16:24:50 +00:00
|
|
|
TRY(m_socket->read_until_filled(receive_buffer));
|
2021-06-01 13:21:01 +00:00
|
|
|
|
|
|
|
// Once we get server hello we can start sending.
|
|
|
|
if (m_connect_pending) {
|
2022-12-29 13:53:47 +00:00
|
|
|
TRY(m_connect_pending->resolve({}));
|
2021-06-01 13:21:01 +00:00
|
|
|
m_connect_pending.clear();
|
|
|
|
m_buffer.clear();
|
2021-12-18 12:38:44 +00:00
|
|
|
return {};
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_buffer[m_buffer.size() - 1] == '\n') {
|
|
|
|
// Don't try parsing until we have a complete line.
|
|
|
|
auto response = m_parser.parse(move(m_buffer), m_expecting_response);
|
2021-12-18 12:38:44 +00:00
|
|
|
TRY(handle_parsed_response(move(response)));
|
2021-06-01 13:21:01 +00:00
|
|
|
m_buffer.clear();
|
|
|
|
}
|
2021-12-18 12:38:44 +00:00
|
|
|
|
|
|
|
return {};
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ReadonlyBytes command_byte_buffer(CommandType command)
|
|
|
|
{
|
|
|
|
switch (command) {
|
|
|
|
case CommandType::Noop:
|
|
|
|
return "NOOP"sv.bytes();
|
2021-06-01 13:42:12 +00:00
|
|
|
case CommandType::Capability:
|
|
|
|
return "CAPABILITY"sv.bytes();
|
2021-06-01 15:10:20 +00:00
|
|
|
case CommandType::Logout:
|
|
|
|
return "LOGOUT"sv.bytes();
|
2021-06-02 13:17:22 +00:00
|
|
|
case CommandType ::Idle:
|
|
|
|
return "IDLE"sv.bytes();
|
2021-06-01 15:10:20 +00:00
|
|
|
case CommandType::Login:
|
|
|
|
return "LOGIN"sv.bytes();
|
2021-06-01 14:39:50 +00:00
|
|
|
case CommandType::List:
|
|
|
|
return "LIST"sv.bytes();
|
|
|
|
case CommandType::Select:
|
|
|
|
return "SELECT"sv.bytes();
|
2021-06-02 13:32:03 +00:00
|
|
|
case CommandType::Fetch:
|
|
|
|
return "FETCH"sv.bytes();
|
2021-06-02 14:40:41 +00:00
|
|
|
case CommandType::Store:
|
|
|
|
return "STORE"sv.bytes();
|
2021-06-02 14:50:13 +00:00
|
|
|
case CommandType::Copy:
|
|
|
|
return "COPY"sv.bytes();
|
|
|
|
case CommandType::Create:
|
|
|
|
return "CREATE"sv.bytes();
|
|
|
|
case CommandType::Delete:
|
|
|
|
return "DELETE"sv.bytes();
|
2021-06-02 14:36:42 +00:00
|
|
|
case CommandType::Search:
|
|
|
|
return "SEARCH"sv.bytes();
|
2021-06-02 13:32:03 +00:00
|
|
|
case CommandType::UIDFetch:
|
|
|
|
return "UID FETCH"sv.bytes();
|
2021-06-02 14:40:41 +00:00
|
|
|
case CommandType::UIDStore:
|
|
|
|
return "UID STORE"sv.bytes();
|
2021-06-02 14:50:13 +00:00
|
|
|
case CommandType::UIDCopy:
|
|
|
|
return "UID COPY"sv.bytes();
|
2021-06-02 14:36:42 +00:00
|
|
|
case CommandType::UIDSearch:
|
|
|
|
return "UID SEARCH"sv.bytes();
|
2021-06-02 14:51:46 +00:00
|
|
|
case CommandType::Append:
|
|
|
|
return "APPEND"sv.bytes();
|
2021-06-02 14:53:08 +00:00
|
|
|
case CommandType::Examine:
|
|
|
|
return "EXAMINE"sv.bytes();
|
|
|
|
case CommandType::ListSub:
|
|
|
|
return "LSUB"sv.bytes();
|
|
|
|
case CommandType::Expunge:
|
|
|
|
return "EXPUNGE"sv.bytes();
|
|
|
|
case CommandType::Subscribe:
|
|
|
|
return "SUBSCRIBE"sv.bytes();
|
|
|
|
case CommandType::Unsubscribe:
|
|
|
|
return "UNSUBSCRIBE"sv.bytes();
|
|
|
|
case CommandType::Authenticate:
|
|
|
|
return "AUTHENTICATE"sv.bytes();
|
|
|
|
case CommandType::Check:
|
|
|
|
return "CHECK"sv.bytes();
|
|
|
|
case CommandType::Close:
|
|
|
|
return "CLOSE"sv.bytes();
|
2021-06-02 14:50:13 +00:00
|
|
|
case CommandType::Rename:
|
|
|
|
return "RENAME"sv.bytes();
|
2021-06-02 14:40:41 +00:00
|
|
|
case CommandType::Status:
|
|
|
|
return "STATUS"sv.bytes();
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2021-12-18 12:38:44 +00:00
|
|
|
ErrorOr<void> Client::send_raw(StringView data)
|
2021-06-01 13:21:01 +00:00
|
|
|
{
|
2023-03-01 16:24:50 +00:00
|
|
|
TRY(m_socket->write_until_depleted(data.bytes()));
|
|
|
|
TRY(m_socket->write_until_depleted("\r\n"sv.bytes()));
|
2021-12-18 12:38:44 +00:00
|
|
|
|
|
|
|
return {};
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Promise<Optional<Response>>> Client::send_command(Command&& command)
|
|
|
|
{
|
|
|
|
m_command_queue.append(move(command));
|
|
|
|
m_current_command++;
|
|
|
|
|
|
|
|
auto promise = Promise<Optional<Response>>::construct();
|
|
|
|
m_pending_promises.append(promise);
|
|
|
|
|
|
|
|
if (m_pending_promises.size() == 1)
|
2021-12-18 12:38:44 +00:00
|
|
|
MUST(send_next_command());
|
2021-06-01 13:21:01 +00:00
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:39:50 +00:00
|
|
|
template<typename T>
|
|
|
|
RefPtr<Promise<Optional<T>>> cast_promise(RefPtr<Promise<Optional<Response>>> promise_variant)
|
|
|
|
{
|
|
|
|
auto new_promise = promise_variant->map<Optional<T>>(
|
|
|
|
[](Optional<Response>& variant) {
|
|
|
|
return variant.has_value() ? move(variant->get<T>()) : Optional<T>();
|
|
|
|
});
|
|
|
|
return new_promise;
|
|
|
|
}
|
|
|
|
|
2021-06-01 15:10:20 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::login(StringView username, StringView password)
|
|
|
|
{
|
2021-07-24 17:09:41 +00:00
|
|
|
auto command = Command { CommandType::Login, m_current_command, { serialize_astring(username), serialize_astring(password) } };
|
2021-06-01 15:10:20 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:39:50 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::list(StringView reference_name, StringView mailbox)
|
|
|
|
{
|
|
|
|
auto command = Command { CommandType::List, m_current_command,
|
2022-12-04 18:02:33 +00:00
|
|
|
{ DeprecatedString::formatted("\"{}\"", reference_name),
|
|
|
|
DeprecatedString::formatted("\"{}\"", mailbox) } };
|
2021-06-01 14:39:50 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
2021-06-02 14:53:08 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::lsub(StringView reference_name, StringView mailbox)
|
|
|
|
{
|
|
|
|
auto command = Command { CommandType::ListSub, m_current_command,
|
2022-12-04 18:02:33 +00:00
|
|
|
{ DeprecatedString::formatted("\"{}\"", reference_name),
|
|
|
|
DeprecatedString::formatted("\"{}\"", mailbox) } };
|
2021-06-02 14:53:08 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:32:03 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::fetch(FetchCommand request, bool uid)
|
|
|
|
{
|
|
|
|
auto command = Command { uid ? CommandType::UIDFetch : CommandType::Fetch, m_current_command, { request.serialize() } };
|
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
2021-06-01 13:21:01 +00:00
|
|
|
RefPtr<Promise<Optional<Response>>> Client::send_simple_command(CommandType type)
|
|
|
|
{
|
|
|
|
auto command = Command { type, m_current_command, {} };
|
|
|
|
return send_command(move(command));
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:39:50 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::select(StringView string)
|
|
|
|
{
|
2021-07-24 17:09:41 +00:00
|
|
|
auto command = Command { CommandType::Select, m_current_command, { serialize_astring(string) } };
|
2021-06-01 14:39:50 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
2021-12-18 12:38:44 +00:00
|
|
|
ErrorOr<void> Client::handle_parsed_response(ParseStatus&& parse_status)
|
2021-06-01 13:21:01 +00:00
|
|
|
{
|
|
|
|
if (!m_expecting_response) {
|
|
|
|
if (!parse_status.successful) {
|
|
|
|
dbgln("Parsing failed on unrequested data!");
|
|
|
|
} else if (parse_status.response.has_value()) {
|
|
|
|
unrequested_response_callback(move(parse_status.response.value().get<SolidResponse>().data()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool should_send_next = false;
|
|
|
|
if (!parse_status.successful) {
|
|
|
|
m_expecting_response = false;
|
2022-12-29 13:53:47 +00:00
|
|
|
TRY(m_pending_promises.first()->resolve({}));
|
2021-06-01 13:21:01 +00:00
|
|
|
m_pending_promises.remove(0);
|
|
|
|
}
|
|
|
|
if (parse_status.response.has_value()) {
|
|
|
|
m_expecting_response = false;
|
|
|
|
should_send_next = parse_status.response->has<SolidResponse>();
|
2022-12-29 13:53:47 +00:00
|
|
|
TRY(m_pending_promises.first()->resolve(move(parse_status.response)));
|
2021-06-01 13:21:01 +00:00
|
|
|
m_pending_promises.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_send_next && !m_command_queue.is_empty()) {
|
2021-12-18 12:38:44 +00:00
|
|
|
TRY(send_next_command());
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-18 12:38:44 +00:00
|
|
|
|
|
|
|
return {};
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 12:38:44 +00:00
|
|
|
ErrorOr<void> Client::send_next_command()
|
2021-06-01 13:21:01 +00:00
|
|
|
{
|
|
|
|
auto command = m_command_queue.take_first();
|
|
|
|
ByteBuffer buffer;
|
2022-12-04 18:02:33 +00:00
|
|
|
auto tag = AK::DeprecatedString::formatted("A{} ", m_current_command);
|
2021-06-01 13:21:01 +00:00
|
|
|
buffer += tag.to_byte_buffer();
|
|
|
|
auto command_type = command_byte_buffer(command.type);
|
|
|
|
buffer.append(command_type.data(), command_type.size());
|
|
|
|
|
|
|
|
for (auto& arg : command.args) {
|
|
|
|
buffer.append(" ", 1);
|
|
|
|
buffer.append(arg.bytes().data(), arg.length());
|
|
|
|
}
|
|
|
|
|
2021-12-18 12:38:44 +00:00
|
|
|
TRY(send_raw(buffer));
|
2021-06-01 13:21:01 +00:00
|
|
|
m_expecting_response = true;
|
2021-12-18 12:38:44 +00:00
|
|
|
return {};
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
2021-06-02 14:53:08 +00:00
|
|
|
|
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::examine(StringView string)
|
|
|
|
{
|
2021-07-24 17:09:41 +00:00
|
|
|
auto command = Command { CommandType::Examine, m_current_command, { serialize_astring(string) } };
|
2021-06-02 14:53:08 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
2021-06-02 14:50:13 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::create_mailbox(StringView name)
|
|
|
|
{
|
2021-07-24 17:09:41 +00:00
|
|
|
auto command = Command { CommandType::Create, m_current_command, { serialize_astring(name) } };
|
2021-06-02 14:50:13 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::delete_mailbox(StringView name)
|
|
|
|
{
|
2021-07-24 17:09:41 +00:00
|
|
|
auto command = Command { CommandType::Delete, m_current_command, { serialize_astring(name) } };
|
2021-06-02 14:50:13 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::store(StoreMethod method, Sequence sequence_set, bool silent, Vector<DeprecatedString> const& flags, bool uid)
|
2021-06-02 14:40:41 +00:00
|
|
|
{
|
|
|
|
StringBuilder data_item_name;
|
|
|
|
switch (method) {
|
|
|
|
case StoreMethod::Replace:
|
2022-07-11 17:32:29 +00:00
|
|
|
data_item_name.append("FLAGS"sv);
|
2021-06-02 14:40:41 +00:00
|
|
|
break;
|
|
|
|
case StoreMethod::Add:
|
2022-07-11 17:32:29 +00:00
|
|
|
data_item_name.append("+FLAGS"sv);
|
2021-06-02 14:40:41 +00:00
|
|
|
break;
|
|
|
|
case StoreMethod::Remove:
|
2022-07-11 17:32:29 +00:00
|
|
|
data_item_name.append("-FLAGS"sv);
|
2021-06-02 14:40:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (silent) {
|
2022-07-11 17:32:29 +00:00
|
|
|
data_item_name.append(".SILENT"sv);
|
2021-06-02 14:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder flags_builder;
|
|
|
|
flags_builder.append('(');
|
2022-07-11 20:10:18 +00:00
|
|
|
flags_builder.join(' ', flags);
|
2021-06-02 14:40:41 +00:00
|
|
|
flags_builder.append(')');
|
|
|
|
|
2023-01-26 18:58:09 +00:00
|
|
|
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() } };
|
2021-06-02 14:40:41 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
2022-12-04 18:02:33 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::search(Optional<DeprecatedString> charset, Vector<SearchKey>&& keys, bool uid)
|
2021-06-02 14:36:42 +00:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> args;
|
2021-06-02 14:36:42 +00:00
|
|
|
if (charset.has_value()) {
|
2022-07-11 17:32:29 +00:00
|
|
|
args.append("CHARSET "sv);
|
2021-06-02 14:36:42 +00:00
|
|
|
args.append(charset.value());
|
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
for (auto const& item : keys) {
|
2021-06-02 14:36:42 +00:00
|
|
|
args.append(item.serialize());
|
|
|
|
}
|
|
|
|
auto command = Command { uid ? CommandType::UIDSearch : CommandType::Search, m_current_command, args };
|
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:17:22 +00:00
|
|
|
RefPtr<Promise<Optional<ContinueRequest>>> Client::idle()
|
|
|
|
{
|
|
|
|
auto promise = send_simple_command(CommandType::Idle);
|
|
|
|
return cast_promise<ContinueRequest>(promise);
|
|
|
|
}
|
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::finish_idle()
|
|
|
|
{
|
|
|
|
auto promise = Promise<Optional<Response>>::construct();
|
|
|
|
m_pending_promises.append(promise);
|
2022-07-11 17:32:29 +00:00
|
|
|
MUST(send_raw("DONE"sv));
|
2021-06-02 13:17:22 +00:00
|
|
|
m_expecting_response = true;
|
|
|
|
return cast_promise<SolidResponse>(promise);
|
|
|
|
}
|
2021-06-01 13:21:01 +00:00
|
|
|
|
2021-06-02 14:40:41 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::status(StringView mailbox, Vector<StatusItemType> const& types)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> args;
|
2021-06-02 14:40:41 +00:00
|
|
|
for (auto type : types) {
|
|
|
|
switch (type) {
|
|
|
|
case StatusItemType::Recent:
|
2022-07-11 17:32:29 +00:00
|
|
|
args.append("RECENT"sv);
|
2021-06-02 14:40:41 +00:00
|
|
|
break;
|
|
|
|
case StatusItemType::UIDNext:
|
2022-07-11 17:32:29 +00:00
|
|
|
args.append("UIDNEXT"sv);
|
2021-06-02 14:40:41 +00:00
|
|
|
break;
|
|
|
|
case StatusItemType::UIDValidity:
|
2022-07-11 17:32:29 +00:00
|
|
|
args.append("UIDVALIDITY"sv);
|
2021-06-02 14:40:41 +00:00
|
|
|
break;
|
|
|
|
case StatusItemType::Unseen:
|
2022-07-11 17:32:29 +00:00
|
|
|
args.append("UNSEEN"sv);
|
2021-06-02 14:40:41 +00:00
|
|
|
break;
|
|
|
|
case StatusItemType::Messages:
|
2022-07-11 17:32:29 +00:00
|
|
|
args.append("MESSAGES"sv);
|
2021-06-02 14:40:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StringBuilder types_list;
|
|
|
|
types_list.append('(');
|
2022-07-11 20:10:18 +00:00
|
|
|
types_list.join(' ', args);
|
2021-06-02 14:40:41 +00:00
|
|
|
types_list.append(')');
|
2023-01-26 18:58:09 +00:00
|
|
|
auto command = Command { CommandType::Status, m_current_command, { mailbox, types_list.to_deprecated_string() } };
|
2021-06-02 14:40:41 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::append(StringView mailbox, Message&& message, Optional<Vector<DeprecatedString>> flags, Optional<Core::DateTime> date_time)
|
2021-06-02 14:51:46 +00:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> args = { mailbox };
|
2021-06-02 14:51:46 +00:00
|
|
|
if (flags.has_value()) {
|
|
|
|
StringBuilder flags_sb;
|
|
|
|
flags_sb.append('(');
|
2022-07-11 20:10:18 +00:00
|
|
|
flags_sb.join(' ', flags.value());
|
2021-06-02 14:51:46 +00:00
|
|
|
flags_sb.append(')');
|
2023-01-26 18:58:09 +00:00
|
|
|
args.append(flags_sb.to_deprecated_string());
|
2021-06-02 14:51:46 +00:00
|
|
|
}
|
|
|
|
if (date_time.has_value())
|
2022-12-06 01:12:49 +00:00
|
|
|
args.append(date_time.value().to_deprecated_string("\"%d-%b-%Y %H:%M:%S +0000\""sv));
|
2021-06-02 14:51:46 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
args.append(DeprecatedString::formatted("{{{}}}", message.data.length()));
|
2021-06-02 14:51:46 +00:00
|
|
|
|
|
|
|
auto continue_req = send_command(Command { CommandType::Append, m_current_command, args });
|
|
|
|
|
|
|
|
auto response_promise = Promise<Optional<Response>>::construct();
|
|
|
|
m_pending_promises.append(response_promise);
|
|
|
|
|
2022-12-29 13:53:47 +00:00
|
|
|
continue_req->on_resolved = [this, message2 { move(message) }](auto& data) -> ErrorOr<void> {
|
2021-06-02 14:51:46 +00:00
|
|
|
if (!data.has_value()) {
|
2022-12-29 13:53:47 +00:00
|
|
|
TRY(handle_parsed_response({ .successful = false, .response = {} }));
|
2021-06-02 14:51:46 +00:00
|
|
|
} else {
|
2022-12-29 13:53:47 +00:00
|
|
|
TRY(send_raw(message2.data));
|
2021-06-02 14:51:46 +00:00
|
|
|
m_expecting_response = true;
|
|
|
|
}
|
2022-12-29 13:53:47 +00:00
|
|
|
return {};
|
2021-06-02 14:51:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return cast_promise<SolidResponse>(response_promise);
|
|
|
|
}
|
2021-06-02 14:53:08 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::subscribe(StringView mailbox)
|
|
|
|
{
|
2021-07-24 17:09:41 +00:00
|
|
|
auto command = Command { CommandType::Subscribe, m_current_command, { serialize_astring(mailbox) } };
|
2021-06-02 14:53:08 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::unsubscribe(StringView mailbox)
|
|
|
|
{
|
2021-07-24 17:09:41 +00:00
|
|
|
auto command = Command { CommandType::Unsubscribe, m_current_command, { serialize_astring(mailbox) } };
|
2021-06-02 14:53:08 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
RefPtr<Promise<Optional<Response>>> Client::authenticate(StringView method)
|
|
|
|
{
|
|
|
|
auto command = Command { CommandType::Authenticate, m_current_command, { method } };
|
|
|
|
return send_command(move(command));
|
|
|
|
}
|
2021-06-02 14:50:13 +00:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::rename(StringView from, StringView to)
|
|
|
|
{
|
2021-07-24 17:09:41 +00:00
|
|
|
auto command = Command { CommandType::Rename, m_current_command, { serialize_astring(from), serialize_astring(to) } };
|
2021-06-02 14:50:13 +00:00
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> Client::copy(Sequence sequence_set, StringView name, bool uid)
|
|
|
|
{
|
|
|
|
auto command = Command {
|
2021-07-24 17:09:41 +00:00
|
|
|
uid ? CommandType::UIDCopy : CommandType::Copy, m_current_command, { sequence_set.serialize(), serialize_astring(name) }
|
2021-06-02 14:50:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return cast_promise<SolidResponse>(send_command(move(command)));
|
|
|
|
}
|
2021-12-18 12:38:44 +00:00
|
|
|
|
2021-06-01 13:21:01 +00:00
|
|
|
void Client::close()
|
|
|
|
{
|
2022-02-02 15:51:55 +00:00
|
|
|
m_socket->close();
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|
2021-12-18 12:38:44 +00:00
|
|
|
|
|
|
|
bool Client::is_open()
|
|
|
|
{
|
2022-02-02 15:51:55 +00:00
|
|
|
return m_socket->is_open();
|
2021-12-18 12:38:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 13:21:01 +00:00
|
|
|
}
|