Objects.h 19 KB

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