Client.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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>>> list(StringView reference_name, StringView mailbox_name);
  20. RefPtr<Promise<Optional<SolidResponse>>> select(StringView string);
  21. void close();
  22. Function<void(ResponseData&&)> unrequested_response_callback;
  23. private:
  24. StringView m_host;
  25. unsigned m_port;
  26. RefPtr<Core::Socket> m_socket;
  27. RefPtr<TLS::TLSv12> m_tls_socket;
  28. void on_ready_to_receive();
  29. void on_tls_ready_to_receive();
  30. bool m_tls;
  31. int m_current_command = 1;
  32. bool connect_tls();
  33. bool connect_plaintext();
  34. // Sent but response not received
  35. Vector<RefPtr<Promise<Optional<Response>>>> m_pending_promises;
  36. // Not yet sent
  37. Vector<Command> m_command_queue {};
  38. RefPtr<Promise<bool>> m_connect_pending {};
  39. ByteBuffer m_buffer;
  40. Parser m_parser;
  41. bool m_expecting_response { false };
  42. void handle_parsed_response(ParseStatus&& parse_status);
  43. void send_next_command();
  44. };
  45. }