-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailbox.cpp
345 lines (294 loc) · 8.04 KB
/
mailbox.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
#include "..\litestep248\lsapi\lsapi.h"
#include <string.h>
#include <winerror.h>
#include <windows.h>
#include "mailbox.h"
#include "acidmail.h"
#include "version.h"
#include "resource.h"
#define BUFSIZE 2048
// Globals
BOOL CALLBACK GetPassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM /* lParam */);
extern HINSTANCE hwndInstance;
char temp_name[64] = {0}, temp_pass[64] = {0};
Mailbox::Mailbox(char* name, unsigned numSubjects):
mail()
{
// Copy args
strcpy(sName, name);
iSubjects = numSubjects;
// Copy the rest of the args derived from the mailbox name
strcpy(sSetting, name);
strcat(sSetting, "server");
if(GetRCString(sSetting, sServer, NULL, MAX_LINE_LENGTH))
{
strcpy(sSetting, name);
strcat(sSetting, "type");
GetRCString(sSetting, sType, "pop3", MAX_LINE_LENGTH);
strcpy(sSetting, name);
strcat(sSetting, "user");
GetRCString(sSetting, sUser, "anonymous", MAX_LINE_LENGTH);
strcpy(sSetting, name);
strcat(sSetting, "port");
if (!strcmp("imap", sType)) GetRCString(sSetting, sPort, "143", MAX_LINE_LENGTH);
else GetRCString(sSetting, sPort, "110", MAX_LINE_LENGTH);
strcpy(sSetting, name);
strcat(sSetting, "password");
GetRCString(sSetting, sPass, "", MAX_LINE_LENGTH);
strcpy(sSetting, name);
strcat(sSetting, "folder");
GetRCString(sSetting, sFolder, "inbox", MAX_LINE_LENGTH);
// If no password was found in config, get it from database/dialog
if (!(*sPass)) GetPass();
// Set evars
strcpy(sEvar, "AcidMail");
strcat(sEvar, name);
LSSetVariable(sEvar, "0");
strcpy(sErrorVar, sEvar);
strcat(sErrorVar, "Error");
LSSetVariable(sErrorVar, "none");
}
else
{
ErrorHandler(Error(false, LOG_ERROR, "No host found in config files", NULL));
mail.bError = true;
}
}
Mailbox::~Mailbox()
{
}
Mail Mailbox::CheckMail()
{
s = INVALID_SOCKET;
try
{
char data[BUFSIZE];
char* buf = data;
// structs for socket info
struct addrinfo aiHints, *aiList = NULL, *ptr = NULL;
ZeroMemory( &aiHints, sizeof(aiHints) ); //Clear aiHints before use
aiHints.ai_family = AF_UNSPEC;
aiHints.ai_socktype = SOCK_STREAM;
aiHints.ai_protocol = IPPROTO_TCP;
// get socket info
if ((getaddrinfo(sServer, sPort, &aiHints, &aiList)) != 0)
throw Error(false, LOG_WARNING, "Could not find host:", sServer);
// loop sockets
for (ptr=aiList; (ptr != NULL) && (s == INVALID_SOCKET) ;ptr=ptr->ai_next)
{
// Create a SOCKET for connecting to server
s = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (s == INVALID_SOCKET) continue;
if (connect(s, ptr->ai_addr, (int)ptr->ai_addrlen) == SOCKET_ERROR) // Connect to server
s = INVALID_SOCKET;
}
if (s == INVALID_SOCKET)
{
char errnum[32];
sprintf(errnum, "%d", WSAGetLastError());
throw Error(true, LOG_WARNING, "Could not connect to host, socket error number:", errnum);
}
if (!strcmp("pop3", sType))
mail.iNumMail = Pop3Check(buf);
else if(!strcmp("imap", sType))
mail.iNumMail = ImapCheck(buf);
else
throw Error(true, LOG_ERROR, "Invalid mailbox type:", sType);
// Try to shutdown grasefully, otherwise simply closesocket()
if (!shutdown(s, SD_SEND))
while(recv(s, buf, BUFSIZE, 0) > 0);
closesocket(s);
freeaddrinfo(aiList); // aiList needs to be freed after use
char snum[8];
sprintf(snum, "%d", mail.iNumMail);
LSSetVariable(sEvar, snum);
mail.bError = false;
}
catch (Error& error)
{
ErrorHandler(error);
mail.bError = true;
}
return mail;
}
int Mailbox::Pop3Check(char*& buf)
{
unsigned ans = 0;
sprintf(buf, " ");
PopCmd(buf);
sprintf(buf, "user %s\r\n" ,sUser);
PopCmd(buf);
sprintf(buf, "pass %s\r\n" ,sPass);
PopCmd(buf);
sprintf(buf, "stat\r\n");
PopCmd(buf);
char test[8];
strcpy(test, strtok(buf, " "));
if (test != NULL)
{
ans = atoi(strtok(NULL, " "));
// Get subjects and write to vars
int subs;
if (iSubjects < ans)
subs = iSubjects;
else
subs = ans;
for (int ansc = ans; subs > 0; subs--)
{
sprintf(buf, "top %d 0\r\n", subs);
PopCmd(buf);
char tmpbuf[8];
recv(s, tmpbuf, 8, 0);
char * ptr = strstr(buf, "Subject: ");
if(ptr)
{
strtok(ptr, " ");
ptr = strtok(NULL, "\r");
char sEvarSub[140], sSubject[1024];
sprintf(sEvarSub, "%s%s%d", sEvar, "Sub", subs);
sprintf(sSubject, "\"%s\"", ptr);
LSSetVariable(sEvarSub, sSubject);
}
}
}
else
throw Error(true, LOG_ERROR, "Didn't get number of mail:", strtok(buf, "\r"));
send(s, "quit\r\n", 6, 0);
return ans;
}
void Mailbox::PopCmd(char*& buf)
{
if(send(s, buf, static_cast<int>(strlen(buf)), 0) <= 0)
throw Error(true, LOG_WARNING, "Connection error", NULL);
if(recv(s, buf, BUFSIZE, 0) <= 0)
throw Error(true, LOG_WARNING, "Connection error", NULL);
if(!strstr(buf, "+OK"))
throw Error(true, LOG_WARNING, "Bad responce:", strtok(buf, "\r"));
}
int Mailbox::ImapCheck(char*& buf)
{
unsigned ans = 0;
sprintf(buf, " ");
ImapCmd(buf);
if(buf[2] != 'O' || buf[3] != 'K') throw Error(true, LOG_WARNING, "Bad response:", strtok(buf, "\r"));
sprintf(buf, "a01 login %s %s\r\n", sUser, sPass);
ImapCmd(buf);
if(buf[4] != 'O' || buf[5] != 'K') throw Error(true, LOG_WARNING, "Bad response:", strtok(buf, "\r"));
sprintf(buf, "a02 examine %s\r\n", sFolder);
ImapCmd(buf);
if(!strstr(buf, "OK")) throw Error(true, LOG_WARNING, "Bad response:", strtok(buf, "\r"));
send(s, "a03 logout\r\n", 12, 0);
/* char * ptr = strstr(buf, "RECENT");
if(ptr)
{
ptr-=3;
while (*ptr != ' ')
ptr-=1;
ans = atoi(strtok(ptr, " "));
for (int subs = iSubjects; subs > 0; subs--)
{
}
}*/
else throw Error(true, LOG_ERROR, "Didn't get number of mail:", strtok(buf, "\r"));
return ans;
}
void Mailbox::ImapCmd(char *&buf)
{
if(send(s, buf, static_cast<int>(strlen(buf)), 0) <= 0)
throw Error(true, LOG_WARNING, "Connection error", NULL);
if(recv(s, buf, BUFSIZE, 0) <= 0)
throw Error(true, LOG_WARNING, "Connection error", NULL);
}
void Mailbox::ClearMail()
{
mail.iNumMail = 0;
mail.bError = false;
LSSetVariable(sEvar, "0");
}
void Mailbox::GetPass()
{
char temp[256];
sprintf(temp, "%s/%s", sServer, sUser);
GetProfileString("AcidMail", temp, "", temp, 256);
if (!strlen(temp))
{
strcpy(temp_name, sName);
DialogBox(hwndInstance, MAKEINTRESOURCE(IDD_GETPASS), NULL, GetPassProc);
if (strlen(temp_pass))
{
strcpy(sPass, temp_pass);
sprintf(temp, "%s/%s", sServer, sUser);
Encrypt(&temp_pass[0]);
WriteProfileString("AcidMail", temp, temp_pass);
}
else
strcpy(sPass, "");
memset(&(temp_pass), 0, sizeof(temp_pass));
}
else
Encrypt(temp);
strcpy(sPass, temp);
}
void Mailbox::Encrypt(char* pass)
{
for (; *pass; *pass++)
{
*pass=*pass ^ 0x5405;
}
}
void Mailbox::ErrorHandler(Error& errstc)
{
if(errstc.bSock) closesocket(s);
char sError[1022], sError2[1024];
sprintf(sError, "Error with mailbox: %s. %s %s", sName, errstc.sErrstr, errstc.sErradd);
LSLog(errstc.iLvl, szAppName, sError);
sprintf(sError2, "\"%s\"", sError);
LSSetVariable(sErrorVar, sError2);
}
BOOL CALLBACK GetPassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM /* lParam */)
{
switch (msg)
{
case WM_INITDIALOG:
{
char temp[256] = "";
sprintf(temp, "%s Password", temp_name);
SetWindowText(hwnd, temp);
return TRUE;
}
case WM_COMMAND:
{
if (HIWORD(wParam) == BN_CLICKED)
{
switch (LOWORD(wParam))
{
case IDOK:
{
GetDlgItemText(hwnd, IDC_PASSWORD, temp_pass, 64);
}
case IDCANCEL:
{
EndDialog(hwnd, 0);
}
break;
}
}
}
break;
}
return false;
}
// Simple default constructor for the Mail struct
Mail::Mail()
{
iNumMail = 0;
bError = false;
}
// Constructor for exception error struct
Error::Error(bool Sock, int lvl, const char* errstr, const char* erradd)
{
bSock = Sock;
iLvl = lvl;
sErrstr = errstr;
sErradd = erradd;
}