forked from wheresjames/rulib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mime.cpp
1699 lines (1330 loc) · 40.4 KB
/
Mime.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************
// Copyright (c) 2000, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
//
*******************************************************************/
// Mime.cpp: implementation of the CMime class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMime::CMime()
{_STT();
m_header = NULL;
m_dwSize = 0;
m_pHead = NULL;
m_pTail = NULL;
m_dwAttachments = 0;
m_dwExtras = 0;
m_dwPriority = 3;
}
CMime::~CMime()
{_STT();
Destroy();
}
void CMime::Destroy()
{_STT();
m_header = NULL;
// Lose the data
m_dwSize = 0;
m_pTail = NULL;
while ( m_pHead != NULL )
{
LPMIMEBLOCK next = m_pHead->pNext;
// Lose block data
if ( m_pHead->pdata != NULL )
{ delete [] m_pHead->pdata;
m_pHead->pdata = NULL;
} // end if
// Lose the descriptor
delete m_pHead;
m_pHead = next;
} // end while
m_dwAttachments = 0;
m_dwExtras = 0;
}
LPMIMEBLOCK CMime::AddBlock(LPMIMEBLOCK node)
{_STT();
if ( node == NULL ) return NULL;
// Count an attachment
if ( ( node->f1 & MBF1_ATTACHMENT ) != 0 )
m_dwAttachments++;
// Is it an extra file?
else if ( *node->fname )
node->f1 |= MBF1_EXTRA, m_dwExtras++;
else if ( *node->ctype )
if ( strnicmp( node->ctype, "multipart/", strlen( "multipart/" ) ) != 0 &&
strnicmp( node->ctype, "text/", strlen( "text/" ) ) != 0 )
node->f1 |= MBF1_EXTRA, m_dwExtras++;
// Decode the data
DecodeBlock( node );
// Add this node to the list
node->pNext = NULL;
node->pPrev = m_pTail;
if ( m_pTail == NULL ) m_pHead = node;
else m_pTail->pNext = node;
m_pTail = node;
m_dwSize++;
return node;
}
BOOL CMime::RemoveBlock(LPMIMEBLOCK node)
{_STT();
if ( node == NULL ) return FALSE;
// Remove this node from the list
if ( node->pPrev != NULL ) node->pPrev->pNext = node->pNext;
if ( node->pNext != NULL ) node->pNext->pPrev = node->pPrev;
if ( m_pHead == node ) m_pHead = node->pNext;
if ( m_pTail == node ) m_pTail = node->pPrev;
// Strike one attachment
if ( ( node->f1 & MBF1_ATTACHMENT ) != 0 )
if ( m_dwAttachments ) m_dwAttachments--;
// Lose block data
if ( node->pdata != NULL )
{ delete [] node->pdata;
node->pdata = NULL;
} // end if
// Delete this node
delete node;
// Decrement the count
if ( m_dwSize ) m_dwSize--;
return TRUE;
}
BOOL CMime::Load(LPCTSTR pFile)
{_STT();
// Out with the old
Destroy();
CWinFile file;
// Open the file
if ( !file.OpenExisting( pFile, GENERIC_READ ) )
return FALSE;
// Get file size
DWORD size = file.Size();
if ( size == 0 ) return FALSE;
// Allocate memory
TMem< BYTE > buf;
if ( !buf.allocate( size + 1 ) ) return FALSE;
// Read in the data into ram
DWORD read;
if ( !file.Read( buf, size, &read ) || read != size )
return FALSE;
buf[ size ] = 0;
// Load the file
if ( !LoadFromMemory( buf, size ) )
return FALSE;
return TRUE;
}
BOOL CMime::LoadFromMemory(LPBYTE buf, DWORD size)
{_STT();
Destroy();
DWORD i = 0;
// Sanity check
if ( buf == NULL || size == 0 ) return FALSE;
// Read all MIME blocks (up to 100)
DWORD max = 100;
while( --max && i < size && ReadBlock( &buf[ i ], size - i, &i ) );
return TRUE;
}
BOOL CMime::ReadBlock(LPBYTE buf, DWORD size, LPDWORD bsize)
{_STT();
BOOL endvars = FALSE;
DWORD i = 0;
char token[ MIME_STRSIZE ];
// Allocate memory for block header
LPMIMEBLOCK node = new MIMEBLOCK;
if ( node == NULL ) return FALSE;
ZeroMimeBlock( node );
while ( !endvars && i < size )
{
// Forgive one CRLF
if ( i < size && buf[ i ] == 0x0d ) i++;
if ( i < size && buf[ i ] == 0x0a ) i++;
// Skip white space
while ( i < size && ( buf[ i ] <= ' ' || buf[ i ] > '~' ) &&
buf[ i ] != 0x0d && buf[ i ] != 0x0a )
i++;
// Are there any variables?
if ( buf[ i ] != 0x0d && buf[ i ] != 0x0a )
{
// Punt if end of data
if ( i >= size ) { delete node; return FALSE; }
// Copy token
DWORD x = 0;
while ( i < size &&
buf[ i ] != ':' &&
buf[ i ] > ' ' &&
buf[ i ] <= '~' &&
x < sizeof( token ) - 1 )
token[ x++ ] = buf[ i++ ];
token[ x ] = NULL;
// Did we get the separator?
if ( buf[ i++ ] == ':' )
{
// Skip white space
while ( ( buf[ i ] <= ' ' || buf[ i ] > '~' ) &&
buf[ i ] != 0x0d && buf[ i ] != 0x0a ) i++;
DWORD e = i, skip;
// Find the end
BOOL end = FALSE;
while ( !end && !endvars )
{
// Find the end
while ( buf[ e ] != 0x0d && buf[ e ] != 0x0a && e < size ) e++;
if( buf[ e ] != 0x0d && buf[ e ] != 0x0a ) return FALSE;
if ( buf[ ++e ] == 0x0a ) e++;
if ( buf[ e ] > ' ' || buf[ e ] == 0x0d || buf[ e ] == 0x0a )
end = TRUE;
// Check for end of variables
if ( buf[ e ] == 0x0d || buf[ e ] == 0x0a ) endvars = TRUE;
} // end while
// Remember where the end is
skip = e;
// +++
e--;
// Lose trailing white space
while ( e > i && ( buf[ e ] <= ' ' || buf[ e ] > '~' ) ) e--;
if ( e > i )
{
// Add this variable
node->var.AddVar( token, (LPVOID)&buf[ i ], e - i + 1 );
} // end if
// Encoding type
if ( !strcmpi( token, "Content-Type" ) )
{
// Save the content-type
DWORD c;
for ( c = 0; c < sizeof( node->ctype ) &&
buf[ i + c ] > ' ' &&
buf[ i + c ] != ';'; c++ )
node->ctype[ c ] = buf[ i + c ];
node->ctype[ c ] = 0;
if ( buf[ i + c ] == ';' ) c++;
// Skip to next var
while( ( i + c ) < size &&
( buf[ i + c ] <= ' ' || buf[ i + c ] > '~' ) ) c++;
if ( e > ( i + c ) )
{
// Is their a boundry string?
CCfgFile cfg;
cfg.SetTerminator( ';' );
cfg.LoadFromMem( &buf[ i + c ], e - ( i + c ) );
// Get file name if specified
cfg.GetValue( (LPCTSTR)NULL, "name", node->fname, sizeof( node->fname ) );
// Get boundry string
cfg.GetValue( (LPCTSTR)NULL, "boundary",
(LPSTR)&node->boundry, sizeof( node->boundry ) );
} // end if
} // end if
// Encoding type
if ( !strcmpi( token, "Content-Transfer-Encoding" ) )
{
if ( !strnicmp( (LPCTSTR)&buf[ i ], "base64", strlen( "base64" ) ) )
node->encode |= MBEN_BASE64;
else if ( !strnicmp( (LPCTSTR)&buf[ i ], "quoted-printable", strlen( "quoted-printable" ) ) )
node->encode |= MBEN_QP;
} // end if
// Attachment?
if ( !strcmpi( token, "Content-Disposition" ) )
{
if ( !strnicmp( (LPCTSTR)&buf[ i ], "attachment", strlen( "attachment" ) ) )
node->f1 |= MBF1_ATTACHMENT;
CCfgFile cfg;
cfg.LoadFromMem( &buf[ i ], e - i );
// Get filename
cfg.GetValue( (LPCTSTR)NULL, "filename", node->fname, sizeof( node->fname ) );
} // end if
// cid:
if ( !strcmpi( token, "Content-ID" ) )
{
DWORD s = i;
while( s < e && buf[ s ] != '<' ) s++;
// Copy the cid
if ( buf[ s ] == '<' )
{ DWORD x = 0;
s++;
while( s < e && buf[ s ] != '>' ) node->cid[ x++ ] = buf[ s++ ];
node->cid[ x ] = 0;
} // end if
} // end if
// Forge ahead
i = skip;
} // end if
// Skip to next line
else while ( i < size && buf[ i ] != 0x0d && buf[ i ] != 0x0a ) i++;
} // end if
else endvars = TRUE;
} // end while
// Skip next CRLF
if ( buf[ i ] == 0x0d ) i++;
if ( buf[ i ] == 0x0a ) i++;
// Save header if non attached multipart
if ( m_header != NULL &&
( node->f1 & MBF1_ATTACHMENT ) == 0 &&
!strnicmp( node->ctype, "multipart/", strlen( "multipart/" ) ) )
i = 0;
// Was this the header info?
if ( m_header == NULL ) m_header = node;
DWORD done = FALSE;
DWORD start = i;
DWORD end = i;
DWORD blen = strlen( m_header->boundry );
while( !done && i < size )
{
// This may be the end
end = i;
// Check for end tag ( some of this here for efficiency )
if ( buf[ i ] == '-' && ( i + 1 + blen ) < size &&
buf[ i + 1 ] == m_header->boundry[ 0 ] &&
!strnicmp( (LPCTSTR)&buf[ i + 1 ], m_header->boundry, blen ) )
{
// Forget this line
while ( end > start && buf[ end ] != '\r' && buf[ end ] != '\n' ) end--;
if ( bsize != NULL )
*bsize += i + 1 + strlen( m_header->boundry );
done = TRUE;
} // end if
if ( !done ) i++;
} // end while
// Attempt to allocate memory for block
node->dsize = end - start + 1;
node->pdata = new BYTE[ node->dsize + 1 ];
if ( node->pdata == NULL ) { delete node; return done; }
memcpy( node->pdata, &buf[ start ], node->dsize );
// Attempt to add the block to the list
if ( !AddBlock( node ) ) delete node;
return done;
}
BOOL CMime::SaveToFile(LPMIMEBLOCK pmb, LPCTSTR pFile)
{_STT();
// Sanity check
if ( pmb == NULL || pFile == NULL ) return FALSE;
if ( pmb->pdata == NULL || pmb->dsize == 0 ) return FALSE;
if( ( pmb->f1 & MBF1_DECODED ) == 0 )
{
// If encoding
if ( ( pmb->encode & MBEN_BASE64 ) != 0 )
return SaveBase64( pmb, pFile );
} // end if
// Open file
CWinFile f;
if ( !f.OpenNew( pFile ) ) return FALSE;
// Just write out if no encoding
f.Write( pmb->pdata, pmb->dsize );
f.Close();
return TRUE;
}
BOOL CMime::SaveBase64(LPMIMEBLOCK pmb, LPCTSTR pFile)
{_STT();
// Open file
CWinFile f;
if ( !f.OpenNew( pFile ) ) return FALSE;
DWORD i = 0;
BYTE buf[ 1024 ];
DWORD size = sizeof( buf ), decoded;
DWORD done = 0;
// Decode to file
while( !done && i < pmb->dsize )
{
DWORD block = 1024 < ( pmb->dsize - i ) ? 1024 : pmb->dsize - i;
if ( !CBase64::Decode( &( (LPCTSTR)pmb->pdata )[ i ], &block, buf, &decoded, &done ) )
done = TRUE;
// Write data to file
if ( decoded ) f.Write( buf, decoded );
// Increment buffer pointer
i += block;
} // end while
// Close the file
f.Close();
return TRUE;
}
BOOL CMime::SaveAttachments(LPCTSTR pDir)
{_STT();
char fname[ MIME_STRSIZE ];
LPMIMEBLOCK pmb = NULL;
while ( ( pmb = GetNext( pmb ) ) != NULL )
{
if ( ( pmb->f1 & MBF1_ATTACHMENT ) != 0 )
{
// Name the nameless
if ( !pmb->fname[ 0 ] )
{
char ext[ 256 ];
// Get content extension
if ( GetContentExtension( pmb->ctype, ext ) )
{ strcpy( pmb->fname, "Attachment." ); strcat( pmb->fname, ext ); }
// If message we'll assume outlook can handle it
else if ( !strnicmp( pmb->ctype, "message/", sizeof( "message/" ) ) )
strcpy( pmb->fname, "Attachment.dat" );
} // end if
if ( pmb->fname[ 0 ] )
{
if ( pDir != NULL ) CWinFile::BuildPath( fname, pDir, pmb->fname );
else strcpy( fname, pmb->fname );
// Ensure unique file name
CWinFile::GetUniqueFileName( fname, fname );
// Ensure directory exists
char path[ MIME_STRSIZE ];
CWinFile::GetPathFromFileName( fname, path );
CWinFile::CreateDirectory( path );
// Copy new file name
CWinFile::GetFileNameFromPath( fname, pmb->fname );
// Save to disk
SaveToFile( pmb, fname );
} // end if
} // end if
} // end while
return TRUE;
}
// files that are not attachments
BOOL CMime::SaveExtras(LPCTSTR pDir)
{_STT();
char fname[ MIME_STRSIZE ];
LPMIMEBLOCK pmb = NULL;
while ( ( pmb = GetNext( pmb ) ) != NULL )
{
if ( ( pmb->f1 & MBF1_ATTACHMENT ) == 0 )
{
// Name the nameless
if ( !pmb->fname[ 0 ] )
{
char ext[ 256 ];
// Get content extension
if ( GetContentExtension( pmb->ctype, ext ) )
{ strcpy( pmb->fname, "Extra." ); strcat( pmb->fname, ext ); }
// If message we'll assume outlook can handle it
else if ( !strnicmp( pmb->ctype, "message/", strlen( "message/" ) ) )
strcpy( pmb->fname, "Extra.dat" );
} // end if
if ( pmb->fname[ 0 ] )
{
if ( pDir != NULL ) CWinFile::BuildPath( fname, pDir, pmb->fname );
else strcpy( fname, pmb->fname );
// Ensure unique file name
CWinFile::GetUniqueFileName( fname, fname );
// Ensure directory exists
char path[ MIME_STRSIZE ];
CWinFile::GetPathFromFileName( fname, path );
CWinFile::CreateDirectory( path );
// Copy new file name
CWinFile::GetFileNameFromPath( fname, pmb->fname );
// Save to disk
SaveToFile( pmb, fname );
} // end if
} // end if
} // end while
return TRUE;
}
BOOL CMime::DecodeQP(LPCTSTR pSrc, DWORD dwSrc, LPSTR pDst, LPDWORD pdwDst)
{_STT();
// Sanity checks
if ( pSrc == NULL || dwSrc == 0 ) return FALSE;
if ( pDst == NULL || pdwDst == NULL || *pdwDst == 0 ) return FALSE;
DWORD x = 0, y = 0;
while ( x < dwSrc )
{
// Save if normal character
if ( pSrc[ x ] != '=' )
{
if ( y < *pdwDst ) pDst[ y++ ] = pSrc[ x++ ];
else x = dwSrc;
} // end if
else
{
// Skip '='
x++;
// Check for hex encoding
if ( ( pSrc[ x ] >= '0' && pSrc[ x ] <= '9' ) ||
( pSrc[ x ] >= 'A' && pSrc[ x ] <= 'F' ) ||
( pSrc[ x ] >= 'a' && pSrc[ x ] <= 'f' ) )
{
pDst[ y++ ] = (BYTE)ahtoud( &pSrc[ x ], 2 );
x += 2;
} // end if
else while ( x < dwSrc &&
( pSrc[ x ] <= ' ' || pSrc[ x ] > '~' ) ) x++;
} // end else
} // end while
// NULL terminate
pDst[ y ] = 0;
// How many chars did we copy?
*pdwDst = y;
return TRUE;
}
DWORD CMime::ahtoud( LPCTSTR pBuffer, BYTE ucBytes )
{_STT();
DWORD num, i;
num = 0;
// For Each ASCII Digit
for ( i = 0; i < ucBytes; i++ )
{
// Convert ASCII Digit Between 0 And 9
if ( pBuffer[ i ] >= '0' && pBuffer[ i ] <= '9' )
num = ( num << 4 ) + ( pBuffer[ i ] - '0' );
// Convert ASCII Digit Between A And F
else if ( pBuffer[ i ] >= 'A' && pBuffer[ i ] <= 'F' )
num = ( num << 4 ) + ( pBuffer[ i ] - 'A' ) + 10;
// Convert ASCII Digit Between a And f
else if ( pBuffer[ i ] >= 'a' && pBuffer[ i ] <= 'f' )
num = ( num << 4 ) + ( pBuffer[ i ] - 'a' ) + 10;
} // end for
return num;
} // end ahtou()
BOOL CMime::DecodeBlock(LPMIMEBLOCK pmb)
{_STT();
// Is it already decoded?
if ( ( pmb->f1 & MBF1_DECODED ) != 0 ) return TRUE;
// If base64 encoded
if ( ( pmb->encode & MBEN_BASE64 ) != 0 )
{
DWORD done = FALSE;
DWORD decoded = 0;
DWORD size = pmb->dsize;
TMem< BYTE > buf;
if ( !buf.allocate( size + 1 ) ) return FALSE;
// Do the decode
if ( CBase64::Decode( (LPCTSTR)pmb->pdata, &size,
buf, &decoded, &done ) && decoded > 0 )
{
// Allocate buffer of correct size
LPBYTE cbuf = new BYTE[ decoded + 1 ];
if ( cbuf == NULL ) return FALSE;
// Copy data
memcpy( cbuf, buf, decoded );
// Save decoded data
delete [] pmb->pdata;
pmb->pdata = cbuf;
pmb->dsize = decoded;
pmb->f1 |= MBF1_DECODED;
} // end if
} // end if
// If quoted-printable encoded
else if ( ( pmb->encode & MBEN_QP ) != 0 )
{
DWORD size = pmb->dsize;
TMem< char > buf;
if ( !buf.allocate( size + 1 ) ) return FALSE;
if ( DecodeQP( (LPCTSTR)pmb->pdata, pmb->dsize, buf, &size ) )
{
// Allocate buffer of correct size
LPBYTE cbuf = new BYTE[ size + 1 ];
if ( cbuf == NULL ) return FALSE;
// Copy data
memcpy( cbuf, buf, size );
// Save decoded data
delete [] pmb->pdata;
pmb->pdata = cbuf;
pmb->dsize = size;
pmb->f1 |= MBF1_DECODED;
} // end if
} // end else if
return TRUE;
}
BOOL CMime::ReplaceCid(LPCTSTR pCid, LPCTSTR pReplace, LPCTSTR buf, DWORD size, LPSTR dest)
{_STT();
DWORD i = 0, x = 0;
DWORD cidlen = strlen( pCid );
TMem< char > cid;
if ( !cid.allocate( cidlen + 32 ) ) return FALSE;
// Fix up cid
strcpy( cid, "cid:" );
strcat( cid, pCid );
cidlen = strlen( cid );
// Simple replace routine
while( i < size )
{
if ( strnicmp( cid, &buf[ i ], cidlen ) ) dest[ x++ ] = buf[ i++ ];
else
{ strcpy( &dest[ x ], pReplace );
x += strlen( pReplace );
i += cidlen;
} // end else
} // end while
dest[ x ] = 0;
return TRUE;
}
BOOL CMime::ReplaceCid(LPMIMEBLOCK pmb, LPCTSTR pPath )
{_STT();
if ( pmb == NULL || pmb->pdata == NULL ) return FALSE;
LPMIMEBLOCK extra = NULL;
while ( ( extra = GetNext( extra ) ) != NULL )
{
if ( /*( extra->f1 & MBF1_ATTACHMENT ) == 0 && */
extra->fname[ 0 ] != 0 && extra->cid[ 0 ] != 0 )
{
char *buf = new char[ pmb->dsize * 2 ];
if ( buf == NULL ) return FALSE;
char fname[ CWF_STRSIZE ];
if ( pPath != NULL ) CWinFile::BuildPath( fname, pPath, extra->fname );
else strcpy( fname, extra->fname );
ReplaceCid( extra->cid, fname, (LPCTSTR)pmb->pdata, pmb->dsize, buf );
delete [] pmb->pdata;
pmb->pdata = buf;
pmb->dsize = strlen( buf );
} // end if
} // end while
return TRUE;
}
DWORD CMime::m_dwCounter = 0;
BOOL CMime::GenBoundry(LPSTR pBoundry)
{_STT();
CMD5 md5;
if ( pBoundry == NULL ) return FALSE;
SYSTEMTIME st;
GetLocalTime( &st );
// Get random hash
md5.Random( "Mime (October 4, 2002)" );
wsprintf( pBoundry, "----_=_NextPart_%03lX_%04lX_%08lX.%08lX.%08lX%08lX",
m_dwCounter++,
( ( st.wYear % 16 ) * 0x1000 ) + ( st.wMonth * 0x100 ) + st.wDay,
md5.m_hash[ 0 ], md5.m_hash[ 1 ], md5.m_hash[ 2 ], md5.m_hash[ 3 ] );
return TRUE;
}
static char s_days[][ 8 ] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static char s_months[][ 8 ] = { "Inv", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
LPMIMEBLOCK CMime::Create( LPCTSTR pTo, LPCTSTR pFrom, LPCTSTR pSubject )
{_STT();
// Lose old mime message
Destroy();
// Allocate memory for header
LPMIMEBLOCK node = new MIMEBLOCK;
if ( node == NULL ) return NULL;
ZeroMimeBlock( node );
// Header is always first in the list
m_header = node;
// Fill in header information
strcpy_sz( m_header->ctype, "multipart/mixed" );
// Get boundry type
GenBoundry( m_header->boundry );
// Add user information
// Copy from email list
if ( pFrom != NULL && *pFrom != 0 )
{ TMem< char > buf;
if ( buf.allocate( ( strlen( pFrom ) * 2 ) + 1 ) )
{ VerifyEmailList( buf, pFrom, ",\r\n\t" );
m_header->var.AddVar( "From", buf );
} // end if
} // end if
// Copy to email list
if ( pTo != NULL && *pTo != 0 )
{ TMem< char > buf;
if ( buf.allocate( ( strlen( pTo ) * 2 ) + 1 ) )
{ VerifyEmailList( buf, pTo, ",\r\n\t" );
m_header->var.AddVar( "To", buf );
} // end if
} // end if
// Copy subject
if ( pSubject != NULL ) m_header->var.AddVar( "Subject", pSubject );
// add time/date
SYSTEMTIME st;
GetLocalTime( &st );
// Get time-zone information
TIME_ZONE_INFORMATION tzi;
ZeroMemory( &tzi, sizeof( tzi ) );
GetTimeZoneInformation( &tzi );
// Change to correct format
long hour = tzi.Bias / 60;
if ( tzi.Bias < 0 ) tzi.Bias = -tzi.Bias;
long min = tzi.Bias % 60;
if ( hour <= 12 ) hour = -hour;
else if ( hour > 12 ) hour = 24 - hour;
char tzone[ MIME_STRSIZE ] = { 0 };
if ( hour < 0 ) wsprintf( tzone, "%03li%02lu", hour, min );
else wsprintf( tzone, "+%02li%02lu", hour, min );
char date[ MIME_STRSIZE ] = { 0 };
wsprintf( date, "%s, %lu %s %lu %02lu:%02lu:%02lu %s",
s_days[ st.wDayOfWeek ],
st.wDay,
s_months[ st.wMonth ],
st.wYear,
st.wHour, st.wMinute, st.wSecond,
tzone );
char tzname[ MIME_STRSIZE ];
wcstombs( tzname, tzi.StandardName, sizeof( tzname ) - 1 );
m_header->var.AddVar( "Date", date );
// end add time/date
// Mime version
m_header->var.AddVar( "MIME-Version", "1.0" );
char type[ MIME_STRSIZE ];
wsprintf( type, "multipart/mixed;\r\n\tboundary=\"%s\"", m_header->boundry );
m_header->var.AddVar( "Content-Type", type );
// Add X- MIME Data
m_header->var.AddVar( "X-Time-Zone", tzname );
char pri[ MIME_STRSIZE ];
wsprintf( pri, "%lu", m_dwPriority );
m_header->var.AddVar( "X-Priority", pri );
m_header->var.AddVar( "X-MIME-Encoder", "WheresJames MIME Encoder (www.wheresjames.com) " MIMEENCODERVERSTR );
// Default message
char *msg = "This message is in MIME format. Since your mail\r\n"
"reader does not understand this format, some or\r\n"
"all of this message may not be legible.";
// Add message to header
m_header->dsize = strlen( msg );
m_header->pdata = new char[ m_header->dsize + 1 ];
if ( m_header->pdata != NULL )
{ memcpy( m_header->pdata, msg, m_header->dsize );
( (LPBYTE)m_header->pdata )[ m_header->dsize ] = 0;
} // end if
else m_header->dsize = 0;
// Add the node to the list
return AddBlock( node );
}
BOOL CMime::Save(LPCTSTR pFile)
{_STT();
CWinFile f;
// Attempt to open the new file
if ( !f.OpenNew( pFile ) ) return FALSE;
// How much data
DWORD size = SaveToMem( NULL, 0 );
if ( size == 0 ) return TRUE;
// Allocate memory
TMem< BYTE > buf;
if ( !buf.allocate( size + 1 ) ) return FALSE;
// Write MIME to buffer
DWORD bytes = SaveToMem( buf, size );
// Write out the data to disk file
f.Write( buf, bytes );
return TRUE;
}
DWORD CMime::SaveToMem(LPBYTE buf, DWORD size)
{_STT();
// Anything to save?
if ( Size() == 0 ) return 0;
if ( m_header == NULL ) return 0;
DWORD i = 0;
LPMIMEBLOCK pmb = NULL;
// Do they just want to know the size?
if ( buf == NULL ) size = MAXDWORD;
// For each block
while ( i < size && ( pmb = GetNext( pmb ) ) != NULL )
{
RULIB_TRY
{
if ( size > i )
{
// Get size
if ( buf == NULL ) i += SaveBlock( pmb, 0, 0 );
// Save block information to buffer
else if ( i < size ) i += SaveBlock( pmb, &buf[ i ], size - i );
} // end if
} // end try
// Ignore bad blocks
RULIB_CATCH_ALL { ASSERT( 0 ); }
// Will there be another block?
// If so write out the boundry
// if ( pmb->pNext != NULL && m_header != NULL )
{
if ( buf != NULL ) strcpy( (char*)&buf[ i ], "\r\n\r\n--" );
i += 6;
if ( buf != NULL ) strcpy( (char*)&buf[ i ], m_header->boundry );
i += strlen( m_header->boundry );
// Be like Outlook
if ( pmb->pNext != NULL )
{ if ( buf != NULL ) strcpy( (char*)&buf[ i ], "\r\n" );
i += 2;
} // end if
else
{ if ( buf != NULL ) strcpy( (char*)&buf[ i ], "--\r\n" );
i += 4;
} // end else
} // end if
} // end while
// NULL terminate
if ( buf != NULL )
{ if ( i < size ) buf[ i++ ] = 0; }
else i++;
return i;