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

📄 chat.cpp

📁 MudMaster 2000 的C++源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:

				case CHAT_TEXT_PERSONAL :
					IncomingChatPersonal(pChat, m_pChatBuf+1);
					break;

				case CHAT_TEXT_GROUP :
					IncomingChatGroup(pChat, m_pChatBuf+1);
					break;

				case CHAT_MESSAGE :
					IncomingMessage(pChat, m_pChatBuf+1);
					break;

				case CHAT_DO_NOT_DISTURB :
					IncomingDnd(pChat, m_pChatBuf+1);
					break;

				case CHAT_VERSION :
					IncomingVersion(pChat, m_pChatBuf+1);
					break;

				case CHAT_PING_REQUEST :
					IncomingPingRequest(pChat, m_pChatBuf+1);
					break;

				case CHAT_PING_RESPONSE :
					IncomingPingResponse(pChat, m_pChatBuf+1);
					break;

				case CHAT_FILE_START :
					IncomingFileStart(pChat, m_pChatBuf+1);
					break;

				case CHAT_FILE_DENY :
					IncomingFileDeny(pChat, m_pChatBuf+1);
					break;

				case CHAT_FILE_BLOCK_REQUEST :
					IncomingFileBlockRequest(pChat, m_pChatBuf+1);
					break;

				case CHAT_FILE_BLOCK :
					IncomingFileBlock(pChat, m_pChatBuf+1);
					break;

				case CHAT_FILE_END :
					IncomingFileEnd(pChat);
					break;

				case CHAT_FILE_CANCEL :
					IncomingFileCancel(pChat);
					break;

				case CHAT_SEND_ACTION :
				case CHAT_SEND_ALIAS :
				case CHAT_SEND_MACRO : 
				case CHAT_SEND_VARIABLE :
				case CHAT_SEND_EVENT :
				case CHAT_SEND_GAG :
				case CHAT_SEND_HIGHLIGHT :
				case CHAT_SEND_LIST :
				case CHAT_SEND_ARRAY :
				case CHAT_SEND_BARITEM :
				case CHAT_SEND_SUBSTITUTE :
					IncomingCommand(pChat, chCommand,m_pChatBuf+1);
					break;

			} // switch(chCommand)

			m_nChatIndex = 0;
			nIndex++;
			continue;

		} // if ((unsigned char)(*(pszBuf+nIndex)) == CHAT_END_OF_COMMAND)

		m_pChatBuf[m_nChatIndex] = *(pBuf+nIndex);

		// If we are receiving a block, jump to the end of the block size.
		// There may be data in the block that looks like th end of command
		// character and need to skip over it.
		if (!m_nChatIndex && m_pChatBuf[m_nChatIndex] == CHAT_FILE_BLOCK)
		{
			// Copy the next blocksize chars into the chat buf.
			memcpy(m_pChatBuf+m_nChatIndex+1,pBuf+nIndex+1,CHAT_BLOCKSIZE);

			m_nChatIndex += CHAT_BLOCKSIZE;
			nIndex += CHAT_BLOCKSIZE;

			// The normal increment below should then point the buffer
			// at the end of command char.
		}

		m_nChatIndex++;
		nIndex++;

	} // while(m_nChatIndex < CHAT_BUF_SIZE && nIndex < nLen)

	if (m_nChatIndex >= CHAT_BUF_SIZE)
		m_nChatIndex = 0;
}

void CChat::ChangeName(CHAT *pChat, const char *pszBuf)
{
	if (!pChat->bIgnore)
	{
		CString strText;
		strText.Format("\n<CHAT> %s is now %s.\n",
			(const char *)pChat->strName,pszBuf);
		ChatPrint(strText);
	}
	pChat->strName = pszBuf;
	pChat->strSearchName = pszBuf;
	pChat->strSearchName.MakeLower();
}

BOOL CChat::RequestConnects(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)
		{
			CString strText;
			strText.Format("%c%c",
				(unsigned char)CHAT_REQUEST_CONNECTIONS,
				(unsigned char)CHAT_END_OF_COMMAND);
			send(pChat->hSocket,strText,strText.GetLength(),0);
			return(TRUE);
		}
	}
	return(FALSE);
}

void CChat::SendPublicConnections(const CHAT *pChat)
{
	CString strConnects;
	CString strTemp;
	CString strAddress;
	CHAT *pTemp;
	for (int i=0;i<m_nCount;i++)
	{
		pTemp = (CHAT *)m_ptrList.GetAt(i);
		if (pChat != pTemp && !pTemp->bPrivate)
		{
			if (!strConnects.IsEmpty())
				strConnects += ",";

			if (pTemp->strAddress.GetLength() < 8)
				strAddress = "<Unknown>";
			else
				strAddress = pTemp->strAddress;

			strTemp.Format("%s,%u",(const char *)strAddress,pTemp->nPort);
			strConnects += strTemp;
		}
	}

	if (!strConnects.IsEmpty())
	{
		CString strText;
		strText.Format("\n<CHAT> %s has requested your public connections.\n",
			(const char *)pChat->strName);
		ChatPrint(strText);

		char *ptr = strText.GetBuffer(strConnects.GetLength()+5);
		int nResult = sprintf(ptr,"%c%s%c",
			(unsigned char)CHAT_CONNECTION_LIST,
			(const char *)strConnects,
			(unsigned char)CHAT_END_OF_COMMAND);
		strText.ReleaseBuffer(nResult);
		send(pChat->hSocket,strText,strText.GetLength(),0);
	}
}

void CChat::ProcessConnectionList(const CHAT *pChat, char *pszBuf)
{
	int nCount = 0;
	int nIndex = 0;
	while(*(pszBuf+nIndex))
	{
		if (*(pszBuf+nIndex) == ',')
			nCount++;
		nIndex++;
	}
	nCount /= 2;
	nCount++;

	CString strText;
	strText.Format("\n<CHAT> %d connections returned.\n",nCount);
	ChatPrint(strText);

	CString strBuf(pszBuf);
	CString strIP;
	CString strPort;
	while(!strBuf.IsEmpty())
	{
		nIndex = strBuf.Find(',');
		if (nIndex == -1)
			return;
		strIP = strBuf.Left(nIndex);
		strBuf = strBuf.Right(strBuf.GetLength()-nIndex-1);
		nIndex = strBuf.Find(',');
		if (nIndex == -1)
		{
			// Last port number won't have a comma.
			if (strBuf.IsEmpty())
				return;
			strPort = strBuf;
			strBuf.Empty();
		}
		else
		{
			strPort = strBuf.Left(nIndex);
			strBuf = strBuf.Right(strBuf.GetLength()-nIndex-1);
		}

		if (FindAddress(strIP,atoi(strPort)))
		{
			strText.Format("Already connected with: %s:%s",
				(const char *)strIP,(const char *)strPort);
			PrintMessage(strText);
		}
		else
		{
			// Don't call yourself.
			if (strIP == _config.strIPAddress && atoi(strPort) == _nListenPort)
				continue;
			if (Connect(strIP,atoi(strPort)))
				PrintMessage("Chat connected.");
			else
				PrintMessage("Chat connection failed.");
		}
	}
}

void CChat::ParseAddress(const char *pszBuf, char *pszAddress)
{
	int nIndex = 0;
	while(nIndex < 15 && *(pszBuf+nIndex) != ' ')
	{
		pszAddress[nIndex] = *(pszBuf+nIndex);
		nIndex++;
	}
	pszAddress[nIndex] = '\x0';
}

void CChat::IncomingChatGroup(const CHAT *pChat, const char *pszBuf)
{
	if (!pChat->bIgnore)
		ChatPrint(pszBuf+15);
}

void CChat::IncomingChatPersonal(const CHAT *pChat, const char *pszBuf)
{
	if (!pChat->bIgnore)
		ChatPrint(pszBuf);
}

void CChat::IncomingChatEverybody(const CHAT *pChat, const char *pszBuf)
{
	if (m_bIgnoreAll)
		return;

	if (!pChat->bIgnore)
		ChatPrint(pszBuf);

	// If we are serving this chat connection, need to echo
	// the chat to everybody.  
	if (pChat->bServe)
	{
		CString strText;
		char *ptr = strText.GetBuffer(strlen(pszBuf)+5);
		int nResult = sprintf(ptr,"%c%s%c",
			(unsigned char)CHAT_TEXT_EVERYBODY,
			pszBuf,
			(unsigned char)CHAT_END_OF_COMMAND);
		strText.ReleaseBuffer(nResult);

		CHAT *pTemp;
		for (int i=0;i<m_nCount;i++)
		{
			pTemp = (CHAT *)m_ptrList.GetAt(i);
			if (pTemp != pChat)
				send(pTemp->hSocket,strText,strText.GetLength(),0);
		}
	}
	else
	{
		// If not serving see if there are any others we ARE serving
		// and need to echo this too.
		CString strText;
		char *ptr = strText.GetBuffer(strlen(pszBuf)+5);
		int nResult = sprintf(ptr,"%c%s%c",
			(unsigned char)CHAT_TEXT_EVERYBODY,
			pszBuf,
			(unsigned char)CHAT_END_OF_COMMAND);
		strText.ReleaseBuffer(nResult);

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

void CChat::IncomingMessage(const CHAT *pChat, const char *pszBuf)
{
	ChatPrint(pszBuf);
}

void CChat::IncomingDnd(const CHAT *pChat, const char *pszBuf)
{
/*	CString strText;
	strText.Format("\n<CHAT> %s has do not disturb enabled.\n",pszBuf);
	ChatPrint(strText); */
}

void CChat::IncomingVersion(CHAT *pChat, const char *pszBuf)
{
	pChat->strVersion = pszBuf;
}

void CChat::IncomingCommand(const CHAT *pChat, unsigned char chCommand, const char *pszBuf)
{
	// Make sure we are allowing commands form this user.
	if (!pChat->bCommands)
	{
		// Send a message back letting the person know we are not accepting.
		CString strText;
		strText.Format("%c\n<CHAT> %s is not allowing commands.\n%c",
			(unsigned char)CHAT_MESSAGE,
			(const char *)m_strChatName,
			(unsigned char)CHAT_END_OF_COMMAND);
		send(pChat->hSocket,strText,strText.GetLength(),0);
		return;
	}

	CString strType;
	switch(chCommand)
	{
		case CHAT_SEND_ACTION :
			strType = "Action";
			break;

		case CHAT_SEND_ALIAS :
			strType = "Alias";
			break;

		case CHAT_SEND_MACRO : 
			strType = "Macro";
			break;

		case CHAT_SEND_VARIABLE :
			strType = "Variable";
			break;

		case CHAT_SEND_EVENT :
			strType = "Event";
			break;

		case CHAT_SEND_GAG :
			strType = "Gag";
			break;

		case CHAT_SEND_HIGHLIGHT :
			strType = "Highlight";
			break;

		case CHAT_SEND_LIST :
			strType = "List";
			break;

		case CHAT_SEND_ARRAY :
			strType = "Array";
			break;

		case CHAT_SEND_BARITEM :
			strType = "Bar Item";
			break;

		case CHAT_SEND_SUBSTITUTE :
			strType = "Substitute";
			break;

		default :
			return;
	}

	CString strText;
	strText.Format("\n<CHAT> %s received.\n",(const char *)strType);
	ChatPrint(strText);

	strText = pszBuf;
	HandleInput(strText);
}

CString CChat::FilterText(const char *pszText)
{
	CString strText;
	int nLen = strlen(pszText);
	int nIndex = 0;
	char *ptr = strText.GetBufferSetLength(nLen);
	while(nIndex < nLen)
	{
		switch(*(pszText+nIndex))
		{
			case 8 : // Backspace
			case 7 : // Bell
				*(ptr+nIndex) = '*';
				break;

			default :
				*(ptr+nIndex) = *(pszText+nIndex);
				break;
		}
		nIndex++;
	}
	strText.ReleaseBuffer(nLen);
	return(strText);
}

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

BOOL CChat::SendFile(int nIndex, const char *pszFilename)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(FALSE);
	CHAT *pChat = (CHAT *)m_ptrList.GetAt(nIndex);

	if (pChat->nTransferType != CHAT_TRANSFER_NONE)
	{
		ChatPrint("\n<CHAT> There is already a file transfer in progress.\n");
		return(FALSE);
	}

	CString strText;
	char *ptr;
	int nResult;
	
	// Open the file.
	CString strFilename(_config.strUploadPath+pszFilename);
	if (!pChat->file.Open(strFilename,CFile::modeRead|CFile::typeBinary|CFile::shareDenyWrite))
	{
		ptr = strText.GetBuffer(strFilename.GetLength()+50);
		nResult = sprintf(ptr,"\n<CHAT> File not found: %s\n",(const char *)strFilename);
		strText.ReleaseBuffer(nResult);
		ChatPrint(strText);
		return(FALSE);
	}

	pChat->nTransferType = CHAT_TRANSFER_SEND;
	pChat->nFileLength = pChat->file.GetLength();
	pChat->tTransferStart = time(NULL);

	ptr = strText.GetBuffer(strlen(pChat->file.GetFileName())+40);
	nResult = sprintf(ptr,"%c%s,%u%c",
		(unsigned char)CHAT_FILE_START,
		(const char *)pChat->file.GetFileName(),
		pChat->nFileLength,		
		(unsigned char)CHAT_END_OF_COMMAND);
	strText.ReleaseBuffer(nResult);
	send(pChat->hSocket,strText,strText.GetLength(),0);

	ChatPrint("\n<CHAT> File transfer started.\n");
	return(TRUE);
}

BOOL CChat::CancelTransfer(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(CancelTransfer(i));
	}
	return(FALSE);
}

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

	CHAT *pChat = (CHAT *)m_ptrList.GetAt(nIndex);
	if (pChat->nTransferType == CHAT_TRANSFER_NONE)
		return(FALSE);

	pChat->nTransferType = CHAT_TRANSFER_NONE;
	pChat->file.Close();

	// Send the cancel notification.
	char pBuf[2];
	pBuf[0] = (unsigned char)CHAT_FILE_CANCEL;
	pBuf[1] = (unsigned char)CHAT_END_OF_COMMAND;
	send(pChat->hSocket,pBuf,2,0);

	ChatPrint("\n<CHAT> File transfer cancelled.\n");

	return(TRUE);
}

void CChat::IncomingFileStart(CHAT *pChat, char *pszBuf)
{
	CString strText;
	// Make sure we are accepting transfers.
	if (!pChat->bTransfers)
	{
		strText.Format("%cNot allowing file transfers.%c",
			(unsigned char)CHAT_FILE_DENY,
			(unsigned char)CHAT_END_OF_COMMAND);

⌨️ 快捷键说明

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