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

📄 chat.h

📁 MudMaster 2000 的C++源码
💻 H
字号:
/************************************************************************************
	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.

**************************************************************************************/
#define DEFAULT_CHAT_ARRAY_SIZE		150
#define DEFAULT_CHAT_GROW_SIZE		20

#define CHAT_BUF_SIZE					5000

// Command char definitions.
#define CHAT_NAME_CHANGE				1
#define CHAT_REQUEST_CONNECTIONS		2
#define CHAT_CONNECTION_LIST			3
#define CHAT_TEXT_EVERYBODY			4
#define CHAT_TEXT_PERSONAL				5
#define CHAT_TEXT_GROUP					6
#define CHAT_MESSAGE						7
#define CHAT_DO_NOT_DISTURB			8
#define CHAT_SEND_ACTION				9
#define CHAT_SEND_ALIAS					10
#define CHAT_SEND_MACRO					11
#define CHAT_SEND_VARIABLE				12
#define CHAT_SEND_EVENT					13
#define CHAT_SEND_GAG					14
#define CHAT_SEND_HIGHLIGHT			15
#define CHAT_SEND_LIST					16
#define CHAT_SEND_ARRAY					17
#define CHAT_SEND_BARITEM				18
#define CHAT_VERSION						19
#define CHAT_FILE_START					20
#define CHAT_FILE_DENY					21
#define CHAT_FILE_BLOCK_REQUEST		22
#define CHAT_FILE_BLOCK					23
#define CHAT_FILE_END					24
#define CHAT_FILE_CANCEL				25
#define CHAT_PING_REQUEST				26
#define CHAT_PING_RESPONSE				27
#define CHAT_PEEK_CONNECTIONS			28
#define CHAT_PEEK_LIST					29
#define CHAT_SNOOP						30		// Sent to start/stop the snoop.
#define CHAT_SNOOP_DATA					31		// Actualy snooped data.
#define CHAT_SNOOP_COLOR				32		// Sends current color for start of snoop block.
#define CHAT_SEND_SUBSTITUTE			33

// File Transfers:  The sender send a CHAT_FILE_START with the
// filename.  The receiver checks to make sure files are allowed
// from that person, then checks to make sure the file doesn't
// already exists.  If files are allowed and the file doesn't exist
// a CHAT_FILE_BLOCK_REQUEST is sent back, otherwise a 
// CHAT_FILE_DENY is sent back.  Then sender then sends out a
// block of chata with CHAT_FILE_BLOCK, and the receiver replies
// with a CHAT_FILE_BLOCK_REQUEST.  The loop continues until the
// sender sends out a CHAT_FILE_END.
#define CHAT_TRANSFER_NONE				0
#define CHAT_TRANSFER_SEND				1
#define CHAT_TRANSFER_RECEIVE			2

#define CHAT_BLOCKSIZE					500


// This flags the end of a chat block.  Should should allow for
// multiple commands to get stacked up in the buffer and still
// handle it properly.
#define CHAT_END_OF_COMMAND			255

// Possible result code from Accept().
#define CHAT_ACCEPT_FAILED				0
#define CHAT_ACCEPT_SUCCESS			1
#define CHAT_ACCEPT_DND					2


struct CHAT
{
	CString strName;			// Name of the person connected.
	CString strSearchName;	// Name to use when searching list.  This one is all lower case.
	CString strAddress;		// Address of person connected.  This is sent from the other machine.
	CString strSockAddress;	// This is the address reported by the socket.
	u_short nPort;				// Port connected to.
	CString strGroup;
	CString strSearchGroup;	// Name to user when searching list.

	// Status flags.
	BOOL    bPrivate;			// Flagged TRUE when other users should not be able to retrieve this address.
	BOOL	  bIgnore;			// TRUE to ignore incoming chats.	
	BOOL	  bCommands;		// TRUE to allow others to add commands to your command file.
	BOOL    bTransfers;		// TRUE to accept files from the person.
	BOOL	  bServe;			// True when serving this user.
	BOOL	  bAllowSnoop;		// TRUE when allow this connection to snoop you.
	BOOL	  bSnooped;			// TRUE when being snooped.

	SOCKET  hSocket;			// Socket descriptor.
	int     nDndId;			// Way to identify a socket to disconnect
									// when DND is turned on.
	CString strVersion;		// Version of MM the person is using.

	// File transfers.
	CFile   file;
	int     nTransferType;	// See above for types.
	unsigned int nFileLength;	// Length of the file.
	unsigned int nBytesWritten;	// Bytes written so far.
	time_t  tTransferStart;
};

///////////////////////////////////////////////////////
// Notes on the bServe flag.
//
//	When a connection is flagged as serve you act as a
// chat server to that connection.  If a connection is
// marked as serve, and a /chatall is received from that
// person, it turns around and echoes the /chatall to
// all of your connections.  If a group chat comes in
// the chat gets echoed to everybody in the same group.
// Anytime a group chat comes in from somebody in the 
// group, it is echo'd to the people being served. Same
// with /chatall from people, anybody being served will
// get it echo'd.
//

class CChat
{
public:
	CChat();
	~CChat();

	void SetChatName(const char *pszChatName)   { m_strChatName = pszChatName; }
	CString GetChatName()			{ return(m_strChatName); }

	// Connects a socket to the remote mud master.
	BOOL Connect(const char *pszAddress, int nPort=0);

	// Call when there is a socket waiting to be accepted only.
	int Accept(SOCKET hSocket,SOCKADDR_IN &sockAddr);

	BOOL SendChatToEverybody(const char *pszText);
	BOOL SendChatToGroup(const char *pszGroup, const char *pszText);
	BOOL SendChatToPerson(const char *pszName, const char *pszText);
	BOOL SendChatToPerson(int nIndex, const char *pszText);

	BOOL SendEmoteToEverybody(const char *pszText);
	BOOL SendEmoteToGroup(const char *pszGroup, const char *pszText);
	BOOL SendEmoteToPerson(const char *pszName, const char *pszText);
	BOOL SendEmoteToPerson(int nIndex, const char *pszText);

	// Sends the text to all of your snooped connections.
	void SendSnoop(const char *pszText);

	// Following two functions for sending command file items to another
	// chat connection.  For nCommand, use the defines above.
	BOOL SendCommand(unsigned char chCommand, const char *pszName, const char *pszCommand);
	BOOL SendCommand(unsigned char chCommand, int nIndex, const char *pszCommand);

	// Pings another chat connection.
	BOOL Ping(const char *pszName);
	BOOL Ping(int nIndex);
	BOOL Ping(CHAT *pChat);

	// Gets rid of any characters that I don't want to be sent.
	CString FilterText(const char *pszText);

	// Prints text to the terminal screen.
	void ChatPrint(const char *pszText);

	// Disconnects a chat socket.
	void Disconnect(const char *pszName);

	// When the user changes his chat name, this function is called
	// to let all the other connected systems know about it.
	void BroadcastNameChange(const char *pszName);

	// Handles commands from the chat sockets. pChat is the chat
	// connection the text is from.
	void DoCommands(CHAT *pChat, char *pBuf, int nBufLen);

	// Command handling routines.
	void ChangeName(CHAT *pChat, const char *pszBuf);

	// Sends all the public chat connection IP addresses back to
	// the person who requested it.
	void SendPublicConnections(const CHAT *pChat);

	void SendPublicPeek(const CHAT *pChat);

	// Take the list of incoming connections and connect to all of
	// them.
	void ProcessConnectionList(const CHAT *pChat, char *pszBuf);

	// Sends out a request to a person to retrieve their public 
	// IPs.
	BOOL RequestConnects(const char *pszName);

	// Requests a list of another chat user's connections.  When the
	// list is returned, they are just show the info, not attempt
	// is made to connect to them.
	BOOL PeekConnections(const char *pszName);
	BOOL PeekConnections(int nIndex);
	BOOL PeekConnections(const CHAT *pChat);

	// Toggles snooping of the chat connection on/off.
	void Snoop(const CHAT *pChat);

	// Takes an incoming command and pulls the IP address off of it.
	// The address is returned in pszAddress.
	void ParseAddress(const char *pszBuf, char *pszAddress);

	void IncomingChatGroup(const CHAT *pChat, const char *pszBuf);
	void IncomingChatPersonal(const CHAT *pChat, const char *pszBuf);
	void IncomingChatEverybody(const CHAT *pChat, const char *pszBuf);
	void IncomingMessage(const CHAT *pChat, const char *pszBuf);
	void IncomingDnd(const CHAT *pChat, const char *pszBuf);
	void IncomingVersion(CHAT *pChat, const char *pszBuf);
	void IncomingPeekList(const CHAT *pChat, char *pszBuf);
	void IncomingSnoop(CHAT *pChat);
	void IncomingSnoopData(const CHAT *pChat, const char *pszBuf);

	// Make sure commands are being allowed from this person.
	void IncomingCommand(const CHAT *pChat, unsigned char chCommand, const char *pszBuf);

	// Sends a ping response back.
	void IncomingPingRequest(const CHAT *pChat, const char *pszBuf);
	// Response to a ping just came in.
	void IncomingPingResponse(const CHAT *pChat, const char *pszBuf);

	// File Transfers.
	BOOL SendFile(const char *pszName, const char *pszFilename);
	BOOL SendFile(int nIndex, const char *pszFilename);
	BOOL CancelTransfer(const char *pszName);
	BOOL CancelTransfer(int nIndex);
	void IncomingFileStart(CHAT *pChat, char *pszBuf);
	void IncomingFileDeny(CHAT *pChat, const char *pszBuf);
	void IncomingFileBlockRequest(CHAT *pChat, const char *pszBuf);
	void IncomingFileBlock(CHAT *pChat, const char *pszBuf);
	void IncomingFileEnd(CHAT *pChat);
	void IncomingFileCancel(CHAT *pChat);

	// Sends a string of text to a chat connection using as a CHAT_MESSAGE.
	void SendMessage(const CHAT *pChat, const char *pszMessage);

	// For adding and removing chat connections to the chat group.
	// Passing in a * for the name will set the group for all calls.
	BOOL AddGroup(const char *pszName, const char *pszGroup);
	BOOL AddGroup(int nIndex, const char *pszGroup);
	BOOL RemoveGroup(const char *pszName);
	BOOL RemoveGroup(int nIndex);

	void RemoveAll();
	BOOL Remove(const char *pszName);
	BOOL Remove(int nIndex);

	// For picking apart an incoming peek.
	void GetPeekParam(CString &strText, CString &strParam);

	// Returns a pointer to the first ALIAS_MAP in the array.
	// Returns NULL if the array is empty.
	CHAT* GetFirst();

	// Returns a pointer to the next ALIAS_MAP in the array.
	// Returns NULL when the end of the array is reached.
	CHAT* GetNext();

	CHAT* GetAt(int nIndex);

	// Searches the list for a specific IP Address.  Returns a
	// pointer to the matching entry, or NULL if not found.
	CHAT* FindAddress(const char *pszAddress, u_short nPort);
	CHAT* FindName(const char *pszName);

	void SetIgnoreAll(BOOL bValue)			{m_bIgnoreAll = bValue;}
	BOOL GetIgnoreAll()							{return(m_bIgnoreAll);}

	int GetCount()			{ return(m_nCount); }
	int GetFindIndex()	{ return(m_nGetIndex); }

	// Returns the number of chat connections snooping.
	int GetSnoopCount()	{ return(m_nSnoopCount); }

	void SetFore(int nColor);
	void SetBack(int nColor);
	int  GetFore()			{ return(m_nFore); }
	int  GetBack()			{ return(m_nBack); }
	WORD GetColor()		{ return(m_wAttr); }

private:
	CString m_strChatName;

	char *m_pChatBuf;
	int m_nChatIndex;

	// Buffer used for sending files.  It is allocated at 
	// 1+TRANSFER_BLOCKSIZE+1. 
	char *m_pSendBuf;

	CPtrArray m_ptrList;
	int m_nCount;
	int m_nGetIndex;

	// Keeps track of number of connections snooping.  Check this instead of
	// looping thru all the chat connections each time text is printed.
	int m_nSnoopCount;

	// Color of the status bar.
	int  m_nFore;
	WORD m_wFore;
	int  m_nBack;
	WORD m_wBack;
	WORD m_wAttr;

	// When TRUE the user only sees group chats and private chats.
	BOOL m_bIgnoreAll;

	// Set to 1 in the constructor.  A number other than 0 is a 
	// valid chat connection.
	int m_nDndCount;

	// Pass in a number from 0 to 7, returns the color attribute.
	WORD BackColorAttribute(int nColor);
	// Pass in a number from 0 to 15, returns the color attribute.
	WORD ForeColorAttribute(int nColor);
};

⌨️ 快捷键说明

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