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