Client.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. RefPtr<Promise<Optional<SolidResponse>>> search(Optional<String> charset, Vector<SearchKey>&& search_keys, bool uid);
  23. RefPtr<Promise<Optional<SolidResponse>>> fetch(FetchCommand request, bool uid);
  24. RefPtr<Promise<Optional<SolidResponse>>> store(StoreMethod, Sequence, bool silent, Vector<String> const& flags, bool uid);
  25. RefPtr<Promise<Optional<SolidResponse>>> copy(Sequence sequence_set, StringView name, bool uid);
  26. RefPtr<Promise<Optional<SolidResponse>>> create_mailbox(StringView name);
  27. RefPtr<Promise<Optional<SolidResponse>>> delete_mailbox(StringView name);
  28. RefPtr<Promise<Optional<SolidResponse>>> rename(StringView from, StringView to);
  29. RefPtr<Promise<Optional<ContinueRequest>>> idle();
  30. RefPtr<Promise<Optional<SolidResponse>>> finish_idle();
  31. RefPtr<Promise<Optional<SolidResponse>>> status(StringView mailbox, Vector<StatusItemType> const& types);
  32. RefPtr<Promise<Optional<SolidResponse>>> append(StringView mailbox, Message&& message, Optional<Vector<String>> flags = {}, Optional<Core::DateTime> date_time = {});
  33. void close();
  34. Function<void(ResponseData&&)> unrequested_response_callback;
  35. private:
  36. StringView m_host;
  37. unsigned m_port;
  38. RefPtr<Core::Socket> m_socket;
  39. RefPtr<TLS::TLSv12> m_tls_socket;
  40. void on_ready_to_receive();
  41. void on_tls_ready_to_receive();
  42. bool m_tls;
  43. int m_current_command = 1;
  44. bool connect_tls();
  45. bool connect_plaintext();
  46. // Sent but response not received
  47. Vector<RefPtr<Promise<Optional<Response>>>> m_pending_promises;
  48. // Not yet sent
  49. Vector<Command> m_command_queue {};
  50. RefPtr<Promise<bool>> m_connect_pending {};
  51. ByteBuffer m_buffer;
  52. Parser m_parser;
  53. bool m_expecting_response { false };
  54. void handle_parsed_response(ParseStatus&& parse_status);
  55. void send_next_command();
  56. };
  57. }