diff options
author | sin-ack <sin-ack@users.noreply.github.com> | 2021-12-18 12:38:44 +0000 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-01-13 15:16:12 +0330 |
commit | aedb013ee365f9abe92c78fae9e6455139cb7d28 (patch) | |
tree | 1e8af41c951b018fdf4f8443e0c25c683fc06998 /Userland/Libraries/LibIMAP/Client.h | |
parent | 53e9d757fed26a42477611cba20c8ede64ae53b8 (diff) | |
download | serenity-aedb013ee365f9abe92c78fae9e6455139cb7d28.zip |
LibIMAP+Userland: Convert LibIMAP::Client to the Serenity Stream APIs
You now cannot get an unconnected LibIMAP::Client, but you can still
close it. This makes for a nicer API where we don't have a Client object
in a limbo state between being constructed and being connected.
This code still isn't as nice as it should be, as TLS::TLSv12 is still
not a Core::Stream::Socket subclass, which would allow for consolidating
most of the TLS/non-TLS code into a single implementation.
Diffstat (limited to 'Userland/Libraries/LibIMAP/Client.h')
-rw-r--r-- | Userland/Libraries/LibIMAP/Client.h | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/Userland/Libraries/LibIMAP/Client.h b/Userland/Libraries/LibIMAP/Client.h index cea34fe37c..9796dab86e 100644 --- a/Userland/Libraries/LibIMAP/Client.h +++ b/Userland/Libraries/LibIMAP/Client.h @@ -8,6 +8,7 @@ #include <AK/Function.h> #include <LibCore/Promise.h> +#include <LibCore/Stream.h> #include <LibIMAP/Parser.h> #include <LibTLS/TLSv12.h> @@ -16,15 +17,23 @@ template<typename T> using Promise = Core::Promise<T>; class Client { + AK_MAKE_NONCOPYABLE(Client); friend class Parser; public: - Client(StringView host, unsigned port, bool start_with_tls); + static ErrorOr<NonnullOwnPtr<Client>> connect_tls(StringView host, u16 port); + static ErrorOr<NonnullOwnPtr<Client>> connect_plaintext(StringView host, u16 port); + + Client(Client&&); + + RefPtr<Promise<Empty>> connection_promise() + { + return m_connect_pending; + } - RefPtr<Promise<Empty>> connect(); RefPtr<Promise<Optional<Response>>> send_command(Command&&); RefPtr<Promise<Optional<Response>>> send_simple_command(CommandType); - void send_raw(StringView data); + ErrorOr<void> send_raw(StringView data); RefPtr<Promise<Optional<SolidResponse>>> login(StringView username, StringView password); RefPtr<Promise<Optional<SolidResponse>>> list(StringView reference_name, StringView mailbox_name); RefPtr<Promise<Optional<SolidResponse>>> lsub(StringView reference_name, StringView mailbox_name); @@ -45,37 +54,42 @@ public: RefPtr<Promise<Optional<SolidResponse>>> status(StringView mailbox, Vector<StatusItemType> const& types); RefPtr<Promise<Optional<SolidResponse>>> append(StringView mailbox, Message&& message, Optional<Vector<String>> flags = {}, Optional<Core::DateTime> date_time = {}); + bool is_open(); 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; + Client(StringView host, u16 port, NonnullRefPtr<TLS::TLSv12>); + Client(StringView host, u16 port, NonnullOwnPtr<Core::Stream::Socket>); + void setup_callbacks(); - void on_ready_to_receive(); - void on_tls_ready_to_receive(); + ErrorOr<void> on_ready_to_receive(); + ErrorOr<void> on_tls_ready_to_receive(); + + ErrorOr<void> handle_parsed_response(ParseStatus&& parse_status); + ErrorOr<void> send_next_command(); + + StringView m_host; + u16 m_port; bool m_tls; - int m_current_command = 1; + // FIXME: Convert this to a single `NonnullOwnPtr<Core::Stream::Socket>` + // once `TLS::TLSv12` is converted to a `Socket` as well. + OwnPtr<Core::Stream::Socket> m_socket; + RefPtr<TLS::TLSv12> m_tls_socket; + RefPtr<Promise<Empty>> m_connect_pending {}; - bool connect_tls(); - bool connect_plaintext(); + int m_current_command = 1; // Sent but response not received Vector<RefPtr<Promise<Optional<Response>>>> m_pending_promises; // Not yet sent Vector<Command> m_command_queue {}; - RefPtr<Promise<Empty>> m_connect_pending {}; - ByteBuffer m_buffer; - Parser m_parser; + Parser m_parser {}; bool m_expecting_response { false }; - void handle_parsed_response(ParseStatus&& parse_status); - void send_next_command(); }; } |