summaryrefslogtreecommitdiff
path: root/protocols/IRCG/src/irclib.cpp
blob: cf6a7914d759f6a27a4c434898d51d6a235ec9a9 (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
/*
IRC plugin for Miranda IM

Copyright (C) 2003-05 Jurgen Persson
Copyright (C) 2007-09 George Hazan

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "stdafx.h"

#define DCCCHATTIMEOUT 300
#define DCCSENDTIMEOUT 120

using namespace irc;

int CDccSession::nDcc = 0;

static int CompareHandlers( const CIrcHandler* p1, const CIrcHandler* p2 )
{
	return mir_wstrcmp( p1->m_name, p2->m_name );
}

OBJLIST<CIrcHandler> CIrcProto::m_handlers( 30, CompareHandlers );

////////////////////////////////////////////////////////////////////

CIrcMessage::CIrcMessage( CIrcProto* _pro, const wchar_t* lpszCmdLine, int codepage, bool bIncoming, bool bNotify ) :
	m_proto( _pro ),
	m_bIncoming( bIncoming ),
	m_bNotify( bNotify ),
	m_codePage( codepage ),
	parameters( 10 )
{
	ParseIrcCommand(lpszCmdLine);
}

CIrcMessage::CIrcMessage(const CIrcMessage& m) :
	sCommand( m.sCommand ),
	m_bIncoming( m.m_bIncoming ), 
	m_bNotify( m.m_bNotify ),
	m_codePage( m.m_codePage ),
	m_proto( m.m_proto ),
	parameters( m.parameters.getCount())
{
	prefix.sNick = m.prefix.sNick;
	prefix.sUser = m.prefix.sUser;
	prefix.sHost = m.prefix.sHost;

	for (int i = 0; i < m.parameters.getCount(); i++)
		parameters.insert(new CMStringW(m.parameters[i]));
}

CIrcMessage::~CIrcMessage()
{
}

void CIrcMessage::Reset()
{
	prefix.sNick = prefix.sUser = prefix.sHost = sCommand = L"";
	m_bIncoming = false;
	m_bNotify = true;

	parameters.destroy();
}

CIrcMessage& CIrcMessage::operator = (const CIrcMessage& m)
{
	if (&m != this) {
		sCommand = m.sCommand;
		parameters = m.parameters;
		prefix.sNick = m.prefix.sNick;
		prefix.sUser = m.prefix.sUser;
		prefix.sHost = m.prefix.sHost;
		m_bIncoming = m.m_bIncoming;
		m_bNotify = m.m_bNotify;
	}
	return *this;
}

CIrcMessage& CIrcMessage::operator = (const wchar_t* lpszCmdLine)
{
	Reset();
	ParseIrcCommand(lpszCmdLine);
	return *this;
}

void CIrcMessage::ParseIrcCommand(const wchar_t* lpszCmdLine)
{
	const wchar_t* p1 = lpszCmdLine;
	const wchar_t* p2 = lpszCmdLine;

	// prefix exists ?
	if (*p1 == ':') {
		// break prefix into its components (nick!user@host)
		p2 = ++p1;
		while (*p2 && !wcschr(L" !", *p2))
			++p2;
		prefix.sNick.SetString(p1, p2 - p1);
		if (*p2 != '!')
			goto end_of_prefix;
		p1 = ++p2;
		while (*p2 && !wcschr(L" @", *p2))
			++p2;
		prefix.sUser.SetString(p1, p2 - p1);
		if (*p2 != '@')
			goto end_of_prefix;
		p1 = ++p2;
		while (*p2 && *p2 != ' ')
			++p2;
		prefix.sHost.SetString(p1, p2 - p1);
end_of_prefix:
		while (*p2 && *p2 == ' ')
			++p2;
		p1 = p2;
	}

	// get command
	p2 = p1;
	while (*p2 && *p2 != ' ')
		++p2;

	sCommand.SetString(p1, p2 - p1);
	sCommand.MakeUpper();
	while (*p2 && *p2 == ' ')
		++p2;
	p1 = p2;

	// get parameters
	while (*p1) {
		if (*p1 == ':') {
			++p1;

			// seek end-of-message
			while (*p2)
				++p2;
			parameters.insert(new CMStringW(p1, p2 - p1));
			break;
		}
		else {
			// seek end of parameter
			while (*p2 && *p2 != ' ')
				++p2;
			parameters.insert(new CMStringW(p1, p2 - p1));
			// see next parameter
			while (*p2 && *p2 == ' ')
				++p2;
			p1 = p2;
		}
	}
}

////////////////////////////////////////////////////////////////////

int CIrcProto::getCodepage() const
{
	return (con != NULL) ? codepage : CP_ACP;
}

void CIrcProto::SendIrcMessage(const wchar_t* msg, bool bNotify, int cp)
{
	if (cp == -1)
		cp = getCodepage();

	if (this) {
		char* str = mir_u2a_cp(msg, cp);
		rtrim(str);
		int cbLen = (int)mir_strlen(str);
		str = (char*)mir_realloc(str, cbLen + 3);
		mir_strcat(str, "\r\n");
		NLSend((const BYTE*)str, cbLen + 2);
		mir_free(str);

		if (bNotify) {
			CIrcMessage ircMsg(this, msg, cp);
			if (!ircMsg.sCommand.IsEmpty() && ircMsg.sCommand != L"QUIT")
				Notify(&ircMsg);
		}
	}
}

bool CIrcProto::Connect(const CIrcSessionInfo& info)
{
	codepage = m_codepage;

	NETLIBOPENCONNECTION ncon = { 0 };
	ncon.cbSize = sizeof(ncon);
	ncon.szHost = info.sServer.c_str();
	ncon.wPort = info.iPort;
	con = Netlib_OpenConnection(m_hNetlibUser, &ncon);
	if (con == NULL) {
		wchar_t szTemp[300];
		mir_snwprintf(szTemp, L"\0035%s \002%s\002 (%S: %u).", TranslateT("Failed to connect to"), si.sNetwork.c_str(), si.sServer.c_str(), si.iPort);
		DoEvent(GC_EVENT_INFORMATION, SERVERWINDOW, NULL, szTemp, NULL, NULL, NULL, true, false);
		return false;
	}

	FindLocalIP(con); // get the local ip used for filetransfers etc

	if (info.m_iSSL > 0) {
		if (!Netlib_StartSsl(con, 0) && info.m_iSSL == 2) {
			Netlib_CloseHandle(con);
			con = NULL;
			m_info.Reset();
			return false;
		}
	}

	if (Miranda_IsTerminated()) {
		Disconnect();
		return false;
	}

	m_info = info;

	// start receiving messages from host
	ForkThread(&CIrcProto::ThreadProc, NULL);
	Sleep(100);
	if (info.sPassword.GetLength())
		NLSend("PASS %s\r\n", info.sPassword.c_str());
	NLSend(L"NICK %s\r\n", info.sNick.c_str());

	CMStringW userID = GetWord(info.sUserID.c_str(), 0);
	wchar_t szHostName[MAX_PATH];
	DWORD cbHostName = _countof(szHostName);
	GetComputerName(szHostName, &cbHostName);
	CMStringW HostName = GetWord(szHostName, 0);
	if (userID.IsEmpty())
		userID = L"Miranda";
	if (HostName.IsEmpty())
		HostName = L"host";
	NLSend(L"USER %s %s %s :%s\r\n", userID.c_str(), HostName.c_str(), L"server", info.sFullName.c_str());

	return con != NULL;
}

void CIrcProto::Disconnect(void)
{
	static const DWORD dwServerTimeout = 5 * 1000;

	if (con == NULL)
		return;

	KillIdent();

	if (m_quitMessage[0])
		NLSend(L"QUIT :%s\r\n", m_quitMessage);
	else
		NLSend("QUIT \r\n");

	Sleep(50);

	if (con)
		Netlib_Shutdown(con);

	m_info.Reset();
	return;
}

void CIrcProto::Notify(const CIrcMessage* pmsg)
{
	OnIrcMessage(pmsg);
}

int CIrcProto::NLSend(const unsigned char* buf, int cbBuf)
{
	if (!con || !buf)
		return 0;
	if (m_scriptingEnabled && cbBuf == 0)
		cbBuf = (int)mir_strlen((const char *)buf);
	return Netlib_Send(con, (const char*)buf, cbBuf, MSG_DUMPASTEXT);
}

int CIrcProto::NLSend(const wchar_t* fmt, ...)
{
	va_list marker;
	va_start(marker, fmt);

	wchar_t szBuf[1024 * 4];
	mir_vsnwprintf(szBuf, _countof(szBuf), fmt, marker);
	va_end(marker);

	char* buf = mir_u2a_cp(szBuf, getCodepage());
	int result = NLSend((unsigned char*)buf, (int)mir_strlen(buf));
	mir_free(buf);
	return result;
}

int CIrcProto::NLSend(const char* fmt, ...)
{
	va_list marker;
	va_start(marker, fmt);

	char szBuf[1024 * 4];
	int cbLen = mir_vsnprintf(szBuf, _countof(szBuf), fmt, marker);
	va_end(marker);

	return NLSend((unsigned char*)szBuf, cbLen);
}

int CIrcProto::NLSendNoScript(const unsigned char* buf, int cbBuf)
{
	if (con)
		return Netlib_Send(con, (const char*)buf, cbBuf, MSG_DUMPASTEXT);

	return 0;
}

int CIrcProto::NLReceive(unsigned char* buf, int cbBuf)
{
	return Netlib_Recv(con, (char*)buf, cbBuf, MSG_DUMPASTEXT);
}

void CIrcProto::KillIdent()
{
	if (hBindPort) {
		HANDLE hPort = hBindPort;
		hBindPort = NULL;
		Netlib_CloseHandle(hPort);
	}
}

void CIrcProto::InsertIncomingEvent(wchar_t* pszRaw)
{
	CIrcMessage msg(this, pszRaw, true);
	Notify(&msg);
	return;
}

void CIrcProto::createMessageFromPchar(const char* p)
{
	wchar_t* ptszMsg;
	if (codepage != CP_UTF8 && m_utfAutodetect) {
		if (mir_utf8decodecp(NEWSTR_ALLOCA(p), codepage, &ptszMsg) == NULL)
			ptszMsg = mir_a2u_cp(p, codepage);
	}
	else ptszMsg = mir_a2u_cp(p, codepage);
	CIrcMessage msg(this, ptszMsg, codepage, true);
	Notify(&msg);
	mir_free(ptszMsg);
}

void CIrcProto::DoReceive()
{
	char chBuf[1024 * 4 + 1];
	int cbInBuf = 0;

	if (m_info.bIdentServer && m_info.iIdentServerPort != NULL) {
		NETLIBBIND nb = {};
		nb.cbSize = sizeof(NETLIBBIND);
		nb.pfnNewConnectionV2 = DoIdent;
		nb.pExtra = this;
		nb.wPort = m_info.iIdentServerPort;
		
		hBindPort = Netlib_BindPort(m_hNetlibUser, &nb);
		if (!hBindPort || nb.wPort != m_info.iIdentServerPort) {
			debugLogA("Error: unable to bind local port %u", m_info.iIdentServerPort);
			KillIdent();
		}
	}

	while (con) {
		int nLinesProcessed = 0;

		int cbRead = NLReceive((unsigned char*)chBuf + cbInBuf, sizeof(chBuf)-cbInBuf - 1);
		if (cbRead <= 0)
			break;

		cbInBuf += cbRead;
		chBuf[cbInBuf] = '\0';

		char* pStart = chBuf;
		while (*pStart) {
			char* pEnd;

			// seek end-of-line
			for (pEnd = pStart; *pEnd && *pEnd != '\r' && *pEnd != '\n'; ++pEnd)
				;
			if (*pEnd == '\0')
				break; // uncomplete message. stop parsing.

			++nLinesProcessed;

			// replace end-of-line with NULLs and skip
			while (*pEnd == '\r' || *pEnd == '\n')
				*pEnd++ = '\0';

			// process single message by monitor objects
			if (*pStart) {
				if (m_scriptingEnabled) {
					char* pszTemp = mir_strdup(pStart);

					if (pszTemp) {
						char* p1 = pszTemp;
						// replace end-of-line with NULLs
						while (*p1 != '\0') {
							if (*p1 == '\r' || *p1 == '\n')
								*p1 = '\0';
							p1++;
						}

						createMessageFromPchar(pszTemp);
					}

					mir_free(pszTemp);
				}
				else createMessageFromPchar(pStart);
			}

			cbInBuf -= pEnd - pStart;
			pStart = pEnd;
		}

		// discard processed messages
		if (nLinesProcessed != 0)
			memmove(chBuf, pStart, cbInBuf + 1);
	}

	if (con) {
		Netlib_CloseHandle(con);
		con = NULL;
	}

	// notify monitor objects that the connection has been closed
	Notify(NULL);
}

void __cdecl CIrcProto::ThreadProc(void*)
{
	Thread_SetName("IRC: CIrcProto");
	DoReceive();
	m_info.Reset();
}

void CIrcProto::AddDCCSession(MCONTACT, CDccSession *dcc)
{
	mir_cslock lck(m_dcc);

	CDccSession* p = m_dcc_chats.find(dcc);
	if (p)
		m_dcc_chats.remove(p);

	m_dcc_chats.insert(dcc);
}

void CIrcProto::AddDCCSession(DCCINFO*, CDccSession *dcc)
{
	mir_cslock lck(m_dcc);
	m_dcc_xfers.insert(dcc);
}

void CIrcProto::RemoveDCCSession(MCONTACT hContact)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_chats.getCount(); i++)
		if (m_dcc_chats[i]->di->hContact == hContact) {
			m_dcc_chats.remove(i);
			break;
		}
}

void CIrcProto::RemoveDCCSession(DCCINFO *pdci)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_xfers.getCount(); i++) {
		if (m_dcc_xfers[i]->di == pdci) {
			m_dcc_xfers.remove(i);
			break;
		}
	}
}

CDccSession* CIrcProto::FindDCCSession(MCONTACT hContact)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_chats.getCount(); i++)
		if (m_dcc_chats[i]->di->hContact == hContact)
			return m_dcc_chats[i];

	return 0;
}

CDccSession* CIrcProto::FindDCCSession(DCCINFO *pdci)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_xfers.getCount(); i++)
		if (m_dcc_xfers[i]->di == pdci)
			return m_dcc_xfers[i];

	return 0;
}

CDccSession* CIrcProto::FindDCCSendByPort(int iPort)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_xfers.getCount(); i++) {
		CDccSession *p = m_dcc_xfers[i];
		if (p->di->iType == DCC_SEND && p->di->bSender && iPort == p->di->iPort)
			return p;
	}

	return 0;
}

CDccSession* CIrcProto::FindDCCRecvByPortAndName(int iPort, const wchar_t* szName)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_xfers.getCount(); i++) {
		CDccSession* p = m_dcc_xfers[i];
		DBVARIANT dbv;
		if (!getWString(p->di->hContact, "Nick", &dbv)) {
			if (p->di->iType == DCC_SEND && !p->di->bSender && !mir_wstrcmpi(szName, dbv.ptszVal) && iPort == p->di->iPort) {
				db_free(&dbv);
				return p;
			}
			db_free(&dbv);
		}
	}

	return 0;
}

CDccSession* CIrcProto::FindPassiveDCCSend(int iToken)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_xfers.getCount(); i++)
		if (m_dcc_xfers[i]->iToken == iToken)
			return m_dcc_xfers[i];

	return 0;
}

CDccSession* CIrcProto::FindPassiveDCCRecv(CMStringW sName, CMStringW sToken)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_xfers.getCount(); i++) {
		CDccSession *p = m_dcc_xfers[i];
		if (sToken == p->di->sToken && sName == p->di->sContactName)
			return p;
	}

	return 0;
}

void CIrcProto::DisconnectAllDCCSessions(bool Shutdown)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_chats.getCount(); i++)
		if (m_disconnectDCCChats || Shutdown)
			m_dcc_chats[i]->Disconnect();
}

void CIrcProto::CheckDCCTimeout(void)
{
	mir_cslock lck(m_dcc);

	for (int i = 0; i < m_dcc_chats.getCount(); i++) {
		CDccSession* p = m_dcc_chats[i];
		if (time(0) > p->tLastActivity + DCCCHATTIMEOUT)
			p->Disconnect();
	}

	for (int j = 0; j < m_dcc_xfers.getCount(); j++) {
		CDccSession* p = m_dcc_xfers[j];
		if (time(0) > p->tLastActivity + DCCSENDTIMEOUT)
			p->Disconnect();
	}
}

////////////////////////////////////////////////////////////////////

CIrcIgnoreItem::CIrcIgnoreItem(const wchar_t* _mask, const wchar_t* _flags, const wchar_t* _network) :
	mask(_mask),
	flags(_flags),
	network(_network)
{
}

CIrcIgnoreItem::CIrcIgnoreItem(int codepage, const char* _mask, const char* _flags, const char* _network) :
	mask((wchar_t*)_A2T(_mask, codepage)),
	flags((wchar_t*)_A2T(_flags, codepage)),
	network((wchar_t*)_A2T(_network, codepage))
{
}

CIrcIgnoreItem::~CIrcIgnoreItem()
{
}

////////////////////////////////////////////////////////////////////

CIrcSessionInfo::CIrcSessionInfo() :
	iPort(0),
	bIdentServer(false),
	iIdentServerPort(0)
{
}

CIrcSessionInfo::CIrcSessionInfo(const CIrcSessionInfo& si) :
	sServer(si.sServer),
	sServerName(si.sServerName),
	iPort(si.iPort),
	sNick(si.sNick),
	sUserID(si.sUserID),
	sFullName(si.sFullName),
	sPassword(si.sPassword),
	bIdentServer(si.bIdentServer),
	m_iSSL(si.m_iSSL),
	sIdentServerType(si.sIdentServerType),
	sNetwork(si.sNetwork),
	iIdentServerPort(si.iIdentServerPort)
{
}

void CIrcSessionInfo::Reset()
{
	sServer = "";
	sServerName = L"";
	iPort = 0;
	sNick = L"";
	sUserID = L"";
	sFullName = L"";
	sPassword = "";
	bIdentServer = false;
	bNickFlag = false;
	m_iSSL = 0;
	sIdentServerType = L"";
	iIdentServerPort = 0;
	sNetwork = L"";
}

////////////////////////////////////////////////////////////////////

void CIrcProto::OnIrcMessage(const CIrcMessage* pmsg)
{
	if (pmsg != NULL) {
		PfnIrcMessageHandler pfn = FindMethod(pmsg->sCommand.c_str());
		if (pfn) {
			// call member function. if it returns 'false',
			// call the default handling
			if (!(this->*pfn)(pmsg))
				OnIrcDefault(pmsg);
		}
		else // handler not found. call default handler
			OnIrcDefault(pmsg);
	}
	else OnIrcDisconnected();
}

PfnIrcMessageHandler CIrcProto::FindMethod(const wchar_t* lpszName)
{
	CIrcHandler temp(lpszName, NULL);
	CIrcHandler* p = m_handlers.find(&temp);
	return (p == NULL) ? NULL : p->m_handler;
}

////////////////////////////////////////////////////////////////////

#define IPC_ADDR_SIZE				4		/* Size of IP address, change for IPv6. */

char* ConvertIntegerToIP(unsigned long int_ip_addr)
{
	IN_ADDR intemp;
	IN_ADDR in;
	intemp.S_un.S_addr = int_ip_addr;

	in.S_un.S_un_b.s_b1 = intemp.S_un.S_un_b.s_b4;
	in.S_un.S_un_b.s_b2 = intemp.S_un.S_un_b.s_b3;
	in.S_un.S_un_b.s_b3 = intemp.S_un.S_un_b.s_b2;
	in.S_un.S_un_b.s_b4 = intemp.S_un.S_un_b.s_b1;

	return inet_ntoa(in);
}

unsigned long ConvertIPToInteger(char* IP)
{
	IN_ADDR in;
	IN_ADDR intemp;

	if (IP == 0 || mir_strlen(IP) == 0)
		return 0;

	intemp.S_un.S_addr = inet_addr(IP);

	in.S_un.S_un_b.s_b1 = intemp.S_un.S_un_b.s_b4;
	in.S_un.S_un_b.s_b2 = intemp.S_un.S_un_b.s_b3;
	in.S_un.S_un_b.s_b3 = intemp.S_un.S_un_b.s_b2;
	in.S_un.S_un_b.s_b4 = intemp.S_un.S_un_b.s_b1;
	return in.S_un.S_addr;
}

////////////////////////////////////////////////////////////////////

// initialize basic stuff needed for the dcc objects, also start a timer for checking the status of connections (timeouts)
CDccSession::CDccSession(CIrcProto* _pro, DCCINFO *pdci) :
	m_proto(_pro),
	NewFileName(0),
	dwWhatNeedsDoing(0),
	tLastPercentageUpdate(0),
	dwTotal(0),
	iGlobalToken(),
	dwResumePos(0),
	hEvent(0),
	con(0),
	hBindPort(0)
{
	tLastActivity = time(0);

	di = pdci; // Setup values passed to the constructor

	memset(&pfts, 0, sizeof(PROTOFILETRANSFERSTATUS));
	pfts.cbSize = sizeof(PROTOFILETRANSFERSTATUS);

	if (di->iType == DCC_SEND && di->bSender == false)
		hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	if (nDcc == 0)
		m_proto->SetChatTimer(m_proto->DCCTimer, 20 * 1000, DCCTimerProc);

	nDcc++; // increase the count of existing objects

	iGlobalToken++;
	if (iGlobalToken == 1000)
		iGlobalToken = 1;
	iToken = iGlobalToken;

	iPacketSize = m_proto->getWord("PacketSize", 4096);

	if (di->dwAdr)
		m_proto->setDword(di->hContact, "IP", di->dwAdr); // mtooltip stuff
}

CDccSession::~CDccSession() // destroy all that needs destroying
{
	if (di->iType == DCC_SEND) {
		// ack SUCCESS or FAILURE
		if (dwTotal == di->dwSize)
			ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, (void *)di, 0);
		else
			ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (void *)di, 0);
	}

	if (di->iType == DCC_CHAT) {
		CDccSession *dcc = m_proto->FindDCCSession(di->hContact);
		if (dcc && this == dcc) {
			m_proto->RemoveDCCSession(di->hContact); // objects automatically remove themselves from the list of objects
			m_proto->setWord(di->hContact, "Status", ID_STATUS_OFFLINE);
		}
	}

	if (di->iType == DCC_SEND)
		m_proto->RemoveDCCSession(di);

	if (hEvent != NULL) {
		SetEvent(hEvent);
		CloseHandle(hEvent);
		hEvent = NULL;
	}

	delete di;
	nDcc--;

	if (nDcc < 0)
		nDcc = 0;

	if (nDcc == 0)
		m_proto->KillChatTimer(m_proto->DCCTimer); // destroy the timer when no dcc objects remain
}

int CDccSession::NLSend(const unsigned char* buf, int cbBuf)
{
	tLastActivity = time(0);

	if (con)
		return Netlib_Send(con, (const char*)buf, cbBuf, di->iType == DCC_CHAT ? MSG_DUMPASTEXT : MSG_NODUMP);

	return 0;
}

int CDccSession::NLReceive(const unsigned char* buf, int cbBuf)
{
	int n = 0;

	if (con)
		n = Netlib_Recv(con, (char*)buf, cbBuf, di->iType == DCC_CHAT ? MSG_DUMPASTEXT : MSG_NODUMP);

	tLastActivity = time(0);
	return n;
}

int CDccSession::SendStuff(const wchar_t* fmt)
{
	CMStringA buf = _T2A(fmt, m_proto->getCodepage());
	return NLSend((const unsigned char*)buf.c_str(), buf.GetLength());
}

// called when the user wants to connect/create a new connection given the parameters in the constructor.
int CDccSession::Connect()
{
	if (!di->bSender || di->bReverse) {
		if (!con)
			mir_forkthread(ConnectProc, this); // spawn a new thread for time consuming activities, ie when connecting to a remote computer
		return true;
	}

	if (!con)
		return SetupConnection(); // no need to spawn thread for setting up a listening port locally

	return false;
}

void __cdecl CDccSession::ConnectProc(void *pparam)
{
	Thread_SetName("IRC: ConnectProc");

	CDccSession* pThis = (CDccSession*)pparam;
	if (!pThis->con)
		pThis->SetupConnection();
}

// small function to setup the address and port of the remote computer fror passive filetransfers
void CDccSession::SetupPassive(DWORD adress, DWORD port)
{
	di->dwAdr = adress;
	di->iPort = (int)port;

	m_proto->setDword(di->hContact, "IP", di->dwAdr); // mtooltip stuff
}

int CDccSession::SetupConnection()
{
	// if it is a dcc chat connection make sure it is "offline" to begin with, since no connection exists still
	if (di->iType == DCC_CHAT)
		m_proto->setWord(di->hContact, "Status", ID_STATUS_OFFLINE);

	// Set up stuff needed for the filetransfer dialog (if it is a filetransfer)
	if (di->iType == DCC_SEND) {
		file[0] = (wchar_t*)di->sFileAndPath.c_str();
		file[1] = 0;

		pfts.tszCurrentFile = (wchar_t*)di->sFileAndPath.c_str();
		pfts.tszWorkingDir = (wchar_t*)di->sPath.c_str();
		pfts.hContact = di->hContact;
		pfts.flags = PFTS_UNICODE + ((di->bSender) ? PFTS_SENDING : PFTS_RECEIVING);
		pfts.totalFiles = 1;
		pfts.currentFileNumber = 0;
		pfts.totalBytes = di->dwSize;
		pfts.currentFileSize = pfts.totalBytes;
		pfts.ptszFiles = file;
		pfts.totalProgress = 0;
		pfts.currentFileProgress = 0;
		pfts.currentFileTime = (unsigned long)time(0);
	}

	// create a listening socket for outgoing chat/send requests. The remote computer connects to this computer. Used for both chat and filetransfer.
	if (di->bSender && !di->bReverse) {
		NETLIBBIND nb = {};
		nb.cbSize = sizeof(NETLIBBIND);
		nb.pfnNewConnectionV2 = DoIncomingDcc; // this is the (helper) function to be called once an incoming connection is made. The 'real' function that is called is IncomingConnection()
		nb.pExtra = (void *)this;
		nb.wPort = 0;
		
		hBindPort = Netlib_BindPort(m_proto->hNetlibDCC, &nb);
		if (hBindPort == NULL) {
			delete this; // dcc objects destroy themselves when the connection has been closed or failed for some reasson.
			return 0;
		}

		di->iPort = nb.wPort; // store the port internally so it is possible to search for it (for resuming of filetransfers purposes)
		return nb.wPort; // return the created port so it can be sent to the remote computer in a ctcp/dcc command
	}

	// If a remote computer initiates a chat session this is used to connect to the remote computer (user already accepted at this point). 
	// also used for connecting to a remote computer for remote file transfers
	if (di->iType == DCC_CHAT && !di->bSender || di->iType == DCC_SEND && di->bSender && di->bReverse) {
		NETLIBOPENCONNECTION ncon = { 0 };
		ncon.cbSize = sizeof(ncon);
		ncon.szHost = ConvertIntegerToIP(di->dwAdr);
		ncon.wPort = (WORD)di->iPort;
		con = Netlib_OpenConnection(m_proto->hNetlibDCC, &ncon);
	}

	// If a remote computer initiates a filetransfer this is used to connect to that computer (the user has chosen to accept but it is possible the file exists/needs to be resumed etc still)
	if (di->iType == DCC_SEND && !di->bSender) {

		// this following code is for miranda to be able to show the resume/overwrite etc dialog if the file that we are receiving already exists. 
		// It must be done before we create the connection or else the other party will begin sending packets while the user is still deciding if 
		// s/he wants to resume/cancel or whatever. Just the way dcc is...

		// if the file exists (dialog is shown) WaitForSingleObject() till the dialog is closed and PS_FILERESUME has been processed. 
		// dwWhatNeedsDoing will be set using InterlockedExchange() (from other parts of the code depending on action) before SetEvent() is called.
		// If the user has chosen rename then InterlockedExchange() will be used for setting NewFileName to a string containing the new name.
		// Furthermore dwResumePos will be set using InterlockedExchange() to indicate what the file position to start from is.
		if (ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_FILERESUME, (void *)di, (LPARAM)&pfts)) {
			WaitForSingleObject(hEvent, INFINITE);
			switch (dwWhatNeedsDoing) {
			case FILERESUME_RENAME:
				// If the user has chosen to rename the file we need to change variables accordingly. NewFileName has been set using
				// InterlockedExchange()
				if (NewFileName) { // the user has chosen to rename the new incoming file.
					di->sFileAndPath = NewFileName;

					int i = di->sFileAndPath.ReverseFind('\\');
					if (i != -1) {
						di->sPath = di->sFileAndPath.Mid(0, i + 1);
						di->sFile = di->sFileAndPath.Mid(i + 1);
					}

					pfts.tszCurrentFile = di->sFileAndPath.GetBuffer();
					pfts.tszWorkingDir = di->sPath.GetBuffer();
					pfts.totalBytes = di->dwSize;
					pfts.currentFileSize = pfts.totalBytes;

					delete[] NewFileName;
					NewFileName = NULL;
				}
				break;

			case FILERESUME_OVERWRITE:
			case FILERESUME_RESUME:
				// no action needed at this point, just break out of the switch statement
				break;

			case FILERESUME_CANCEL:
				return FALSE;

			case FILERESUME_SKIP:
			default:
				delete this; // per usual dcc objects destroy themselves when they fail or when connection is closed
				return FALSE;
			}
		}

		// hack for passive filetransfers
		if (di->iType == DCC_SEND && !di->bSender && di->bReverse) {
			NETLIBBIND nb = {};
			nb.cbSize = sizeof(NETLIBBIND);
			nb.pfnNewConnectionV2 = DoIncomingDcc; // this is the (helper) function to be called once an incoming connection is made. The 'real' function that is called is IncomingConnection()
			nb.pExtra = (void *)this;
			nb.wPort = 0;

			hBindPort = Netlib_BindPort(m_proto->hNetlibDCC, &nb);
			if (hBindPort == NULL) {
				m_proto->DoEvent(GC_EVENT_INFORMATION, 0, m_proto->m_info.sNick.c_str(), LPGENW("DCC ERROR: Unable to bind local port for passive file transfer"), NULL, NULL, NULL, true, false);
				delete this; // dcc objects destroy themselves when the connection has been closed or failed for some reasson.
				return 0;
			}

			di->iPort = nb.wPort; // store the port internally so it is possible to search for it (for resuming of filetransfers purposes)

			CMStringW sFileWithQuotes = di->sFile;

			// if spaces in the filename surround with quotes
			if (sFileWithQuotes.Find(' ', 0) != -1) {
				sFileWithQuotes.Insert(0, L"\"");
				sFileWithQuotes.Insert(sFileWithQuotes.GetLength(), L"\"");
			}

			// send out DCC RECV command for passive filetransfers
			unsigned long ulAdr = 0;
			if (m_proto->m_manualHost)
				ulAdr = ConvertIPToInteger(m_proto->m_mySpecifiedHostIP);
			else
				ulAdr = m_proto->m_IPFromServer ? ConvertIPToInteger(m_proto->m_myHost) : nb.dwExternalIP;

			if (di->iPort && ulAdr)
				m_proto->PostIrcMessage(L"/CTCP %s DCC SEND %s %u %u %I64u %s", di->sContactName.c_str(), sFileWithQuotes.c_str(), ulAdr, di->iPort, di->dwSize, di->sToken.c_str());

			return TRUE;
		}

		// connect to the remote computer from which you are receiving the file (now all actions to take (resume/overwrite etc) have been decided
		NETLIBOPENCONNECTION ncon = { 0 };
		ncon.cbSize = sizeof(ncon);
		ncon.szHost = ConvertIntegerToIP(di->dwAdr);
		ncon.wPort = (WORD)di->iPort;

		con = Netlib_OpenConnection(m_proto->hNetlibDCC, &ncon);
	}

	// if for some reason the plugin has failed to connect to the remote computer the object is destroyed.
	if (con == NULL) {
		delete this;
		return FALSE; // failed to connect
	}

	// if it is a chat connection set the user to online now since we now know there is a connection
	if (di->iType == DCC_CHAT)
		m_proto->setWord(di->hContact, "Status", ID_STATUS_ONLINE);

	// spawn a new thread to handle receiving/sending of data for the new chat/filetransfer connection to the remote computer
	mir_forkthread(ThreadProc, this);

	return con != NULL;
}

// called by netlib for incoming connections on a listening socket (chat/filetransfer)
int CDccSession::IncomingConnection(HANDLE hConnection, DWORD dwIP)
{
	con = hConnection;
	if (con == NULL) {
		delete this;
		return false; // failed to connect
	}

	m_proto->setDword(di->hContact, "IP", dwIP); // mToolTip stuff

	if (di->iType == DCC_CHAT)
		m_proto->setWord(di->hContact, "Status", ID_STATUS_ONLINE); // set chat to online

	// same as above, spawn a new thread to handle receiving/sending of data for the new incoming chat/filetransfer connection  
	mir_forkthread(ThreadProc, this);
	return true;
}

// here we decide which function to use for communicating with the remote computer, depending on connection type
void __cdecl CDccSession::ThreadProc(void *pparam)
{
	Thread_SetName("IRC: CDccSession::ThreadProc");

	CDccSession* pThis = (CDccSession*)pparam;

	// if it is an incoming connection on a listening port, then we should close the listenting port so only one can connect (the one you offered
	// the connection to) can connect and not evil IRCopers with haxxored IRCDs
	if (pThis->hBindPort) {
		Netlib_CloseHandle(pThis->hBindPort);
		pThis->hBindPort = NULL;
	}

	if (pThis->di->iType == DCC_CHAT)
		pThis->DoChatReceive(); // dcc chat

	else if (!pThis->di->bSender)
		pThis->DoReceiveFile(); // receive a file

	else if (pThis->di->bSender)
		pThis->DoSendFile(); // send a file
}

// this is done when the user is initiating a filetransfer to a remote computer
void CDccSession::DoSendFile()
{
	// initialize the filetransfer dialog
	ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, (void *)di, 0);
	ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, (void *)di, 0);

	WORD wPacketSize = m_proto->getWord("DCCPacketSize", 1024 * 4);

	if (wPacketSize < 256)
		wPacketSize = 256;

	if (wPacketSize > 32 * 1024)
		wPacketSize = 32 * 1024;

	BYTE* chBuf = new BYTE[wPacketSize + 1];

	// is there a connection?
	if (con) {
		// open the file for reading
		int hFile = _wopen(di->sFileAndPath.c_str(), _O_RDONLY | _O_BINARY, _S_IREAD);
		if (hFile >= 0) {
			unsigned __int64 dwLastAck = 0;

			// if the user has chosen to resume a file, dwResumePos will contain a value (set using InterlockedExchange())
			// and then the variables and the file pointer are changed accordingly.
			if (dwResumePos && dwWhatNeedsDoing == FILERESUME_RESUME) {
				_lseeki64(hFile, dwResumePos, SEEK_SET);
				dwTotal = dwResumePos;
				dwLastAck = dwResumePos;
				pfts.totalProgress = dwResumePos;
				pfts.currentFileProgress = dwResumePos;
			}

			// initial ack to set the 'percentage-ready meter' to the correct value
			ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (void *)di, (LPARAM)&pfts);

			tLastActivity = time(0);

			// create a packet receiver to handle receiving ack's from the remote computer.
			HANDLE hPackrcver = Netlib_CreatePacketReceiver(con, sizeof(DWORD));

			NETLIBPACKETRECVER npr = {};
			npr.dwTimeout = 60 * 1000;
			npr.bufferSize = sizeof(DWORD);

			// until the connection is dropped it will spin around in this while() loop
			while (con) {
				// read a packet
				int iRead = _read(hFile, chBuf, wPacketSize);
				if (iRead <= 0)
					break; // break out if everything has already been read

				// send the package
				int cbSent = NLSend((unsigned char*)chBuf, iRead);
				if (cbSent <= 0)
					break; // break out if connection is lost or a transmission error has occured

				if (!con)
					break;

				dwTotal += cbSent;

				// block connection and receive ack's from remote computer (if applicable)
				if (m_proto->m_DCCMode == 0) {
					DWORD dwRead = 0;
					DWORD dwPacket = NULL;

					do {
						dwRead = Netlib_GetMorePackets(hPackrcver, &npr);
						npr.bytesUsed = sizeof(DWORD);

						if (dwRead <= 0)
							break; // connection closed, or a timeout occurred.

						dwPacket = *(DWORD*)npr.buffer;
						dwLastAck = ntohl(dwPacket);

					}
					while (con && dwTotal != dwLastAck);

					if (!con || dwRead <= 0)
						goto DCC_STOP;
				}

				if (m_proto->m_DCCMode == 1) {
					DWORD dwRead = 0;
					DWORD dwPacket = 0;

					do {
						dwRead = Netlib_GetMorePackets(hPackrcver, &npr);
						npr.bytesUsed = sizeof(DWORD);
						if (dwRead <= 0)
							break; // connection closed, or a timeout occurred.

						dwPacket = *(DWORD*)npr.buffer;
						dwLastAck = ntohl(dwPacket);
					}
					while (con && (di->dwSize != dwTotal
					&& dwTotal - dwLastAck >= 100 * 1024
					|| di->dwSize == dwTotal // get the last packets when the whole file has been sent
					&& dwTotal != dwLastAck));

					if (!con || dwRead <= 0)
						goto DCC_STOP;
				}

				// update the filetransfer dialog's 'percentage-ready meter' once per second only to save cpu
				if (tLastPercentageUpdate < time(0)) {
					tLastPercentageUpdate = time(0);
					pfts.totalProgress = dwTotal;
					pfts.currentFileProgress = dwTotal;
					ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (void *)di, (LPARAM)&pfts);
				}

				// close the connection once the whole file has been sent an completely ack'ed
				if (dwLastAck >= di->dwSize) {
					Netlib_CloseHandle(con);
					con = NULL;
				}
			}

		DCC_STOP:
			// need to close the connection if it isn't allready
			if (con) {
				Netlib_CloseHandle(con);
				con = NULL;
			}

			// ack the progress one final time
			tLastActivity = time(0);
			pfts.totalProgress = dwTotal;
			pfts.currentFileProgress = dwTotal;
			ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (void *)di, (LPARAM)&pfts);

			_close(hFile);
		}
		else { // file was not possible to open for reading
			ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (void *)di, 0);
			if (con) {
				Netlib_CloseHandle(con);
				con = NULL;
			}
		}
	}

	delete[]chBuf;
	delete this; // ... and hopefully all went right, cuz here the object is deleted in any case
}

// This is called when receiving a file from a remote computer.
void CDccSession::DoReceiveFile()
{
	// initialize the filetransfer dialog
	ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, (void *)di, 0);

	BYTE chBuf[1024 * 32 + 1];

	// do some stupid thing so  the filetransfer dialog shows the right thing
	ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, (void *)di, 0);

	// open the file for writing (and reading in case it is a resume)
	int hFile = _wopen(di->sFileAndPath.c_str(),
		(dwWhatNeedsDoing == FILERESUME_RESUME ? _O_APPEND : _O_TRUNC | _O_CREAT) | _O_RDWR | _O_BINARY,
		_S_IREAD | _S_IWRITE);
	if (hFile >= 0) {
		unsigned __int64 dwLastAck = 0;

		// dwResumePos and dwWhatNeedsDoing has possibly been set using InterlockedExchange()
		// if set it is a resume and we adjust variables and the file pointer accordingly.
		if (dwResumePos && dwWhatNeedsDoing == FILERESUME_RESUME) {
			_lseeki64(hFile, dwResumePos, SEEK_SET);
			dwTotal = dwResumePos;
			dwLastAck = dwResumePos;
			pfts.totalProgress = dwResumePos;
			pfts.currentFileProgress = dwResumePos;
		}

		// send an initial ack for the percentage-ready meter
		ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (void *)di, (LPARAM)&pfts);

		// the while loop will spin around till the connection is dropped, locally or by the remote computer.
		while (con) {
			// read
			int cbRead = NLReceive((unsigned char*)chBuf, sizeof(chBuf));
			if (cbRead <= 0)
				break;

			// write it to the file
			_write(hFile, chBuf, cbRead);

			dwTotal += cbRead;

			// this snippet sends out an ack for every 4 kb received in send ahead
			// or every packet for normal mode
			if (!di->bTurbo) {
				DWORD no = dwTotal;
				no = htonl(no);
				NLSend((unsigned char *)&no, sizeof(DWORD));
				dwLastAck = dwTotal;
			}
			else dwLastAck = dwTotal;

			// sets the 'last update time' to check for timed out connections, and also make sure we only
			// ack the 'percentage-ready meter' only once a second to save CPU.
			if (tLastPercentageUpdate < time(0)) {
				tLastPercentageUpdate = time(0);
				pfts.totalProgress = dwTotal;
				pfts.currentFileProgress = dwTotal;
				ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (void *)di, (LPARAM)&pfts);
			}

			// if file size is known and everything is received then disconnect 
			if (di->dwSize && di->dwSize == dwTotal) {
				Netlib_CloseHandle(con);
				con = NULL;
			}
		}
		// receiving loop broken locally or by remote computer, just some cleaning up left....

		pfts.totalProgress = dwTotal;
		pfts.currentFileProgress = dwTotal;
		ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_DATA, (void *)di, (LPARAM)&pfts);
		_close(hFile);
	}
	else {
		ProtoBroadcastAck(m_proto->m_szModuleName, di->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (void *)di, 0);
		if (con) { // file not possible to open for writing so we ack FAILURE and close the handle
			Netlib_CloseHandle(con);
			con = NULL;
		}
	}

	delete this; // and finally the object is deleted
}

// this function handles receiving text in dcc chats and then send it in the protochain. very uncomplicated...
// For sending text the SendStuff() function is called (with the help of some function in CIrcProto to find the right
// Dcc object). See CIrcProto for info on how the dcc objects are stored, retrieved and deleted.

void CDccSession::DoChatReceive()
{
	char chBuf[1024 * 4 + 1];
	int cbInBuf = 0;

	// loop to spin around while there is a connection
	while (con) {
		int cbRead;
		int nLinesProcessed = 0;

		cbRead = NLReceive((unsigned char*)chBuf + cbInBuf, sizeof(chBuf)-cbInBuf - 1);
		if (cbRead <= 0)
			break;

		cbInBuf += cbRead;
		chBuf[cbInBuf] = '\0';

		char* pStart = chBuf;
		while (*pStart) {
			char* pEnd;

			// seek end-of-line
			for (pEnd = pStart; *pEnd && *pEnd != '\r' && *pEnd != '\n'; ++pEnd)
				;
			if (*pEnd == '\0')
				break; // uncomplete message. stop parsing.

			++nLinesProcessed;

			// replace end-of-line with NULLs and skip
			while (*pEnd == '\r' || *pEnd == '\n')
				*pEnd++ = '\0';

			if (*pStart) {
				// send it off to some messaging module
				PROTORECVEVENT pre = { 0 };
				pre.timestamp = (DWORD)time(NULL);
				pre.szMessage = pStart;
				ProtoChainRecvMsg(di->hContact, &pre);
			}

			cbInBuf -= pEnd - pStart;
			pStart = pEnd;
		}

		// discard processed messages
		if (nLinesProcessed != 0)
			memmove(chBuf, pStart, cbInBuf + 1);
	}

	delete this; // delete the object when the connection is dropped
}

// disconnect the stuff
int CDccSession::Disconnect()
{
	if (hBindPort) {
		Netlib_CloseHandle(hBindPort);
		hBindPort = NULL;
	}

	// if 'con' exists it is cuz a connection exists. 
	// Terminating 'con' will cause any spawned threads to die and then the object will destroy itself.
	if (con) {
		Netlib_CloseHandle(con);
		con = NULL;
	}
	else delete this; // if 'con' do not exist (no connection made so far from the object) the object is destroyed

	return TRUE;
}

////////////////////////////////////////////////////////////////////
// check if the dcc chats should disconnect ( default 5 minute timeout )

VOID CALLBACK DCCTimerProc(HWND, UINT, UINT_PTR idEvent, DWORD)
{
	CIrcProto *ppro = GetTimerOwner(idEvent);
	if (ppro)
		ppro->CheckDCCTimeout();
}

// helper function for incoming dcc connections.
void DoIncomingDcc(HANDLE hConnection, DWORD dwRemoteIP, void * p1)
{
	CDccSession *dcc = (CDccSession*)p1;
	dcc->IncomingConnection(hConnection, dwRemoteIP);
}

// ident server

void DoIdent(HANDLE hConnection, DWORD, void* extra)
{
	CIrcProto *ppro = (CIrcProto*)extra;

	char szBuf[1024];
	int cbTotal = 0;

	while (true) {
		int cbRead = Netlib_Recv(hConnection, szBuf + cbTotal, sizeof(szBuf)-1 - cbTotal, 0);
		if (cbRead == SOCKET_ERROR || cbRead == 0)
			break;

		cbTotal += cbRead;
		szBuf[cbTotal] = '\0';

LBL_Parse:
		char* EOLPos = strstr(szBuf, "\r\n");
		if (EOLPos == NULL)
			continue;

		EOLPos[0] = EOLPos[1] = '\0';
		rtrim(szBuf);
		ppro->debugLogA("Got Ident request: %s", szBuf);

		unsigned int PeerPortNrRcvd = 0, LocalPortNrRcvd = 0;
		int iParamCnt = sscanf(szBuf, "%d , %d", &LocalPortNrRcvd, &PeerPortNrRcvd);

		int cbLen = 0;
		char buf[1024 * 4];

		if (iParamCnt != 2)
			cbLen = mir_snprintf(buf, "%s : ERROR : UNKNOWN-ERROR\r\n", szBuf);
		else {
			for (int i = 0; i < g_Instances.getCount(); i++) {
				if (PeerPortNrRcvd == g_Instances[i]->m_info.iPort && LocalPortNrRcvd == g_Instances[i]->m_myLocalPort) {
					cbLen = mir_snprintf(buf, "%s : USERID : %S : %S\r\n",
						szBuf, g_Instances[i]->m_info.sIdentServerType.c_str(), g_Instances[i]->m_info.sUserID.c_str());
					break;
				}
			}

			if (cbLen == 0)
				cbLen = mir_snprintf(buf, "%s : ERROR : INVALID-PORT\r\n", szBuf);
		}

		if (Netlib_Send(hConnection, (const char*)buf, cbLen, 0) > 0)
			ppro->debugLogA("Sent Ident answer: %s", buf);
		else
			ppro->debugLogA("Sending Ident answer failed.");

		if (ppro->m_identTimer)
			break;

		cbTotal -= EOLPos + 2 - szBuf;
		strdel(szBuf, int(EOLPos + 2 - szBuf));
		goto LBL_Parse;
	}
	Netlib_CloseHandle(hConnection);
}