2021-06-01 10:21:01 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2021-07-15 01:13:02 -03:00
|
|
|
#include <LibCore/Promise.h>
|
2021-06-01 10:21:01 -03:00
|
|
|
#include <LibIMAP/Parser.h>
|
|
|
|
|
#include <LibTLS/TLSv12.h>
|
|
|
|
|
|
|
|
|
|
namespace IMAP {
|
2021-07-15 01:13:02 -03:00
|
|
|
template<typename T>
|
|
|
|
|
using Promise = Core::Promise<T>;
|
|
|
|
|
|
2021-06-01 10:21:01 -03:00
|
|
|
class Client {
|
|
|
|
|
friend class Parser;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Client(StringView host, unsigned port, bool start_with_tls);
|
|
|
|
|
|
2021-09-01 17:56:31 -03:00
|
|
|
RefPtr<Promise<Empty>> connect();
|
2021-06-01 10:21:01 -03:00
|
|
|
RefPtr<Promise<Optional<Response>>> send_command(Command&&);
|
|
|
|
|
RefPtr<Promise<Optional<Response>>> send_simple_command(CommandType);
|
|
|
|
|
void send_raw(StringView data);
|
2021-06-01 12:10:20 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> login(StringView username, StringView password);
|
2021-06-01 11:39:50 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> list(StringView reference_name, StringView mailbox_name);
|
2021-06-02 11:53:08 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> lsub(StringView reference_name, StringView mailbox_name);
|
2021-06-01 11:39:50 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> select(StringView string);
|
2021-06-02 11:53:08 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> examine(StringView string);
|
2021-06-02 11:36:42 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> search(Optional<String> charset, Vector<SearchKey>&& search_keys, bool uid);
|
2021-06-02 10:32:03 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> fetch(FetchCommand request, bool uid);
|
2021-06-02 11:40:41 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> store(StoreMethod, Sequence, bool silent, Vector<String> const& flags, bool uid);
|
2021-06-02 11:50:13 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> copy(Sequence sequence_set, StringView name, bool uid);
|
|
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> create_mailbox(StringView name);
|
|
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> delete_mailbox(StringView name);
|
2021-06-02 11:53:08 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> subscribe(StringView mailbox);
|
|
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> unsubscribe(StringView mailbox);
|
2021-06-02 11:50:13 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> rename(StringView from, StringView to);
|
2021-06-02 11:53:08 -03:00
|
|
|
RefPtr<Promise<Optional<Response>>> authenticate(StringView method);
|
2021-06-02 10:17:22 -03:00
|
|
|
RefPtr<Promise<Optional<ContinueRequest>>> idle();
|
|
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> finish_idle();
|
2021-06-02 11:40:41 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> status(StringView mailbox, Vector<StatusItemType> const& types);
|
2021-06-02 11:51:46 -03:00
|
|
|
RefPtr<Promise<Optional<SolidResponse>>> append(StringView mailbox, Message&& message, Optional<Vector<String>> flags = {}, Optional<Core::DateTime> date_time = {});
|
2021-06-01 11:39:50 -03:00
|
|
|
|
2021-06-01 10:21:01 -03:00
|
|
|
void close();
|
|
|
|
|
|
|
|
|
|
Function<void(ResponseData&&)> unrequested_response_callback;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
StringView m_host;
|
|
|
|
|
unsigned m_port;
|
|
|
|
|
RefPtr<Core::Socket> m_socket;
|
|
|
|
|
RefPtr<TLS::TLSv12> m_tls_socket;
|
|
|
|
|
|
|
|
|
|
void on_ready_to_receive();
|
|
|
|
|
void on_tls_ready_to_receive();
|
|
|
|
|
|
|
|
|
|
bool m_tls;
|
|
|
|
|
int m_current_command = 1;
|
|
|
|
|
|
|
|
|
|
bool connect_tls();
|
|
|
|
|
bool connect_plaintext();
|
|
|
|
|
|
|
|
|
|
// Sent but response not received
|
|
|
|
|
Vector<RefPtr<Promise<Optional<Response>>>> m_pending_promises;
|
|
|
|
|
// Not yet sent
|
|
|
|
|
Vector<Command> m_command_queue {};
|
|
|
|
|
|
2021-09-03 14:11:51 -03:00
|
|
|
RefPtr<Promise<Empty>> m_connect_pending {};
|
2021-06-01 10:21:01 -03:00
|
|
|
|
|
|
|
|
ByteBuffer m_buffer;
|
|
|
|
|
Parser m_parser;
|
|
|
|
|
|
|
|
|
|
bool m_expecting_response { false };
|
|
|
|
|
void handle_parsed_response(ParseStatus&& parse_status);
|
|
|
|
|
void send_next_command();
|
|
|
|
|
};
|
|
|
|
|
}
|