-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessEml.c
447 lines (335 loc) · 12.2 KB
/
ProcessEml.c
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
#include "YahooEml.h"
#include <time.h>
enum {HASATTACH=0x01, HASPLAIN=0x02, HASHTML=0x04 }
// Return a set of flags showing what elements are present in this email
unsigned FindParts(MIMEREC *MimeRec)
{
unsigned res = 0;
while (MimeRec)
{
if (MimeRec->Child) res |= FindParts(MimeRec->Child);
if (MimeRec->FileName) res |= HASATTACH;
else if (MimeRec->ContentType == CT_TEXT_HTML) res |= HASHTML;
else if (MimeRec->ContentType == CT_TEXT_PLAIN) res|= HASPLAIN;
MimeRec = MimeRec->Next;
}
return(res);
}
int CountAttach(MIMEREC *MimeRec)
{
int Count = 0;
while (MimeRec)
{
Count += CountAttach(MimeRec->Child);
if (MimeRec->FileName) Count++;
MimeRec->Flag = FALSE; // clear flag
MimeRec = MimeRec->Next;
}
return(Count);
}
// Search for the next MimeRec with a matching type.
// Returns NULL if no more records found.
// Ctype can be HASATTACH, HASTEXT, or HASHTML
MIMEREC *GetNextRec(MIMEREC *MimeRec, int Ctype)
{
while (MimeRec)
{
if (MimeRec->Child)
{
MIMEREC *ptr;
ptr = GetNextRec(MimeRec->Child, Ctype);
if (ptr) return(ptr);
}
if (!MimeRec->Flag) // ignore if we have already returned it
{
if ((Ctype == HASATTACH && MimeRec->FileName) ||
(Ctype == HASPLAIN && MimeRec->ContentType == CT_TEXT_PLAIN) ||
(Ctype == HASHTML && MimeRec->ContentType == CT_TEXT_HTML))
{
MimeRec->Flag = TRUE; // mark as returned
return(MimeRec);
}
}
MimeRec = MimeRec->Next;
}
return(NULL); // not found
}
char *GetMessageAscii(MIMEREC *TextRec)
{
int i;
char *retstr;
if (!TextRec->BodyStart /* || !TextRec->BodyEnd */)
return(NULL);
retstr = TextRec->BodyStart;
switch(TextRec->Encoding)
{
case ENC_7BIT: // take bytes as is
case ENC_8BIT:
case ENC_BINARY:
case ENC_UNKNOWN:
break;
case ENC_QP: // decode quoted printable into binary.
i = QPToBinary(TextRec->BodyStart);
TextRec->BodyStart[i] = 0; // null terminate output
break;
case ENC_BASE64: // decode Base64 into binary
i = Base64ToBinary(TextRec->BodyStart);
TextRec->BodyStart[i] = 0; // null terminate
break;
default:
retstr = NULL;
break;
}
if (retstr && TextRec->Charset) // convert charset if necessary
{
CpToCp1252(TextRec->Charset, retstr);
}
return(retstr);
}
// convert an "encoded word" string to US_ASCII
char *UnEncodeWord(char *Str)
{
return(Str);
}
// convert unix time to ascii
// Proper date format is: "Date: Thu, 28 Nov 2019 13:04:06 -0500"
char *UnixTime2Ascii(time_t t)
{
static char str[80];
strftime(str, sizeof(str), "%a, %d %b %Y %H:%M:%S %z", localtime(&t));
return(str);
}
// Convert IP in unsigned to dotted quad string
char *Ip2Ascii(unsigned ip)
{
static char str[16];
sprintf(str, "%d.%d.%d.%d", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
(ip >> 8) & 0xFF, ip & 0xFF);
return(str);
}
// search for and open the attachment if found
FILE *OpenAttachment(char *DirName, int MsgNum, char *fname)
{
char TmpStr[512];
FILE *fp;
if (!fname || strlen(fname) > sizeof(TmpStr) - 64) return(NULL);
sprintf(TmpStr, "%s/%d-%s", DirName, MsgNum, fname);
fp = fopen(TmpStr, "rb");
return(fp);
}
void WriteCompactEml(JSONHEADERS *jHdr, MIMEREC *MimeRec, FILE *fp)
{
unsigned parts, ctype;
char *Boundary = ",,Yahoo/Eml/Converter:=?";
char *Unk = "Unknown";
char *ptr, *TextPtr;
MIMEREC *TextRec, *AttachRec;
//int OriginalLength;
// write headers
// Write FROM:
UnEscapeString(jHdr->SenderEmail);
fprintf(fp, "From: %s\r\n", (jHdr->SenderEmail) ? UnEncodeWord(jHdr->SenderEmail) : Unk);
// Write TO:
UnEscapeString(MimeRec->To);
fprintf(fp, "To: %s\r\n", MimeRec->To ? UnEncodeWord(MimeRec->To) : Unk);
// Write SUBJECT:
UnEscapeString(jHdr->Subject);
fprintf(fp, "Subject: %s\r\n", jHdr->Subject ? UnEncodeWord(jHdr->Subject) : Unk);
// Write DATE:
fprintf(fp, "Date: %s\r\n", UnixTime2Ascii((time_t) jHdr->PostDate));
// Write X-Originating-IP:
if (MimeRec->SenderIp)
fprintf(fp, "X-Originating-IP: %s\r\n", Ip2Ascii(MimeRec->SenderIp));
// Write X-Yahoo-Profile: (Poster User ID)
if (jHdr->YahooId)
fprintf(fp, "X-Yahoo-Profile: %s\r\n", UnEncodeWord(jHdr->YahooId));
// Write X-Msg-Id: -- message number
fprintf(fp, "X-Msg-Id: %d\r\n", jHdr->MsgNum);
// Write X-Topic-Id: -- message number of first topic
fprintf(fp, "X-Topic-Id: %d\r\n", jHdr->TopicId);
// Write X-Topic-Next: -- message number of next in thread
fprintf(fp, "X-Topic-Next: %d\r\n", jHdr->NextInTopic);
// Write X-Topic-Prev: -- Message Number of previous in thread
fprintf(fp, "X-Topic-Prev: %d\r\n", jHdr->PrevInTopic);
// Write MIME-Version: 1.0
fprintf(fp, "MIME-Version: 1.0\r\n");
parts = FindParts(MimeRec);
if (parts & HASATTACH)
{
// multipart/mixed because there are attachments
fprintf(fp, "Content-Type: multipart/mixed; boundary=%s\r\n", Boundary);
fprintf(fp, "\r\n"); // end of headers marker
fprintf(fp, "\r\n--%s\r\n", Boundary); // boundary marker
}
fprintf(fp, "Content-Type: text/%s; ", (parts & HASHTML) ? "html" : "plain");
fprintf(fp, "charset=Windows-1252;\r\n");
fprintf(fp, "Content-Transfer-Encoding: 8bit\r\n");
fprintf(fp, "\r\n"); // end of headers marker
ctype = parts & HASHTML;
if (!ctype) ctype = HASPLAIN;
TextPtr = NULL;
while ((TextRec = GetNextRec(MimeRec, ctype)) != NULL)
{
if (TextPtr) fprintf(fp, "\r\n=====-\r\n"); // Been here before
//OriginalLength = strlen(TextRec->BodyStart);
TextPtr = GetMessageAscii(TextRec);
//UnEscapeString(TextPtr);
RemoveYinv(TextPtr); // remove yahoo css
if (TextRec->ContentType == CT_TEXT_HTML)
{
Html2BBCode(TextPtr);
//PrintBBCode(fp, TextPtr);
PrintHtmlCode(fp, TextPtr);
// if html out, we reconvert it to html here
}
else
{
// output the text
if (TextPtr)
{
for (ptr = TextPtr; *ptr; ptr++)
{
fputc(*ptr, fp);
}
}
}
}
if (parts & HASATTACH)
{
FILE *fpAt;
while ((AttachRec = GetNextRec(MimeRec, HASATTACH)) != NULL)
{
fprintf(fp, "\r\n--%s\r\n", Boundary); // boundary marker
// print attachment boundary header stuff
fprintf(fp, "Content-Type: %s; name=\"%s\"\r\n",
GetContTypeStr(AttachRec->ContentType),
AttachRec->FileName);
fprintf(fp, "Content-Disposition: attachment; filename=\"%s\"\r\n",
AttachRec->FileName);
fprintf(fp, "Content-Transfer-Encoding: base64\r\n");
fprintf(fp, "\r\n"); // end of headers marker
// locate attachment file and base64 it here
fpAt = OpenAttachment(jHdr->DirName, jHdr->MsgNum, AttachRec->FileName);
if (fpAt)
{
Base64Print(fp, fpAt);
fclose(fpAt);
}
else
{
fprintf(fp, "[ Attachment could not be found ] \n");
}
}
fprintf(fp, "\r\n--%s--\r\n", Boundary); // final boundary marker
}
// Content-Type:
//Content-Type: multipart/alternative;
// boundary="----=_Part_223176_1800133257.1505487319309"
//Content-Type: text/plain; charset=utf-8; format=flowed
//Content-Transfer-Encoding: 8bit
//--------------26D1C33446561D3F6AF764BC
//Content-Type: text/html; charset=utf-8
//Content-Transfer-Encoding: 8bit
//Content-Type: text/plain; charset="ISO-8859-1"
//Content-Transfer-Encoding: quoted-printable
}
void WriteBBCode(JSONHEADERS *jHdr, MIMEREC *MimeRec, FILE *fp)
{
unsigned parts, ctype;
char *Unk = "Unknown";
char *ptr, *TextPtr;
MIMEREC *TextRec, *AttachRec;
int i;
// write headers
// Write FROM:
UnEscapeString(jHdr->SenderEmail);
fprintf(fp, "From: %s\r\n", (jHdr->SenderEmail) ? UnEncodeWord(jHdr->SenderEmail) : Unk);
// Write TO:
UnEscapeString(MimeRec->To);
fprintf(fp, "To: %s\r\n", MimeRec->To ? UnEncodeWord(MimeRec->To) : Unk);
// Write SUBJECT:
UnEscapeString(jHdr->Subject);
fprintf(fp, "Subject: %s\r\n", jHdr->Subject ? UnEncodeWord(jHdr->Subject) : Unk);
// Write DATE:
fprintf(fp, "Date: %s\r\n", UnixTime2Ascii((time_t) jHdr->PostDate));
// Write X-Originating-IP:
if (MimeRec->SenderIp)
fprintf(fp, "X-Originating-IP: %s\r\n", Ip2Ascii(MimeRec->SenderIp));
// Write X-Yahoo-Profile: (Poster User ID)
if (jHdr->YahooId)
fprintf(fp, "X-User-Id: %s\r\n", UnEncodeWord(jHdr->YahooId));
// Write X-Msg-Id: -- message number
fprintf(fp, "X-Msg-Id: %d\r\n", jHdr->MsgNum);
// Write X-Topic-Id: -- message number of first topic
fprintf(fp, "X-Topic-Id: %d\r\n", jHdr->TopicId);
// Write X-Topic-Next: -- message number of next in thread
fprintf(fp, "X-Topic-Next: %d\r\n", jHdr->NextInTopic);
// Write X-Topic-Prev: -- Message Number of previous in thread
fprintf(fp, "X-Topic-Prev: %d\r\n", jHdr->PrevInTopic);
// Write MIME-Version: 1.0
fprintf(fp, "X-Charset: UTF-8\r\n");
parts = FindParts(MimeRec);
if (parts & HASATTACH)
{
// Print attachment headers
i = 0;
while ((AttachRec = GetNextRec(MimeRec, HASATTACH)) != NULL)
{
fprintf(fp, "X-Attach: \"%s\" ID=%d\r\n", AttachRec->FileName, ++i);
}
CountAttach(MimeRec); // reset all flags
}
fprintf(fp, "\r\n"); // end of headers marker
// Now go through and print all HTML if its there else print all text email
ctype = parts & HASHTML;
if (!ctype) ctype = HASPLAIN;
TextPtr = NULL;
while ((TextRec = GetNextRec(MimeRec, ctype)) != NULL)
{
if (TextPtr) fprintf(fp, "\r\n======\r\n"); // Been here before
//OriginalLength = strlen(TextRec->BodyStart);
TextPtr = GetMessageAscii(TextRec);
//UnEscapeString(TextPtr);
RemoveYinv(TextPtr); // remove yahoo css
if (TextRec->ContentType == CT_TEXT_HTML) // converting HTML
{
Html2BBCode(TextPtr); // convert to tokenized BBCode
// BBCleanup(TextPtr); -- not working yet
PrintBBCode(fp, TextPtr);
}
else // text only - no conversion
{
// output the text
if (TextPtr)
{
for (ptr = TextPtr; *ptr; ptr++)
{
fputc(*ptr, fp);
}
}
}
}
// Now we process the attachments
if (parts & HASATTACH)
{
FILE *fpAt;
while ((AttachRec = GetNextRec(MimeRec, HASATTACH)) != NULL)
{
// This boundary marker is illegal in UTF-8 so it makes a good
// universal boundary marker.
fprintf(fp, "\r\n%c\r\n", 0xFF); // boundary marker
// In BB files, there is no header here, just raw Base64 code
// locate attachment file and base64 it here
fpAt = OpenAttachment(jHdr->DirName, jHdr->MsgNum, AttachRec->FileName);
if (fpAt)
{
Base64Print(fp, fpAt);
fclose(fpAt);
}
else
{
fprintf(fp, "[ Attachment could not be found ] \n");
}
}
}
}