summaryrefslogtreecommitdiff
path: root/plugins/Msg_Export/src/utils.cpp
blob: 08d7323b3623f0f18fd61cf04ee4c4275b5cc8dd (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705

//This file is part of Msg_Export a Miranda IM plugin
//Copyright (C)2002 Kennet Nielsen ( http://sourceforge.net/projects/msg-export/ )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program 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 General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.



#include <stdio.h>

#include "Glob.h"
#include "Utils.h"
#include "FileViewer.h"

#include <m_protosvc.h>
#include <m_icq.h>

// Default error string used upon errors 
const _TCHAR *pszNickError   = LPGENT("No_Nick");
const _TCHAR *pszGroupError  = LPGENT("No_Group");
const _TCHAR *pszDbPathError = _T(".");

// Replacement for chareteres not alowed in file names.
const _TCHAR cBadCharReplace = _T('_');

// sTimeFormat 
tstring sTimeFormat;

// path from options dialog
tstring sExportDir;

// The default filename. Used if no other file name is specifyed in DB.
tstring sDefaultFile;

// path used then %dbpath% is used in export file path
tstring sDBPath = pszDbPathError;

// path to miranda exe file used when to avoid relative paths
tstring sMirandaPath = pszDbPathError;

// Used to store the width of the user name for a file.
// if a file contains events from many users the one user name
// may be shorter. so to make all event have the same first
// column width this map contains the largest user name
map<tstring , string::size_type , less<tstring> > clFileTo1ColWidth;

// default line width
int nMaxLineWidth = 80;

const _TCHAR *pszReplaceList[] = 
{ 
	 _T("%FirstName%")  ,
	 _T("%LastName%")  ,
	 _T("%e-mail%") ,
	 _T("%Nick%") ,
	 _T("%City%") ,
	 _T("%State%") ,
	 _T("%Phone%") ,
	 _T("%Homepage%") ,
	 _T("%About%")
};
const char *pszReplaceListA[] = 
{ 
	"FirstName" ,
	"LastName" ,
	"e-mail" ,
	"Nick" ,
	"City" ,
	"State" ,
	"Phone" ,
	"Homepage" ,
	"About"
};

// Alowes this plugin to replace the history function of miranda !!
bool bReplaceHistory = false;

// This enum define the actions which MsgExport must take when a File is renamed
ENDialogAction enRenameAction = eDAPromptUser;

// This enum define the actions which MsgExport must take when a user is delete
ENDialogAction enDeleteAction = eDAPromptUser;

// If true MsgExport will use << and >> insted of the nick in the exported format
bool bUseLessAndGreaterInExport = false;

bool bAppendNewLine = true;
bool bUseUtf8InNewFiles = true;

const char szUtf8ByteOrderHeader[] = "\xEF\xBB\xBF";
bool bIsUtf8Header( BYTE * pucByteOrder )
{ 
	return memcmp( pucByteOrder , szUtf8ByteOrderHeader , 3 ) == 0;
}

/////////////////////////////////////////////////////////////////////
// Member Function : ShowDebugInfo
// Type            : Global
// Parameters      : None
// Returns         : void
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 021228 , 28 December 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void ShowDebugInfo()
{
	tstring sDebug = _T("Debug information\r\nsDBPath :");
	sDebug += sDBPath;
	sDebug += _T("\r\nsMirandaPath :");
	sDebug += sMirandaPath;
	sDebug += _T("\r\nsDefaultFile :");
	sDebug += sDefaultFile;

	sDebug += _T("\r\nGetFilePathFromUser( NULL ) :");
	sDebug += GetFilePathFromUser( NULL );

	MessageBox( NULL , sDebug.c_str() ,MSG_BOX_TITEL,MB_OK );
}


/////////////////////////////////////////////////////////////////////
// Member Function : nGetFormatCount
// Type            : Global
// Parameters      : pszToCheck - ?
// Returns         : int
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 030107 , 07 January 2003
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

int nGetFormatCount( const _TCHAR * pszToCheck )
{
	if( !pszToCheck || pszToCheck[0] == 0 )
		return 0;

	int nCount = 0;
	for( ; pszToCheck[1] != 0 ; pszToCheck++)
	{
		if( pszToCheck[0] == '%' && pszToCheck[1] != '%' )
			nCount++;
	}
	return nCount;
}

/////////////////////////////////////////////////////////////////////
// Member Function : CheckedTranslate
// Type            : Global
// Parameters      : szEng        - ?
//                   nFormatCount - ?
// Returns         : _TCHAR *
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 030107 , 07 January 2003
// Developer       : KN   
/////////////////////////////////////////////////////////////////////
/*
_TCHAR * CheckedTranslate( const _TCHAR *szEng , int nFormatCount )//= -1 
{
	_TCHAR * szRet = TranslateTS( szEng );
	if( szEng == szRet )
		return (_TCHAR*)szEng;

	if( nFormatCount == -1 )
		nFormatCount = nGetFormatCount( szEng );

	if( nFormatCount != nGetFormatCount( szRet ) )
	{
		tstring sError = _T("The language pack you are using has an error in the transalation of\r\n");
      sError += szEng;
		  sError += _T("\r\n---------------      It was translated to      ---------------\r\n");
	   sError += szRet;
		MessageBox( NULL , sError.c_str() ,MSG_BOX_TITEL ,MB_OK );
		return (_TCHAR*)szEng;
	}
	return szRet;
}
*/
/////////////////////////////////////////////////////////////////////
// Member Function : sGetErrorString
// Type            : Global
// Parameters      : dwError - ?
// Returns         : string
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 021012 , 12 October 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

tstring sGetErrorString(DWORD dwError)
{
   LPVOID lpMsgBuf;
   FormatMessage( 
       FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
       NULL,
       dwError,
       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
       (LPTSTR) &lpMsgBuf,
       0,
       NULL );
   // Process any inserts in lpMsgBuf.
   // ...
   // Display the string.
   tstring ret = (LPCTSTR)lpMsgBuf;
	ReplaceAll( ret , _T("\r") , _T(" "));
	ReplaceAll( ret , _T("\n") , _T(" "));
	ReplaceAll( ret , _T("  ") , _T(" "));

   // Free the buffer.
   LocalFree( lpMsgBuf );
   return ret;
}

tstring sGetErrorString()
{ 
   return sGetErrorString(GetLastError());
}

void DisplayLastError(const _TCHAR * pszError)
{
	tstring sError = pszError;
	DWORD error = GetLastError();

	_TCHAR szTemp[50];
	_sntprintf( szTemp , sizeof( szTemp ) , LPGENT("\r\nErrorCode : %d\r\n") , error );
	sError += szTemp;
	sError += sGetErrorString(error);
	MessageBox( NULL , sError.c_str() ,MSG_BOX_TITEL ,MB_OK );
}


/////////////////////////////////////////////////////////////////////
// Member Function : NickFromHandle
// Type            : Global
// Parameters      : hContact - ?
// Returns         : _TCHAR*
// Description     : Reads a Nick from the database and returns a 
//                   pointer to this.
// References      : -
// Remarks         : -
// Created         : 020422 , 22 April 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

const _TCHAR* NickFromHandle(HANDLE hContact)
{
	const _TCHAR * psz = (const _TCHAR *)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR );
	if( psz )
		return psz;
	return pszNickError;
}

/////////////////////////////////////////////////////////////////////
// Member Function : _DBGetString
// Type            : Global
// Parameters      : hContact  - ?
//                   szModule  - ?
//                   szSetting - ?
//                   pszError  - ?
// Returns         : string
// Description     : Reads a string from the database 
//                   Just like those in database.h
// References      : -
// Remarks         : -
// Created         : 020422 , 22 April 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

tstring _DBGetStringW(HANDLE hContact,const char *szModule,const char *szSetting , const _TCHAR * pszError )
{
	tstring ret;
	DBVARIANT dbv = {0};
	//DBGetContactSetting
	if( ! DBGetContactSettingWString( hContact , szModule , szSetting , &dbv ) )
	{
		if( dbv.type != DBVT_WCHAR)
		{
			MessageBox(NULL, LPGENT("DB: Attempt to get wrong type of value, string"), MSG_BOX_TITEL,MB_OK);
			ret = pszError;
		}
		else
		{
			ret = (_TCHAR*)dbv.pszVal;
		}
	}
	else
		ret = pszError;
	DBFreeVariant(&dbv);
	return ret;
}

string _DBGetStringA(HANDLE hContact,const char *szModule,const char *szSetting , const char * pszError )
{
	string ret;
	DBVARIANT dbv = {0};
	if( ! DBGetContactSetting( hContact , szModule , szSetting , &dbv ) )
	{
		if( dbv.type != DBVT_ASCIIZ)
		{
			MessageBox(NULL, LPGENT("DB: Attempt to get wrong type of value, string"), MSG_BOX_TITEL,MB_OK);
			ret = pszError;
		}
		else
		{
			ret = dbv.pszVal;
		}
	}
	else
		ret = pszError;
	DBFreeVariant(&dbv);
	return ret;
}

/////////////////////////////////////////////////////////////////////
// Member Function : ReplaceAll
// Type            : Global
// Parameters      : sSrc       - string to replace in
//                   pszReplace - what to replace
//                   sNew       - the string to insert insted of pszReplace
// Returns         : void
// Description     : will replace all acurances of a string with another string
//                   used to replace %user% , and other user 
// References      : -
// Remarks         : -
// Created         : 020422 , 22 April 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void ReplaceAll( tstring &sSrc , const _TCHAR * pszReplace , const tstring &sNew)
{
	string::size_type nCur = 0;
	while( (nCur = sSrc.find(pszReplace,nCur)) != sSrc.npos )
	{
		sSrc.replace( nCur , _tcslen( pszReplace ) , sNew );
		nCur += sNew.size();
	}
}

void ReplaceAll( tstring &sSrc , const _TCHAR * pszReplace , const _TCHAR * pszNew)
{
	tstring sNew = pszNew;
	ReplaceAll( sSrc , pszReplace , sNew );
}


/////////////////////////////////////////////////////////////////////
// Member Function : bCreatePathToFile
// Type            : Global
// Parameters      : sFilePath - File name to create path to ( file name may be empty ( i.e. c:\Folder\ )
// Returns         : Returns true if the path is created or already exists 
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 020525 , 25 May 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

bool bCreatePathToFile( tstring sFilePath )
{
	string::size_type nPos = sFilePath.rfind( '\\' );
	if(  nPos != string::npos )
	{
		if( nPos + 1 < sFilePath.size() )
			sFilePath.erase( nPos + 1);
	}
	else
	{
		// cant find \ 
		return false;
	}
	
	// create directory
	if( ! CreateDirectory( sFilePath.c_str() , NULL ) )
	{
		DWORD dwE = GetLastError();
		if( dwE == 183 ) // Cannot create a file when that file already exists. 
			return true;
		if( ! bCreatePathToFile( sFilePath.substr( 0 , nPos ) ) )
			return false;

		// try again 
		if( ! CreateDirectory( sFilePath.c_str() , NULL ) )
		{
			return false;
		}
	}
	return true;
}

/////////////////////////////////////////////////////////////////////
// Member Function : bWriteToFile
// Type            : Global
// Parameters      : hFile  - ?
//                   pszSrc - in UTF8 or ANSII
//                   nLen   - ?
// Returns         : Returns true if all the data was written to the file
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 020629 , 29 June 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

bool bWriteToFile( HANDLE hFile , const char * pszSrc , int nLen = -1 )
{
	if( nLen < 0 )
		nLen = (int)strlen( pszSrc );
	DWORD dwBytesWritten;
	return WriteFile( hFile , pszSrc , nLen , &dwBytesWritten , NULL ) && (dwBytesWritten == (DWORD)nLen);
}


/////////////////////////////////////////////////////////////////////
// Member Function : bWriteTextToFile
// Type            : Global
// Parameters      : hFile     - ?
//                   pszSrc    - ?
//                   bUtf8File - ?
// Returns         : Returns true if 
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 060130 , 30 januar 2006
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

bool bWriteTextToFile( HANDLE hFile , const _TCHAR * pszSrc , bool bUtf8File ,int nLen = -1 )
{
	if( nLen < 0 )
		nLen = (int)_tcslen( pszSrc );
	if( ! bUtf8File )
	{
// We need to downgrade text to ansi
		char * pszAstr = new char[nLen];
		int nAnsiLen = WideCharToMultiByte(CP_ACP, 0, pszSrc, nLen, pszAstr , nLen , NULL , NULL );
		bool bRet = bWriteToFile( hFile , pszAstr , nAnsiLen );
		delete [] pszAstr;
		return bRet;
	}
	char * pszUtf8 = new char[nLen*3];// UCS-2 can't generate more then this.
	int nUtf8Len = WideCharToMultiByte(CP_UTF8, 0, pszSrc, nLen, pszUtf8 , nLen * 3 , NULL , NULL );
	bool bRet = bWriteToFile( hFile , pszUtf8 , nUtf8Len );

	return bRet;
}


bool bWriteTextToFile( HANDLE hFile , const char * pszSrc , bool bUtf8File ,int nLen = -1 )
{
	if( nLen == -1 )
		nLen = (int)strlen( pszSrc );
	wchar_t * pszWstr = new wchar_t[nLen];
	bool bRet = false;
	if( MultiByteToWideChar(CP_ACP, 0, pszSrc, nLen, pszWstr, nLen ) == nLen )
	{
		bRet = bWriteTextToFile( hFile , pszWstr , bUtf8File , nLen );
	}
	return bRet;
}

/////////////////////////////////////////////////////////////////////
// Member Function : bWriteNewLine
// Type            : Global
// Parameters      : hFile   - ?
//                   nIndent - ?
// Returns         : Returns true if all the data was written to the file
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 020629 , 29 June 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

const char szNewLineIndent[] = "\r\n                                                                                                   ";
bool bWriteNewLine( HANDLE hFile , DWORD dwIndent )
{
	if( dwIndent > sizeof( szNewLineIndent ) - 2 )
		dwIndent = sizeof( szNewLineIndent ) - 2;
	return bWriteToFile( hFile , szNewLineIndent , dwIndent + 2 );
}

/////////////////////////////////////////////////////////////////////
// Member Function : bWriteHexToFile
// Type            : Global
// Parameters      : hFile - ?
//                         - ?
//                   nSize - ?
// Returns         : void
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 021203 , 03 December 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

bool bWriteHexToFile( HANDLE hFile , void * pData, int nSize )
{
	char cBuf[10];
	BYTE * p = (BYTE*)pData;
	for( int n = 0 ; n < nSize ; n++ )
	{
		sprintf( cBuf , "%.2X " , p[n] );
		if( ! bWriteToFile( hFile , cBuf , 3 ) )
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////
// Member Function : bReadMirandaDirAndPath
// Type            : Global
// Parameters      : None
// Returns         : void
// Description     : Used to set the internal path.
//                   Handles the reading from the mirandaboot.ini to get the %dbpath%
// References      : -
// Remarks         : -
// Created         : 020422 , 22 April 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////


bool bReadMirandaDirAndPath()
{
	_TCHAR szDBPath[MAX_PATH];
	char tmp[MAX_PATH];
	TCHAR *tmp2;
	_tcscpy( szDBPath , pszDbPathError );
	CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"miranda32.exe", (LPARAM)tmp);
	tmp2 = mir_utf8decodeT(tmp);
	sMirandaPath = tmp2;
	sMirandaPath.erase(sMirandaPath.find_last_of(_T("\\")));
	CallService(MS_DB_GETPROFILEPATHT, (WPARAM)MAX_PATH - 1, (LPARAM)szDBPath);
	sDBPath = szDBPath;
	CallService(MS_DB_GETPROFILENAMET, (WPARAM)MAX_PATH - 1, (LPARAM)szDBPath);
	sDBPath.append(_T("\\")).append(szDBPath);
	sDBPath.erase(sDBPath.size()-4);
	return true;
}

/////////////////////////////////////////////////////////////////////
// Member Function : ReplaceDBPath
// Type            : Global
// Parameters      : sRet - ?
// Returns         : void
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 021020 , 20 October 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void ReplaceDBPath( tstring &sRet )
{
	ReplaceAll( sRet , _T("%dbpath%") , sDBPath );
	// Try to firure out if it is a relative path ( ..\..\MsgExport\ )
	if( sRet.size() <= 2 || ! ( sRet[1] == ':' ||
				 ( sRet[0] == '\\' && sRet[1] == '\\' ) )
		)
	{
		// Relative path
		// we will prepend the mirande exe path to avoid problems 
		// if the current directory changes ( User receives a file )
		sRet = sMirandaPath + sRet;
	}
}


/////////////////////////////////////////////////////////////////////
// Member Function : GetFilePathFromUser
// Type            : Global
// Parameters      : hContact - Handle to user
// Returns         : string contaning the compleate file name and path
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 020422 , 22 April 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

tstring GetFilePathFromUser( HANDLE hContact )
{
	tstring sFilePath = sExportDir + _DBGetString( hContact , MODULE , "FileName" , sDefaultFile.c_str() );

	bool bNickUsed = sFilePath.find( _T("%nick%") ) != string::npos;

	ReplaceDefines( hContact , sFilePath );

	tstring sNoDBPath = sFilePath;

	ReplaceTimeVariables( sFilePath );
	ReplaceDBPath( sFilePath );

	 // Previous file name check to see if it has changed !!
	tstring sPrevFileName = _DBGetString( hContact , MODULE , "PrevFileName" , _T("") );
	if( sNoDBPath != sPrevFileName )
	{
		if( ! sPrevFileName.empty() )
		{
			ReplaceDBPath( sPrevFileName );

			// Here we will try to avoide the (Unknown Contact) in cases where the protocol for 
			// this user has been removed.
			if( bNickUsed && ( _tcsstr( NickFromHandle( hContact ),LPGENT("(Unknown Contact)")) != 0) )
			{
				// Then the filename must have changed from a correct path to one including the (Unknown Contact)
				return sPrevFileName;
			}

			// file name has changed

			if( enRenameAction != eDANothing )
			{

				// we can not use FILE_SHARE_DELETE because this is not supported by 
				// win 98 / ME 
				HANDLE hPrevFile = CreateFile( sPrevFileName.c_str() , 
					GENERIC_READ , 
					FILE_SHARE_READ | FILE_SHARE_WRITE ,
					NULL,
					OPEN_EXISTING ,
					FILE_ATTRIBUTE_NORMAL , 
					NULL );

				if( hPrevFile != INVALID_HANDLE_VALUE )
				{
					CloseHandle( hPrevFile ); 
					_TCHAR szTemp[500];
					// There is a previous file we can move 
					// ask user ?
					bool bTryRename;

					if( enRenameAction != eDAAutomatic )
					{
						tstring sRemoteUser = NickFromHandle(hContact);
						_sntprintf( szTemp , sizeof( szTemp ) , 
							LPGENT("File name for the user \"%s\" has changed !\n\nfrom:\t%s\nto:\t%s\n\nDo you wish to rename file ?"), 
							sRemoteUser.c_str(),
							sPrevFileName.c_str(),
							sFilePath.c_str() );
						bTryRename = MessageBox( NULL , szTemp ,MSG_BOX_TITEL ,MB_YESNO ) == IDYES;
					}
					else
						bTryRename = true;
						
				
					if( bTryRename )
					{
						if( ! MoveFile( sPrevFileName.c_str(), sFilePath.c_str() ) )
						{
							// this might be because the new path isent created 
							// so we will try to create it 
							bCreatePathToFile( sFilePath );

							while( ! MoveFile( sPrevFileName.c_str(), sFilePath.c_str() ) )
							{
								_sntprintf( szTemp , sizeof( szTemp ) , 
									LPGENT("Failed to rename file\n\nfrom:\t%s\nto:\t%s\n\nFailed with error: %s"), 
									sPrevFileName.c_str(),
									sFilePath.c_str() ,
									sGetErrorString().c_str() );

								if( MessageBox( NULL , szTemp ,MSG_BOX_TITEL,MB_RETRYCANCEL ) != IDRETRY )
									break;
							}
						}
					}
				}
			}
		}

		// Store the Filename used so that we can check if it changes.
		DBWriteContactSettingTString( hContact , MODULE , "PrevFileName" , sNoDBPath.c_str() );
	}

	return sFilePath;
}


/////////////////////////////////////////////////////////////////////
// Member Function : FileNickFromHandle
// Type            : Global
// Parameters      : hContact - ?
// Returns         : string
// Description     : Replaceses invalid file name chars
// References      : -
// Remarks         : -
// Created         : 030107 , 07 January 2003
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

tstring FileNickFromHandle( HANDLE hContact)
{
	tstring ret = NickFromHandle( hContact );
	string::size_type nCur = 0;
	while( (nCur = ret.find_first_of(_T(":\\"),nCur)) != ret.npos  )
		ret[nCur] = cBadCharReplace;
	return ret;
}


/////////////////////////////////////////////////////////////////////
// Member Function : ReplaceAllNoColon
// Type            : Global
// Parameters      : sSrc       - ?
//                   pszReplace - ?
//                   sNew       - ?
// Returns         : void
// Description     : Removes any ':' in the new string
//                   
// References      : -
// Remarks         : -
// Created         : 040205 , 05 februar 2004
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void ReplaceAllNoColon( tstring &sSrc , const _TCHAR * pszReplace , tstring &sNew)
{
	tstring::size_type nCur = 0;
	while( (nCur = sNew.find_first_of(_T(':'),nCur)) != sNew.npos  )
		sNew[nCur] = cBadCharReplace;
	ReplaceAll( sSrc , pszReplace , sNew );
}

/////////////////////////////////////////////////////////////////////
// Member Function : ReplaceDefines
// Type            : Global
// Parameters      : hContact - Handle to user
//                   sTarget  - String with either %user% or %UIN%, to replace in
// Returns         : void
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 020525 , 25 May 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void ReplaceDefines( HANDLE hContact , tstring & sTarget )
{
	if( sTarget.find( _T("%nick%") ) != string::npos )
	{
		ReplaceAll( sTarget , _T("%nick%") , FileNickFromHandle( hContact ) );
	}

	bool bUINUsed = sTarget.find( _T("%UIN%") ) != string::npos;
	bool bEMailUsed = sTarget.find( _T("%e-mail%") ) != string::npos;
	bool bProtoUsed = sTarget.find( _T("%protocol%") ) != string::npos;
	bool bIdentifierUsed = sTarget.find( _T("%identifier%") ) != string::npos;

	if( bUINUsed || bEMailUsed || bProtoUsed || bIdentifierUsed )
	{
		string sProto = _DBGetStringA( hContact , "Protocol" , "p" , "" );
		if( bUINUsed || ( bIdentifierUsed && sProto == "ICQ" ) )
		{
			DWORD dwUIN = DBGetContactSettingDword(hContact, sProto.c_str(), "UIN", 0);
			tstring sReplaceUin;
			if( dwUIN )
			{
				_TCHAR sTmp[20];
				_sntprintf( sTmp , sizeof( sTmp ) ,_T("%d") , dwUIN );
				sReplaceUin = sTmp;
			}
			else
			{
				sReplaceUin = FileNickFromHandle( hContact );
			}

			if( bUINUsed )
				ReplaceAll( sTarget , _T("%UIN%") , sReplaceUin );
			if( bIdentifierUsed && sProto == "ICQ" )
			{
				bIdentifierUsed = false;
				ReplaceAll( sTarget , _T("%identifier%") , sReplaceUin );
			}
		}

		if( bEMailUsed || ( bIdentifierUsed && sProto == "MSN"))
		{
			tstring sEMail = _DBGetString( hContact , sProto.c_str() , "e-mail" , _T("") );
			if( sEMail.empty() )
			{
				sEMail = _DBGetString( hContact , "MSN" , "e-mail" , _T("") );
				if( sEMail.empty() )
				{
					// We can't finde the E-mail address we will use the the nick
					sEMail = FileNickFromHandle( hContact );
				}
			}
			if( bEMailUsed )
				ReplaceAllNoColon( sTarget , _T("%e-mail%") , sEMail );
			if( bIdentifierUsed && sProto == "MSN")
			{
				bIdentifierUsed = false;
				ReplaceAllNoColon( sTarget , _T("%identifier%") , sEMail );
			}
		}
		if( bIdentifierUsed && sProto == "Jabber" )
		{
			tstring sReplace = _DBGetString( hContact , "Jabber" , "jid" , _T("") );
			if( sReplace.empty() )
			{
				sReplace = FileNickFromHandle( hContact );
			}
			bIdentifierUsed = false;
			ReplaceAll( sTarget , _T("%identifier%") , sReplace );
		}
		if( bProtoUsed )
		{
			tstring tmp = _DBGetString( hContact , "Protocol" , "p" , _T("") );
			ReplaceAllNoColon( sTarget , _T("%protocol%") , tmp );
		}
		if( bIdentifierUsed )
		{
			// It has still not been replaced we will just use nick
			ReplaceAll( sTarget , _T("%nick%") , FileNickFromHandle( hContact ) );
		}
	}

	if( sTarget.find( _T("%group%") ) != string::npos )
	{
		tstring sGroup = _DBGetString( hContact , "CList" , "Group" , _T("") );
		ReplaceAllNoColon( sTarget , _T("%group%") , sGroup );
	}

	// We can't replace the : here because if the user uses C:\... in the file path 
	// this will also be replaced 
   string::size_type nCur = 0;
	while( (nCur = sTarget.find_first_of(_T("/*?<>|\""),nCur)) != sTarget.npos  )
		sTarget[nCur] = cBadCharReplace;

}


/////////////////////////////////////////////////////////////////////
// Member Function : ReplaceTimeVariables
// Type            : Global
// Parameters      : sRet - ?
// Returns         : void
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 040219 , 19 februar 2004
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void ReplaceTimeVariables( tstring &sRet )
{
	if( sRet.find( _T("%year%") ) != string::npos ||
		 sRet.find( _T("%month%") ) != string::npos ||
		 sRet.find( _T("%day%") ) != string::npos )
	{
		SYSTEMTIME stTime;
		GetLocalTime( &stTime );
		_TCHAR sTmp[20];

		_sntprintf( sTmp , sizeof( sTmp ) ,_T("%d") , stTime.wYear );
		ReplaceAll( sRet , _T("%year%") , sTmp );
		_sntprintf( sTmp , sizeof( sTmp ) ,_T("%.2d") , stTime.wMonth );
		ReplaceAll( sRet , _T("%month%") , sTmp );
		_sntprintf( sTmp , sizeof( sTmp ) ,_T("%.2d") , stTime.wDay );
		ReplaceAll( sRet , _T("%day%") , sTmp );
	}
}

/////////////////////////////////////////////////////////////////////
// Member Function : UpdateFileToColWidth
// Type            : Global
// Parameters      : None
// Returns         : void
// Description     : updates clFileTo1ColWidth,
//                   
// References      : -
// Remarks         : -
// Created         : 020422 , 22 April 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void UpdateFileToColWidth()
{
	clFileTo1ColWidth.clear();

	HANDLE hContact = db_find_first();
	for(;;)
	{
		tstring sNick = NickFromHandle( hContact );
		string::size_type &rnValue = clFileTo1ColWidth[ GetFilePathFromUser( hContact ) ];
		if( rnValue  < sNick.size() )
			rnValue = sNick.size();

		if( ! hContact )
			break;

		hContact = db_find_next(hContact);
	}
}



/////////////////////////////////////////////////////////////////////
// Member Function : DisplayErrorDialog
// Type            : Global
// Parameters      : pszError - ?
//                   sFile    - ?
//                   dbei     - ?
// Returns         : void
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 021203 , 03 December 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void DisplayErrorDialog( const _TCHAR * pszError , tstring& sFilePath , DBEVENTINFO * dbei )
{
	tstring sError = TranslateTS( pszError );
	sError += sFilePath;
	sError += LPGENT("\nError :");
	sError += sGetErrorString();
	sError += _T("\n");
	sError += LPGENT("Message has not been saved !\n");
	sError += LPGENT("Do you wish to save debug information ?");
	if( MessageBox( NULL , sError.c_str() ,MSG_BOX_TITEL,MB_YESNO ) == IDYES )
	{
		OPENFILENAME ofn;       // common dialog box structure
		_TCHAR szFile[260];       // buffer for file name
		_tcscpy( szFile , _T("DebugInfo.txt") );

		// Initialize OPENFILENAME
		ZeroMemory(&ofn, sizeof(OPENFILENAME));
		ofn.lStructSize = sizeof(OPENFILENAME);
		//ofn.hwndOwner = NULL;
		ofn.lpstrFile = szFile;
		ofn.nMaxFile = sizeof(szFile);
		ofn.lpstrFilter = _T("All\0*.*\0Text\0*.TXT\0\0");
		ofn.nFilterIndex = 1;
		ofn.lpstrFileTitle = NULL;
		ofn.nMaxFileTitle = 0;
		ofn.lpstrInitialDir = NULL;
		ofn.Flags = 0 /*OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST*/;
		ofn.lpstrDefExt = _T("TXT");

		// Display the Open dialog box. 

		if( GetSaveFileName(&ofn) )
		{
			HANDLE hf;              // file handle
			hf = CreateFile(ofn.lpstrFile, GENERIC_WRITE, 
				  0, (LPSECURITY_ATTRIBUTES) NULL,
				  CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
				  (HANDLE) NULL);

			bWriteTextToFile( hf , sError.c_str() , false );
			if( dbei )
			{
				bWriteToFile( hf , "\r\ndbei          :" );

				bWriteHexToFile( hf , dbei , sizeof( DBEVENTINFO ));
				if( dbei->pBlob )
				{
					bWriteToFile( hf , "\r\ndbei.pBlob    :" );
					bWriteHexToFile( hf , dbei->pBlob , min( dbei->cbBlob , 10000 ));
				}
				if( dbei->szModule )
				{
					bWriteToFile( hf , "\r\ndbei.szModule :" );
					bWriteToFile( hf , dbei->szModule );
				}
			}
			CloseHandle( hf );
		}
	}
}

/////////////////////////////////////////////////////////////////////
// Member Function : ExportDBEventInfo
// Type            : Global
// Parameters      : hContact - handle to contact
//                   dbei     - Event to export
// Returns         : void
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 050429 , 29 april 2005
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void ExportDBEventInfo(HANDLE hContact, DBEVENTINFO &dbei )
{
	_TCHAR szTemp[500];
	tstring sFilePath = GetFilePathFromUser( hContact );
	
	GetLastError();// Clear last error !!

	HANDLE hFile = CreateFile( sFilePath.c_str() , GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ , 0 ,OPEN_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL );
	if( hFile == INVALID_HANDLE_VALUE )
	{
		// this might be because the path isent created 
		// so we will try to create it 
		if( bCreatePathToFile( sFilePath ) )
		{
			hFile = CreateFile( sFilePath.c_str() , GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ , 0 ,OPEN_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL );
		}
	}

	if( hFile == INVALID_HANDLE_VALUE )
	{
		DisplayErrorDialog( LPGENT("Failed to open or create file :\n") , sFilePath , NULL );
		return;
	}

	tstring sLocalUser;
	tstring sRemoteUser;
	string::size_type nFirstColumnWidth;

	if( bUseLessAndGreaterInExport )
	{
		sLocalUser = _T("<<");
		sRemoteUser = _T(">>");
		nFirstColumnWidth = 4;
	}
	else
	{
		sLocalUser = NickFromHandle(0);
		sRemoteUser = NickFromHandle(hContact);
		nFirstColumnWidth = max( sRemoteUser.size() , clFileTo1ColWidth[sFilePath] );
		nFirstColumnWidth = max( sLocalUser.size() , nFirstColumnWidth );
		nFirstColumnWidth += 2;
	}

	bool bWriteUTF8Format = false;

	{
		DWORD dwLowSize;
		DWORD dwHighSize = 0;

		dwLowSize = GetFileSize( hFile , &dwHighSize );

		if( dwLowSize == INVALID_FILE_SIZE || dwLowSize != 0 || dwHighSize != 0 )
		{
			DWORD dwDataRead = 0;
			BYTE ucByteOrder[3];
			if( ReadFile( hFile ,  ucByteOrder , 3 , &dwDataRead , NULL ) )
			{
				bWriteUTF8Format = bIsUtf8Header( ucByteOrder );
			}
			DWORD dwPtr = SetFilePointer( hFile , 0 , 0 , FILE_END );
			if( dwPtr == INVALID_SET_FILE_POINTER )
			{
				// we need to aborte mission here because if we continue we risk 
				// overwriting old log.
				DisplayErrorDialog( LPGENT("Failed to move to the end of the file :\n") , sFilePath , NULL );
				CloseHandle( hFile );
				return;
			}
		}
		else
		{
			bWriteUTF8Format = bUseUtf8InNewFiles;
			if( bWriteUTF8Format )
			{
				if( ! bWriteToFile( hFile , szUtf8ByteOrderHeader, sizeof( szUtf8ByteOrderHeader ) - 1 ) )
				{
					DisplayErrorDialog( LPGENT("Failed to UTF8 byte order code to file :\n") , sFilePath , NULL );
					CloseHandle( hFile );
					return;
				}
			}
			tstring output = _T("------------------------------------------------\r\n")
					          LPGENT("      History for\r\n")
								LPGENT("User      : %User%\r\n")
								LPGENT("Protocol  : %Proto%\r\n")
								LPGENT("UIN       : %UIN%\r\n")
								LPGENT("FirstName : %FirstName%\r\n")
 								LPGENT("LastName  : %LastName%\r\n")
								LPGENT("Age       : %Age%\r\n")
								LPGENT("Gender    : %Gender%\r\n")
 								LPGENT("e-mail    : %e-mail%\r\n")
								LPGENT("Nick      : %Nick%\r\n")
								LPGENT("City      : %City%\r\n")
								LPGENT("State     : %State%\r\n")
								LPGENT("Phone     : %Phone%\r\n")
								LPGENT("Homepage  : %Homepage%\r\n")
								LPGENT("- About -\r\n%About%\r\n")
								 _T("------------------------------------------------\r\n");

			// This is written this way because I expect this will become a string the user may set 
			// in the options dialog.
			ReplaceAll( output , _T("%User%") , sRemoteUser );

			string sProto = _DBGetStringA( hContact , "Protocol" , "p" , "" );
			ReplaceAll( output , _T("%Proto%") , _DBGetString( hContact , "Protocol" , "p" , _T("") ) );

			for( int nCur = 0 ; nCur < 9 ; nCur++ )
			{
				ReplaceAll( output , pszReplaceList[nCur] , _DBGetString( hContact , sProto.c_str() , pszReplaceListA[nCur] , _T("") ) );
			}

			_sntprintf( szTemp , sizeof( szTemp ) , _T("%d") , DBGetContactSettingDword(hContact, sProto.c_str(), "UIN", 0) );
			ReplaceAll( output , _T("%UIN%") , szTemp );
			
			_sntprintf( szTemp , sizeof( szTemp ) , _T("%d") , DBGetContactSettingWord(hContact, sProto.c_str(), "Age", 0));
			ReplaceAll( output , _T("%Age%") , szTemp );

			szTemp[0] = (_TCHAR)DBGetContactSettingByte(hContact, sProto.c_str(), "Gender", 0);
			szTemp[1] = 0;
			ReplaceAll( output , _T("%Gender%") , szTemp );

			if( ! bWriteTextToFile( hFile , output.data(), bWriteUTF8Format, (int)output.size() ) )
			{
				DisplayErrorDialog( LPGENT("Failed to write user details to file :\n") , sFilePath , NULL );
				CloseHandle( hFile );
				return;
			}
		}
	}

	int nIndent;
	{  // Get time stamp 
		
		nIndent = _sntprintf( szTemp , sizeof( szTemp ) , _T("%-*s") , 
			nFirstColumnWidth , 
			dbei.flags & DBEF_SENT ? sLocalUser.c_str() : sRemoteUser.c_str() );

		DBTIMETOSTRINGT dbtts;
		dbtts.cbDest = sizeof(szTemp) - nIndent - 2;
		dbtts.szDest = &szTemp[nIndent];
		dbtts.szFormat = (_TCHAR*)sTimeFormat.c_str();

		CallService(MS_DB_TIME_TIMESTAMPTOSTRINGT,dbei.timestamp,(LPARAM)&dbtts);

		nIndent = (int)_tcslen( szTemp );
		szTemp[nIndent++] = ' ';
		
		// Write first part of line with name and timestamp
		if( ! bWriteTextToFile( hFile , szTemp , bWriteUTF8Format , nIndent ) )
		{
			DisplayErrorDialog( LPGENT("Failed to write timestamp and username to file :\n") , sFilePath , &dbei );
			CloseHandle( hFile );
			return;
		}
	}

	if( dbei.pBlob != NULL && dbei.cbBlob >= 2 )
	{
		dbei.pBlob[ dbei.cbBlob ] = 0; 

		switch(dbei.eventType)
		{
			case EVENTTYPE_MESSAGE:
			{
				TCHAR* msg = DbGetEventTextT( &dbei, CP_ACP );
				if( ! bWriteIndentedToFile( hFile , nIndent , msg , bWriteUTF8Format ) )
				{
					DisplayErrorDialog( LPGENT("Failed to write message to the file :\n") , sFilePath , &dbei );
				}
				mir_free(msg);
				break;
/*
				const char * pszData = (const char*)dbei.pBlob;
				bool bConvertedToUtf8 = false;
				if( bWriteUTF8Format )// Write UTF-8 format in file ?
				{
					int nAnsiLen = strlen((char *) dbei.pBlob)+1;
					if( nAnsiLen < (int)dbei.cbBlob )
					{
						// Message is also encoded in unicode UTF-16/UCS-2, little endian.
						if( WideCharToMultiByte( CP_UTF8 , 0 , (wchar_t*)&dbei.pBlob[ nAnsiLen ] , nAnsiLen , szTemp , sizeof(szTemp) , 0 , 0 ) ) 
						{
							pszData = szTemp;
							bConvertedToUtf8 = true;
						}
					}
					// We need to write in UTF8 format so we have to convert ansi string to UTF8
				}
				if( ! bWriteIndentedToFile( hFile , nIndent , pszData , bWriteUTF8Format ) )
				{
					DisplayErrorDialog( _T("Failed to write message to the file :\n") , sFilePath , &dbei );
				}
				break;*/
			}
			case EVENTTYPE_URL:
			case EVENTTYPE_FILE:
			{
				const _TCHAR * pszType;
				const char * pszData;

				if( dbei.eventType == EVENTTYPE_URL )
				{
					pszType = LPGENT("URL: ");
					pszData = (char *)dbei.pBlob;
				}
				else
				{
					pszType = LPGENT("File: ");
					pszData = (char *)(dbei.pBlob + sizeof( DWORD ));
				}

				bool bWriteOk = false;

				int nLen = (int)strlen( pszData );
				if( (pszData - (char *)dbei.pBlob) + nLen < (int)dbei.cbBlob )
				{
					if( bWriteTextToFile( hFile , pszType , bWriteUTF8Format ) && 
						 bWriteIndentedToFile( hFile , nIndent , pszData , bWriteUTF8Format ) )
					{
						pszData += nLen + 1;
						if( (pszData - (char *)dbei.pBlob) >= (int)dbei.cbBlob )
						{
							bWriteOk = true;
						}
						else
						{
							nLen = (int)strlen( pszData );
							if( (pszData - (char *)dbei.pBlob) + nLen < (int)dbei.cbBlob )
							{
								if( bWriteNewLine( hFile , nIndent ) &&  
									 bWriteTextToFile( hFile , LPGENT("Description: ") , bWriteUTF8Format) && 
									 bWriteIndentedToFile( hFile , nIndent , pszData , bWriteUTF8Format ) )
								{
									bWriteOk = true;
								}
							}
						}
					}
				}

				if( ! bWriteOk )
					DisplayErrorDialog( LPGENT("Failed to write URL/File to the file :\n") , sFilePath , &dbei );
				break;
			}
			case EVENTTYPE_AUTHREQUEST:
			case EVENTTYPE_ADDED:
			{
				const _TCHAR * pszTypes[] = {
					LPGENT("Nick      :"),
					LPGENT("FirstName :"),
					LPGENT("LastName  :"),
					LPGENT("e-mail    :"),
					LPGENT("Reason    :")};

				if( dbei.cbBlob < 8 || dbei.cbBlob > 5000 )
				{
					int n = _sntprintf( szTemp , sizeof( szTemp ) , LPGENT("Invalid Database event received. Type %d, size %d"),dbei.eventType, dbei.cbBlob );
					if( ! bWriteTextToFile( hFile , szTemp , bWriteUTF8Format , n ) )
						DisplayErrorDialog( LPGENT("Failed to write Invalid Database event the file :\n") , sFilePath , &dbei );
					break;
				}

				bool bWriteOk = false;

				int nStringCount;
				const _TCHAR *pszTitle;
				char *pszCurBlobPos;
				if( dbei.eventType == EVENTTYPE_AUTHREQUEST )
				{	// request 
					//blob is: uin(DWORD), hContact(DWORD), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), email(ASCIIZ), reason(ASCIIZ)
					nStringCount = 5;
					pszCurBlobPos = (char *)dbei.pBlob + sizeof( DWORD )*2;
					pszTitle = LPGENT("The following user made an authorization request:");
				}
				else
				{  // Added
					//blob is: uin(DWORD), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), email(ASCIIZ)
					pszCurBlobPos = (char *)dbei.pBlob + sizeof(DWORD);
					nStringCount = 4;
					pszTitle = LPGENT("The following user added you to their contact list:");
				}

				if( bWriteTextToFile( hFile , pszTitle , bWriteUTF8Format ) &&
					 bWriteNewLine( hFile , nIndent ) &&
					 bWriteTextToFile( hFile , LPGENT("UIN       :") , bWriteUTF8Format ) )
				{
					DWORD uin = *((PDWORD)(dbei.pBlob));
					int n = _sntprintf( szTemp , sizeof( szTemp ) ,_T("%d"), uin );
					if( bWriteTextToFile( hFile , szTemp , bWriteUTF8Format , n ) )
					{
						char *pszEnd = (char *) (dbei.pBlob + dbei.cbSize);
						for( int n = 0 ; n < nStringCount && pszCurBlobPos < pszEnd ; n++ )
						{
							if( *pszCurBlobPos )
							{
								if( ! bWriteNewLine( hFile , nIndent ) ||
									 ! bWriteTextToFile(  hFile , TranslateTS(pszTypes[ n ]) , bWriteUTF8Format ) ||
									 ! bWriteIndentedToFile(  hFile , nIndent , pszCurBlobPos , bWriteUTF8Format ) )
								{
									break;
								}
								pszCurBlobPos += strlen( pszCurBlobPos );
							}
							pszCurBlobPos++;
						}
						bWriteOk = true;
					}
				}

				if( ! bWriteOk )
					DisplayErrorDialog( LPGENT("Failed to write AUTHREQUEST or ADDED to the file :\n") , sFilePath , &dbei );

				break;
			}
			case ICQEVENTTYPE_EMAILEXPRESS:
			case ICQEVENTTYPE_WEBPAGER:
			{
				//e-mail express 
				//db event added to NULL contact
				//blob format is:
				//ASCIIZ    text, usually of the form "Subject: %s\r\n%s"
				//ASCIIZ    from name
				//ASCIIZ    from e-mail

				//www pager
				//db event added to NULL contact
				//blob format is:
				//ASCIIZ    text, usually "Sender IP: xxx.xxx.xxx.xxx\r\n%s"
				//ASCIIZ    from name
				//ASCIIZ    from e-mail
				const char* pszStr = (const char*)dbei.pBlob;

				if( dbei.eventType == ICQEVENTTYPE_EMAILEXPRESS )
					bWriteTextToFile(  hFile , LPGENT("EmailExpress from:") , bWriteUTF8Format);
				else
					bWriteTextToFile(  hFile , LPGENT("WebPager from:") , bWriteUTF8Format );

				bWriteNewLine( hFile , nIndent );

				size_t nMsgLenght = strlen( pszStr ) + 1;
				if( nMsgLenght < dbei.cbBlob )
				{
					size_t nFriendlyLen = strlen( &pszStr[nMsgLenght] );
					bWriteTextToFile(  hFile , &pszStr[nMsgLenght] , bWriteUTF8Format , (int)nFriendlyLen );
					size_t nEmailOffset = nMsgLenght + nFriendlyLen + 1;
					if( nEmailOffset < dbei.cbBlob )
					{
						bWriteTextToFile(  hFile , _T("<") , bWriteUTF8Format );
						size_t nEmailLen = strlen( &pszStr[nEmailOffset] );
						bWriteTextToFile(  hFile , &pszStr[nEmailOffset] , bWriteUTF8Format , (int)nEmailLen );
						bWriteTextToFile(  hFile , _T(">") , bWriteUTF8Format );
					}
				}
				else
				{
					bWriteTextToFile(  hFile , LPGENT("No from address") , bWriteUTF8Format );
				}

				if( ! bWriteNewLine( hFile , nIndent ) ||
					 ! bWriteIndentedToFile(  hFile , nIndent , pszStr , bWriteUTF8Format ) )
				{
					DisplayErrorDialog( LPGENT("Failed to write EmailExpress to the file :\n") , sFilePath , &dbei );
				}
				break;
			}
			case ICQEVENTTYPE_SMS:
			{
				if( ! bWriteIndentedToFile(  hFile , nIndent , (const char*)dbei.pBlob , bWriteUTF8Format ) )
				{
					DisplayErrorDialog( LPGENT("Failed to write SMS to the file :\n") , sFilePath , &dbei );
				}
				break;
			}
			default:
			{
				int n = _sntprintf( szTemp , sizeof( szTemp ) , LPGENT("Unknown event type %d, size %d") , dbei.eventType, dbei.cbBlob );
				if( ! bWriteTextToFile( hFile , szTemp , bWriteUTF8Format , n ) )
				{
					DisplayErrorDialog( LPGENT("Failed to write Unknown event to the file :\n") , sFilePath , &dbei );
				}
				break;
			}
		}
	}
	else
	{
		int n = _sntprintf( szTemp , sizeof( szTemp ) , LPGENT("Unknown event type %d, size %d") , dbei.eventType, dbei.cbBlob );
		bWriteTextToFile( hFile , szTemp , bWriteUTF8Format , n );
	}
	bWriteToFile(  hFile , bAppendNewLine ? "\r\n\r\n" : "\r\n" );

	CloseHandle( hFile );

	UpdateFileViews( sFilePath.c_str() );
}

/////////////////////////////////////////////////////////////////////
// Member Function : nExportEvent
// Type            : Global
// Parameters      : wparam - handle to contact
//                   lparam - handle to the new DB event
// Returns         : int
// Description     : Called when an event is added to the DB
//                   Or from the Export All funktion
// References      : -
// Remarks         : -
// Created         : 020422 , 22 April 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////


int nExportEvent(WPARAM wparam,LPARAM lparam)
{
	HANDLE hContact = (HANDLE)wparam;

	if( ! DBGetContactSettingByte(hContact,MODULE,"EnableLog",1) )
		return 0;

	DBEVENTINFO dbei={0};
	dbei.cbSize=sizeof(dbei);
	char szTemp[500];

	{ // Get Blob data size
		
		int nSize = (int)CallService(MS_DB_EVENT_GETBLOBSIZE,(WPARAM)lparam,0);
		if( nSize > 0 )
		{
			dbei.cbBlob = nSize;
			dbei.pBlob = (PBYTE)malloc(dbei.cbBlob + 2 );
			dbei.pBlob[dbei.cbBlob] = 0;
			dbei.pBlob[dbei.cbBlob+1] = 0; 
			// Double null terminate, this shut pervent most errors 
			// where the blob received has an invalid format
		}
		// else dbei.cbBlob will be 0 
	}

	if( ! CallService(MS_DB_EVENT_GET,(WPARAM)lparam,(LPARAM)&dbei) )
	{
		if( dbei.eventType != EVENTTYPE_STATUSCHANGE )
		{
			_snprintf( szTemp , sizeof( szTemp ) , "DisableProt_%s" , dbei.szModule );
			if( DBGetContactSettingByte(NULL,MODULE,szTemp,1) )
			{
				ExportDBEventInfo( hContact , dbei );
			}
		}
	}
	if( dbei.pBlob )
		free(dbei.pBlob);
	return 0;
}

#ifdef _UNICODE
bool bWriteIndentedToFile( HANDLE hFile , int nIndent , const char * pszSrc , bool bUtf8File )
{
	int nLen = (int)strlen( pszSrc );
	wchar_t * pszWstr = new wchar_t[nLen+1];
	bool bRet = false;
	if( MultiByteToWideChar(CP_ACP, 0, pszSrc, nLen, pszWstr, nLen ) == nLen )
	{
		pszWstr[nLen] = NULL;
		bRet = bWriteIndentedToFile( hFile , nIndent , pszWstr , bUtf8File );
	}
	return bRet;
}
#endif

/////////////////////////////////////////////////////////////////////
// Member Function : bWriteIndentedToFile
// Type            : Global
// Parameters      : hFile   - ?
//                   nIndent - ?
//                   pszSrc  - 
// Returns         : Returns true if 
// Description     : 
//                   
// References      : -
// Remarks         : -
// Created         : 020629 , 29 June 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

bool bWriteIndentedToFile( HANDLE hFile , int nIndent , const _TCHAR * pszSrc , bool bUtf8File )
{
	if (pszSrc == NULL)
		return true;

	bool bOk = true;
	bool bFirstLine = true;
	
	while( *pszSrc )
	{	// first we will scan forward in string to finde either new line or "max line with"
		int nLineLen = 0;
		do
		{
			if( pszSrc[nLineLen] == _T('\n') || pszSrc[nLineLen] == _T('\r'))
				break;

			if( nLineLen >= nMaxLineWidth )
			{	// ok the line was not broken. we need to force a break
				// we will scan backwards again to finde a space !!
				// then we will look for a ? and so on.

				const _TCHAR ac[] = { _T(' '),_T('?'),_T('-'),_T('.'),_T(',') };
				for( int y = 0 ; y < sizeof( ac ) ; y++)
				{
					for( int n = nLineLen ; n > 0 ; n-- )
					{
						if( pszSrc[n] == ac[y] )
						{
							nLineLen = n;
							goto SuperBreak;
						}
					}
				}
				break;
			}
			nLineLen++;
		}
		while( pszSrc[nLineLen] );
		
		// trim away traling spaces !!
		if( nLineLen > 0 )
		{
			while( pszSrc[nLineLen-1] == ' ' )
				nLineLen--;
		}

SuperBreak:


		// nLineLen should contain the number af chars we need to write to the file 
		if( nLineLen > 0 )
		{
			if( !bFirstLine )
			{
				if( ! bWriteNewLine( hFile , nIndent ) )
				{
					bOk = false;
				}
			}
/*			if( bUtf8Src )
			{
				// Programming error writing UTF8 string to ansi file
				if( ! bUtf8File )
				{
					MessageBox( NULL , _T("Programming error writing UTF8 string to ansi file")  ,MSG_BOX_TITEL,MB_OK );
					// bUtf8File must be true here 
				}
				if( !bWriteToFile( hFile , pszSrc , nLineLen ) )
				{
					bOk = false;
				}
			}
			else*/
			{// Text format !!
				if( ! bWriteTextToFile( hFile , pszSrc , bUtf8File , nLineLen ) )
					bOk = false;
			}
		}
		bFirstLine = false;

		// skip any noice chars, MAC style '\r' '\r' '\n' 
		// and excess spaces 
		const _TCHAR * pszPrev = pszSrc;
		pszSrc += nLineLen;
		while( *pszSrc == _T(' ') || *pszSrc == _T('\n') || *pszSrc == _T('\r') ) 
			pszSrc++;

		if( pszPrev == pszSrc )
		{
			// this is an programming error we have not moved forward in string 
			MessageBox( NULL , LPGENT("Programming error on line __LINE__ please report this")  ,MSG_BOX_TITEL,MB_OK );
			break;
		}
	}

	// if bOk if false file writing failed 
	return bOk;
}


/////////////////////////////////////////////////////////////////////
// Member Function : nContactDeleted
// Type            : Global
// Parameters      : wparam - handle to the deleted Contact
//                   lparam - 0
// Returns         : int
// Description     : Called when an contact is about to be deleted
//                   
// References      : -
// Remarks         : -
// Created         : 021222 , 22 December 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

int nContactDeleted(WPARAM wparam,LPARAM /*lparam*/)
{
	if( enDeleteAction == eDANothing )
		return 0;

	HANDLE hContact = (HANDLE)wparam;

	tstring sFilePath = GetFilePathFromUser( hContact );

	{ // Test if there is another user using this file 
		HANDLE hOtherContact = db_find_first();
		for(;;)
		{
			if( hContact != hOtherContact && sFilePath == GetFilePathFromUser( hOtherContact ) )
			{
				return 0; // we found another contact abort mission :-)
			}

			if( ! hOtherContact )
				break;
			hOtherContact = db_find_next(hOtherContact);
		}
	}

	// Test to see if there is a file to delete
	HANDLE hPrevFile = CreateFile( sFilePath.c_str() , 
		GENERIC_READ , 
		FILE_SHARE_READ | FILE_SHARE_WRITE ,
		NULL,
		OPEN_EXISTING ,
		FILE_ATTRIBUTE_NORMAL , 
		NULL );

	if( hPrevFile != INVALID_HANDLE_VALUE )
	{
		CloseHandle( hPrevFile ); 

		_TCHAR szTemp[500];
		_sntprintf( szTemp , sizeof( szTemp ) , _T("%s\r\n%s") ,
			LPGENT("User has been deleted do you want to delete the file ?"), sFilePath.c_str() );

		if( enDeleteAction == eDAAutomatic ||
			 MessageBox( NULL , szTemp ,MSG_BOX_TITEL,MB_YESNO ) == IDYES )
		{
			if( ! DeleteFile( sFilePath.c_str() ) )
			{
				_sntprintf( szTemp , sizeof( szTemp ) , 
					_T("%s\r\n%s"),
					LPGENT("Failed to delete the file"),
					sFilePath.c_str() );

				DisplayLastError( szTemp );
			}
		}
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////
// Member Function : SaveSettings
// Type            : Global
// Parameters      : None
// Returns         : void
// Description     : Save Settings
//                   
// References      : -
// Remarks         : -
// Created         : 020429 , 29 April 2002
// Developer       : KN   
/////////////////////////////////////////////////////////////////////

void SaveSettings()
{
	DBWriteContactSettingWord( NULL , MODULE , "MaxLineWidth" , (WORD) nMaxLineWidth );
	DBWriteContactSettingTString( NULL , MODULE , "ExportDir" , sExportDir.c_str() );
	DBWriteContactSettingTString( NULL , MODULE , "DefaultFile" , sDefaultFile.c_str() );
	DBWriteContactSettingTString( NULL , MODULE , "TimeFormat" , sTimeFormat.c_str() );

	DBWriteContactSettingTString( NULL , MODULE , "FileViewerPrg" , sFileViewerPrg.c_str() );
	DBWriteContactSettingByte( NULL , MODULE , "UseInternalViewer" , bUseInternalViewer() );
	DBWriteContactSettingByte( NULL , MODULE , "ReplaceHistory" , bReplaceHistory );
	DBWriteContactSettingByte( NULL , MODULE , "AppendNewLine" , bAppendNewLine );
	DBWriteContactSettingByte( NULL , MODULE , "UseUtf8InNewFiles" , bUseUtf8InNewFiles );
	DBWriteContactSettingByte( NULL , MODULE , "UseLessAndGreaterInExport" , bUseLessAndGreaterInExport );

	DBWriteContactSettingByte( NULL , MODULE , "RenameAction" , (BYTE)enRenameAction );
	DBWriteContactSettingByte( NULL , MODULE , "DeleteAction" , (BYTE)enDeleteAction );
}