/
muaimabs.h
446 lines (409 loc) · 10.6 KB
/
muaimabs.h
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
/*
Copyright (C) 2016-2017 Claude SIMON (http://zeusw.org/epeios/contact.html).
This file is part of 'MMUAq' software.
'MMUAq' is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
'MMUAq' is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with 'MMUAq'. If not, see <http://www.gnu.org/licenses/>.
*/
// MUA IMA(P) BaSe
#ifndef MUAIMABS_INC_
# define MUAIMABS_INC_
# ifdef XXX_DBG
# define MUAXMP__DBG
# endif
# include "muabsc.h"
namespace muaimabs {
typedef fdr::rRDressedDriver rDriver_;
typedef flx::sStringIFlow sSIFlow_;
class rPendingIFlow_
: public sSIFlow_
{
private:
str::wString Data_;
public:
void reset( bso::sBool P = true )
{
sSIFlow_::reset( P );
tol::reset( P, Data_ );
}
qCDTOR( rPendingIFlow_ );
void Init( const str::dString &Data )
{
Data_.Init( Data );
sSIFlow_::Init( Data_ );
}
bso::sBool IsEmpty( void )
{
return ( Data_.Amount() == 0 ) || (sSIFlow_::EndOfFlow() );
}
};
// Main delimiter, which is dicarded ; only the content is returned.
qENUM( Delimiter ) {
dNone, // No delimiter detected yet.
dCRLF, // To read the entire message until 'CRLF' of an status response.
dBracket, // '[' : for optional response code.
dParenthesis, // '(' : parenthisized list.
dQuote, // Quoted ('"') string.
dLiteral, // Literal ('{') string.
// dSpace, // Separation of basic strings. Can be a space, CRLF, EOF.
d_amount,
d_Undefined
};
namespace _ {
// Delimiters are re-issued.
qENUM( Context ) {
cFree,
cQuoted, // Quoted string.
cLiteral, // Reading literal string _size_. The content itself is handled through 'Force_'.
cEOF, // All data red;
// No parenthensis, as 'Level_' is enough to inform about.
// No bracket corresponding item, because it's always a root delimiter; or should be handled as regular char.
c_amount,
c_Undefined
};
}
inline void SkipCRLF_( flw::rRFlow &Flow )
{
if ( Flow.Get() != '\r' )
qRGnr();
if ( Flow.Get() != '\n' )
qRGnr();
}
class rDriverBase_
{
private:
qRMV( flw::rRFlow, F_, Flow_ );
eDelimiter Delimiter_;
_::eContext Context_;
rPendingIFlow_ Pending_;
bso::sSize Level_; // Parenthensis level.
bso::sSize Force_; // Remaining amount of byte to put without taking care of value.
bso::sBool HandleFreeContext_(
fdr::sByte Byte,
flw::rRFlow &Flow,
fdr::sByte *Buffer,
fdr::sSize &Amount );
void HandleQuotedContext_(
fdr::sByte Byte,
flw::rRFlow &Flow,
fdr::sByte *Buffer,
fdr::sSize &Amount );
void HandleLiteralContext_(
fdr::sByte Byte,
flw::rRFlow &Flow,
fdr::sByte *Buffer,
fdr::sSize &Amount );
void HandleContext_(
flw::rRFlow &Flow,
fdr::sByte *Buffer,
fdr::sSize &Amount );
public:
void reset( bso::sBool P = true )
{
tol::reset( P, Flow_, Pending_ );
Delimiter_ = d_Undefined;
Context_ = _::c_Undefined,
Force_ = 0;
Level_ = 0;
}
qCVDTOR( rDriverBase_ );
void Init(
flw::rRFlow &Flow,
const str::dString &PendingData,
eDelimiter Delimiter )
{
Delimiter_ = Delimiter;
Context_ = _::cFree;
Flow_ = &Flow;
Pending_.Init( PendingData );
Force_ = 0;
Level_ = 0;
}
fdr::sSize Read(
fdr::sSize Maximum,
fdr::sByte *Buffer );
bso::sBool Dismiss(
bso::sBool Unlock,
qRPN )
{
// F_().Dismiss( Unlock, qRP );
return true;
}
fdr::sTID RTake( fdr::sTID Owner )
{
return F_().RDriver().RTake( Owner );
}
bso::sBool EndOfFlow( void )
{
return Context_ == _::cEOF;
}
};
class rResponseDriver_
: public rDriver_
{
private:
rDriverBase_ Base_;
flw::rDressedRFlow<> Flow_;
protected:
virtual fdr::sSize FDRRead(
fdr::sSize Maximum,
fdr::sByte* Buffer ) override
{
return Base_.Read( Maximum, Buffer );
}
virtual bso::sBool FDRDismiss(
bso::sBool Unlock,
qRPN) override
{
return Base_.Dismiss(Unlock, qRP);
}
virtual fdr::sTID FDRRTake(fdr::sTID Owner) override
{
return Base_.RTake( Owner );
}
public:
void reset( bso::sBool P = true )
{
rDriver_::reset( P );
tol::reset( P, Base_, Flow_ );
}
qCVDTOR( rResponseDriver_ );
void Init(
flw::rRFlow &Flow,
eDelimiter Delimiter,
const str::dString &PendingData = str::wString() )
{
Base_.Init( Flow, PendingData, Delimiter );
rDriver_::Init( fdr::ts_Default );
// The object 'Init(...)' several times during its lifespan, so the 'Dismissed', which drives the 'verbose' driver, is sometines called to late.
// By calling below method, 'Dimissed()' is called each time EOF is reached.
rDriver_::SetAutoDismissOnEOF();
}
void Init(
fdr::rRDriver &Driver,
eDelimiter Delimiter,
const str::dString &PendingData = str::wString() )
{
Flow_.Init( Driver );
Base_.Init( Flow_, PendingData, Delimiter );
rDriver_::Init( fdr::ts_Default );
}
void Purge( void )
{
fdr::Purge( *this );
}
};
// Response code.
qENUM( ResponseCode ) {
// Not really response code, but simplifies the detection of a status.
rcOK,
rcNo,
rcBad,
rcPreAuth,
rcBye,
// Not an 'IMAP' response code, but it's the counterpart of the one defined in 'eStatus'.
rcDisconnected,
// Response codes which may optionnaly be contained in status response.
rcAlert,
rcBadCharSet,
rcCapability,
rcParse,
rcPermanentFlags,
rcReadOnly,
rcReadWrite,
rcTryCreate,
rcUIDNext,
rcUIDValidity,
rcUnseen,
// Below response codes are from RFC 55530.
rcUnavailable,
rcAuthenticationFailed,
rcAuthorizationFailed,
rcExpired,
rcPrivacyRequired,
rcContactAdmin,
rcNoPerm,
rcInUse,
rcExpungeIssued,
rcCorruption,
rcServerBug,
rcClientBug,
rcCanNot,
rcLimit,
rcOverQuota,
rcAlreadyExists,
rcNonExistent,
// Reponses to commands.
// rcCapability, // Already present above.
rcList,
rcLSub,
rcStatus,
rcSearch,
rcFlags,
rcExists,
rcRecent,
rcExpunge,
rcFetch,
rc_amount,
rc_None,
rc_Undefined
};
const char *GetLabel( eResponseCode Code );
qENUM( Status ) {
sOK,
sNO,
sBAD,
sDisconnected, // Not an 'IMAP' status, but hndling this here is easier.
s_amount,
s_Erroneous, // Server returned a not 'IMAP' compliant answer.
s_Undefined
};
const char *GetLabel( eStatus Status );
class rConsole
{
private:
str::wString Tag_;
flw::rDressedRFlow<> IFlow_;
txf::rWFlow OFlow_;
rResponseDriver_ ResponseDriver_;
eResponseCode PendingCode_;
bso::sBool PendingCodeIsStatus_;
bso::sBool NoTaggedStatusResponse_; // To handle the connection, where there is no tagged status response.
public:
void reset( bso::sBool P = true )
{
tol::reset( P, Tag_, IFlow_, OFlow_, PendingCodeIsStatus_, ResponseDriver_, NoTaggedStatusResponse_ );
PendingCode_ = rc_Undefined;
}
qCDTOR( rConsole );
void Init( fdr::rRWDriver &Driver )
{
tol::Init( Tag_ );
PendingCode_ = rc_Undefined;
IFlow_.Init( Driver );
OFlow_.Init( Driver );
PendingCodeIsStatus_ = false;
NoTaggedStatusResponse_ = false;
// 'ReponseDriver_' will be initialized as needed.
}
void ReportUntaggedStatusResponse( void ) // To handle the connection, where there is no tagged status response.
{
NoTaggedStatusResponse_ = true;
}
const str::dString &GetNextTag( void )
{
return muabsc::GetNextIMAPTag( Tag_ );
}
const str::dString &GetCurrentTag( void ) const
{
if ( Tag_.Amount() == 0)
qRGnr();
return Tag_;
}
txf::sWFlow &WFlow( void )
{
return OFlow_;
}
eResponseCode GetPendingResponseCode( void );
bso::sBool Search( eResponseCode WantedCode )
{
eResponseCode Code = rc_Undefined;
while ( ( ( Code = GetPendingResponseCode() ) != rc_None ) && ( Code != WantedCode ) )
SkipResponse();
return Code == WantedCode;
}
fdr::rRDriver &GetResponseDriver( void )
{
return ResponseDriver_;
}
void SkipResponse( void )
{
ResponseDriver_.Purge();
}
void SkipRemainingReponses( void )
{
while ( GetPendingResponseCode() != rc_None )
SkipResponse();
}
// Also resets the 'PendingCodeIsStatus_'
// You have still have to handle 'GetResponseCode(...)' or launch 'SkipReponse(...');
eStatus GetStatus( void )
{
// Althought 'PendingCode' is a status, the 'PendingCodeIsStatus_' is already set to false by 'GetPendingCode(...').
eStatus Status = s_Undefined;
switch ( PendingCode_ ) {
case rcOK:
Status = sOK;
break;
case rcNo:
Status = sNO;
break;
case rcBad:
Status = sBAD;
break;
case rcDisconnected:
Status = sDisconnected;
break;
default:
qRGnr();
break;
}
PendingCode_ = rc_Undefined;
return Status;
}
};
// The 'COPY', 'FETCH', 'SEARCH', 'STORE' can be sub-commands of the 'UID' command.
qENUM( Flavor ) {
fRegular,
fUID,
f_amount,
f_Undefined,
f_Default = fRegular,
};
/*
First, call 'Connect(...)', then 'Login(...)' (technically, it's not mandatory
but if you dont, you would not be able to do very much), and end with
'Logout(...)'. Between 'Login(....)' and 'Logout(...)', call all other
functions as needed.
After calling each functions, call 'rConsole.GetPendingCode(...)',
then handle 'rConsole::GetResponseDriver(...)' or call 'rConsole::SkipReponse(...)'
until 'rConsole.GetPendingCode(...)' returns 'c_None'. Then Call the
'rConsole::GetStatus(...)', then handle 'rConsole::GetResponseDriver(...)',
or call 'rConsole::SkipReponse(...)'.
*/
// This is the first command to call after opening a connection to the 'IMAP' server.
void Connect( rConsole &Console );
// To log in. Most commands are not available when this is skipped or not done successfully.
void Login(
const str::dString &Username,
const str::dString &Password,
rConsole &Console );
// To call just before closing the connexion. You do _not_ need to be logged in to log out.
void Logout( rConsole &Console );
// Launches the corresponding 'IMAP' command.
void Capability( rConsole &Console );
void Select(
const str::dString &Mailbox,
rConsole &Console );
void List(
const str::dString &Reference,
const str::dString &Mailbox,
rConsole &Console );
void LSub(
const str::dString &Reference,
const str::dString &Mailbox,
rConsole &Console );
void Fetch(
eFlavor Flavor,
const str::dString &SequenceSet,
const str::dString &Items,
rConsole &Console );
}
#endif