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

📄 ircclient.cpp

📁 IRC的客户端演示
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CIrcClient.cpp : implementation file
//

#include "stdafx.h"
#include "hfIRC.h"
#include "ircClient.h"

#include "hfIRCView.h"
//#include "hfIRCDoc.h"
#include "Clientsocket.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CIrcClient

CIrcClient::CIrcClient(CHfIRCView* pView)
//CIrcClient::CIrcClient(CHfIRCDoc* pDoc)
{
	m_pView		= pView;
	//m_pDoc		= pDoc;
	m_pSocket	= NULL;
	m_strCurChannel =	CString("");
	m_strHostIPAddress = CString("");
	m_strServerName =		CString("");
	m_strMyNickName =		CString("");

	m_strClientMsg =  CString("");
	m_strRecvMsg = CString("NULL");

	m_strlstDisplay.RemoveAll();

}

CIrcClient::~CIrcClient()
{
	if(m_pSocket != NULL) 
	{
		m_pSocket->ShutDown(2);
		m_pSocket->Close();
		delete m_pSocket;
		m_pSocket = NULL;
	}

}

void CIrcClient::Init()
{

	//Get the Nickname, Hostname, ....


	//build the socket link
	if( !ClientConnectSocket(m_strServerName) )  //connect the default server
	{
		//something is wrong 
	}

	if(m_pSocket == NULL) return;
	DoList();

	DoJoin("#cbkmis");
	DoNames();
	//DoJoin("#cbk");
	//DoTopic();
	//DoInfo();
	//DoWhois();
	DoPrivmsg("cbkmis","Hi I am the machine, auto send the Message.");	
	//DoQuit("I am send the Message Automaticly");

}


/////////////////////////////////////////////////////////////////////////////
// CIrcClient message handlers


void CIrcClient::DoJoin(LPCSTR lpszChannel)
{
	char	buf[50];
	int		iTem;

	if(m_pSocket == NULL) return;
	sprintf(buf, "JOIN %s",lpszChannel);
	iTem = strlen(buf);
	buf[iTem] = 0x0d;
	buf[iTem+1] = 0x0a;
	buf[iTem+2] = 0x00;
	m_pSocket->Send( (void*)buf, strlen(buf));
	
}

void CIrcClient::DoList()
{
	char	buf[50];
	int		iTem;

	if(m_pSocket == NULL) return;
	strcpy(buf,"LIST");
	iTem = strlen(buf);
	buf[iTem] = 0x0d;
	buf[iTem+1] = 0x0a;
	buf[iTem+2] = 0x00;
	m_pSocket->Send( (void*)buf, strlen(buf));

}

void CIrcClient::DoNick()
{

}


void CIrcClient::DoPrivmsg(LPCSTR lpszWho,LPCSTR lpszMessage)
{
	char	buf[512];
	int		iTem;

	//the message len is must less 510
	if(strlen(lpszMessage) > 510)
	{
		buf[510] = '\0';
		buf[511] = '\0';
	}

	//check the lpszWho , if the string is: "#chinese"  --> "chinese"

	if(m_pSocket == NULL) return;
	sprintf(buf, "PRIVMSG %s :%s",lpszWho,lpszMessage);
	iTem = strlen(buf);
	buf[iTem] = 0x0d;
	buf[iTem+1] = 0x0a;
	buf[iTem+2] = 0x00;
	m_pSocket->Send( (void*)buf, strlen(buf));

}

void CIrcClient::DoWhois()
{


}

void CIrcClient::DoPong(LPCSTR lpszServerName)
{
	char	buf[50];
	int		iTem;

	//the message len is must less 510
	if(strlen(lpszServerName) > 50)
	{
		//something wrong
		return;
	}

	if(m_pSocket == NULL) return;
	sprintf(buf, "PONG %s",lpszServerName);
	iTem = strlen(buf);
	buf[iTem] = 0x0d;
	buf[iTem+1] = 0x0a;
	buf[iTem+2] = 0x00;
	m_pSocket->Send( (void*)buf, strlen(buf));

}




BOOL CIrcClient::ClientConnectSocket(LPCTSTR lpszHostname,UINT nPort)
//BOOL CHfIRCDoc::ClientConnectSocket(LPCTSTR lpszHandle, LPCTSTR lpszAddress, UINT nPort)
{
	char	buf[512];
	int		iTem;
	
	m_pSocket = new CClientSocket(this);

	if (!m_pSocket->Create())
	{
		//??
		m_pSocket->Close();

		//??? 1998-07-21 closesocket();
		delete m_pSocket;
		m_pSocket = NULL;
		AfxMessageBox("Socket Create Error");
		return FALSE;
	}

	while(!m_pSocket->Connect(lpszHostname, nPort) )
	{
		//一但不成功,server 端的 nPort 口就被占用而 cbkcomsrv cannot startup ?????
		if (AfxMessageBox("ReConnect again?",MB_YESNO) == IDNO)
		{
			m_pSocket->Close();

			//??? 1998-07-21 closesocket();
			delete m_pSocket;
			m_pSocket = NULL;
			AfxMessageBox("Socket connect fail");
		
			return FALSE;
		}

	}

	//AfxMessageBox("Socket connect successfully");

	//Init:
	m_pSocket->m_pFile = new CSocketFile(m_pSocket);
	m_pSocket->m_pArchiveIn = new CArchive(m_pSocket->m_pFile,CArchive::load);
	m_pSocket->m_pArchiveOut = new CArchive(m_pSocket->m_pFile,CArchive::store);


	//Connect the host:
	//send the nickname:
	sprintf( buf,"NICK %s",LPCSTR(m_strMyNickName) );
	iTem = strlen(buf);
	buf[iTem] = 0x0d;
	buf[iTem+1] = 0x0a;
	buf[iTem+2] = 0x00;
	m_pSocket->Send( (void*)buf, strlen(buf));

	//send the username, hostname ,....
	////Command: USER
	//Parameters: <username> <hostname> <servername> <realname>
	//sprintf(buf, "USER %s * * %s\n", userinfo->pw_name,userinfo->pw_gecos);
	sprintf(buf, "USER %s \"intl-1\" \"23.150.0.1\" :%s",LPCSTR(m_strMyNickName),LPCSTR(m_strMyNickName));
	//sprintf(buf, "USER tempHF \"intl-1\" \"23.150.0.1\" :tempHF");
	iTem = strlen(buf);
	buf[iTem] = 0x0d;
	buf[iTem+1] = 0x0a;
	buf[iTem+2] = 0x00;
	m_pSocket->Send( (void*)buf, strlen(buf));

	
	CString strTemp;
	//m_pSocket->GetSockName( m_pSocket->m_strIPAddress, m_pSocket->m_uPort );
	

	return TRUE;
}


void CIrcClient::ParseRecvedStr(CString strMsg)
{
	int iEnd;
	//int iTem;
	char cTem;
	BOOL bTem;
	CString strPrefix,strCommand,strParams;
	CString strTem,strTem2;
	CString strChannel,strWhoTalkToMe;


	//format is: ":<prefix>[ ]<command>[ ]<params><CRLF>" 
	//<prefix>   ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
	//<params>   ::= <SPACE> [ ':' <trailing> | <middle> <params> ]

	//":irc2.cbkmis.boc 001 tempHF :Welcome to the Internet Relay Chat network, tempHF!tempHF@INTL_1"
	//":tempHF!tempHF@INTL_1 JOIN :#cbkmis"
	//":cbkmis!mIRC@23.150.0.1 PRIVMSG tempHF :asdkf"



	cTem = strMsg[0];
	if(cTem == ':')
	{
		//get the prefix:
		iEnd   = strMsg.Find(_T(' '));
		if(iEnd == -1)
		{
			//this is not a command
			m_pView->m_roomStatus.m_strContent += CString(_T("\r\nThis message is not a right command: "));
			m_pView->m_roomStatus.m_strContent += strMsg;
			m_pView->MyUpdateView();
			return;
		}
		strPrefix = strMsg.Mid(1,iEnd);
		
		//get the command:
		strTem = strMsg.Mid( iEnd+1,strMsg.GetLength() );
		iEnd   = strTem.Find(_T(' '));
		if(iEnd == -1)
		{
			//this is not a command
			return;
		}
		strCommand = strTem.Mid(0,iEnd);

		//get the params:
		strParams = strTem.Mid( iEnd+1,strTem.GetLength() );


		//according the command to process this message:
		if( strCommand == CString("001") )
		{
			m_strServerName = strPrefix;

			m_pView->m_roomStatus.m_strContent += CString(_T("\r\n"));
			m_pView->m_roomStatus.m_strContent += CString("The Server Name is: ");
			m_pView->m_roomStatus.m_strContent += LPCTSTR(m_strServerName);
			m_pView->MyUpdateView();


		}
		else if( strCommand == CString("002") )
		{

		}
		else if( strCommand == CString("003") )
		{

		}
		else if( strCommand == CString("004") )
		{

		}
		else if( strCommand == CString("005") )
		{

		}
		//LIST:
		else if( strCommand == CString("321") )
		{
		}
		else if( strCommand == CString("322") )
		{
			//response to the 'LIST'
			//have two channel #cbkmis #cbk2:
			//":irc2.cbkmis.boc 321 Guest3 Channel :Users Name"
			//":irc2.cbkmis.boc 322 Guest3 #cbkmis 3 :"
			//":irc2.cbkmis.boc 322 Guest3 #cbk2 3 :"
			//":irc2.cbkmis.boc 323 Guest3 :End of /LIST"
			
		}
		else if( strCommand == CString("333") )
		{
		}
		//NAMES:
		else if( strCommand == CString("353") )
		{
			//response to the 'NAMES'
			//only one channel:
			//":irc2.cbkmis.boc 353 Guest1 = #cbkmis :Guest1 Guest2 @cbkmis"
			//":irc2.cbkmis.boc 366 Guest1 * :End of /NAMES list."

			//have two channel: #cbkmis #cbk2
			//":irc2.cbkmis.boc 353 Guest1 = #cbkmis :Guest1 Guest2 @cbkmis"
			//":irc2.cbkmis.boc 353 Guest1 = #cbk2 :Guest1 @cbkmis"
			//":irc2.cbkmis.boc 366 Guest1 * :End of /NAMES list."


			//find the channel name:
			iEnd   = strParams.Find(_T('#'));
			if(iEnd == -1)
			{
				//this is something wrong 
			}
			strTem = strParams.Mid( iEnd,strParams.GetLength() );

			iEnd   = strTem.Find(_T(' '));
			if(iEnd == -1)
			{
				//this is something wrong
			}
			strChannel = strTem.Mid(0,iEnd);//??
			strTem = strTem.Mid( iEnd+2,strTem.GetLength() );
			//now strTem = ":Guest1 Guest2 @cbkmis"
			strTem.TrimLeft();

			//find the channel Room:
///////////////////
			//find the correspond room
			CRoom* proomTem;
			//bTem = FALSE;
			for(int i=0; i< m_pView->m_objlstRoomPtr.GetCount(); i++)
			{

				proomTem = (CRoom*) m_pView->m_objlstRoomPtr.GetAt(
					m_pView->m_objlstRoomPtr.FindIndex(i) );
					
				if(proomTem->m_iRoomType == HF_ROOM_TYPE_CHANNEL_PUBLIC &&
					proomTem->m_strRoomName == strChannel )
				{
					//clear the old data:
					proomTem->m_strChannelMember = CString(_T(""));

					
					//add the member to the Channel:
					for(;;)
					{
						iEnd   = strTem.Find(_T(' '));
						if(iEnd == -1)
						{
							proomTem->m_strChannelMember += strTem;
							proomTem->m_strChannelMember += CString(_T(""));
							break;
						}
						strTem2 = strTem.Mid(0,iEnd);//now strTem2 is contain the nickname
						strTem.TrimLeft();
						proomTem->m_strChannelMember += strTem2;
						proomTem->m_strChannelMember += CString(_T("\r\n"));
						strTem = strTem.Mid( iEnd+1,strTem.GetLength() );

					}
					m_pView->MyUpdateView();
					break;
				}

			}

⌨️ 快捷键说明

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