-
Notifications
You must be signed in to change notification settings - Fork 76
/
chatsock.cpp
1738 lines (1378 loc) · 53 KB
/
chatsock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// chatsock.cpp : implementation file
//
// But does it get goat's blood out?
#include "stdafx.h"
#include "MUSHclient.h"
#include "mainfrm.h"
#include "doc.h"
#include <stddef.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#define CHAT_DEBUG 0
IMPLEMENT_DYNAMIC(CChatSocket, CAsyncSocket)
CChatSocket::CChatSocket(CMUSHclientDoc* pDoc)
{
m_pDoc = pDoc;
m_hNameLookup = NULL;
m_pGetHostStruct = NULL;
ZeroMemory (&m_ServerAddr, sizeof m_ServerAddr);
m_bDeleteMe = false;
m_bIncoming = false;
m_bIgnore = false;
m_bCanSnoop = false;
m_bYouAreSnooping = false;
m_bHeIsSnooping = false;
m_bCanSendCommands = false;
m_bPrivate = false;
m_bCanSendFiles = false;
m_bDoingFileTransfer = false;
m_bWasConnected = false;
m_iChatStatus = eChatClosed;
m_iChatConnectionType = eChatMudMaster;
m_iUserOption = 0;
// session ID
if (++m_pDoc->m_iNextChatID > LONG_MAX)
m_pDoc->m_iNextChatID = 1; // wrap around
m_iChatID = m_pDoc->m_iNextChatID; // unique session ID
// zChat
SHS_INFO shsInfo;
MakeRandomNumber (m_pDoc, shsInfo);
m_zChatStamp = shsInfo.digest [0];
m_zChatStatus = 1; // normal status
// times
m_tWhenStarted = CTime::GetCurrentTime();
m_tLastIncoming = 0;
m_tLastOutgoing = 0;
m_iPingStartTime.QuadPart = 0;
m_fLastPingTime = 0.0;
// their address, port
m_iAllegedPort = DEFAULT_CHAT_PORT;
m_strAllegedAddress = "<Unknown>";
// file stuff
m_bDoingFileTransfer = false;
m_bSendFile = false;
m_iFileSize = 0;
m_iFileBlocks = 0;
m_iBlocksTransferred = 0;
m_pFile = NULL;
m_pFileBuffer = NULL;
m_tStartedFileTransfer = 0;
m_iFileBlockSize = 500;
// zero counters
m_iCountIncomingPersonal = 0;
m_iCountIncomingAll = 0;
m_iCountIncomingGroup = 0;
m_iCountOutgoingPersonal = 0;
m_iCountOutgoingAll = 0;
m_iCountOutgoingGroup = 0;
m_iCountMessages = 0;
m_iCountFileBytesIn = 0;
m_iCountFileBytesOut = 0;
}
CChatSocket::~CChatSocket()
{
StopFileTransfer (true);
// cancel pending host name lookup
if (m_hNameLookup)
WSACancelAsyncRequest (m_hNameLookup); // cancel host name lookup in progress
delete [] m_pGetHostStruct;
ShutDownSocket (*this);
// if he was ever connected, we will tell our plugins he has gone
if (m_bWasConnected)
{
// tell each plugin about the departing user
m_pDoc->SendToAllPluginCallbacks (ON_PLUGIN_CHAT_USERDISCONNECT,
m_iChatID, // user ID
string (m_strRemoteUserName),
false, false);
} // end of needing to notify about him
}
void CChatSocket::StopFileTransfer (const bool bAbort)
{
if (!m_bDoingFileTransfer || m_pDoc == NULL)
return;
delete m_pFile; // close file
m_pFile = NULL;
delete [] m_pFileBuffer;
m_pFileBuffer = NULL;
if (bAbort)
{
// tell them to cancel it
if (m_iChatStatus == eChatConnected)
SendChatMessage (CHAT_FILE_CANCEL, "");
// half-received - delete it
if (m_bSendFile)
m_pDoc->ChatNote (eChatFile,
CFormat (_T("Aborted sending file %s"), (LPCTSTR) m_strOurFileName));
else
{
m_pDoc->ChatNote (eChatFile,
CFormat (_T("Aborted receiving file %s"), (LPCTSTR) m_strOurFileName));
m_pDoc->ChatNote (eChatFile,
CFormat (_T("File %s deleted."), (LPCTSTR) m_strOurFileName));
CFile::Remove (m_strOurFileName);
}
}
m_bDoingFileTransfer = false;
m_bSendFile = false;
m_iFileSize = 0;
m_iFileBlocks = 0;
m_iBlocksTransferred = 0;
m_tStartedFileTransfer = 0;
} // end of CChatSocket::StopFileTransfer
void CChatSocket::OnReceive(int nErrorCode)
{
char buff [1000];
int count = Receive (buff, sizeof (buff) - 1);
if (count == SOCKET_ERROR)
{
OnClose (GetLastError ());
return;
}
if (count <= 0)
return;
m_tLastIncoming = CTime::GetCurrentTime();
switch (m_iChatStatus)
{
case eChatAwaitingConnectConfirm:
{
#if CHAT_DEBUG
m_pDoc->Note (TFormat (_T("Incoming packet on %i: \"%s\""),
m_iChatID,
(LPCTSTR) CString (buff, count)));
#endif
if (count < 5 || memcmp (buff, "YES:", 4) != 0)
{
m_pDoc->ChatNote (eChatConnection,
_T("Server rejected chat session attempt."));
OnClose (0);
return;
} // end of not being accepted
// get rid of "YES:" (4 bytes)
memmove (buff, &buff [4], count - 4);
count -= 4;
CString strName = CString (buff, count);
int i = strName.Find ('\n');
if (i == -1)
m_strRemoteUserName = strName;
else
m_strRemoteUserName = strName.Left (i);
m_pDoc->ChatNote (eChatSession,
CFormat (_T("Chat session accepted, remote server: \"%s\""),
(LPCTSTR) m_strRemoteUserName));
if (i == -1)
count = 0;
else
{ // get rid of initial acceptance message
i++; // get rid of \n too
memmove (buff, &buff [i], count - i);
count -= i;
}
// connected now
m_iChatStatus = eChatConnected;
m_bWasConnected = true;
// identify ourselves
CString strVersion = "MUSHclient v";
strVersion += MUSHCLIENT_VERSION;
SendChatMessage (CHAT_VERSION, strVersion);
if (m_iChatConnectionType == eChatZMud)
{
SendChatMessage (CHAT_STATUS, (char) 1); // normal status
SendChatMessage (CHAT_STAMP, MakeStamp (m_zChatStamp)); // send it
}
// tell each plugin about the new user
m_pDoc->SendToAllPluginCallbacks (ON_PLUGIN_CHAT_NEWUSER,
m_iChatID, // user ID
string (m_strRemoteUserName),
false,
false);
if (count <= 0)
return; // only the negotiation text here
}
break; // end of eChatAwaitingConnectConfirm
case eChatAwaitingConnectionRequest:
#if CHAT_DEBUG
m_pDoc->Note (TFormat ("Incoming packet on %i: \"%s\"",
m_iChatID,
(LPCTSTR) CString (buff, count)));
#endif
if (count < 7 || !(memcmp (buff, "CHAT:", 5) == 0 ||
memcmp (buff, "ZCHAT:", 6) == 0))
{
m_pDoc->ChatNote (eChatConnection,
_T("Unexpected chat negotiation."));
SendData ("NO");
OnClose (0);
return;
} // end of not accepting it
// get rid of "CHAT:" (5 bytes)
if (memcmp (buff, "CHAT:", 5) == 0)
{
memmove (buff, &buff [5], count - 5);
count -= 5;
m_iChatConnectionType = eChatMudMaster;
}
else
{
memmove (buff, &buff [6], count - 6);
count -= 6;
m_iChatConnectionType = eChatZMud;
m_iFileBlockSize = 1024; // larger size for zChat
}
CString strName = CString (buff, count);
int i = strName.Find ('\n');
if (i == -1)
m_strRemoteUserName = strName;
else
m_strRemoteUserName = strName.Left (i);
// zChat has a tab after the name
int iTab = m_strRemoteUserName.Find ('\t');
if (iTab != -1)
m_strRemoteUserName = m_strRemoteUserName.Left (iTab);
if (i == -1)
count = 0;
else
{ // get rid of initial connection message
i++; // get rid of \n too
memmove (buff, &buff [i], count - i);
count -= i;
}
CString strRest = CString (buff, count);
// throw away zChat security stuff (after the next newline)
i = strRest.Find ('\n');
if (i != -1)
strRest = strRest.Left (i);
if (strRest.GetLength () > 5)
{
m_iAllegedPort = atoi (strRest.Right (5));
m_strAllegedAddress = strRest.Left (strRest.GetLength () - 5);
}
count = 0; // can't see how this packet can be reasonably terminated
if (!m_pDoc->SendToAllPluginCallbacks (ON_PLUGIN_CHAT_ACCEPT,
CFormat ("%s,%s",
(LPCTSTR) inet_ntoa (m_ServerAddr.sin_addr),
(LPCTSTR) m_strRemoteUserName
),
true)) // stop on false response
{
// tell them our rejection
SendData ("NO");
OnClose (0);
return; // false means plugin rejects him
}
if (m_pDoc->m_bValidateIncomingCalls)
{
m_pDoc->Activate ();
if (::UMessageBox (
TFormat ("Incoming chat call to world %s from %s, IP address: %s.\n\nAccept it?",
(LPCTSTR) m_pDoc->m_mush_name,
(LPCTSTR) m_strRemoteUserName,
(LPCTSTR) inet_ntoa (m_ServerAddr.sin_addr)),
MB_YESNO) != IDYES)
{
// tell them our rejection
SendData ("NO");
OnClose (0);
return;
}
} // end of needing to validate calls
m_pDoc->ChatNote (eChatSession,
TFormat ("Chat session accepted, remote user: \"%s\"",
(LPCTSTR) m_strRemoteUserName));
// tell them our acceptance, and chat name
SendData (CFormat ("YES:%s\n", m_pDoc->m_strOurChatName));
// connected now
m_iChatStatus = eChatConnected;
m_bWasConnected = true;
// identify ourselves
CString strVersion = "MUSHclient v";
strVersion += MUSHCLIENT_VERSION;
SendChatMessage (CHAT_VERSION, strVersion);
if (m_iChatConnectionType == eChatZMud)
{
SendChatMessage (CHAT_STATUS, (char) 1); // normal status
SendChatMessage (CHAT_STAMP, MakeStamp (m_zChatStamp)); // send it
}
// tell each plugin about the new user
m_pDoc->SendToAllPluginCallbacks (ON_PLUGIN_CHAT_NEWUSER,
m_iChatID, // user ID
string (m_strRemoteUserName),
false,
false);
if (count <= 0)
return; // only the negotiation text here
break; // end of eChatAwaitingConnectionRequest
} // end of switch on m_iChatStatus
// now take the incoming text and break into blocks
m_outstanding_input += CString (buff, count); // add to any left over
while (!m_bDeleteMe && !m_outstanding_input.IsEmpty ())
{
switch (m_iChatConnectionType)
{
case eChatMudMaster:
{
// file blocks are fixed length and might contain nulls
if (m_outstanding_input [0] == CHAT_FILE_BLOCK)
{
int iLength = m_outstanding_input.GetLength ();
// must have file block size + 2 (command and terminator)
// otherwise we will get them later
if (iLength < (m_iFileBlockSize + 2))
return;
// make a buffer of exactly the file data
CString strBuffer = m_outstanding_input.Mid (1, m_iFileBlockSize);
// discard the file block and message number and terminator byte
m_outstanding_input = m_outstanding_input.Mid (m_iFileBlockSize + 2);
// now process the incoming file block
ProcessChatMessage (CHAT_FILE_BLOCK, strBuffer);
} // end of file block
else
{ // not a file block - use variable-length terminator
int iTerminator = m_outstanding_input.Find ((char) CHAT_END_OF_COMMAND);
// if no terminator, wait for one to arrive in the next packet
if (iTerminator == -1)
return;
CString strChatMessage = m_outstanding_input.Left (iTerminator);
m_outstanding_input = m_outstanding_input.Mid (iTerminator + 1);
ProcessChatMessage ((unsigned char) strChatMessage [0], strChatMessage.Mid (1));
} // end of not file block
} // end of MudMaster chat type
break;
case eChatZMud:
{
int iInputLength = m_outstanding_input.GetLength ();
// must have 4 bytes (command:2, and length:2) or we cannot have a packet
if (iInputLength < 4)
return; // wait for them
int iCommand = (int) (unsigned char) m_outstanding_input [0] |
(((int) (unsigned char) m_outstanding_input [1]) << 8);
int iLength = (int) (unsigned char) m_outstanding_input [2] |
(((int) (unsigned char) m_outstanding_input [3]) << 8);
if (iInputLength < (iLength + 4))
return; // whole block has not arrived yet
CString strChatMessage = m_outstanding_input.Mid (4, iLength);
m_outstanding_input = m_outstanding_input.Mid (iLength + 4);
ProcessChatMessage (iCommand, strChatMessage);
} // end of zChat chat type
break;
} // end of switch on chat connection type
} // end of extracting messages from the incoming stream
}
void CChatSocket::OnSend(int nErrorCode)
{
int count;
// receive nothing if shutting down
if (m_bDeleteMe)
return;
if (nErrorCode) // had an error, give up!
return;
// if we have outstanding data to send, do it
if (m_outstanding_output.GetLength () <= 0)
return;
count = Send (m_outstanding_output, m_outstanding_output.GetLength ());
if (count != SOCKET_ERROR)
m_pDoc->m_nBytesOut += count; // count bytes out
if (count > 0) // good send - do rest later
m_outstanding_output = m_outstanding_output.Mid (count);
else
{
int nError = GetLastError ();
if (count == SOCKET_ERROR && nError != WSAEWOULDBLOCK)
{
m_pDoc->ChatNote (eChatConnection,
TFormat ("Unable to send to \"%s\", code = %i (%s)",
(LPCTSTR) m_strServerName,
nError,
m_pDoc->GetSocketError (nError)));
ShutDownSocket (*this);
m_outstanding_output.Empty ();
OnClose (nError); // ????
} // end of an error other than "would block"
} // end of an error
}
void CChatSocket::OnClose(int nErrorCode)
{
if (m_iChatStatus == eChatConnected)
m_pDoc->ChatNote (eChatSession,
CFormat ("Chat session to %s closed.",
(LPCTSTR) m_strRemoteUserName));
m_iChatStatus = eChatClosed;
m_bDeleteMe = true;
} // end of OnClose
void CChatSocket::OnConnect(int nErrorCode)
{
if (nErrorCode != 0)
{
m_pDoc->ChatNote (eChatConnection,
TFormat ("Unable to connect to \"%s\", code = %i (%s)",
(LPCTSTR) m_strServerName,
nErrorCode,
m_pDoc->GetSocketError (nErrorCode)));
OnClose (nErrorCode);
return;
}
m_pDoc->ChatNote (eChatSession,
TFormat ("Session established to %s.",
(LPCTSTR) m_strServerName));
CString strHostName;
CString strAddresses;
GetHostNameAndAddresses (strHostName, strAddresses);
// if more than one IP address, take first
int i = strAddresses.Find (',');
if (i != -1)
strAddresses = strAddresses.Left (i);
// tell them our chat name, IP address, incoming port
if (m_iChatConnectionType == eChatZMud)
SendData (CFormat ("ZCHAT:%s\t\n%s%05u",
m_pDoc->m_strOurChatName, strAddresses, m_pDoc->m_IncomingChatPort));
else
SendData (CFormat ("CHAT:%s\n%s%-5u",
m_pDoc->m_strOurChatName, strAddresses, m_pDoc->m_IncomingChatPort));
m_iChatStatus = eChatAwaitingConnectConfirm;
} // end of OnConnect
void CChatSocket::HostNameResolved (WPARAM wParam, LPARAM lParam)
{
m_hNameLookup = NULL; // handle not needed now
if (WSAGETASYNCERROR (lParam))
{
m_pDoc->ChatNote (eChatConnection,
TFormat ("Chat session cannot resolve host name: %s.",
(LPCTSTR) m_strServerName));
m_bDeleteMe = true;
m_iChatStatus = eChatClosed;
return;
} // end of error in host name lookup
struct hostent * pHostent = (struct hostent * ) m_pGetHostStruct;
m_ServerAddr.sin_addr.s_addr = ((LPIN_ADDR)pHostent->h_addr)->s_addr;
delete [] m_pGetHostStruct; // delete buffer used by host name lookup
m_pGetHostStruct = NULL;
// we know the address - get on with it
MakeCall ();
} // end of HostNameResolved
void CChatSocket::MakeCall (void)
{
// the alleged address and port are what we actually used :)
m_strAllegedAddress = inet_ntoa (m_ServerAddr.sin_addr);
m_iAllegedPort = ntohs (m_ServerAddr.sin_port);
// first check if we are already connected
for (POSITION chatpos = m_pDoc->m_ChatList.GetHeadPosition (); chatpos; )
{
CChatSocket * pSocket = m_pDoc->m_ChatList.GetNext (chatpos);
if (pSocket == this || pSocket->m_iChatStatus != eChatConnected)
continue;
if (m_strAllegedAddress == inet_ntoa (pSocket->m_ServerAddr.sin_addr) &&
ntohs (pSocket->m_ServerAddr.sin_port) == m_iAllegedPort)
{
m_pDoc->ChatNote (eChatConnection,
TFormat ("You are already connected to %s port %d",
(LPCTSTR) m_strAllegedAddress,
m_iAllegedPort));
OnClose (0);
return;
} // end of found it
} // end of checking them
m_pDoc->ChatNote (eChatConnection,
TFormat ("Calling chat server at %s port %d",
(LPCTSTR) m_strAllegedAddress,
m_iAllegedPort));
m_iChatStatus = eChatConnecting;
BOOL connected = Connect((SOCKADDR*)&m_ServerAddr, sizeof(m_ServerAddr));
if (connected)
{
OnConnect (0); // we have connected already! Do logon of character etc.
return;
}
// if error code is "would block" then it will finish later
int iStatus = GetLastError ();
if (iStatus == WSAEWOULDBLOCK)
return;
// this will display the error message
OnConnect (iStatus);
} // end of MakeCall
void CChatSocket::SendData (const CString & strText)
{
// send nothing if shutting down
if (m_bDeleteMe)
return;
m_tLastOutgoing = CTime::GetCurrentTime();
m_outstanding_output += strText;
OnSend (0); // in case FD_WRITE message got lost, try to send again
return;
} // end of SendData
void CChatSocket::ProcessChatMessage (const int iMessage, const CString strMessage)
{
if (m_bDeleteMe || m_iChatStatus != eChatConnected)
return;
#if CHAT_DEBUG
m_pDoc->Note (TFormat ("Received chat message %i on %i, data: \"%s\"",
iMessage, m_iChatID,
(LPCTSTR) strMessage));
#endif
if (!m_pDoc->SendToAllPluginCallbacks (ON_PLUGIN_CHAT_MESSAGE,
m_iChatID, // who we are
iMessage, // message number
string (strMessage), // message text
false,
true)) // stop on false response
return; // false means plugin handled it
switch (iMessage)
{
case CHAT_TEXT_EVERYBODY: Process_Text_everybody (strMessage); break;
case CHAT_TEXT_PERSONAL: Process_Text_personal (strMessage); break;
case CHAT_MESSAGE: Process_Message (strMessage); break;
case CHAT_TEXT_GROUP: Process_Text_group (strMessage); break;
case CHAT_PING_REQUEST: Process_Ping_request (strMessage); break;
case CHAT_PING_RESPONSE: Process_Ping_response (strMessage); break;
case CHAT_VERSION: Process_Version (strMessage); break;
case CHAT_REQUEST_CONNECTIONS: Process_Request_connections (strMessage); break;
case CHAT_CONNECTION_LIST: Process_Connection_list (strMessage); break;
case CHAT_PEEK_CONNECTIONS: Process_Peek_connections (strMessage); break;
case CHAT_PEEK_LIST: Process_Peek_list (strMessage); break;
case CHAT_SNOOP: Process_Snoop (strMessage); break;
case CHAT_SNOOP_DATA: Process_Snoop_data (strMessage); break;
case CHAT_NAME_CHANGE: Process_Name_change (strMessage); break;
case CHAT_SEND_COMMAND: Process_Send_command (strMessage); break;
case CHAT_FILE_START: Process_File_start (strMessage); break;
case CHAT_FILE_DENY: Process_File_deny (strMessage); break;
case CHAT_FILE_BLOCK_REQUEST: Process_File_block_request (strMessage); break;
case CHAT_FILE_BLOCK: Process_File_block (strMessage); break;
case CHAT_FILE_END: Process_File_end (strMessage); break;
case CHAT_FILE_CANCEL: Process_File_cancel (strMessage); break;
case CHAT_ICON: Process_Icon (strMessage); break;
case CHAT_STATUS: Process_Status (strMessage); break;
case CHAT_EMAIL_ADDRESS: Process_EmailAddress (strMessage); break;
case CHAT_STAMP: Process_Stamp (strMessage); break;
case CHAT_REQUEST_PGP_KEY: Process_RequestPGPkey (strMessage); break;
case CHAT_PGP_KEY: Process_PGPkey (strMessage); break;
default:
// tell them we don't support that
SendChatMessage (CHAT_MESSAGE,
TFormat ("\n%s does not support the chat command %i.\n",
m_pDoc->m_strOurChatName, iMessage));
// tell us we got it
m_pDoc->ChatNote (eChatInformation,
TFormat ("Received unsupported chat command %i from %s",
iMessage,
(LPCTSTR) m_strRemoteUserName));
break;
} // end of switch
} // end of ProcessChatMessage
// sends a chat message to the other end
void CChatSocket::SendChatMessage (const int iMessage,
const CString strMessage,
const long iStamp)
{
if (m_bDeleteMe || m_iChatStatus != eChatConnected)
return;
#if CHAT_DEBUG
m_pDoc->Note (TFormat ("Sending chat message %i on %i, data: \"%s\"",
iMessage, m_iChatID,
(LPCTSTR) strMessage));
#endif
if (!m_pDoc->SendToAllPluginCallbacks (ON_PLUGIN_CHAT_MESSAGE_OUT,
m_iChatID, // which chat ID
iMessage, // message number
string (strMessage), // message text
false,
true)) // stop on false response
return; // false means plugin discarded it
if (iMessage == CHAT_SNOOP && m_bYouAreSnooping)
m_bYouAreSnooping = false;
CString strData;
switch (m_iChatConnectionType)
{
case eChatMudMaster:
{
// messages start with a message number and end with 0xFF
strData = (char) (unsigned char) iMessage;
strData += strMessage;
// can't have 0xFF imbedded in messages, except file blocks :)
// 0xFF is y with 2 dots on it so y will look reasonable
if (iMessage != CHAT_FILE_BLOCK)
strData.Replace ((unsigned char) CHAT_END_OF_COMMAND, 'y');
strData += (unsigned char) CHAT_END_OF_COMMAND;
}
break;
case eChatZMud:
{
// start of with 2 bytes of message number
strData = (char) (unsigned char) (iMessage & 0xFF);
strData += (unsigned char) ((iMessage >> 8) & 0xFF);
CString strStampedMessage;
// now add message stamp if message requires
switch (iMessage)
{
// stamp it if necessary
case CHAT_TEXT_EVERYBODY:
case CHAT_TEXT_PERSONAL:
case CHAT_TEXT_GROUP:
// zero means use this connection's stamp
strStampedMessage = MakeStamp (iStamp ? iStamp : m_zChatStamp) +
strMessage;
break;
default:
strStampedMessage = strMessage;
break;
}
// then 2 bytes of message data length
int iLength = strStampedMessage.GetLength ();
strData += (char) (unsigned char) (iLength & 0xFF);
strData += (char) (unsigned char) ((iLength >> 8) & 0xFF);
// then the message itself
strData += strStampedMessage;
}
break;
} // end of switch on chat connection type
SendData (strData);
} // end of ProcessChatMessage
void CChatSocket::Process_Name_change (const CString strMessage)
{
CString strOldName = m_strRemoteUserName;
m_strRemoteUserName = strMessage;
m_pDoc->ChatNote (eChatNameChange,
TFormat ("%s has changed his/her name to %s.",
(LPCTSTR) strOldName,
(LPCTSTR) m_strRemoteUserName));
} // end of CChatSocket::Process_Name_change
void CChatSocket::Process_Request_connections (const CString strMessage)
{
CString strResult;
for (POSITION chatpos = m_pDoc->m_ChatList.GetHeadPosition (); chatpos; )
{
CChatSocket * pSocket = m_pDoc->m_ChatList.GetNext (chatpos);
if (pSocket->m_iChatStatus == eChatConnected && !pSocket->m_bPrivate)
{
// omit self
if (pSocket == this)
continue;
if (!strResult.IsEmpty ())
strResult += ",";
strResult += pSocket->m_strAllegedAddress;
strResult += ",";
strResult += CFormat ("%d", pSocket->m_iAllegedPort);
} // end of this one wanted
} // end of doing all
m_pDoc->ChatNote (eChatInformation,
TFormat ("%s has requested your public connections",
(LPCTSTR) m_strRemoteUserName));
SendChatMessage (CHAT_CONNECTION_LIST, strResult);
} // end of CChatSocket::Process_Request_connections
void CChatSocket::Process_Connection_list (const CString strMessage)
{
CStringList strList;
StringToList (strMessage, ",", strList);
int iCount = strList.GetCount () / 2; // number of connections
m_pDoc->ChatNote (eChatConnectionList,
TFormat ("Found %i connection%s to %s",
PLURAL (iCount),
(LPCTSTR) m_strRemoteUserName));
// for each one, connect to it
for (int i = 0; i < iCount; i++)
{
CString strIP;
long iPort;
strIP = strList.RemoveHead ();
iPort = atol (strList.RemoveHead ());
m_pDoc->ChatCall(strIP, iPort);
} // end of doing each connection
} // end of CChatSocket::Process_Connection_list
void CChatSocket::Process_Text_everybody (const CString strMessage)
{
CString strFixedMessage = strMessage;
long iStamp = GetStamp (strFixedMessage);
if (!m_bIgnore)
{
// anti-message loop provision
if (strFixedMessage == m_pDoc->m_strLastMessageSent)
{
CTimeSpan timediff = CTime::GetCurrentTime() - m_pDoc->m_tLastMessageTime;
if (timediff.GetTotalSeconds () < LOOP_DISCARD_SAME_MESSAGE_SECONDS)
return;
} // end of same message that we just sent
// zChat anti-loop provision
if (m_iChatConnectionType == eChatZMud &&
m_zChatStamp == iStamp)
return;
m_iCountIncomingAll++;
m_pDoc->ChatNote (eChatIncomingEverybody, strFixedMessage);
if (m_bIncoming)
m_pDoc->SendChatMessageToAll (CHAT_TEXT_EVERYBODY,
strFixedMessage,
true, // unless ignoring them
false, // to everyone
false,
m_iChatID, // except us
"", // no particular group
iStamp); // use this message stamp
else
m_pDoc->SendChatMessageToAll (CHAT_TEXT_EVERYBODY,
strFixedMessage,
true, // unless ignoring them
true, // to incoming only (ones we serve)
false,
m_iChatID, // except us
"", // no particular group
iStamp); // use this message stamp
} // end of not ignoring him
} // end of CChatSocket::Process_Text_everybody
void CChatSocket::Process_Text_personal (const CString strMessage)
{
if (!m_bIgnore)
{
CString strFixedMessage = strMessage;
long iStamp = GetStamp (strFixedMessage);
// zChat anti-loop provision
if (m_iChatConnectionType == eChatZMud &&
m_zChatStamp == iStamp)
return;
m_iCountIncomingPersonal++;
m_pDoc->ChatNote (eChatIncomingPersonal, strFixedMessage);
}
} // end of CChatSocket::Process_Text_personal
void CChatSocket::Process_Text_group (const CString strMessage)
{
if (!m_bIgnore)
{
// anti-message loop provision
if (strMessage == m_pDoc->m_strLastGroupMessageSent)
{
CTimeSpan timediff = CTime::GetCurrentTime() - m_pDoc->m_tLastGroupMessageTime;
if (timediff.GetTotalSeconds () < LOOP_DISCARD_SAME_MESSAGE_SECONDS)
return;
} // end of same message that we just sent
CString strFixedMessage = strMessage;
long iStamp = GetStamp (strFixedMessage);
// zChat anti-loop provision
if (m_iChatConnectionType == eChatZMud &&
m_zChatStamp == iStamp)
return;
if (strFixedMessage.GetLength () > 15)
{
m_iCountIncomingGroup++;
CString strGroup = strFixedMessage.Left (15);
strGroup.TrimLeft ();
strGroup.TrimRight ();
m_pDoc->ChatNote (eChatIncomingGroup, strFixedMessage.Mid (15)); // strip group
if (!m_bIncoming) // if from one we called
m_pDoc->SendChatMessageToAll (CHAT_TEXT_GROUP,
strFixedMessage,
true, // unless ignoring them
true, // to incoming only (ones we serve)
false,
m_iChatID, // except us
strGroup, // onsend to same group
iStamp); // use this message stamp
} // end of message not ridiculously small
} // end of not ignoring
} // end of CChatSocket::Process_Text_group
void CChatSocket::Process_Message (const CString strMessage)
{
if (!m_bIgnore)
{
m_iCountMessages++;
m_pDoc->ChatNote (eChatMessage, strMessage);