Objects.h 18 KB

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