⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chat.cpp

📁 MudMaster 2000 的C++源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/************************************************************************************
	Copyright (c) 2000 Aaron O'Neil
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

		1) Redistributions of source code must retain the above copyright notice, 
				this list of conditions and the following disclaimer.
		2) Redistributions in binary form must reproduce the above copyright notice, 
				this list of conditions and the following disclaimer in the documentation
				and/or other materials provided with the distribution.
		3) Redistributions in binary form must reproduce the above copyright notice on
				program startup. Additional credits for program modification are acceptable
				but original copyright and credits must be visible at startup.
		4) You may charge a reasonable copying fee for any distribution of Mud Master. 
				You may charge any fee you choose for support of Mud Master. You may not 
				charge a fee for Mud Master itself.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************************/

#include "StdAfx.h"
#include "Extern.h"
#include "Chat.h"
#include "TextInput.h"
#include "MudWindow.h"
#include "Sound.h"
#include "Notifications.h"

CChat::CChat()
{
	m_ptrList.SetSize(DEFAULT_CHAT_ARRAY_SIZE,DEFAULT_CHAT_GROW_SIZE);
	m_nCount = 0;
	m_nGetIndex = 0;

	m_nFore = 12;
	m_wFore = F_LIGHTRED;
	m_nBack = 0;
	m_wBack = B_BLACK;
	m_wAttr = m_wFore | m_wBack;

	m_pChatBuf = new char[CHAT_BUF_SIZE+1];
	m_nChatIndex = 0;

	m_nDndCount = 1;
	m_nSnoopCount = 0;
	m_bIgnoreAll = FALSE;

	m_pSendBuf = new char[CHAT_BLOCKSIZE+2];
}

CChat::~CChat()
{
	RemoveAll();
	delete [] m_pSendBuf;
	delete [] m_pChatBuf;
}

void CChat::RemoveAll()
{
	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		if (pChat->bSnooped)
			m_nSnoopCount--;
		closesocket(pChat->hSocket);
		delete pChat;
	}
	m_ptrList.RemoveAll();
	m_nCount = 0;
	m_nGetIndex = 0;
}

BOOL CChat::Remove(const char *pszName)
{
	CString strName(pszName);
	strName.MakeLower();

	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		if (pChat->strSearchName == strName)
		{
			if (pChat->hSocket != INVALID_SOCKET)
				closesocket(pChat->hSocket);
			if (pChat->bSnooped)
				m_nSnoopCount--;
			m_ptrList.RemoveAt(i);
			m_nCount--;
			delete pChat;
			return(TRUE);
		}
	}
	return(FALSE);
}

BOOL CChat::Remove(int nIndex)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(FALSE);

	CHAT *pChat = (CHAT *)m_ptrList.GetAt(nIndex);
	if (pChat == NULL)
		return(FALSE);
	if (pChat->hSocket != INVALID_SOCKET)
		closesocket(pChat->hSocket);
	if (pChat->bSnooped)
		m_nSnoopCount--;
	m_ptrList.RemoveAt(nIndex);
	m_nCount--;
	delete pChat;
	return(TRUE);
}

CHAT* CChat::GetFirst()
{
	if (!m_nCount)
		return(NULL);
	m_nGetIndex = 0;
	return((CHAT *)m_ptrList.GetAt(m_nGetIndex));
}

CHAT* CChat::GetNext()
{
	if (m_nGetIndex+1 == m_nCount)
		return(NULL);
	m_nGetIndex++;
	return((CHAT *)m_ptrList.GetAt(m_nGetIndex));
}

CHAT* CChat::GetAt(int nIndex)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(NULL);
	return((CHAT*)m_ptrList.GetAt(nIndex));
}

CHAT* CChat::FindAddress(const char *pszAddress, u_short nPort)
{
	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		if (pChat->strAddress == pszAddress && pChat->nPort == nPort)
			return(pChat);
	}
	return(NULL);
}

CHAT* CChat::FindName(const char *pszName)
{
	CString strName(pszName);
	strName.MakeLower();
	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		if (pChat->strSearchName == strName)
			return(pChat);
	}
	return(NULL);
}

BOOL CChat::Connect(const char *pszAddress, int nPort)
{
	if (!nPort)
		nPort = 4050;

	CHAT *pChat = new CHAT;
	pChat->strAddress = pszAddress;
	pChat->strAddress.TrimRight();
	pChat->strSockAddress = pszAddress;
	pChat->strSockAddress.TrimRight();
	pChat->nPort = nPort;
	pChat->bPrivate = FALSE;
	pChat->bServe = FALSE;
	pChat->bIgnore = FALSE;
	pChat->bCommands = FALSE;
	pChat->bTransfers = FALSE;
	pChat->bSnooped = FALSE;
	pChat->bAllowSnoop = FALSE;
	pChat->hSocket = INVALID_SOCKET;
	pChat->nDndId = 0;
	pChat->nTransferType = CHAT_TRANSFER_NONE;

	SOCKADDR_IN sockAddr;
	memset(&sockAddr,0,sizeof(sockAddr));

	sockAddr.sin_family = AF_INET;
	sockAddr.sin_addr.s_addr = inet_addr(pszAddress);
	sockAddr.sin_port = htons((u_short)nPort);

	if (sockAddr.sin_addr.s_addr == INADDR_NONE)
	{
		CString strMessage("Looking up host: ");
		strMessage += pszAddress;
		strMessage += "...";
		PrintMessage(strMessage);

		LPHOSTENT lphost;
		lphost = gethostbyname(pszAddress);
		if (lphost != NULL)
			sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
		else
		{
			PrintMessage("Error: Unable to find host.");
			delete pChat;
			return(FALSE);
		}
	}

	// Get a socket handle.
	pChat->hSocket = socket(PF_INET,SOCK_STREAM,0);
	if (pChat->hSocket == INVALID_SOCKET)
	{
		PrintMessage("Error: Unable to get a socket handle.");
		delete pChat;
		return(FALSE);
	}

	// Make sure it is a non-blocking socket.
	unsigned long lNonBlocking = 1;
	ioctlsocket(pChat->hSocket,FIONBIO,&lNonBlocking);

	CString strMessage;
	strMessage.Format("Connecting to %s:%u (ESC to abort) ...",
		pszAddress, nPort);
	PrintMessage(strMessage);
	int nResult = connect(pChat->hSocket,(SOCKADDR*)&sockAddr, sizeof(sockAddr));
	if (nResult == SOCKET_ERROR)
	{
		if (WSAGetLastError() == WSAEWOULDBLOCK)
		{
			// Timeout value must be 0 or the call will block.
			timeval timeout = {0, 0};

			// Create socket descriptor arrays for polling the
			// connection status.
			fd_set fdsWrite;
			fd_set fdsExcept;

			while(1)
			{
				// Give the user a chance to abort it.
				if (kbhit() && getch() == '\x1b')
				{
					PrintMessage("Chat connection cancelled.");
					closesocket(pChat->hSocket);
					delete pChat;
					return(FALSE);
				}

				// Make sure the array is cleared, the put the handle
				// for our socket in the array.  A socket is connected
				// when is is writable.  Have to also poll the exception
				// status to determine if there was an error.
				FD_ZERO(&fdsWrite);
				FD_SET(pChat->hSocket,&fdsWrite);
				FD_ZERO(&fdsExcept);
				FD_SET(pChat->hSocket,&fdsExcept);

				// Select changes the arrays.  After the call the arrays
				// contain only the handles of the sockets that met
				// either the read, write or execption status change.
				nResult = select(0,NULL,&fdsWrite,&fdsExcept,&timeout);

				// Something changed.
				if (nResult > 0)
				{
					// If writable, we are connected.
					if (fdsWrite.fd_count)
					{
						break;
					}

					// Encountered an error.
					if (fdsExcept.fd_count)
					{
						closesocket(pChat->hSocket);
						delete pChat;
						return(FALSE);
					}
				}

			}
		}
		else
		{
			PrintError();
			closesocket(pChat->hSocket);
			delete pChat;
			return(FALSE);
		}
	}

	// Socket is connected.  Send the chat request.
	CString strText;
	strText.Format("CHAT:%s\n%s%-5u",(const char *)m_strChatName,
		(const char *)_config.strIPAddress,_nListenPort);
	int nNumSent = send(pChat->hSocket,strText,strText.GetLength(),0);
	if (nNumSent == SOCKET_ERROR)
	{
		PrintError();
		closesocket(pChat->hSocket);
		delete pChat;
		return(FALSE);
	}

	PrintMessage("Waiting for response (ESC Abort) ...",FALSE);

	char szInBuf[101];
	int nRecvLen;
	while(1)
	{
		if (kbhit())
		{
			if (getch() == 27)
			{
				PrintMessage("Chat Canceled.");
				closesocket(pChat->hSocket);
				delete pChat;
				return(FALSE);
			}
		}
	
		nRecvLen = recv(pChat->hSocket,szInBuf,100,0);
		if (nRecvLen == SOCKET_ERROR)
		{
			nRecvLen = WSAGetLastError();
			if (nRecvLen != WSAEWOULDBLOCK)
			{
				closesocket(pChat->hSocket);
				delete pChat;
				return(FALSE);
			}
		}
		else
			if (nRecvLen > 0)
			{
				szInBuf[nRecvLen] = '\x0';
				break;
			}
			else
			{
				closesocket(pChat->hSocket);
				delete pChat;
				return(FALSE);
			}
	}

	strText = szInBuf;
	if (strText.Left(2) == "NO")
	{
		PrintMessage("Chat request denied.");
		closesocket(pChat->hSocket);
		delete pChat;
		return(FALSE);
	}

	PrintMessage("Chat request accepted.");

	CString strName;
	int nLen = strText.GetLength();
	int nIndex = 4;
	while(nIndex < nLen && strText.GetAt(nIndex) != '\n')
	{
		strName += strText.GetAt(nIndex);
		nIndex++;
	}
	pChat->strName = strName;
	pChat->strSearchName = strName;
	pChat->strSearchName.MakeLower();

	// Send out the version number.
	strText.Format("%cMud Master %s%c",
		(unsigned char)CHAT_VERSION,
		(const char *)_config.strVersion,
		(unsigned char)CHAT_END_OF_COMMAND);
	send(pChat->hSocket,strText,strText.GetLength(),0);
	
	// Add the item to the list.
	CHAT *pMap;
	for (int i=0;i<m_nCount;i++)
	{
		pMap = (CHAT *)m_ptrList.GetAt(i);

		// Insert the new entry.
		if (pChat->strName < pMap->strName)
		{
			m_ptrList.InsertAt(i,pChat);
			m_nCount++;
			_notifyChatConnect.Notify(pChat->hSocket,strName);
			return(TRUE);
		}
	}

	// If fell thru the loops, must be a new entry, but
	// goes at the tail of the list.
	m_ptrList.SetAtGrow(i,pChat);
	m_nCount++;

	_notifyChatConnect.Notify(pChat->hSocket,strName);
	return(TRUE);
}

int CChat::Accept(SOCKET hSocket,SOCKADDR_IN &sockAddr)
{
	if (hSocket == INVALID_SOCKET)
		return(CHAT_ACCEPT_FAILED); 

	CHAT *pChat = new CHAT;
	pChat->hSocket = hSocket;

	PrintMessage("Negotiating chat (ESC to Abort) ...");

	char szInBuf[101];
	int nRecvLen;
	while(1)
	{
		if (kbhit())
		{
			if (getch() == 27)
			{
				PrintMessage("Chat Canceled.");
				closesocket(pChat->hSocket);
				delete pChat;
				return(CHAT_ACCEPT_FAILED);
			}
		}
	
		nRecvLen = recv(pChat->hSocket,szInBuf,100,0);
		if (nRecvLen == SOCKET_ERROR)
		{
			nRecvLen = WSAGetLastError();
			if (nRecvLen != WSAEWOULDBLOCK)
			{
				closesocket(pChat->hSocket);
				delete pChat;
				return(CHAT_ACCEPT_FAILED);
			}
		}
		else
			if (nRecvLen > 0)
			{
				szInBuf[nRecvLen] = '\x0';
				break;
			}
			else
			{
				closesocket(pChat->hSocket);
				delete pChat;
				return(CHAT_ACCEPT_FAILED);
			}
	}

	CString strText(szInBuf);
	if (strText.GetLength() < 6)
	{
		closesocket(pChat->hSocket);
		delete pChat;
		return(CHAT_ACCEPT_FAILED);
	}

	if (strText.Left(5) != "CHAT:")
	{
		closesocket(pChat->hSocket);
		delete pChat;
		return(CHAT_ACCEPT_FAILED);
	}

	CString strName;
	int nLen = strText.GetLength();
	int nIndex = 5;
	while(nIndex < nLen && strText.GetAt(nIndex) != '\n')
	{
		strName += strText.GetAt(nIndex);
		nIndex++;
	}

	strName.TrimRight();
	strName.TrimLeft();
	if (strName.IsEmpty())
	{
		closesocket(pChat->hSocket);
		delete pChat;
		return(CHAT_ACCEPT_FAILED);
	}

	// Real machine address.  This is the actual address, not the
	// address that a firewall would nomally scramble.  Should fix
	// the problems I've been having with firewalls giving me a
	// different address than the machine really has.
	pChat->strAddress = strText.Right(strText.GetLength()-nIndex-1);
	pChat->nPort = atoi(pChat->strAddress.Right(5));
	pChat->strAddress = pChat->strAddress.Left(pChat->strAddress.GetLength()-5);
	pChat->strAddress.TrimRight();

	_sound.PlayWave(CString(_config.strSoundPath)+"ring.wav",_config.nDefaultVolume,_config.nDefaultVolume);

	if (!_config.bAutoAccept)
	{
		strText.Format("Chat request from %s.  Accept it (Y/N)?",(const char *)strName);
		PrintMessage(strText,FALSE);
		time_t tStart = time(NULL);
		time_t tNow;
		char ch = 'A';
		while(ch != 'Y' && ch != 'N')
		{
			// Delay for 15 seconds.  Then just called it a no.
			tNow = time(NULL);
			if (tNow-tStart > 15)
			{
				ch = 'N';
				break;
			}

			if (kbhit())
				ch = toupper(getch());
		}
		if (ch == 'N')

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -