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

📄 chat.cpp

📁 MudMaster 2000 的C++源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		{
			strText = "NO";
			send(pChat->hSocket,strText,strText.GetLength(),0);
			closesocket(pChat->hSocket);
			delete pChat;
			return(CHAT_ACCEPT_FAILED);
		}
		else
		{
			strText.Format("YES:%s\n",(const char *)m_strChatName);
			send(pChat->hSocket,strText,strText.GetLength(),0);
		}
	}
	else
	{
		strText.Format("Chat request from %s accepted.",(const char *)strName);
		PrintMessage(strText);
		strText.Format("YES:%s\n",(const char *)m_strChatName);
		send(pChat->hSocket,strText,strText.GetLength(),0);
	}

	pChat->strName = strName;
	pChat->strSearchName = strName;
	pChat->strSearchName.MakeLower();
	pChat->strSockAddress = inet_ntoa(sockAddr.sin_addr);
	pChat->strSockAddress.TrimRight();
	pChat->bServe = FALSE;
	pChat->bPrivate = FALSE;
	pChat->bIgnore = FALSE;
	pChat->bCommands = FALSE;
	pChat->bSnooped = FALSE;
	pChat->bAllowSnoop = FALSE;
	pChat->bTransfers = FALSE;
	pChat->nDndId = 0;
	pChat->nTransferType = CHAT_TRANSFER_NONE;

	// 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(CHAT_ACCEPT_SUCCESS);
		}
	}

	// 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(CHAT_ACCEPT_SUCCESS);
}

BOOL CChat::AddGroup(const char *pszName, const char *pszGroup)
{
	CString strName(pszName);
	strName.MakeLower();

	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		if (strName != "*")
		{
			if (pChat->strSearchName == strName)
			{
				pChat->strGroup = pszGroup;
				pChat->strSearchGroup = pszGroup;
				pChat->strSearchGroup.MakeLower();
				return(TRUE);
			}
		}
		else
		{
			pChat->strGroup = pszGroup;
			pChat->strSearchGroup = pszGroup;
			pChat->strSearchGroup.MakeLower();
		}
	}
	return(strName == "*");
}

BOOL CChat::AddGroup(int nIndex, const char *pszGroup)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(FALSE);

	CHAT *pChat = (CHAT *)m_ptrList.GetAt(nIndex);
	pChat->strGroup = pszGroup;
	pChat->strSearchGroup = pszGroup;
	pChat->strSearchGroup.MakeLower();
	return(TRUE);
}

BOOL CChat::RemoveGroup(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 (strName != "*")
		{
			if (pChat->strSearchName == strName)
			{
				pChat->strGroup.Empty();
				pChat->strSearchGroup.Empty();
				return(TRUE);
			}
		}
		else
		{
			pChat->strGroup.Empty();
			pChat->strSearchGroup.Empty();
		}
	}
	return(strName == "*");
}

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

	CHAT *pChat = (CHAT *)m_ptrList.GetAt(nIndex);
	pChat->strGroup.Empty();
	pChat->strSearchGroup.Empty();
	return(TRUE);
}

void CChat::SetFore(int nColor)
{
	m_nFore = nColor;
	m_wFore = ForeColorAttribute(nColor);
	m_wAttr = m_wFore | m_wBack;
}

void CChat::SetBack(int nColor)
{
	m_nBack = nColor;
	m_wBack = BackColorAttribute(nColor);
	m_wAttr = m_wFore | m_wBack;
}

WORD CChat::BackColorAttribute(int nColor)
{
	WORD wAttr = B_BLACK;
	switch(nColor)
	{
		case 0 : wAttr = B_BLACK; break;
		case 1 : wAttr = B_BLUE; break;
		case 2 : wAttr = B_GREEN; break;
		case 3 : wAttr = B_CYAN; break;
		case 4 : wAttr = B_RED; break;
		case 5 : wAttr = B_MAGENTA; break;
		case 6 : wAttr = B_BROWN; break;
		case 7 : wAttr = B_LIGHTGREY; break;
	}
	return(wAttr);
}

WORD CChat::ForeColorAttribute(int nColor)
{
	WORD wAttr = F_LIGHTGREY;
	switch(nColor)
	{
		case 0 : wAttr = F_BLACK; break;
		case 1 : wAttr = F_BLUE; break;
		case 2 : wAttr = F_GREEN; break;
		case 3 : wAttr = F_CYAN; break;
		case 4 : wAttr = F_RED; break;
		case 5 : wAttr = F_MAGENTA; break;
		case 6 : wAttr = F_BROWN; break;
		case 7 : wAttr = F_LIGHTGREY; break;
		case 8 : wAttr = F_DARKGREY; break;
		case 9 : wAttr = F_LIGHTBLUE; break;
		case 10 : wAttr = F_LIGHTGREEN; break;
		case 11 : wAttr = F_LIGHTCYAN; break;
		case 12 : wAttr = F_LIGHTRED; break;
		case 13 : wAttr = F_LIGHTMAGENTA; break;
		case 14 : wAttr = F_YELLOW; break;
		case 15 : wAttr = F_WHITE; break;
	}
	return(wAttr);
}

void CChat::ChatPrint(const char *pszText)
{
	WORD wOldAttr = _terminal.GetColor();
	_terminal.SetColor(m_wAttr);
	_terminal.Print(pszText);
	_terminal.SetColor(wOldAttr);
	_tiUserInput.SetFocus();
}

BOOL CChat::SendChatToEverybody(const char *pszText)
{
	CString strLine(FilterText(pszText));
	CString strText;
	strText = "\nYou chat to everybody, '";
	strText += strLine + "'\n";
	ChatPrint(strText);
	
	char *ptr = strText.GetBuffer(strLine.GetLength()+80);
	int nResult = sprintf(ptr,"%c\n%s chats to everybody, '%s'\n%c",
		(unsigned char)CHAT_TEXT_EVERYBODY,
		(const char *)m_strChatName,
		(const char *)strLine,
		(unsigned char)CHAT_END_OF_COMMAND);
	strText.ReleaseBuffer(nResult);

	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		if (!pChat->bIgnore)
			send(pChat->hSocket,strText,strText.GetLength(),0);
	}
	return(TRUE);
}

BOOL CChat::SendChatToGroup(const char *pszGroup, const char *pszText)
{
	CString strLine(FilterText(pszText));
	CString strGroup(pszGroup);
	strGroup.MakeLower();

	CString strText;
	char *ptr = strText.GetBuffer(strLine.GetLength()+80);
	int nResult = sprintf(ptr,"\nYou chat to <%s>, '%s'\n",
		pszGroup, (const char *)strLine);
	strText.ReleaseBuffer(nResult);
	ChatPrint(strText);

	ptr = strText.GetBuffer(strLine.GetLength()+120);
	nResult = sprintf(ptr,"%c%-15s\n%s chats to the group, '%s'\n%c",
		(unsigned char)CHAT_TEXT_GROUP,
		(const char *)strGroup,
		(const char *)m_strChatName,
		(const char *)strLine,
		(unsigned char)CHAT_END_OF_COMMAND);
	strText.ReleaseBuffer(nResult);

	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		if (!pChat->bIgnore && pChat->strSearchGroup == strGroup)
			send(pChat->hSocket,strText,strText.GetLength(),0);
	}
	return(TRUE);
}

BOOL CChat::SendChatToPerson(const char *pszName, const char *pszText)
{
	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(SendChatToPerson(i,pszText));
	}
	return(FALSE);
}

BOOL CChat::SendChatToPerson(int nIndex, const char *pszText)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(FALSE);

	CHAT *pChat = (CHAT *)m_ptrList.GetAt(nIndex);

	CString strLine(FilterText(pszText));
	CString strText;
	char *ptr = strText.GetBuffer(strLine.GetLength()+80);
	int nResult = sprintf(ptr,"\nYou chat to %s, '%s'\n",
		(const char *)pChat->strName,
		(const char *)strLine);
	strText.ReleaseBuffer(nResult);
	ChatPrint(strText);
	
	ptr = strText.GetBuffer(strLine.GetLength()+80);
	nResult = sprintf(ptr,"%c\n%s chats to you, '%s'\n%c",
		(unsigned char)CHAT_TEXT_PERSONAL,
		(const char *)m_strChatName,
		(const char *)strLine,
		(unsigned char)CHAT_END_OF_COMMAND);
	strText.ReleaseBuffer(nResult);

	send(pChat->hSocket,strText,strText.GetLength(),0);
	return(TRUE);
}

BOOL CChat::SendEmoteToEverybody(const char *pszText)
{
	CString strLine(FilterText(pszText));
	CString strText;
	char *pBuf = strText.GetBuffer(strLine.GetLength()+80);
	int nResult = sprintf(pBuf,"\nYou emote to everybody: %s %s\n",
		(const char *)m_strChatName,
		(const char *)strLine);
	strText.ReleaseBuffer(nResult);
	ChatPrint(strText);
	
	pBuf = strText.GetBuffer(strLine.GetLength()+80);
	nResult = sprintf(pBuf,"%c\n%s %s\n%c",
		(unsigned char)CHAT_TEXT_EVERYBODY,
		(const char *)m_strChatName,
		(const char *)strLine,
		(unsigned char)CHAT_END_OF_COMMAND);
	strText.ReleaseBuffer(nResult);

	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		if (!pChat->bIgnore)
			send(pChat->hSocket,strText,strText.GetLength(),0);
	}
	return(TRUE);
}

BOOL CChat::SendEmoteToGroup(const char *pszGroup, const char *pszText)
{
	CString strLine(FilterText(pszText));
	CString strGroup(pszGroup);
	strGroup.MakeLower();

	CString strText;
	char *ptr = strText.GetBuffer(strLine.GetLength()+120);
	int nResult = sprintf(ptr,"\nYou emote to <%s>: %s %s\n",
		pszGroup, 
		(const char *)m_strChatName,
		(const char *)strLine);
	strText.ReleaseBuffer(nResult);
	ChatPrint(strText);

	ptr = strText.GetBuffer(strLine.GetLength()+120);
	nResult = sprintf(ptr,"%c%-15s\n%s %s\n%c",
		(unsigned char)CHAT_TEXT_GROUP,
		(const char *)strGroup,
		(const char *)m_strChatName,
		(const char *)strLine,
		(unsigned char)CHAT_END_OF_COMMAND);
	strText.ReleaseBuffer(nResult);

	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		if (!pChat->bIgnore && pChat->strSearchGroup == strGroup)
			send(pChat->hSocket,strText,strText.GetLength(),0);
	}
	return(TRUE);
}

BOOL CChat::SendEmoteToPerson(const char *pszName, const char *pszText)
{
	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(SendEmoteToPerson(i,pszText));
	}
	return(FALSE);
}

BOOL CChat::SendEmoteToPerson(int nIndex, const char *pszText)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(FALSE);

	CHAT *pChat = (CHAT *)m_ptrList.GetAt(nIndex);

	CString strLine(FilterText(pszText));
	CString strText;
	char *ptr = strText.GetBuffer(strLine.GetLength()+120);
	int nResult = sprintf(ptr,"\nYou emote to %s: %s %s\n",
		(const char *)pChat->strName,
		(const char *)m_strChatName,
		(const char *)strLine);
	strText.ReleaseBuffer(nResult);
	ChatPrint(strText);
	
	ptr = strText.GetBuffer(strLine.GetLength()+120);
	nResult = sprintf(ptr,"%c\n%s %s\n%c",
		(unsigned char)CHAT_TEXT_PERSONAL,
		(const char *)m_strChatName,
		(const char *)strLine,
		(unsigned char)CHAT_END_OF_COMMAND);
	strText.ReleaseBuffer(nResult);

	send(pChat->hSocket,strText,strText.GetLength(),0);
	
	return(TRUE);
}

BOOL CChat::SendCommand(unsigned char chCommand, const char *pszName, const char *pszCommand)
{
	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)
		{
			CString strText("\n<CHAT> Command sent.\n");
			ChatPrint(strText);

			char *ptr = strText.GetBuffer(strlen(pszCommand)+5);
			int nResult = sprintf(ptr,"%c%s%c",
				(unsigned char)chCommand,
				pszCommand,
				(unsigned char)CHAT_END_OF_COMMAND);
			strText.ReleaseBuffer(nResult);

			send(pChat->hSocket,strText,strText.GetLength(),0);
			return(TRUE);
		}
	}
	return(FALSE);
}

BOOL CChat::SendCommand(unsigned char chCommand, int nIndex, const char *pszCommand)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(FALSE);

	CHAT *pChat = (CHAT *)m_ptrList.GetAt(nIndex);

	CString strText("\n<CHAT> Command sent.\n");
	ChatPrint(strText);

	char *ptr = strText.GetBuffer(strlen(pszCommand)+5);
	int nResult = sprintf(ptr,"%c%s%c",
		(unsigned char)chCommand,
		pszCommand,
		(unsigned char)CHAT_END_OF_COMMAND);
	strText.ReleaseBuffer(nResult);

	send(pChat->hSocket,strText,strText.GetLength(),0);
	return(TRUE);
}

void CChat::BroadcastNameChange(const char *pszName)
{
	// Need to send the command to all the remote sysetems.
	// First char 255 is sent as a command char, next char
	// is the command, then the IP address
	// is in the parens, followed by the new name.
	CString strText;
	strText.Format("%c%s%c",
		(unsigned char)CHAT_NAME_CHANGE,
		pszName,
		(unsigned char)CHAT_END_OF_COMMAND);

	CHAT *pChat;
	for (int i=0;i<m_nCount;i++)
	{
		pChat = (CHAT *)m_ptrList.GetAt(i);
		send(pChat->hSocket,strText,strText.GetLength(),0);
	}
}

void CChat::DoCommands(CHAT *pChat, char *pBuf, int nBufLen)
{
	unsigned char chCommand;
	int nIndex = 0;
	while(m_nChatIndex < CHAT_BUF_SIZE && nIndex < nBufLen)
	{
		if ((unsigned char)(*(pBuf+nIndex)) == CHAT_END_OF_COMMAND)
		{
			m_pChatBuf[m_nChatIndex] = '\x0';

			chCommand = *m_pChatBuf;
			switch(chCommand)
			{
				case CHAT_NAME_CHANGE :
					ChangeName(pChat, m_pChatBuf+1);
					break;

				case CHAT_PEEK_CONNECTIONS :
					SendPublicPeek(pChat);
					break;

				case CHAT_SNOOP :
					IncomingSnoop(pChat);
					break;

				case CHAT_SNOOP_DATA :
					IncomingSnoopData(pChat, m_pChatBuf+1);
					break;

				case CHAT_PEEK_LIST :
					IncomingPeekList(pChat, m_pChatBuf+1);
					break;

				case CHAT_REQUEST_CONNECTIONS :
					SendPublicConnections(pChat);
					break;

				case CHAT_CONNECTION_LIST :
					ProcessConnectionList(pChat, m_pChatBuf+1);
					break;

				case CHAT_TEXT_EVERYBODY :
					IncomingChatEverybody(pChat, m_pChatBuf+1);
					break;

⌨️ 快捷键说明

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