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
|
/*
* This code implements basics of POP3 protocol
*
* (c) majvan 2002-2004
*/
/* This was made from the libspopc project
* copyright c 2002 Benoit Rouits <brouits@free.fr>
* released under the terms of GNU LGPL
* (GNU Lesser General Public Licence).
* libspopc offers simple API for a pop3 client (MTA).
* See RFC 1725 for pop3 specifications.
* more information on http://brouits.free.fr/libspopc/
*/
/*
* This file is not original and is changed by majvan <om3tn@psg.sk>
* for mail checker purpose. Please see original web page to
* obtain the original. I rewrote it in C++, but good ideas were,
* I think, unchanged.
*
* Note that this file was not designed to work under Unix. It's
* needed to add Unix-specific features. I was interested only in
* Windows for my project. majvan
*
*/
#pragma warning( disable : 4290 )
#include "..\..\yamn.h"
#include "pop3.h"
extern "C" {
#include "../md5.h"
}
extern void __stdcall SSL_DebugLog( const char *fmt, ... );
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//Connects to the server through the netlib
//if not success, exception is throwed
//returns welcome string returned by server
//sets AckFlag
char *CPop3Client::Connect(const char* servername,const int port,BOOL UseSSL, BOOL NoTLS)
{
char *temp = 0;
if (Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
if (NetClient!=NULL)
delete NetClient;
SSL=UseSSL;
NetClient=new CNLClient;
#ifdef DEBUG_DECODE
DebugLog(DecodeFile,"Connect:servername: %s port:%d\n",servername,port);
#endif
POP3Error=EPOP3_CONNECT;
NetClient->Connect(servername,port);
POP3Error=0;
if (SSL)
{
try { NetClient->SSLify(); }
catch (...)
{
NetClient->Disconnect();
return NULL;
}
}
temp = RecvRest(NetClient->Recv(),POP3_SEARCHACK);
extern BOOL SSLLoaded;
if (!NoTLS & !(SSL)){
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
NetClient->Send("STLS\r\n");
free(temp);
temp=RecvRest(NetClient->Recv(),POP3_SEARCHACK);
if (AckFlag==POP3_FOK){ // Ok, we are going to tls
try {
NetClient->SSLify();
} catch (...) {
NetClient->Disconnect();
return NULL;
}
// temp = RecvRest(NetClient->Recv(),POP3_SEARCHACK);
}
}
// SSL_DebugLog("Received: %s",temp);
return temp;
}
//Receives data to the end of packet
// prev- previous data read (appends to this string next received data)
// mode- mode of packet.
// Packet can end with ack state (+OK or -ERR): set mode to POP3_SEARCHACK
// If packet ends with '.' (end of string), set mode to POP3_SEARCHDOT
// size- received data are stored to memory, but if length of data is more than allocated memory, function allocates
// new memory. New allocated memory has allocated size more bytes
// This value can be selectable: if you think it is better to reallocate by 1kB size, select size to 1024,
// default is 128. You do not need to use this parameter
char* CPop3Client::RecvRest(char* prev,int mode,int size)
{
int SizeRead=0;
int SizeLeft=size-NetClient->Rcv;
int RcvAll=NetClient->Rcv;
char *LastString,*PrevString=prev;
AckFlag=0;
while(((mode==POP3_SEARCHDOT) && !SearchFromEnd(PrevString+RcvAll-1,RcvAll-3,POP3_SEARCHDOT) && !SearchFromStart(PrevString,2,POP3_SEARCHERR)) || //we are looking for dot or -err phrase
((mode==POP3_SEARCHACK) && (!SearchFromStart(PrevString,RcvAll-3,mode) || !((RcvAll>3) && SearchFromEnd(PrevString+RcvAll-1,1,POP3_SEARCHNL))))) //we are looking for +ok or -err phrase ended with newline
{ //if not found
if (NetClient->Stopped) //check if we can work with this POP3 client session
{
if (PrevString!=NULL)
free(PrevString);
throw POP3Error=(DWORD)EPOP3_STOPPED;
}
if (SizeLeft==0) //if block is full
{
SizeRead+=size;
SizeLeft=size;
LastString=NetClient->Recv(NULL,SizeLeft);
PrevString=(char *)realloc(PrevString,sizeof(char)*(SizeRead+size));
if (PrevString==NULL)
throw POP3Error=(DWORD)EPOP3_RESTALLOC;
memcpy(PrevString+SizeRead,LastString,size);
free(LastString);
}
else
NetClient->Recv(PrevString+RcvAll,SizeLeft); //to Rcv stores received bytes
SizeLeft=SizeLeft-NetClient->Rcv;
RcvAll+=NetClient->Rcv;
// printf("[Read: %s]\n",PrevString);
}
NetClient->Rcv=RcvAll; //at the end, store the number of all bytes, no the number of last received bytes
return PrevString;
}
// CPop3Client::SearchFromEnd
// returns 1 if substring DOTLINE or ENDLINE found from end in bs bytes
// if you need to add condition for mode, insert it into switch statement
BOOL CPop3Client::SearchFromEnd(char *end,int bs,int mode)
{
while(bs>=0)
{
switch(mode)
{
case POP3_SEARCHDOT:
if (DOTLINE(end))
return 1;
break;
case POP3_SEARCHNL:
if (ENDLINE(end))
return 1;
break;
}
end--;
bs--;
}
return 0;
}
//Finds for a occurence of some pattern in string
// returns 1 if substring OKLINE, ERRLINE or any of them found from start in bs bytes
//call only this function to retrieve ack status (+OK or -ERR), because it sets flag AckFlag
//if you need to add condition for mode, insert it into switch statement
BOOL CPop3Client::SearchFromStart(char *start,int bs,int mode)
{
while(bs>=0)
{
switch(mode)
{
case POP3_SEARCHOK:
if (OKLINE(start))
{
AckFlag=POP3_FOK;
return 1;
}
break;
case POP3_SEARCHERR:
if (ERRLINE(start))
{
AckFlag=POP3_FERR;
return 1;
}
break;
case POP3_SEARCHACK:
if (ACKLINE(start))
{
OKLINE(start) ? AckFlag=POP3_FOK : AckFlag=POP3_FERR;
return 1;
}
break;
}
start++;
bs--;
}
return 0;
}
//Performs "USER" pop query and returns server response
//sets AckFlag
char* CPop3Client::User(char* name)
{
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
char query[128];
char *Result;
sprintf(query,"USER %s\r\n",name);
NetClient->Send(query);
Result=RecvRest(NetClient->Recv(),POP3_SEARCHACK);
if (AckFlag==POP3_FERR)
throw POP3Error=(DWORD)EPOP3_BADUSER;
POP3Error=0;
return Result;
}
//Performs "PASS" pop query and returns server response
//sets AckFlag
char* CPop3Client::Pass(char* pw)
{
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
char query[128];
char *Result;
sprintf(query,"PASS %s\r\n",pw);
NetClient->Send(query);
Result=RecvRest(NetClient->Recv(),POP3_SEARCHACK);
if (AckFlag==POP3_FERR)
throw POP3Error=(DWORD)EPOP3_BADPASS;
return Result;
}
//Performs "APOP" pop query and returns server response
//sets AckFlag
char* CPop3Client::APOP(char* name, char* pw, char* timestamp)
{
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
char query[512];
char *Result;
unsigned char digest[16];
char hexdigest[40];
if (timestamp==NULL)
throw POP3Error=(DWORD)EPOP3_APOP;
MD5Context ctx;
MD5Init(&ctx);
MD5Update(&ctx,(const unsigned char *)timestamp,(unsigned int)strlen(timestamp));
MD5Update(&ctx,(const unsigned char *)pw,(unsigned int)strlen(pw));
MD5Final(digest,&ctx);
hexdigest[0]='\0';
for(int i=0; i<16; i++) {
char tmp[4];
sprintf(tmp, "%02x", digest[i]);
strcat(hexdigest, tmp);
}
sprintf(query,"APOP %s %s\r\n",name, hexdigest);
NetClient->Send(query);
Result=RecvRest(NetClient->Recv(),POP3_SEARCHACK);
if (AckFlag==POP3_FERR)
throw POP3Error=(DWORD)EPOP3_BADUSER;
return Result;
}
//Performs "QUIT" pop query and returns server response
//sets AckFlag
char* CPop3Client::Quit()
{
char query[]="QUIT\r\n";
NetClient->Send(query);
return RecvRest(NetClient->Recv(),POP3_SEARCHACK);
}
//Performs "STAT" pop query and returns server response
//sets AckFlag
char* CPop3Client::Stat()
{
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
char query[]="STAT\r\n";
NetClient->Send(query);
return RecvRest(NetClient->Recv(),POP3_SEARCHACK);
}
//Performs "LIST" pop query and returns server response
//sets AckFlag
char* CPop3Client::List()
{
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
char query[]="LIST\r\n";
NetClient->Send(query);
return RecvRest(NetClient->Recv(),POP3_SEARCHDOT);
}
//Performs "TOP" pop query and returns server response
//sets AckFlag
char* CPop3Client::Top(int nr, int lines)
{
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
char query[128];
sprintf(query,"TOP %d %d\r\n",nr,lines);
NetClient->Send(query);
return RecvRest(NetClient->Recv(),POP3_SEARCHDOT);
}
//Performs "UIDL" pop query and returns server response
//sets AckFlag
char* CPop3Client::Uidl(int nr)
{
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
char query[128];
if (nr)
{
sprintf(query,"UIDL %d\r\n",nr);
NetClient->Send(query);
return RecvRest(NetClient->Recv(),POP3_SEARCHACK);
}
sprintf(query,"UIDL\r\n");
NetClient->Send(query);
return RecvRest(NetClient->Recv(),POP3_SEARCHDOT);
}
//Performs "DELE" pop query and returns server response
//sets AckFlag
char* CPop3Client::Dele(int nr)
{
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
char query[128];
sprintf(query,"DELE %d\r\n",nr);
NetClient->Send(query);
return RecvRest(NetClient->Recv(),POP3_SEARCHACK);
}
//Performs "RETR" pop query and returns server response
//sets AckFlag
char* CPop3Client::Retr(int nr)
{
if (NetClient->Stopped) //check if we can work with this POP3 client session
throw POP3Error=(DWORD)EPOP3_STOPPED;
char query[128];
sprintf(query,"RETR %d\r\n",nr);
NetClient->Send(query);
RecvRest(NetClient->Recv(),POP3_SEARCHACK);
return NetClient->Recv();
}
|