Client.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <LibIMAP/Parser.h>
  9. #include <LibTLS/TLSv12.h>
  10. namespace IMAP {
  11. class Client {
  12. friend class Parser;
  13. public:
  14. Client(StringView host, unsigned port, bool start_with_tls);
  15. Optional<RefPtr<Promise<Empty>>> connect();
  16. RefPtr<Promise<Optional<Response>>> send_command(Command&&);
  17. RefPtr<Promise<Optional<Response>>> send_simple_command(CommandType);
  18. void send_raw(StringView data);
  19. RefPtr<Promise<Optional<SolidResponse>>> login(StringView username, StringView password);
  20. RefPtr<Promise<Optional<SolidResponse>>> list(StringView reference_name, StringView mailbox_name);
  21. RefPtr<Promise<Optional<SolidResponse>>> select(StringView string);
  22. void close();
  23. Function<void(ResponseData&&)> unrequested_response_callback;
  24. private:
  25. StringView m_host;
  26. unsigned m_port;
  27. RefPtr<Core::Socket> m_socket;
  28. RefPtr<TLS::TLSv12> m_tls_socket;
  29. void on_ready_to_receive();
  30. void on_tls_ready_to_receive();
  31. bool m_tls;
  32. int m_current_command = 1;
  33. bool connect_tls();
  34. bool connect_plaintext();
  35. // Sent but response not received
  36. Vector<RefPtr<Promise<Optional<Response>>>> m_pending_promises;
  37. // Not yet sent
  38. Vector<Command> m_command_queue {};
  39. RefPtr<Promise<bool>> m_connect_pending {};
  40. ByteBuffer m_buffer;
  41. Parser m_parser;
  42. bool m_expecting_response { false };
  43. void handle_parsed_response(ParseStatus&& parse_status);
  44. void send_next_command();
  45. };
  46. }