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
use std::{collections::VecDeque, convert::Infallible};
use imap_codec::{
encode::{Encoder, Fragment},
imap_types::{
auth::AuthenticateData,
command::{Command, CommandBody},
core::{LiteralMode, Tag},
extensions::idle::IdleDone,
response::{Status, StatusBody, StatusKind, Tagged},
},
AuthenticateDataCodec, CommandCodec, IdleDoneCodec,
};
use tracing::warn;
use crate::{client::CommandHandle, types::CommandAuthenticate, Interrupt, Io};
pub struct ClientSendState {
command_codec: CommandCodec,
authenticate_data_codec: AuthenticateDataCodec,
idle_done_codec: IdleDoneCodec,
/// FIFO queue for messages that should be sent next.
queued_messages: VecDeque<QueuedMessage>,
/// Message that is currently being sent.
current_message: Option<CurrentMessage>,
}
impl ClientSendState {
pub fn new(
command_codec: CommandCodec,
authenticate_data_codec: AuthenticateDataCodec,
idle_done_codec: IdleDoneCodec,
) -> Self {
Self {
command_codec,
authenticate_data_codec,
idle_done_codec,
queued_messages: VecDeque::new(),
current_message: None,
}
}
pub fn enqueue_command(&mut self, handle: CommandHandle, command: Command<'static>) {
self.queued_messages
.push_back(QueuedMessage { handle, command });
}
/// Terminates the current message depending on the received status.
pub fn maybe_terminate(&mut self, status: &Status) -> Option<ClientSendTermination> {
// TODO: Do we want more checks on the state? Was idle already accepted? Does the command even has a literal? etc.
// If we reach one of the return statements, the current message will be removed
let current_message = self.current_message.take()?;
self.current_message = Some(match current_message {
CurrentMessage::Command(state) => {
// Check if status matches the current command
if let Status::Tagged(Tagged {
tag,
body: StatusBody { kind, .. },
..
}) = status
{
if *kind == StatusKind::Bad && tag == &state.command.tag {
// Terminate command because literal was rejected
return Some(ClientSendTermination::LiteralRejected {
handle: state.handle,
command: state.command,
});
}
}
CurrentMessage::Command(state)
}
CurrentMessage::Authenticate(state) => {
// Check if status matches the current authenticate command
if let Status::Tagged(Tagged {
tag,
body: StatusBody { kind, .. },
..
}) = status
{
if tag == &state.command_authenticate.tag {
match kind {
StatusKind::Ok => {
// Terminate authenticate command because it was accepted
return Some(ClientSendTermination::AuthenticateAccepted {
handle: state.handle,
command_authenticate: state.command_authenticate,
});
}
StatusKind::No | StatusKind::Bad => {
// Terminate authenticate command because it was rejected
return Some(ClientSendTermination::AuthenticateRejected {
handle: state.handle,
command_authenticate: state.command_authenticate,
});
}
};
}
}
CurrentMessage::Authenticate(state)
}
CurrentMessage::Idle(state) => {
// Check if status matches the current idle command
if let Status::Tagged(Tagged {
tag,
body: StatusBody { kind, .. },
..
}) = status
{
if tag == &state.tag {
if matches!(kind, StatusKind::Ok | StatusKind::Bad) {
warn!(got=?status, "Expected command continuation request response or NO command completion result");
warn!("Interpreting as IDLE rejected");
}
// Terminate idle command because it was rejected
return Some(ClientSendTermination::IdleRejected {
handle: state.handle,
});
}
}
CurrentMessage::Idle(state)
}
});
None
}
/// Handles the received continuation request for a literal.
pub fn literal_continue(&mut self) -> bool {
// Check whether in correct state
let Some(current_message) = self.current_message.take() else {
return false;
};
let CurrentMessage::Command(state) = current_message else {
self.current_message = Some(current_message);
return false;
};
let CommandActivity::WaitingForLiteralAccepted { limbo_literal } = state.activity else {
self.current_message = Some(CurrentMessage::Command(state));
return false;
};
// Change state
self.current_message = Some(CurrentMessage::Command(CommandState {
activity: CommandActivity::PushingFragments {
accepted_literal: Some(limbo_literal),
},
..state
}));
true
}
/// Handles the received continuation request for an authenticate data.
pub fn authenticate_continue(&mut self) -> Option<CommandHandle> {
// Check whether in correct state
let current_message = self.current_message.take()?;
let CurrentMessage::Authenticate(state) = current_message else {
self.current_message = Some(current_message);
return None;
};
let AuthenticateActivity::WaitingForAuthenticateResponse = state.activity else {
self.current_message = Some(CurrentMessage::Authenticate(state));
return None;
};
// Change state
self.current_message = Some(CurrentMessage::Authenticate(AuthenticateState {
activity: AuthenticateActivity::WaitingForAuthenticateDataSet,
..state
}));
Some(state.handle)
}
/// Takes the requested authenticate data and sends it to the server.
pub fn set_authenticate_data(
&mut self,
authenticate_data: AuthenticateData<'static>,
) -> Result<CommandHandle, AuthenticateData<'static>> {
// Check whether in correct state
let Some(current_message) = self.current_message.take() else {
return Err(authenticate_data);
};
let CurrentMessage::Authenticate(state) = current_message else {
self.current_message = Some(current_message);
return Err(authenticate_data);
};
let AuthenticateActivity::WaitingForAuthenticateDataSet = state.activity else {
self.current_message = Some(CurrentMessage::Authenticate(state));
return Err(authenticate_data);
};
// Encode authenticate data
let mut fragments = self.authenticate_data_codec.encode(&authenticate_data);
// Authenticate data is a single line by definition
let Some(Fragment::Line {
data: authenticate_data,
}) = fragments.next()
else {
unreachable!()
};
assert!(fragments.next().is_none());
// Change state
self.current_message = Some(CurrentMessage::Authenticate(AuthenticateState {
activity: AuthenticateActivity::PushingAuthenticateData { authenticate_data },
..state
}));
Ok(state.handle)
}
/// Handles the received continuation request for the idle done.
pub fn idle_continue(&mut self) -> Option<CommandHandle> {
// Check whether in correct state
let current_message = self.current_message.take()?;
let CurrentMessage::Idle(state) = current_message else {
self.current_message = Some(current_message);
return None;
};
let IdleActivity::WaitingForIdleResponse = state.activity else {
self.current_message = Some(CurrentMessage::Idle(state));
return None;
};
// Change state
self.current_message = Some(CurrentMessage::Idle(IdleState {
activity: IdleActivity::WaitingForIdleDoneSet,
..state
}));
Some(state.handle)
}
/// Sends the requested idle done to the server.
pub fn set_idle_done(&mut self) -> Option<CommandHandle> {
// Check whether in correct state
let current_message = self.current_message.take()?;
let CurrentMessage::Idle(state) = current_message else {
self.current_message = Some(current_message);
return None;
};
let IdleActivity::WaitingForIdleDoneSet = state.activity else {
self.current_message = Some(CurrentMessage::Idle(state));
return None;
};
// Encode idle done
let mut fragments = self.idle_done_codec.encode(&IdleDone);
// Idle done is a single line by defintion
let Some(Fragment::Line {
data: idle_done, ..
}) = fragments.next()
else {
unreachable!()
};
assert!(fragments.next().is_none());
// Change state
let handle = state.handle;
self.current_message = Some(CurrentMessage::Idle(IdleState {
activity: IdleActivity::PushingIdleDone { idle_done },
..state
}));
Some(handle)
}
pub fn next(&mut self) -> Result<Option<ClientSendEvent>, Interrupt<Infallible>> {
let current_message = match self.current_message.take() {
Some(current_message) => {
// We are currently sending a message but the sending process was aborted for one
// of these reasons:
// - The state was interrupted
// - The server must send a continuation request or a status
// - The client user must provide more data
// Continue the sending process.
current_message
}
None => {
let Some(queued_message) = self.queued_messages.pop_front() else {
// There is currently no message that needs to be sent
return Ok(None);
};
queued_message.start(&self.command_codec)
}
};
// Creates a buffer for writing the current message
let mut write_buffer = Vec::new();
// Push as many bytes of the message as possible to the buffer
let current_message = current_message.push_to_buffer(&mut write_buffer);
if write_buffer.is_empty() {
// Inform the state of the current message that all bytes are sent
match current_message.finish_sending() {
FinishSendingResult::Uncompleted {
state: current_message,
event,
} => {
// Message is not finished yet
self.current_message = Some(current_message);
Ok(event)
}
FinishSendingResult::Completed { event } => {
// Message was sent completely
Ok(Some(event))
}
}
} else {
// Store the current message, we'll continue later
self.current_message = Some(current_message);
// Interrupt the state for sending all bytes of current message
Err(Interrupt::Io(Io::Output(write_buffer)))
}
}
}
/// Queued (and not sent yet) message.
struct QueuedMessage {
handle: CommandHandle,
command: Command<'static>,
}
impl QueuedMessage {
/// Start the sending process for this message.
fn start(self, codec: &CommandCodec) -> CurrentMessage {
let handle = self.handle;
let command = self.command;
let mut fragments = codec.encode(&command);
let tag = command.tag;
match command.body {
CommandBody::Authenticate {
mechanism,
initial_response,
} => {
// The authenticate command is a single line by definition
let Some(Fragment::Line { data: authenticate }) = fragments.next() else {
unreachable!()
};
assert!(fragments.next().is_none());
CurrentMessage::Authenticate(AuthenticateState {
handle,
command_authenticate: CommandAuthenticate {
tag,
mechanism,
initial_response,
},
activity: AuthenticateActivity::PushingAuthenticate { authenticate },
})
}
CommandBody::Idle => {
// The idle command is a single line by definition
let Some(Fragment::Line { data: idle }) = fragments.next() else {
unreachable!()
};
assert!(fragments.next().is_none());
CurrentMessage::Idle(IdleState {
handle,
tag,
activity: IdleActivity::PushingIdle { idle },
})
}
body => CurrentMessage::Command(CommandState {
handle,
command: Command { tag, body },
fragments: fragments.collect(),
activity: CommandActivity::PushingFragments {
accepted_literal: None,
},
}),
}
}
}
/// Currently being sent message.
enum CurrentMessage {
/// Sending state of regular command.
Command(CommandState),
/// Sending state of authenticate command.
Authenticate(AuthenticateState),
/// Sending state of idle command.
Idle(IdleState),
}
impl CurrentMessage {
/// Pushes as many bytes as possible from the message to the buffer.
fn push_to_buffer(self, write_buffer: &mut Vec<u8>) -> Self {
match self {
Self::Command(state) => Self::Command(state.push_to_buffer(write_buffer)),
Self::Authenticate(state) => Self::Authenticate(state.push_to_buffer(write_buffer)),
Self::Idle(state) => Self::Idle(state.push_to_buffer(write_buffer)),
}
}
/// Updates the state after all bytes were sent.
fn finish_sending(self) -> FinishSendingResult<Self> {
match self {
Self::Command(state) => state.finish_sending().map_state(Self::Command),
Self::Authenticate(state) => state.finish_sending().map_state(Self::Authenticate),
Self::Idle(state) => state.finish_sending().map_state(Self::Idle),
}
}
}
/// Updated message state after sending all bytes, see `finish_sending`.
enum FinishSendingResult<S> {
/// Message not finished yet.
Uncompleted {
/// Updated message state.
state: S,
/// Event that needs to be returned by `progress`.
event: Option<ClientSendEvent>,
},
/// Message sent completely.
Completed {
/// Event that needs to be returned by `progress`.
event: ClientSendEvent,
},
}
impl<S> FinishSendingResult<S> {
fn map_state<T>(self, f: impl Fn(S) -> T) -> FinishSendingResult<T> {
match self {
FinishSendingResult::Uncompleted { state, event } => FinishSendingResult::Uncompleted {
state: f(state),
event,
},
FinishSendingResult::Completed { event } => FinishSendingResult::Completed { event },
}
}
}
struct CommandState {
handle: CommandHandle,
command: Command<'static>,
/// Outstanding command fragments that needs to be sent.
fragments: VecDeque<Fragment>,
activity: CommandActivity,
}
impl CommandState {
fn push_to_buffer(self, write_buffer: &mut Vec<u8>) -> Self {
let mut fragments = self.fragments;
let activity = match self.activity {
CommandActivity::PushingFragments { accepted_literal } => {
// First push the accepted literal if available
if let Some(data) = accepted_literal {
write_buffer.extend(data);
}
// Push as many fragments as possible
let limbo_literal = loop {
match fragments.pop_front() {
Some(
Fragment::Line { data }
| Fragment::Literal {
data,
mode: LiteralMode::NonSync,
},
) => {
write_buffer.extend(data);
}
Some(Fragment::Literal {
data,
mode: LiteralMode::Sync,
}) => {
// Stop pushing fragments because a literal needs to be accepted
// by the server
break Some(data);
}
None => break None,
};
};
// Done with pushing
CommandActivity::WaitingForFragmentsSent { limbo_literal }
}
activity => activity,
};
Self {
fragments,
activity,
..self
}
}
fn finish_sending(self) -> FinishSendingResult<Self> {
match self.activity {
CommandActivity::WaitingForFragmentsSent { limbo_literal } => match limbo_literal {
Some(limbo_literal) => FinishSendingResult::Uncompleted {
state: Self {
activity: CommandActivity::WaitingForLiteralAccepted { limbo_literal },
..self
},
event: None,
},
None => FinishSendingResult::Completed {
event: ClientSendEvent::Command {
handle: self.handle,
command: self.command,
},
},
},
activity => FinishSendingResult::Uncompleted {
state: Self { activity, ..self },
event: None,
},
}
}
}
enum CommandActivity {
/// Pushing fragments to the write buffer.
PushingFragments {
/// Literal that was accepted by the server and needs to be sent before the fragments.
accepted_literal: Option<Vec<u8>>,
},
/// Waiting until the pushed fragments are sent.
WaitingForFragmentsSent {
/// Literal that needs to be accepted by the server after the pushed fragments are sent.
limbo_literal: Option<Vec<u8>>,
},
/// Waiting until the server accepts the literal via continuation request or rejects it
/// via status.
WaitingForLiteralAccepted {
/// Literal that needs to be accepted by the server.
limbo_literal: Vec<u8>,
},
}
struct AuthenticateState {
handle: CommandHandle,
command_authenticate: CommandAuthenticate,
activity: AuthenticateActivity,
}
impl AuthenticateState {
fn push_to_buffer(self, write_buffer: &mut Vec<u8>) -> Self {
let activity = match self.activity {
AuthenticateActivity::PushingAuthenticate { authenticate } => {
write_buffer.extend(authenticate);
AuthenticateActivity::WaitingForAuthenticateSent
}
AuthenticateActivity::PushingAuthenticateData { authenticate_data } => {
write_buffer.extend(authenticate_data);
AuthenticateActivity::WaitingForAuthenticateDataSent
}
activity => activity,
};
Self { activity, ..self }
}
fn finish_sending(self) -> FinishSendingResult<Self> {
match self.activity {
AuthenticateActivity::WaitingForAuthenticateSent => FinishSendingResult::Uncompleted {
state: Self {
activity: AuthenticateActivity::WaitingForAuthenticateResponse,
..self
},
event: Some(ClientSendEvent::Authenticate {
handle: self.handle,
}),
},
AuthenticateActivity::WaitingForAuthenticateDataSent => {
FinishSendingResult::Uncompleted {
state: Self {
activity: AuthenticateActivity::WaitingForAuthenticateResponse,
..self
},
event: None,
}
}
activity => FinishSendingResult::Uncompleted {
state: Self { activity, ..self },
event: None,
},
}
}
}
enum AuthenticateActivity {
/// Pushing the authenticate command to the write buffer.
PushingAuthenticate { authenticate: Vec<u8> },
/// Waiting until the pushed authenticate command is sent.
WaitingForAuthenticateSent,
/// Waiting until the server requests more authenticate data via continuation request or
/// accepts/rejects the authenticate command via status.
WaitingForAuthenticateResponse,
/// Waiting until the client user provides the authenticate data.
///
/// Specifically, [`Client::set_authenticate_data`](crate::client::Client::set_authenticate_data).
WaitingForAuthenticateDataSet,
/// Pushing the authenticate data to the write buffer.
PushingAuthenticateData { authenticate_data: Vec<u8> },
/// Waiting until the pushed authenticate data is sent.
WaitingForAuthenticateDataSent,
}
struct IdleState {
handle: CommandHandle,
tag: Tag<'static>,
activity: IdleActivity,
}
impl IdleState {
fn push_to_buffer(self, write_buffer: &mut Vec<u8>) -> Self {
let activity = match self.activity {
IdleActivity::PushingIdle { idle } => {
write_buffer.extend(idle);
IdleActivity::WaitingForIdleSent
}
IdleActivity::PushingIdleDone { idle_done } => {
write_buffer.extend(idle_done);
IdleActivity::WaitingForIdleDoneSent
}
activity => activity,
};
Self { activity, ..self }
}
fn finish_sending(self) -> FinishSendingResult<Self> {
match self.activity {
IdleActivity::WaitingForIdleSent => FinishSendingResult::Uncompleted {
state: Self {
activity: IdleActivity::WaitingForIdleResponse,
..self
},
event: Some(ClientSendEvent::Idle {
handle: self.handle,
}),
},
IdleActivity::WaitingForIdleDoneSent => FinishSendingResult::Completed {
event: ClientSendEvent::IdleDone {
handle: self.handle,
},
},
activity => FinishSendingResult::Uncompleted {
state: Self { activity, ..self },
event: None,
},
}
}
}
enum IdleActivity {
/// Pushing the idle command to the write buffer.
PushingIdle { idle: Vec<u8> },
/// Waiting until the pushed idle command is sent.
WaitingForIdleSent,
/// Waiting until the server accepts the idle command via continuation request or rejects it
/// via status.
WaitingForIdleResponse,
/// Waiting until the client user triggers idle done.
///
/// Specifically, [`Client::set_idle_done`](crate::client::Client::set_idle_done).
WaitingForIdleDoneSet,
/// Pushing the idle done to the write buffer.
PushingIdleDone { idle_done: Vec<u8> },
/// Waiting until the pushed idle done is sent.
WaitingForIdleDoneSent,
}
/// Message sent.
pub enum ClientSendEvent {
Command {
handle: CommandHandle,
command: Command<'static>,
},
Authenticate {
handle: CommandHandle,
},
Idle {
handle: CommandHandle,
},
IdleDone {
handle: CommandHandle,
},
}
/// Message was terminated via [`ClientSendState::maybe_terminate`].
pub enum ClientSendTermination {
/// Command was terminated because its literal was rejected by the server.
LiteralRejected {
handle: CommandHandle,
command: Command<'static>,
},
/// Authenticate command was accepted.
AuthenticateAccepted {
handle: CommandHandle,
command_authenticate: CommandAuthenticate,
},
/// Authenticate command was rejected.
AuthenticateRejected {
handle: CommandHandle,
command_authenticate: CommandAuthenticate,
},
/// Idle command was rejected.
IdleRejected { handle: CommandHandle },
}