-
Notifications
You must be signed in to change notification settings - Fork 0
/
wxMsPop3ExecuteCommand.cpp
461 lines (448 loc) · 13.8 KB
/
wxMsPop3ExecuteCommand.cpp
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
/*-----------------------------------------------------------------
* Name: wxMsPop3ExecuteCommand.cpp
* Purpose:
* Author: A. Wiegert
*
* Copyright:
* Licence: wxWidgets license
*---------------------------------------------------------------- */
/*----------------------------------------------------------------
* Standard wxWidgets headers
*---------------------------------------------------------------- */
// Note __VISUALC__ is defined by wxWidgets, not by MSVC IDE
// and thus won't be defined until some wxWidgets headers are included
#if defined( _MSC_VER )
# if defined ( _DEBUG )
// this statement NEEDS to go BEFORE all headers
# define _CRTDBG_MAP_ALLOC
# endif
#endif
#include "wxMsPreProcDefsh.h" // needs to be first
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
/* For all others, include the necessary headers
* (this file is usually all you need because it
* includes almost all "standard" wxWidgets headers) */
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
// ------------------------------------------------------------------
#include "wxMsFrameh.h"
// ------------------------------------------------------------------
// Note __VISUALC__ is defined by wxWidgets, not by MSVC IDE
// and thus won't be defined until some wxWidgets headers are included
#if defined( _MSC_VER )
// this block needs to go AFTER all headers
#include <crtdbg.h>
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
#endif
// ------------------------------------------------------------------
#if defined( WANT_SOCKET_CODE )
bool MyFrame::ExecutePop3CommandSocket( wxString wsCommand )
{
wxString wsT;
wxString wsT1;
wxString wsT2;
wxString wsResponse;
wxString wsRecPrefix = _T("< ");
wxString wsSendPrefix = _T("> ");
const char *buf1;
unsigned char ucLen;
buf1 = wsCommand.c_str();
wsT = wsCommand.BeforeFirst( ' ' );
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
wxLogMessage(_("Sending %s to the server ..."), wsT );
// don't count and send the terminating '\0'
ucLen = (unsigned char)(wxStrlen(buf1));
m_pSock->Write(buf1, ucLen);
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
{
wsT2 = _T("Sending ") + wsT +_T(":");
wsT2 += ( m_pSock->Error() ? _("failed !") : _("done") );
wxLogMessage( wsT2 );
}
wsT = buf1;
wsT1 = wsSendPrefix + wsT.BeforeFirst( '\r');
wxLogMessage( wsT1 );
if( ! GetNextLineFromServer( m_pSock, wsResponse,
g_iniPrefs.data[IE_POP3_SERVER_TIMEOUT].dataCurrent.lVal ) )
{
wxString wsT3 = _("Server error for: ");
wxLogMessage( wsT3 + wsT1 );
return false;
}
if( ! OkResponse( wsResponse ) )
{
wxString wsT3 = _("Server error for: ");
wxLogMessage( wsT3 + wsResponse );
return false;
}
wsT = wsRecPrefix + wsResponse.BeforeFirst( '\r' );
wxLogMessage( wsT );
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
{
wsT2 = _T("Received ") + wsT +_T(" reply: ");
wsT2 += ( m_pSock->Error() ? _("failed !") : _("done") );
wxLogMessage( wsT2 );
}
return m_pSock->Error();
}
// ------------------------------------------------------------------
/**
* Retrieves the message header as well as however many extra lines
* set by m_lNTopLines (could be made into an option).
* The response will comprise the complete header, plus as many
* lines as requested.
* The header is separated from the content by a blank line, and
* the complete response will be concluded by a line
* with nothing but a ".\r\n" string
*/
bool MyFrame::ExecutePop3AllTopCommand( wxString wsCommand )
{
// wxBusyCursor wait;
long jj;
const char *buf1;
unsigned char ucLen;
wxString wsT;
wxString wsT1;
wxString wsT2;
wxString wsResponse;
wxString wsRecPrefix = _T("< ");
wxString wsSendPrefix = _T("> ");
if ( m_lNMessages )
{
wxString wsMsgHeader;
bool bEndOfHeader = false;
for ( jj = 0; jj < m_lNMessages; jj++ )
{
for (std::list<MyPop3MsgElement>::iterator it = m_Pop3MsgList.begin();
it != m_Pop3MsgList.end(); ++it )
{
if( it->m_lSequence == jj + 1 )
{
if( it->m_bNeedHeader )
{
wxString wsT;
wsT.Printf( _T("TOP %ld %ld\r\n"), jj + 1, m_lNTopLines );
if ( ! ExecutePop3OneTopCommand( wsT ) )
it->m_bNeedHeader = true;
else
it->m_bNeedHeader = false;
break;
}
}
}
}
}
#if defined( WANT_PROGRESS_DIALOG )
if ( m_pProgressDialog != NULL)
{
m_pProgressDialog->Destroy();
m_pProgressDialog = NULL;
}
#endif
return true;
}
// ------------------------------------------------------------------
bool MyFrame::ExecutePop3OneTopCommand( wxString wsCommand )
{
// wxBusyCursor wait;
long jj;
const char *buf1;
unsigned char ucLen;
wxString wsT;
wxString wsT1;
wxString wsT2;
wxString wsResponse;
wxString wsRecPrefix = _T("< ");
wxString wsSendPrefix = _T("> ");
wxString wsMsgHeader;
bool bEndOfHeader = false;
wsT = wsCommand.AfterFirst( ' ' );
wsT = wsT.BeforeFirst( ' ' );
wsT.ToLong( &jj );
jj--;
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
wxLogMessage(_("Sending TOP to the server ..."));
buf1 = wsCommand.c_str();
// don't count and send the terminating '\0'
ucLen = (unsigned char)(wxStrlen(buf1));
m_pSock->Write(buf1, ucLen);
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
{
wsT2 = _T("Sending TOP: ") + ( m_pSock->Error() ? _("failed !") : _("done") );
wxLogMessage( wsT2 );
}
wsT1 = wsSendPrefix + wsCommand.BeforeFirst( '\r');
wxLogMessage( wsT1 );
wsT1.Printf( _T("%s/%ld"), wsT1, m_lNMessages );
SetStatusText( wsT1, 2);
if( ! GetNextLineFromServer( m_pSock, wsResponse,
g_iniPrefs.data[IE_POP3_SERVER_TIMEOUT].dataCurrent.lVal ) )
{
wxString wsT3 = _("Server error for: ");
wxLogMessage( wsT3 + wsCommand );
return false;
}
if( ! OkResponse( wsResponse ) )
{
wxString wsT3 = _("Server Response error: ");
wxLogMessage( wsT3 + wsResponse );
return false;
}
wsT = wsRecPrefix + wsResponse.BeforeFirst( '\r' );
wxLogMessage( wsT );
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
{
wsT2 = _T("Received TOP reply: ") + ( m_pSock->Error() ? _("failed !") : _("done") );
wxLogMessage( wsT2 );
}
long lLinesExpected = m_lNTopLines + 1;
bEndOfHeader = false;
wxString wsMessageTop;
while( true )
{
GetNextLineFromServer( m_pSock, wsResponse,
g_iniPrefs.data[IE_POP3_SERVER_TIMEOUT].dataCurrent.lVal );
if (! wsResponse.IsSameAs(_T(".\r\n")) )
wsMessageTop += wsResponse;
// strip the newline for a better. more compact list
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
{
// need to convert all single '%' to "%%" for logging
wxString wsFix = wsResponse;;
size_t n = wsFix.Replace( "%", "%%" );
wxLogMessage( wsFix.BeforeFirst( '\r' ) );
}
if ( wsResponse.IsSameAs(_T(".\r\n")) )
{
#if defined( WANT_WX_EMAIL )
// End of server reply - interpret/decode the message header
wxEmailMessage* pMsgHeader =
new wxEmailMessage( wsMessageTop, jj, false );
{
// ensure no one the message data while we write it
wxCriticalSectionLocker lock(m_dataCS);
for (std::list<MyPop3MsgElement>::iterator it = m_Pop3MsgList.begin();
it != m_Pop3MsgList.end(); ++it )
{
if( it->m_lSequence == jj + 1 )
{
wxASSERT( it->m_pEmailMessage == NULL );
it->m_pEmailMessage = pMsgHeader;
it->m_bHaveMessage = bEndOfHeader;
it->m_wsHeader = wsMsgHeader;
it->m_bHaveMessage = false;
break;
}
}
m_lMsgIndex = jj;
}
#endif
#if defined( WANT_PROGRESS_DIALOG )
/* Check if we need to create progress dialog */
if (m_pProgressDialog == NULL)
{
m_pProgressDialog = new wxProgressDialog(
_T("wxMS : Retrieving messages"),
_T("Please wait while the messages are retrieved from the server"),
m_lNMessages );
}
/* Update the progress dialog status */
m_pProgressDialog->Update(jj);
#endif
wxQueueEvent(GetEventHandler(), new wxThreadEvent());
break;
}
else
{
// Append content to line
if ( wsMsgHeader != _T(""))
{
wsMsgHeader += _T("\r\n");
}
if (wsResponse.StartsWith(_T("..")))
{
wsMsgHeader += wsResponse.AfterFirst('.');
}
else
{
wsMsgHeader += wsResponse;
}
if( wsResponse.BeforeFirst( '\r' ).IsSameAs( wxEmptyString) )
{
bEndOfHeader = true;
}
if ( bEndOfHeader )
lLinesExpected--;
}
}
return true;
}
// ------------------------------------------------------------------
void MyFrame::GetPop3MessageContent( wxString a_wsAccount, long lIndex,
wxString & wsContent )
{
m_wsPop3Server = m_wsUser = m_wsPassword = wxEmptyString;
MyAccountList::iterator iter;
for ( iter = wxGetApp().m_AccountList.begin();
iter != wxGetApp().m_AccountList.end(); ++iter )
{
MyMsAccountEl *pCurrent = *iter;
if ( a_wsAccount.IsSameAs(pCurrent->m_wsAcctName ) )
{
m_wsPop3Server = pCurrent->m_wsPopServerUrl;
m_wsUser = pCurrent->m_wsUserName;
m_wsPassword = pCurrent->m_wsPassword;
break;
}
}
if ( m_wsPop3Server.IsEmpty() || m_wsUser.IsEmpty() )
{
wxLogMessage(_T("Empty Server URL or empty user name for account: %s"), a_wsAccount );
wxBell();
return;
}
m_wasCommands.Clear();
wxString wsT;
wsT.Printf( _T("RETR %ld\r\n"), lIndex );
m_bRunning = false;
m_wasCommands.Add( wsT );
// Set busy info
// wxBusyInfo pBusy_info( _T("Please wait while the content\nof the message is downloaded") );
RunLibcurlThread(); // handle the mail check via threads
while ( m_bRunning )
{
// ::wxYield();
::wxMilliSleep( 10 );
}
std::list<MyPop3MsgElement>::iterator it;
{
// ensure no one modifies the message data while we
// get ready to display it ???
wxCriticalSectionLocker lock(m_dataCS);
for (it = m_Pop3MsgList.begin(); it != m_Pop3MsgList.end(); ++it )
{
if( it->m_lSequence == lIndex )
{
wsContent = it->m_wsMessage;
break;
}
}
}
}
// ------------------------------------------------------------------
bool MyFrame::ExecutePop3RetrCommand( wxString wsCommand )
{
// wxBusyCursor wait;
long jj;
const char *buf1;
unsigned char ucLen;
wxString wsT;
wxString wsT1;
wxString wsT2;
wxString wsResponse;
wxString wsRecPrefix = _T("< ");
wxString wsSendPrefix = _T("> ");
bool bEndOfHeader = false;
long lMsgSize = 0;
long lIndex;
wxString wsIndex = wsCommand.AfterFirst( ' ' );
wsIndex.ToLong( &lIndex );
if ( m_lNMessages )
{
wxString wsMessage;
bEndOfHeader = false;
{
// ensure no one modifies the message data while we
// get ready to display it ???
wxCriticalSectionLocker lock(m_dataCS);
for (std::list<MyPop3MsgElement>::iterator it = m_Pop3MsgList.begin();
it != m_Pop3MsgList.end(); ++it )
{
if( it->m_lSequence == lIndex )
{
lMsgSize = it->m_lSize;
break;
}
}
}
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
wxLogMessage(_("Sending RETR to the server ..."));
wxString wsT = wsCommand.AfterFirst( ' ');
wsT.ToLong( &jj );
// adjust for array base of 0
jj--;
buf1 = wsCommand.c_str();
// don't count and send the terminating '\0'
ucLen = (unsigned char)(wxStrlen(buf1));
m_pSock->Write(buf1, ucLen);
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
{
wsT2 = _T("Sending RETR: ") + ( m_pSock->Error() ? _("failed !") : _("done") );
wxLogMessage( wsT2 );
}
wsT1 = wsSendPrefix + wsCommand.BeforeFirst( '\r');
wxLogMessage( wsT1 );
wsT1.Printf( _T("%s/%ld"), wsT1, m_lNMessages );
SetStatusText( wsT1, 2);
if( ! GetNextLineFromServer( m_pSock, wsResponse,
g_iniPrefs.data[IE_POP3_SERVER_TIMEOUT].dataCurrent.lVal ) )
{
wxString wsT3 = _("Server returned: ");
wxLogMessage( wsT3 + wsT1 );
return false;
}
if( ! OkResponse( wsResponse ) )
{
wxString wsT3 = _("Server Response error: ");
wxLogMessage( wsT3 + wsResponse );
return false;
}
wsT = wsRecPrefix + wsResponse.BeforeFirst( '\r' );
wxLogMessage( wsT );
if ( g_iniPrefs.data[IE_LOG_VERBOSITY].dataCurrent.lVal > 4 )
{
wsT2 = _T("Received RETR reply: ") + ( m_pSock->Error() ? _("failed !") : _("done") );
wxLogMessage( wsT2 );
}
bool bRet = GetMessage( wsMessage, lMsgSize );
#if defined( WANT_WX_EMAIL )
// strip off the last line with nothing but '.\r\n'
wsMessage = wsMessage.BeforeLast( '.' );
/* End of message -> convert it
* Get the corresponding message */
wxEmailMessage* message =
new wxEmailMessage( wsMessage, jj, true );
{
// ensure no one reads m_data while we refresh the message array
wxCriticalSectionLocker lock(m_dataCS);
for (std::list<MyPop3MsgElement>::iterator it = m_Pop3MsgList.begin();
it != m_Pop3MsgList.end(); ++it )
{
if( it->m_lSequence == jj + 1 )
{
if ( it->m_pEmailMessage )
delete it->m_pEmailMessage;
it->m_pEmailMessage = message;
it->m_bHaveMessage = bEndOfHeader;
it->m_wsMessage = wsMessage;
it->m_bHaveMessage = true;
break;
}
}
m_lMsgIndex = jj;
}
#endif
}
SetStatusText( _T(""), 2);
return true;
}
#endif
// ------------------------------- eof ------------------------------