forked from maccasoft/lora3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpop3.cpp
More file actions
302 lines (276 loc) · 11.7 KB
/
Copy pathpop3.cpp
File metadata and controls
302 lines (276 loc) · 11.7 KB
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
// ----------------------------------------------------------------------
// LoraBBS Professional Edition - Version 3.00.6
// Copyright (c) 1996 by Marco Maccaferri. All rights reserved.
//
// History:
// 02/07/96 - Initial coding.
// ----------------------------------------------------------------------
#include "_ldefs.h"
#include "lserver.h"
static CHAR *WeekDays[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static CHAR *Months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
TPOP3::TPOP3 (void)
{
Tcp = NULL;
User = NULL;
Msg = NULL;
Log = NULL;
}
TPOP3::~TPOP3 (void)
{
if (Tcp != NULL)
delete Tcp;
if (User != NULL)
delete User;
if (Msg != NULL)
delete Msg;
}
VOID TPOP3::GetCommand (VOID)
{
USHORT len = 0, MaxLen = sizeof (Response);
CHAR c, *pszResp = Response;
do {
if (Tcp->BytesReady () == TRUE) {
do {
if ((c = (CHAR)Tcp->ReadByte ()) != '\r') {
if (c != '\n') {
*pszResp++ = c;
if (++len >= MaxLen)
c = '\r';
}
}
} while (Tcp->RxBytes > 0 && c != '\r');
}
#if defined(__OS2__)
DosSleep (1L);
#elif defined(__NT__)
Sleep (1L);
#endif
} while (c != '\r' && Tcp->Carrier () == TRUE);
*pszResp = '\0';
}
VOID TPOP3::Run (VOID)
{
USHORT EndRun, Validated, Index, Found;
CHAR *p;
ULONG Number, Size;
MAILINDEX *MailIdx;
time_t mtime;
struct tm ftm;
EndRun = FALSE;
Validated = FALSE;
sprintf (Temp, "+OK LoraBBS POP3 server ready\r\n");
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
User = new TUser (Cfg->UserFile);
while (Tcp->Carrier () == TRUE && EndRun == FALSE) {
GetCommand ();
if ((p = strtok (Response, " ")) != NULL) {
if (!stricmp (p, "QUIT")) {
if (Validated == TRUE && Msg != NULL) {
if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
do {
if (MailIdx->Deleted == TRUE)
Msg->Delete (MailIdx->Number);
} while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
}
sprintf (Temp, "+OK LoraBBS POP3 server signing off\r\n");
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
EndRun = TRUE;
}
else if (!stricmp (p, "USER")) {
Validated = FALSE;
if ((p = strtok (NULL, "")) != NULL) {
while (*p == ' ')
p++;
strcpy (Response, strlwr (p));
Response[0] = (CHAR)toupper (Response[0]);
while ((p = strchr (Response, '.')) != NULL) {
*p++ = ' ';
*p = (CHAR)toupper (*p);
}
if (User->GetData (Response) == TRUE)
sprintf (Temp, "+OK %s is welcome here\r\n", Response);
else
sprintf (Temp, "-ERR never heard of %s\r\n", Response);
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
}
}
else if (!stricmp (p, "PASS")) {
Validated = FALSE;
if ((p = strtok (NULL, " ")) != NULL) {
if (User->CheckPassword (p) == TRUE) {
MailDrop.Clear ();
if (Cfg->MailStorage == ST_JAM)
Msg = new JAM (Cfg->MailPath);
else if (Cfg->MailStorage == ST_SQUISH)
Msg = new SQUISH (Cfg->MailPath);
else if (Cfg->MailStorage == ST_FIDO)
Msg = new FIDOSDM (Cfg->MailPath);
else if (Cfg->MailStorage == ST_ADEPT)
Msg = new ADEPT (Cfg->MailPath);
Index = 0;
Size = 0L;
if (Msg != NULL) {
Number = Msg->Lowest ();
do {
if (Msg->Read (Number) == TRUE && !stricmp (User->Name, Msg->To)) {
Idx.Index = ++Index;
Idx.Number = Number;
Idx.Deleted = FALSE;
Idx.Size = 0L;
Idx.Size += strlen (Msg->From);
Idx.Size += strlen (Msg->To);
Idx.Size += strlen (Msg->Subject);
if ((p = (CHAR *)Msg->Text.First ()) != NULL)
do {
Idx.Size += strlen (p) + 1;
} while ((p = (CHAR *)Msg->Text.Next ()) != NULL);
Size += Idx.Size;
MailDrop.Add (&Idx, sizeof (Idx));
}
} while (Msg->Next (Number) == TRUE);
}
sprintf (Temp, "+OK maildrop locked and ready\r\n");
Validated = TRUE;
}
else
sprintf (Temp, "-ERR invalid password\r\n");
}
else
sprintf (Temp, "-ERR invalid password\r\n");
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
}
else if (!stricmp (p, "NOOP")) {
sprintf (Temp, "+OK\r\n");
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
}
else if (Validated == TRUE) {
if (!stricmp (p, "STAT")) {
sprintf (Temp, "+OK %u %lu\r\n", Index, Size);
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
}
else if (!stricmp (p, "LIST")) {
if ((p = strtok (NULL, " ")) != NULL) {
Found = FALSE;
if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
do {
if (MailIdx->Index == (USHORT)atoi (p)) {
sprintf (Temp, "+OK %u %lu\r\n", MailIdx->Index, MailIdx->Size);
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
Found = TRUE;
break;
}
} while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
if (Found == FALSE) {
sprintf (Temp, "-ERR no such message\r\n");
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
}
}
else {
sprintf (Temp, "+OK %u messages (%lu octects)\r\n", Index, Size);
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
do {
sprintf (Temp, "%u %lu\r\n", MailIdx->Index, MailIdx->Size);
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
} while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
Tcp->BufferBytes ((UCHAR *)".\r\n", 3);
Tcp->UnbufferBytes ();
}
}
else if (!stricmp (p, "RETR")) {
Found = FALSE;
if ((p = strtok (NULL, " ")) != NULL) {
if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
do {
if (MailIdx->Index == (USHORT)atoi (p)) {
sprintf (Temp, "+OK %lu octects\r\n", MailIdx->Index, MailIdx->Size);
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
Found = TRUE;
break;
}
} while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
}
if (Found == FALSE) {
sprintf (Temp, "-ERR no such message\r\n");
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
}
else {
Msg->Read (MailIdx->Number);
memset (&ftm, 0, sizeof (ftm));
ftm.tm_mday = Msg->Written.Day;
ftm.tm_mon = Msg->Written.Month - 1;
ftm.tm_year = Msg->Written.Year - 1900;
ftm.tm_hour = Msg->Written.Hour;
ftm.tm_min = Msg->Written.Minute;
ftm.tm_sec = Msg->Written.Second;
mtime = mktime (&ftm);
memcpy (&ftm, gmtime (&mtime), sizeof (ftm));
sprintf (Temp, "Date: %s %d %s %d %d:%02d:%02d GMT\r\n", WeekDays[ftm.tm_wday], ftm.tm_mday, Months[ftm.tm_mon], ftm.tm_year % 100, ftm.tm_hour, ftm.tm_min, ftm.tm_sec);
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
sprintf (Temp, "From: %s <%s>\r\n", Msg->From, Msg->FromAddress);
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
sprintf (Temp, "To: %s <%s>\r\n", Msg->To, Msg->ToAddress);
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
sprintf (Temp, "Subject: %s\r\n", Msg->Subject);
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
sprintf (Temp, "\r\n");
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
sprintf (Temp, "\r\n");
if ((p = (CHAR *)Msg->Text.First ()) != NULL)
do {
if (!strcmp (p, "."))
Tcp->BufferBytes ((UCHAR *)"..", 2);
else
Tcp->BufferBytes ((UCHAR *)p, (USHORT)strlen (p));
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
} while ((p = (CHAR *)Msg->Text.Next ()) != NULL);
Tcp->BufferBytes ((UCHAR *)".\r\n", 3);
Tcp->UnbufferBytes ();
}
}
else if (!stricmp (p, "DELE")) {
Found = FALSE;
if ((p = strtok (NULL, " ")) != NULL) {
if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
do {
if (MailIdx->Index == (USHORT)atoi (p)) {
if (MailIdx->Deleted == TRUE)
sprintf (Temp, "-ERR message %lu already deleted\r\n", MailIdx->Index);
else
sprintf (Temp, "+OK message %lu deleted\r\n", MailIdx->Index);
Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
MailIdx->Deleted = TRUE;
Found = TRUE;
break;
}
} while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
}
if (Found == FALSE)
sprintf (Temp, "-ERR no such message\r\n");
else
sprintf (Temp, "+OK\r\n");
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
}
else {
sprintf (Temp, "-ERR command not implemented.\r\n");
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
}
}
else {
sprintf (Temp, "-ERR command not implemented.\r\n");
Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
}
}
#if defined(__OS2__)
DosSleep (1L);
#elif defined(__NT__)
Sleep (1L);
#endif
}
}