Objects.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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/Format.h>
  8. #include <AK/Function.h>
  9. #include <AK/Tuple.h>
  10. #include <AK/Variant.h>
  11. #include <LibCore/DateTime.h>
  12. #include <LibCore/EventLoop.h>
  13. #include <LibCore/Object.h>
  14. #include <utility>
  15. namespace IMAP {
  16. enum class CommandType {
  17. Append,
  18. Capability,
  19. Copy,
  20. Create,
  21. Delete,
  22. Fetch,
  23. Idle,
  24. List,
  25. Login,
  26. Logout,
  27. Noop,
  28. Rename,
  29. Search,
  30. Select,
  31. Status,
  32. Store,
  33. UIDCopy,
  34. UIDFetch,
  35. UIDSearch,
  36. UIDStore,
  37. };
  38. enum class MailboxFlag : unsigned {
  39. All = 1u << 0,
  40. Drafts = 1u << 1,
  41. Flagged = 1u << 2,
  42. HasChildren = 1u << 3,
  43. HasNoChildren = 1u << 4,
  44. Important = 1u << 5,
  45. Junk = 1u << 6,
  46. Marked = 1u << 7,
  47. NoInferiors = 1u << 8,
  48. NoSelect = 1u << 9,
  49. Sent = 1u << 10,
  50. Trash = 1u << 11,
  51. Unmarked = 1u << 12,
  52. Unknown = 1u << 13,
  53. };
  54. enum class ResponseType : unsigned {
  55. Capability = 1u << 0,
  56. List = 1u << 1,
  57. Exists = 1u << 2,
  58. Recent = 1u << 3,
  59. Flags = 1u << 4,
  60. UIDNext = 1u << 5,
  61. UIDValidity = 1u << 6,
  62. Unseen = 1u << 7,
  63. PermanentFlags = 1u << 8,
  64. Fetch = 1u << 9,
  65. Search = 1u << 10,
  66. Bye = 1u << 13,
  67. Status = 1u << 14
  68. };
  69. enum class FetchResponseType : unsigned {
  70. Body = 1u << 1,
  71. UID = 1u << 2,
  72. InternalDate = 1u << 3,
  73. Envelope = 1u << 4,
  74. Flags = 1u << 5,
  75. BodyStructure = 1u << 6,
  76. };
  77. enum class StatusItemType : unsigned {
  78. Recent = 1u << 1,
  79. UIDNext = 1u << 2,
  80. UIDValidity = 1u << 3,
  81. Unseen = 1u << 4,
  82. Messages = 1u << 5,
  83. };
  84. class Parser;
  85. class StatusItem {
  86. public:
  87. [[nodiscard]] unsigned status_items() const
  88. {
  89. return m_status_items;
  90. }
  91. [[nodiscard]] bool contains_status_item_type(StatusItemType type) const
  92. {
  93. return (static_cast<unsigned>(type) & m_status_items) != 0;
  94. }
  95. void add_status_item_type(StatusItemType type)
  96. {
  97. m_status_items |= static_cast<unsigned>(type);
  98. }
  99. void set_mailbox(String&& mailbox) { m_mailbox = move(mailbox); }
  100. String& mailbox() { return m_mailbox; }
  101. unsigned get(StatusItemType type) const
  102. {
  103. VERIFY(contains_status_item_type(type));
  104. switch (type) {
  105. case StatusItemType::Recent:
  106. return m_recent;
  107. case StatusItemType::UIDNext:
  108. return m_uid_next;
  109. case StatusItemType::UIDValidity:
  110. return m_uid_validity;
  111. case StatusItemType::Unseen:
  112. return m_unseen;
  113. case StatusItemType::Messages:
  114. return m_messages;
  115. }
  116. VERIFY_NOT_REACHED();
  117. }
  118. void set(StatusItemType type, unsigned value)
  119. {
  120. add_status_item_type(type);
  121. switch (type) {
  122. case StatusItemType::Recent:
  123. m_recent = value;
  124. break;
  125. case StatusItemType::UIDNext:
  126. m_uid_next = value;
  127. break;
  128. case StatusItemType::UIDValidity:
  129. m_uid_validity = value;
  130. break;
  131. case StatusItemType::Unseen:
  132. m_unseen = value;
  133. break;
  134. case StatusItemType::Messages:
  135. m_uid_next = value;
  136. break;
  137. }
  138. }
  139. private:
  140. unsigned m_status_items { 0 };
  141. unsigned m_messages { 0 };
  142. unsigned m_recent { 0 };
  143. unsigned m_uid_next { 0 };
  144. unsigned m_uid_validity { 0 };
  145. unsigned m_unseen { 0 };
  146. String m_mailbox;
  147. };
  148. struct Address {
  149. Optional<String> name;
  150. Optional<String> source_route;
  151. Optional<String> mailbox;
  152. Optional<String> host;
  153. };
  154. struct Envelope {
  155. Optional<String> date; // Format of date not specified.
  156. Optional<String> subject;
  157. Optional<Vector<Address>> from;
  158. Optional<Vector<Address>> sender;
  159. Optional<Vector<Address>> reply_to;
  160. Optional<Vector<Address>> to;
  161. Optional<Vector<Address>> cc;
  162. Optional<Vector<Address>> bcc;
  163. Optional<String> in_reply_to;
  164. Optional<String> message_id;
  165. };
  166. class BodyStructure;
  167. struct BodyExtension {
  168. AK::Variant<Optional<String>, unsigned, Vector<OwnPtr<BodyExtension>>> data;
  169. };
  170. struct MultiPartBodyStructureData {
  171. Optional<Tuple<String, HashMap<String, String>>> disposition;
  172. Vector<OwnPtr<BodyStructure>> bodies;
  173. Vector<String> langs;
  174. String media_type;
  175. Optional<HashMap<String, String>> params;
  176. Optional<String> location;
  177. Optional<Vector<BodyExtension>> extensions;
  178. };
  179. struct BodyStructureData {
  180. String type;
  181. String subtype;
  182. Optional<String> id {};
  183. Optional<String> desc {};
  184. String encoding;
  185. HashMap<String, String> fields;
  186. unsigned bytes { 0 };
  187. unsigned lines { 0 };
  188. Optional<Envelope> envelope;
  189. Optional<String> md5 {};
  190. Optional<Tuple<String, HashMap<String, String>>> disposition {};
  191. Optional<Vector<String>> langs {};
  192. Optional<String> location {};
  193. Optional<Vector<BodyExtension>> extensions {};
  194. };
  195. class BodyStructure {
  196. friend Parser;
  197. public:
  198. explicit BodyStructure(BodyStructureData&& data)
  199. : m_data(move(data))
  200. {
  201. }
  202. explicit BodyStructure(MultiPartBodyStructureData&& data)
  203. : m_data(move(data))
  204. {
  205. }
  206. private:
  207. AK::Variant<BodyStructureData, MultiPartBodyStructureData> m_data;
  208. };
  209. // Set -1 for '*' i.e highest possible value.
  210. struct Sequence {
  211. int start;
  212. int end;
  213. [[nodiscard]] String serialize() const;
  214. };
  215. struct FetchCommand {
  216. enum class DataItemType {
  217. BodyStructure,
  218. Envelope,
  219. Flags,
  220. InternalDate,
  221. UID,
  222. PeekBody,
  223. BodySection
  224. };
  225. struct DataItem {
  226. enum class SectionType {
  227. Header,
  228. HeaderFields,
  229. HeaderFieldsNot,
  230. Text,
  231. Parts
  232. };
  233. struct Section {
  234. SectionType type;
  235. Optional<Vector<int>> parts {};
  236. bool ends_with_mime {};
  237. Optional<Vector<String>> headers {};
  238. [[nodiscard]] String serialize() const;
  239. };
  240. DataItemType type;
  241. Optional<Section> section {};
  242. bool partial_fetch { false };
  243. int start { 0 };
  244. int octets { 0 };
  245. [[nodiscard]] String serialize() const;
  246. };
  247. Vector<Sequence> sequence_set;
  248. Vector<DataItem> data_items;
  249. String serialize();
  250. };
  251. struct Command {
  252. public:
  253. CommandType type;
  254. int tag;
  255. Vector<String> args;
  256. };
  257. enum class ResponseStatus {
  258. Bad,
  259. No,
  260. OK,
  261. };
  262. struct ListItem {
  263. unsigned flags;
  264. String reference;
  265. String name;
  266. };
  267. class FetchResponseData {
  268. public:
  269. [[nodiscard]] unsigned response_type() const
  270. {
  271. return m_response_type;
  272. }
  273. [[nodiscard]] bool contains_response_type(FetchResponseType response_type) const
  274. {
  275. return (static_cast<unsigned>(response_type) & m_response_type) != 0;
  276. }
  277. void add_response_type(FetchResponseType type)
  278. {
  279. m_response_type |= static_cast<unsigned>(type);
  280. }
  281. void add_body_data(FetchCommand::DataItem&& data_item, Optional<String>&& body)
  282. {
  283. add_response_type(FetchResponseType::Body);
  284. m_bodies.append({ move(data_item), move(body) });
  285. }
  286. Vector<Tuple<FetchCommand::DataItem, Optional<String>>>& body_data()
  287. {
  288. VERIFY(contains_response_type(FetchResponseType::Body));
  289. return m_bodies;
  290. }
  291. void set_uid(unsigned uid)
  292. {
  293. add_response_type(FetchResponseType::UID);
  294. m_uid = uid;
  295. }
  296. [[nodiscard]] unsigned uid() const
  297. {
  298. VERIFY(contains_response_type(FetchResponseType::UID));
  299. return m_uid;
  300. }
  301. void set_internal_date(Core::DateTime time)
  302. {
  303. add_response_type(FetchResponseType::InternalDate);
  304. m_internal_date = time;
  305. }
  306. Core::DateTime& internal_date()
  307. {
  308. VERIFY(contains_response_type(FetchResponseType::InternalDate));
  309. return m_internal_date;
  310. }
  311. void set_envelope(Envelope&& envelope)
  312. {
  313. add_response_type(FetchResponseType::Envelope);
  314. m_envelope = move(envelope);
  315. }
  316. Envelope& envelope()
  317. {
  318. VERIFY(contains_response_type(FetchResponseType::Envelope));
  319. return m_envelope;
  320. }
  321. void set_flags(Vector<String>&& flags)
  322. {
  323. add_response_type(FetchResponseType::Flags);
  324. m_flags = move(flags);
  325. }
  326. Vector<String>& flags()
  327. {
  328. VERIFY(contains_response_type(FetchResponseType::Flags));
  329. return m_flags;
  330. }
  331. void set_body_structure(BodyStructure&& structure)
  332. {
  333. add_response_type(FetchResponseType::BodyStructure);
  334. m_body_structure = move(structure);
  335. }
  336. BodyStructure& body_structure()
  337. {
  338. VERIFY(contains_response_type(FetchResponseType::BodyStructure));
  339. return m_body_structure;
  340. }
  341. FetchResponseData()
  342. : m_body_structure(BodyStructureData {})
  343. {
  344. }
  345. private:
  346. Vector<String> m_flags;
  347. Vector<Tuple<FetchCommand::DataItem, Optional<String>>> m_bodies;
  348. Core::DateTime m_internal_date;
  349. Envelope m_envelope;
  350. unsigned m_uid { 0 };
  351. unsigned m_response_type { 0 };
  352. BodyStructure m_body_structure;
  353. };
  354. String serialize_astring(StringView string);
  355. struct SearchKey {
  356. public:
  357. // clang-format off
  358. struct All { };
  359. struct Answered { };
  360. struct Bcc { String bcc; };
  361. struct Cc { String cc; };
  362. struct Deleted { };
  363. struct Draft { };
  364. struct From { String from; };
  365. struct Header { String header; String value; };
  366. struct Keyword { String keyword; };
  367. struct Larger { unsigned number; };
  368. struct New { };
  369. struct Not { OwnPtr<SearchKey> operand; };
  370. struct Old { };
  371. struct On { Core::DateTime date; };
  372. struct Or { OwnPtr<SearchKey> lhs; OwnPtr<SearchKey> rhs; };
  373. struct Recent { };
  374. struct SearchKeys { Vector<OwnPtr<SearchKey>> keys; };
  375. struct Seen { };
  376. struct SentBefore { Core::DateTime date; };
  377. struct SentOn { Core::DateTime date; };
  378. struct SentSince { Core::DateTime date; };
  379. struct SequenceSet { Sequence sequence; };
  380. struct Since { Core::DateTime date; };
  381. struct Smaller { unsigned number; };
  382. struct Subject { String subject; };
  383. struct Text { String text; };
  384. struct To { String to; };
  385. struct UID { unsigned uid; };
  386. struct Unanswered { };
  387. struct Undeleted { };
  388. struct Undraft { };
  389. struct Unkeyword { String flag_keyword; };
  390. struct Unseen { };
  391. // clang-format on
  392. Variant<Empty, All, Answered, Bcc, Cc, Deleted, Draft, From, Header, Keyword,
  393. Larger, New, Not, Old, On, Or, Recent, SearchKeys, Seen, SentBefore, SentOn,
  394. SentSince, SequenceSet, Since, Smaller, Subject, Text, To, UID, Unanswered,
  395. Undeleted, Undraft, Unkeyword, Unseen>
  396. data;
  397. SearchKey(SearchKey&& other) noexcept
  398. : data(move(other.data))
  399. {
  400. }
  401. template<typename T>
  402. explicit SearchKey(T&& t)
  403. : data(std::forward<T>(t))
  404. {
  405. }
  406. SearchKey& operator=(SearchKey&& other) noexcept
  407. {
  408. if (this == &other) {
  409. return *this;
  410. }
  411. this->data = move(other.data);
  412. return *this;
  413. }
  414. [[nodiscard]] String serialize() const;
  415. };
  416. class ResponseData {
  417. public:
  418. [[nodiscard]] unsigned response_type() const
  419. {
  420. return m_response_type;
  421. }
  422. ResponseData()
  423. : m_response_type(0)
  424. {
  425. }
  426. ResponseData(ResponseData&) = delete;
  427. ResponseData(ResponseData&&) = default;
  428. ResponseData& operator=(const ResponseData&) = delete;
  429. ResponseData& operator=(ResponseData&&) = default;
  430. [[nodiscard]] bool contains_response_type(ResponseType response_type) const
  431. {
  432. return (static_cast<unsigned>(response_type) & m_response_type) != 0;
  433. }
  434. void add_response_type(ResponseType response_type)
  435. {
  436. m_response_type = m_response_type | static_cast<unsigned>(response_type);
  437. }
  438. void add_capabilities(Vector<String>&& capabilities)
  439. {
  440. m_capabilities = move(capabilities);
  441. add_response_type(ResponseType::Capability);
  442. }
  443. Vector<String>& capabilities()
  444. {
  445. VERIFY(contains_response_type(ResponseType::Capability));
  446. return m_capabilities;
  447. }
  448. void add_list_item(ListItem&& item)
  449. {
  450. add_response_type(ResponseType::List);
  451. m_list_items.append(move(item));
  452. }
  453. Vector<ListItem>& list_items()
  454. {
  455. VERIFY(contains_response_type(ResponseType::List));
  456. return m_list_items;
  457. }
  458. void set_exists(unsigned exists)
  459. {
  460. add_response_type(ResponseType::Exists);
  461. m_exists = exists;
  462. }
  463. [[nodiscard]] unsigned exists() const
  464. {
  465. VERIFY(contains_response_type(ResponseType::Exists));
  466. return m_exists;
  467. }
  468. void set_recent(unsigned recent)
  469. {
  470. add_response_type(ResponseType::Recent);
  471. m_recent = recent;
  472. }
  473. [[nodiscard]] unsigned recent() const
  474. {
  475. VERIFY(contains_response_type(ResponseType::Recent));
  476. return m_recent;
  477. }
  478. void set_uid_next(unsigned uid_next)
  479. {
  480. add_response_type(ResponseType::UIDNext);
  481. m_uid_next = uid_next;
  482. }
  483. [[nodiscard]] unsigned uid_next() const
  484. {
  485. VERIFY(contains_response_type(ResponseType::UIDNext));
  486. return m_uid_next;
  487. }
  488. void set_uid_validity(unsigned uid_validity)
  489. {
  490. add_response_type(ResponseType::UIDValidity);
  491. m_uid_validity = uid_validity;
  492. }
  493. [[nodiscard]] unsigned uid_validity() const
  494. {
  495. VERIFY(contains_response_type(ResponseType::UIDValidity));
  496. return m_uid_validity;
  497. }
  498. void set_unseen(unsigned unseen)
  499. {
  500. add_response_type(ResponseType::Unseen);
  501. m_unseen = unseen;
  502. }
  503. [[nodiscard]] unsigned unseen() const
  504. {
  505. VERIFY(contains_response_type(ResponseType::Unseen));
  506. return m_unseen;
  507. }
  508. void set_flags(Vector<String>&& flags)
  509. {
  510. m_response_type |= static_cast<unsigned>(ResponseType::Flags);
  511. m_flags = move(flags);
  512. }
  513. Vector<String>& flags()
  514. {
  515. VERIFY(contains_response_type(ResponseType::Flags));
  516. return m_flags;
  517. }
  518. void set_permanent_flags(Vector<String>&& flags)
  519. {
  520. add_response_type(ResponseType::PermanentFlags);
  521. m_permanent_flags = move(flags);
  522. }
  523. Vector<String>& permanent_flags()
  524. {
  525. VERIFY(contains_response_type(ResponseType::PermanentFlags));
  526. return m_permanent_flags;
  527. }
  528. void add_fetch_response(unsigned message, FetchResponseData&& data)
  529. {
  530. add_response_type(ResponseType::Fetch);
  531. m_fetch_responses.append(Tuple<unsigned, FetchResponseData> { move(message), move(data) });
  532. }
  533. Vector<Tuple<unsigned, FetchResponseData>>& fetch_data()
  534. {
  535. VERIFY(contains_response_type(ResponseType::Fetch));
  536. return m_fetch_responses;
  537. }
  538. void set_search_results(Vector<unsigned>&& results)
  539. {
  540. add_response_type(ResponseType::Search);
  541. m_search_results = move(results);
  542. }
  543. Vector<unsigned>& search_results()
  544. {
  545. VERIFY(contains_response_type(ResponseType::Search));
  546. return m_search_results;
  547. }
  548. void set_bye(Optional<String> message)
  549. {
  550. add_response_type(ResponseType::Bye);
  551. m_bye_message = move(message);
  552. }
  553. Optional<String>& bye_message()
  554. {
  555. VERIFY(contains_response_type(ResponseType::Bye));
  556. return m_bye_message;
  557. }
  558. void set_status(StatusItem&& status_item)
  559. {
  560. add_response_type(ResponseType::Status);
  561. m_status_item = move(status_item);
  562. }
  563. StatusItem& status_item()
  564. {
  565. return m_status_item;
  566. }
  567. private:
  568. unsigned m_response_type;
  569. Vector<String> m_capabilities;
  570. Vector<ListItem> m_list_items;
  571. unsigned m_recent {};
  572. unsigned m_exists {};
  573. unsigned m_uid_next {};
  574. unsigned m_uid_validity {};
  575. unsigned m_unseen {};
  576. Vector<String> m_permanent_flags;
  577. Vector<String> m_flags;
  578. Vector<Tuple<unsigned, FetchResponseData>> m_fetch_responses;
  579. Vector<unsigned> m_search_results;
  580. Optional<String> m_bye_message;
  581. StatusItem m_status_item;
  582. };
  583. enum class StoreMethod {
  584. Replace,
  585. Add,
  586. Remove
  587. };
  588. class SolidResponse {
  589. // Parser is allowed to set up fields
  590. friend class Parser;
  591. public:
  592. ResponseStatus status() { return m_status; }
  593. int tag() const { return m_tag; }
  594. ResponseData& data() { return m_data; }
  595. String response_text() { return m_response_text; };
  596. SolidResponse()
  597. : SolidResponse(ResponseStatus::Bad, -1)
  598. {
  599. }
  600. SolidResponse(ResponseStatus status, int tag)
  601. : m_status(status)
  602. , m_tag(tag)
  603. , m_data(ResponseData())
  604. {
  605. }
  606. private:
  607. ResponseStatus m_status;
  608. String m_response_text;
  609. unsigned m_tag;
  610. ResponseData m_data;
  611. };
  612. struct ContinueRequest {
  613. String data;
  614. };
  615. template<typename Result>
  616. class Promise : public Core::Object {
  617. C_OBJECT(Promise);
  618. private:
  619. Optional<Result> m_pending;
  620. public:
  621. Function<void(Result&)> on_resolved;
  622. void resolve(Result&& result)
  623. {
  624. m_pending = move(result);
  625. if (on_resolved)
  626. on_resolved(m_pending.value());
  627. }
  628. bool is_resolved()
  629. {
  630. return m_pending.has_value();
  631. };
  632. Result await()
  633. {
  634. while (!is_resolved()) {
  635. Core::EventLoop::current().pump();
  636. }
  637. return m_pending.release_value();
  638. }
  639. // Converts a Promise<A> to a Promise<B> using a function func: A -> B
  640. template<typename T>
  641. RefPtr<Promise<T>> map(T func(Result&))
  642. {
  643. RefPtr<Promise<T>> new_promise = Promise<T>::construct();
  644. on_resolved = [new_promise, func](Result& result) mutable {
  645. auto t = func(result);
  646. new_promise->resolve(move(t));
  647. };
  648. return new_promise;
  649. }
  650. };
  651. using Response = Variant<SolidResponse, ContinueRequest>;
  652. }
  653. // An RFC 2822 message
  654. // https://datatracker.ietf.org/doc/html/rfc2822
  655. struct Message {
  656. String data;
  657. };