模拟msn源代码-(与NS服务器通信)
// CMsnLoginClient.cpp : implementation file
//
#include "stdafx.h"
#include "SimMsnMessenger.h"
#include "SimMsnMessengerView.h"
#include "MsnLoginClient.h"
#include "MsnSocket.h"
#include <afxinet.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern char agCurDir[];
/////////////////////////////////////////////////////////////////////////////
// CMsnLoginClient
CMsnLoginClient::CMsnLoginClient( )
{
//m_pView = pView;
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();
m_iSendXh=1;
m_iLoginOk=0;
m_iCountUser=0;
m_iXfrSbUserNo=99999;
}
CMsnLoginClient::~CMsnLoginClient()
{
if(m_pSocket != NULL)
{
FileLog("->断开服务器连接!");
m_pSocket->ShutDown(2);
m_pSocket->Close();
delete m_pSocket;
m_pSocket = NULL;
}
}
void CMsnLoginClient::Init()
{
//build the socket link
//FileLog("Socket connect :[%s,%d]",m_strServerName,6667);
if( !ClientConnectSocket(m_strServerName,1863) ) //connect the default server
{
FileLog("Socket connect Error");
}
if(m_pSocket == NULL) return;
}
void CMsnLoginClient::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));
}
BOOL CMsnLoginClient::ClientConnectSocket(LPCTSTR lpszHostname,UINT nPort)
{
char buf[512];
int iTem;
int iConTimes=0;
m_pSocket = new CMsnSocket(this);
FileLog("->开始连接到服务器[%s,%d] ...... ",lpszHostname,nPort);
if (!m_pSocket->Create())
{
//??
m_pSocket->Close();
//??? 1998-07-21 closesocket();
delete m_pSocket;
m_pSocket = NULL;
FileLog("CMsnLoginClient::ClientConnect ->SocketSocket Create Error");
return FALSE;
}
iConTimes++;
while(!m_pSocket->Connect(lpszHostname, nPort) )
{
iConTimes++;
FileLog("->尝试第%d次连接到服务器[%s,%d] ......",iConTimes,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;
CString strTmp;
strTmp.Format( "Socket connect[%s:%d] fail!",lpszHostname, nPort) ;
AfxMessageBox(strTmp);
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:
m_iSendXh++;
sprintf( buf,"VER %d MSNP9 MSNP8 CVR0",m_iSendXh);
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 CMsnLoginClient::ParseRecvedStr(CString strMsg)
{
CString strPrefix,strCommand,strParams;
CString strTem,strTem2;
CString strChannel,strWhoTalkToMe;
char buf[500];
int iTem;
char alTmp0[1000],alTmp1[100],alTmp2[100],alTmp3[100],alTmp4[1000],alTmp5[1000],alTmp6[1000],alTmp7[1000];
unsigned int i=0;
FileLog("Parse[%s]",(LPCTSTR)strMsg);
//MSN Messenger命令使用纯ASCII码。对非ASCII码字符使用URL编码。
//命令的语法是
//XXX[<SP>TrID<SP>PARAM1<SP>PARAM2…]<CRLF>
//其中,<SP>是空白字符,<CRLF>是回车换行,
//XXX是一个3字符的命令串,TrID是一个流水号,PARAMx是参数,
//[ ]内是可选项。最简单的命令没有流水号和参数。
//为了方便起见,下面讨论时用" "代表<SP>,"\r\n"代表<CRLF>,
//"\x??"代表一个值为0x??字节。
//USR 18 TWN I example@hotmail.com\r\n
strCommand = strMsg.Mid(0,3);
if(strCommand == CString("VER"))
{
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "CVR %d 0x0409 winnt 5.1 i386 MSNMSGR 6.0.0602 MSMSGS %s",m_iSendXh,(LPCTSTR)m_strMyNickName);
iTem = strlen(buf);
buf[iTem] = 0x0d; buf[iTem+1] = 0x0a; buf[iTem+2] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
}
else if(strCommand == CString("CVR"))
{
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "USR %d TWN I %s",m_iSendXh ,(LPCTSTR)m_strMyNickName);
iTem = strlen(buf);
buf[iTem] = 0x0d; buf[iTem+1] = 0x0a; buf[iTem+2] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
}
else if(strCommand == CString("XFR"))
{
//XFR 3 NS 207.46.107.104:1863 0 207.46.104.20:1863%0d%0a
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
memset(alTmp5,0x00,sizeof(alTmp5));
memset(alTmp6,0x00,sizeof(alTmp6));
sprintf(alTmp0,"%s",(LPCTSTR)strMsg);
sscanf(alTmp0,"%s %s %s %s %s %s",alTmp1,alTmp2,alTmp3,alTmp4,alTmp5,alTmp6);
FileLog("XFR: %s %s %s %s %s %s",alTmp1,alTmp2,alTmp3,alTmp4,alTmp5,alTmp6);
unsigned int ilLen=strlen(alTmp4);
for( i=2; i < ilLen ; i++ )
{
if( alTmp4[i]== ':' ) break;
}
alTmp4[i]=0x00;
m_strServerName.Format("%s",alTmp4);
if( memcmp(alTmp3,"NS",2 ) == 0 )
{
FileLog("PostMessage(WPARAM, LPARAM lParam)");
AfxGetApp()->m_pMainWnd->PostMessage( WM_MY_MESSAGE, 0, 0 );
}
else if( memcmp(alTmp3,"SB",2 ) == 0 )
{
FileLog("XFR SB 返回分配SS请求的应答");
//XFR+13+SB+207.46.108.46:1863+CKI+52265.1116983946.14855
if( m_iXfrSbUserNo < m_iCountUser )
{
FileLog("XFR SB:NS->[联系人: i=[%d](%s) ...]", m_iXfrSbUserNo,(LPCTSTR)m_MsnUser[m_iXfrSbUserNo]->m_strEmail );
m_MsnUser[m_iXfrSbUserNo]->m_strSessionID.Format("%s",alTmp1);
m_MsnUser[m_iXfrSbUserNo]->SBInit(alTmp4,alTmp6);
}
m_iXfrSbUserNo=99999;
}
}
else if(strCommand == CString("USR"))
{
//USR 7 TWN S lc=1033,id=507,tw=40,fs=1,ru=http%3A%2F%2Fmessenger%2Emsn%2Ecom,ct=1116208791,kpp=1,kv=6,ver=2.1.6000.1,tpf=3dc9e50a9b371f27a912e60c0fa95a62
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
memset(alTmp5,0x00,sizeof(alTmp5));
sprintf(alTmp0,"%s",(LPCTSTR)strMsg);
sscanf(alTmp0,"%s %s %s %s %s",alTmp1,alTmp2,alTmp3,alTmp4,alTmp5);
FileLog("USR : %s %s %s %s",alTmp1,alTmp2,alTmp3,alTmp4,alTmp5);
//USR 7 OK tcpip_2005@126.com tcpip_2005@126.com 1 0 \r\n
if( memcmp( alTmp3 ,"OK", 2) == 0 )
{
FileLog("MSN Login OK !");
m_iLoginOk=1;
m_strNick.Format("%s", alTmp5 );
//SYN+8+0%0d%0a
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "SYN %d 0",m_iSendXh );
iTem = strlen(buf);
buf[iTem] = 0x0d; buf[iTem+1] = 0x0a; buf[iTem+2] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
return ;
}
else if( memcmp( alTmp3 ,"TWN", 3) == 0 )
{
FileLog("MSN 通过SSL的认证 ... ");
}
//通过SSL的认证过程如下:
//首先在HTTPS端口443向login.passport.com发送一个GET请求,
//将账号、密码和NS给定的一长串信息送出
memset(alTmp4,0x00,sizeof(alTmp4));
int ilRet;
ilRet = DoHTTPSAuthentication(alTmp5,alTmp4);
//在服务器认证成功的返回信息中,
//Authentication-Info字段的from-PP串值,就是所谓的“入场券”。
if( ilRet == 0 )
{
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "USR %d TWN S %s",m_iSendXh,alTmp4 );
iTem = strlen(buf);
buf[iTem] = 0x0d; buf[iTem+1] = 0x0a; buf[iTem+2] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
}
else
{
//如果认证失败,服务器返回401错误
FileLog("如果认证失败,服务器返回401错误");
}
}
else if(strCommand == CString("SYN"))
{
FileLog("SYN ...");
// CHG+9+NLN+268435492%0d%0a]
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "CHG %d NLN 268435492",m_iSendXh );
iTem = strlen(buf);
buf[iTem] = 0x0d; buf[iTem+1] = 0x0a; buf[iTem+2] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
}
else if(strCommand == CString("CHG"))
{
//[BLP+10+BL%0d%0aXFR+11+SB%0d%0a]
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "BLP %d BL",m_iSendXh );
iTem = strlen(buf);
buf[iTem] = 0x0d; buf[iTem+1] = 0x0a; buf[iTem+2] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
DispMainPage();
}
else if(strCommand == CString("CHL"))
{
//CHL+0+84992517860471275122%0d%0a]
//QRY+12+PROD0038W!61ZTF9+32%0d%0a23036a07c59a61ea54bf64930ee3689d]
FileLog("CHL 服务器发出验证要求..");
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
sprintf(alTmp0,"%s",(LPCTSTR)strMsg);
sscanf(alTmp0,"%s %s %s",alTmp1,alTmp2,alTmp3 );
FileLog("CHL : %s %s %s",alTmp1,alTmp2,alTmp3 );
DoMD5( alTmp3, alTmp4 );
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "QRY %d PROD0038W!61ZTF9 32\r\n%s",m_iSendXh ,alTmp4);
iTem = strlen(buf);
buf[iTem] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
}
else if(strCommand == CString("LSG"))
{
FileLog("LSG :获取联系人(组列表)请求的应答!");
}
else if(strCommand == CString("LST"))
{
DoLST( strMsg );
FileLog("LST :获取(联系人列表)请求的应答!");
}
else if(strCommand == CString("BLP"))
{
FileLog("BLP :BLP设置保密策略请求的应答!");
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "PNG\r\n");
m_pSocket->Send( (void*)buf, strlen(buf));
}
else if(strCommand == CString("QNG"))
{
FileLog("QNG :PNG(ping)请求的应答!");
}
else if(strCommand == CString("QRY"))
{
FileLog("QRY :CHL服务器验证通过!");
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "PNG\r\n");
m_pSocket->Send( (void*)buf, strlen(buf));
DoUserInput("AddUser sim_m_s_n@hotmail.com",9999);
DispMainPage(); /*显示*/
}
else if(strCommand == CString("MSG"))
{
//MSG Hotmail Hotmail 499
FileLog("MSG:NS服务器发出消息数据 ...");
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
sprintf(alTmp0,"%s",(LPCTSTR)strMsg);
int iLen;
sscanf(alTmp0,"%s %s %s %s",alTmp1,alTmp2,alTmp3 ,alTmp4);
iLen = atoi( alTmp4 );
FileLog("MSG : %s %s %s ,Len=%d",alTmp1,alTmp2,alTmp3 ,iLen);
m_iBytesForCmd = iLen;
}
else if(strCommand == CString("ILN"))
{
//ILN 10 NLN tcpip_2005@126.com 动力通信工作组 1073791020
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
memset(alTmp5,0x00,sizeof(alTmp5));
memset(alTmp6,0x00,sizeof(alTmp6));
memset(alTmp7,0x00,sizeof(alTmp7));
sprintf(alTmp0,"%s",(LPCTSTR)strMsg);
sscanf(alTmp0,"%s %s %s %s %s %s %s",alTmp1,alTmp2,alTmp3 ,alTmp4,alTmp5,alTmp6,alTmp7);
FileLog("ILN:NS->[联系人: %s %s %s Status:%s]",alTmp4,alTmp5,alTmp6, alTmp3);
//memset(alTmp0,0x00,sizeof(alTmp0));
//memcpy(alTmp0,(LPCTSTR)strMsg,strMsg.GetLength());
for( int i=0;i<m_iCountUser; i ++ )
{
if( !m_MsnUser[i]->m_strEmail.CompareNoCase(alTmp4) )
{
m_MsnUser[i]->m_strStatus.Format("%s",alTmp3);
m_MsnUser[i]->m_strNickName.Format("%s",alTmp5);
FileLog("ILN:NS->联系人 i=[%d](%s) ,更新状态完成!", i,(LPCTSTR)m_MsnUser[i]->m_strEmail );
break;
}
}
DeCodeBuf( alTmp7 , strlen(alTmp7)) ;
alTmp7[0]='_';
FileLog("ILN:NS->[联系人:%s]",alTmp7 );
DispMainPage();
}
else if(strCommand == CString("REA"))
{
FileLog("REA:NS-> OK!");
DispMainPage();
}
else if(strCommand == CString("REM"))
{
FileLog("REM:NS-> 删除联系人OK!");
DispMainPage();
}
else if(strCommand == CString("NLN"))
{
//NLN BSY tcpip_2005@126.com 动力通信工作组 1073791020
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
memset(alTmp5,0x00,sizeof(alTmp5));
memset(alTmp6,0x00,sizeof(alTmp6));
memset(alTmp7,0x00,sizeof(alTmp7));
sprintf(alTmp0,"%s",(LPCTSTR)strMsg);
sscanf(alTmp0,"%s %s %s %s %s %s",alTmp1,alTmp3 ,alTmp4,alTmp5,alTmp6,alTmp7);
FileLog("NLN:NS->[联系人: %s %s %s Status:%s]",alTmp4,alTmp5,alTmp6, alTmp3);
for( int i=0;i<m_iCountUser; i ++ )
{
if( !m_MsnUser[i]->m_strEmail.CompareNoCase(alTmp4) )
{
m_MsnUser[i]->m_strStatus.Format("%s",alTmp3);
m_MsnUser[i]->m_strNickName.Format("%s",alTmp5);
FileLog("NLN:NS->联系人 i=[%d](%s) ,更新状态完成!", i,(LPCTSTR)m_MsnUser[i]->m_strEmail );
break;
}
}
if( i == m_iCountUser )
{
CString strTmp1;
//[LST tcpip_2005@126.com 动力通信工作组 11 1]
strTmp1.Format("LST %s %s 11 0",alTmp4,alTmp5);
//m_MsnUser[i]->m_strStatus.Format("%s",alTmp3);
DoLST(strTmp1);
}
DeCodeBuf( alTmp7 , strlen(alTmp7)) ;
alTmp7[0]='_';
FileLog("NLN:NS->[联系人:%s]",alTmp7 );
DispMainPage();
}
else if(strCommand == CString("FLN"))
{
//FLN @sina.com.cn
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
sprintf(alTmp0,"%s",(LPCTSTR)strMsg);
sscanf(alTmp0,"%s %s ",alTmp3 ,alTmp4);
for( int i=0;i<m_iCountUser; i ++ )
{
if( !m_MsnUser[i]->m_strEmail.CompareNoCase(alTmp4) )
{
m_MsnUser[i]->m_strStatus.Format("%s",alTmp3);
FileLog("FLN:NS->联系人 i=[%d](%s) ,更新状态完成!", i,(LPCTSTR)m_MsnUser[i]->m_strEmail );
break;
}
}
DispMainPage();
}
else if(strCommand == CString("RNG"))
{
//RNG 17149548 207.46.108.49:1863 CKI 1116489150.3878 acd@sina.com.cn 静
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
memset(alTmp5,0x00,sizeof(alTmp5));
memset(alTmp6,0x00,sizeof(alTmp6));
memset(alTmp7,0x00,sizeof(alTmp7));
sprintf(alTmp0,"%s",(LPCTSTR)strMsg);
sscanf(alTmp0,"%s %s %s %s %s %s %s",alTmp1,alTmp2,alTmp3 ,alTmp4,alTmp5,alTmp6,alTmp7);
FileLog("RNG:NS->[联系人: %s(%s) 发出私聊session请求 ...]",alTmp6, alTmp7);
for( int i=0;i<m_iCountUser; i ++ )
{
if( !m_MsnUser[i]->m_strEmail.CompareNoCase(alTmp6) )
{
FileLog("RNG:NS->[联系人: i=[%d](%s) ...]", i,(LPCTSTR)m_MsnUser[i]->m_strEmail );
m_MsnUser[i]->m_strSessionID.Format("%s",alTmp2);
m_MsnUser[i]->SBInit(alTmp3,alTmp5);
break;
}
}
}
else if(strCommand == CString("ADD"))
{
//09:59:18:323 S->C 0046 [ADD 0 RL 0 abc@sina.com.cn %e5%a4%8f%e4%bb%a4%e9%9d%99
//09:59:28:448 C->S 0063 [ADD 13 AL abc@sina.com.cn %25e5%25a4%258f%25e4%25bb%25a4%25e9%259d%2599
//09:59:28:748 S->C 0061 [ADD 13 AL 1 abc@sina.com.cn abc@sina.com.cn
//09:59:42:147 C->S 0065 [ADD 14 FL abc@sina.com.cn %25e5%25a4%258f%25e4%25bb%25a4%25e9%259d%2599 0
//09:59:42:478 S->C 0049 [ADD 14 FL 1 abc@sina.com.cn %e5%a4%8f%e4%bb%a4%e9%9d%99 0
//09:59:42:668 S->C 0304 [ILN 14 NLN abc@sina.com.cn %e5%a4%8f%e4%bb%a4%e9%9d%99 1073795116 %
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
memset(alTmp5,0x00,sizeof(alTmp5));
sprintf(alTmp0,"%s",(LPCTSTR)strMsg);
sscanf(alTmp0,"%s %s %s %s %s",alTmp1,alTmp2,alTmp3,alTmp4,alTmp5);
FileLog("USR : %s %s %s %s",alTmp1,alTmp2,alTmp3,alTmp4,alTmp5);
//USR 7 OK tcpip_2005@126.com tcpip_2005@126.com 1 0 \r\n
if( memcmp( alTmp3 ,"RL", 2) == 0 )
{
//09:59:28:448 C->S 0063 [ADD 13 AL abc@sina.com.cn
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "ADD %d AL %s %s\r\n",m_iSendXh,alTmp5,alTmp5 );
iTem = strlen(buf);
buf[iTem] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
return ;
}
else if( memcmp( alTmp3 ,"AL", 2) == 0 )
{
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "ADD %d FL %s %s 0\r\n",m_iSendXh,alTmp5,alTmp5 );
iTem = strlen(buf);
buf[iTem] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
return ;
}
else if( memcmp( alTmp3 ,"FL", 2) == 0 )
{
FileLog("ADD %s FL OK !" , alTmp5);
return ;
}
}
else
{
FileLog("Error! Command[%s]",(LPCTSTR)strCommand);
}
}
void CMsnLoginClient::ParseInputCommandStr(CString strText)
{
int iEnd;
CString strCommand,strParams;
char buf[50];
int iTem;
//this input is a command
//get the command:
iEnd = strText.Find(_T(' '));
if(iEnd == -1)
{
//the input command like: '/list'
strCommand = strText.Mid(1,strText.GetLength());
strParams = CString(_T(""));
}
else
{
strCommand = strText.Mid(1,iEnd);
strParams = strText.Mid(iEnd+1,strText.GetLength());
strParams.TrimLeft();
strParams.TrimRight();
}
strCommand.MakeUpper();
strCommand.TrimLeft();
strCommand.TrimRight();
if( strCommand == CString(_T("JOIN")) )
{
//DoJoin( LPCSTR(strParams) );
}
else if( strCommand == CString(_T("LIST")) )
{
//DoList();
if(m_pSocket == NULL) return;
memset(buf,0x00,sizeof(buf));
if( iEnd != -1 ) sprintf( buf, "LIST %s",LPCSTR(strParams));
else strcpy(buf,"LIST");
iTem = strlen(buf);
buf[iTem] = 0x0d;
buf[iTem+1] = 0x0a;
buf[iTem+2] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
}
else if( strCommand == CString(_T("CLEAR")) || strCommand == CString(_T("CLS")) )
{
// DoClear();
}
else
{
if(m_pSocket == NULL) return;
memset(buf,0x00,sizeof(buf));
if( iEnd != -1 ) sprintf( buf, "%s %s",LPCSTR(strCommand),LPCSTR(strParams));
else sprintf( buf, "%s",LPCSTR(strCommand));
iTem = strlen(buf);
buf[iTem] = 0x0d;
buf[iTem+1] = 0x0a;
buf[iTem+2] = 0x00;
m_pSocket->Send( (void*)buf, strlen(buf));
}
}
void CMsnLoginClient::DoXFR()
{
FileLog("->XFR:断开服务器连接!");
m_pSocket->ShutDown(2);
m_pSocket->Close();
delete m_pSocket;
m_pSocket = NULL;
m_iLoginOk=0;
Init();
}
int CMsnLoginClient::DoHTTPSAuthentication(char *alTmp3, char *alTmp4)
{
FileLog("DoHTTPSAuthentication:NS给出的信息(S:Subsequent)[%s]", alTmp3);
CInternetSession session("My Session");
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
DWORD dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_SECURE;
char szLogin[] = "https://login.passport.com/login2.srf?lc=1033";
char szHeaders[2000];
//char szBuf[1024];
memset(szHeaders,0x00,sizeof(szHeaders));
//Authorization: Passport1.4 OrgVerb=GET,OrgURL=http%3A%2F%2Fmessenger%2Emsn%2Ecom,sign-in=example%40passport.com,pwd=password, lc=1033,id=507,tw=40,fs=1,ru=http%3A%2F%2Fmessenger%2Emsn%2Ecom,ct=1073355862,kpp=1,kv=5,ver=2.1.0173.1, tpf=ed1c2f217a21c191c61251eb8b73bb60 \r\n
sprintf( szHeaders,"Authorization: Passport1.4 OrgVerb=GET,OrgURL=http%%3A%%2F%%2Fmessenger%%2Emsn%%2Ecom,sign-in=%s,pwd=%s, %s \r\n", (LPCTSTR)m_strMyNickName,(LPCTSTR)m_strPassword,alTmp3 );
FileLog( "szHeaders=[%s]",szHeaders);
try
{
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwRet;
strServerName.Format("login.passport.com");
strObject.Format("/login2.srf?lc=1033");
nPort=443;
FileLog("session.GetHttpConnection(%s,%d) ...",(LPCTSTR)strServerName, nPort);
pServer = session.GetHttpConnection(strServerName, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject,0,1,0,0,dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
unsigned long ContentStrLen = 4023;
char alText[4024];
memset(alText,0x00,sizeof(alText));
pFile->QueryInfo(HTTP_QUERY_STATUS_TEXT ,alText,&ContentStrLen);
FileLog("QueryInfo=[%d] %s",ContentStrLen,alText);
//HTTP_QUERY_RAW_HEADERS_CRLF 22 // special: all headers
memset(alText,0x00,sizeof(alText));
ContentStrLen = 4023;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,alText,&ContentStrLen);
FileLog("QueryInfo all headers:[%d] \n%s",ContentStrLen,alText);
//Authentication-Info: Passport1.4 da-status=success,from-PP='t=6i74VouBNHsO1Z5RJNulJBswkNts5lPEiTXvqZaOuh8fPTvhncTY5FKZZZfch4pXCmyclLdkYpUgeWczNmzu1*3FGWUU5pRU3Gvs*472Oi!y3SkGWzuy!Gxc1qWW2gHxYx6oFxID57eng$&p=6hQ!wH4YGFc!nnfg2kyLEs0N!6HWbEeXo8sHmcWXB6uJXAHPHtyvtLauKmUA9h472zg4CfaJLUVUKXVM5ibNfwzOtcpM1RHs85D4NZseNiXYd1b3jwwPtqo268KVKdICylCyiiPSKcznYawOMc5lUkyH0AvS85rFKi1YCkYD2mpsJVT0FyovDk8Q$$',ru=http://messenger.msn.com
unsigned int i=0,iBegin=0,iEnd=0;
for( i = 10; i< ContentStrLen; i++ )
{
if( memcmp( alText + i , "from-PP='",9 ) == 0 ) iBegin= i+9;
else if( memcmp( alText + i , "',ru=",5 ) == 0 )
{
iEnd = i ;
break;
}
}
FileLog("Authentication-Info: %d %d",iBegin,iEnd);
if( iEnd > iBegin )
{
memcpy(alTmp4,alText+iBegin, iEnd-iBegin );
alTmp4[iEnd-iBegin]=0x00;
}
if (dwRet == HTTP_STATUS_OK)
{
FileLog("http ret=[%d]",dwRet);
/***********
UINT nRead = pFile->Read(szBuf, 1023);
while (nRead > 0)
{
szBuf[nRead]=0;
FileLog("http ret=[%d],%s",nRead, szBuf );//read file...
HexLog(szBuf, nRead);
}
*********************/
}
else FileLog("http Err ret=[%d]",dwRet);
delete pFile;
delete pServer;
}
//catch (CInternetException* pEx)
catch (CException* pEx)
{
pEx->GetErrorMessage(alTmp4,255,NULL);
FileLog("CException: %s",alTmp4 ); //catch errors from WinInet
}
session.Close();
FileLog("DoHTTPSAuthentication:从认证服务器得到的(入场券)[%s]", alTmp4);
return 0;
}
int CMsnLoginClient::DoMD5(char *aIn, char *aOut)
{
//Client ID string Client ID code
//msmsgs@msnmsgr.com Q1P7W2E4J9R8U3S5
//PROD0038W!61ZTF9 VT6PX?UQTM4WM%YR
//PROD0058#7IL2{QD QHDCY@7R1TB6W?5B
//PROD0061VRRZH@4F JXQ6J@TUOGYV@N0M
FileLog("Client ID string=[PROD0038W!61ZTF9],Client ID code=[VT6PX?UQTM4WM%YR]");
FileLog("CHL challenge =[%s]", aIn );
int iLen = strlen(aIn );
memcpy( aIn + iLen , "VT6PX?UQTM4WM%YR", 16 );
aIn[ iLen + 16 ] = 0x00;
FileLog("CHL challenge + Client ID code =[%s]", aIn );
MD5String( aIn, aOut);
FileLog("QRY MD5 string=[%s]", aOut );
return 0;
}
int CMsnLoginClient::DoTimerPNG()
{
char buf[10];
if( m_pSocket == NULL) return -1;
if( m_iLoginOk==0 ) return -2;
memset(buf,0x00,sizeof(buf));
sprintf(buf, "PNG\r\n");
m_pSocket->Send( (void*)buf, strlen(buf));
return 0;
}
int CMsnLoginClient::DoLST(CString aIn)
{
//[LST tcpip_2005@126.com 动力通信工作组 11 1]
char alTmp0[1000],alTmp1[300],alTmp2[300],alTmp3[300],alTmp4[1000];
unsigned int i=0;
memset(alTmp0,0x00,sizeof(alTmp0));
memset(alTmp1,0x00,sizeof(alTmp1));
memset(alTmp2,0x00,sizeof(alTmp2));
memset(alTmp3,0x00,sizeof(alTmp3));
memset(alTmp4,0x00,sizeof(alTmp4));
sprintf(alTmp0,"%s", (LPCTSTR)aIn);
sscanf(alTmp0,"%s %s %s",alTmp1,alTmp2,alTmp3 );
FileLog("LST : %s %s %s",alTmp1,alTmp2,alTmp3 );
m_MsnUser[m_iCountUser] = new CMsnUser();
m_MsnUser[m_iCountUser]->m_iUserNo = m_iCountUser ;
m_MsnUser[m_iCountUser]->m_strEmail.Format("%s",alTmp2);
m_MsnUser[m_iCountUser]->m_strNickName.Format("%s",alTmp3);
m_MsnUser[m_iCountUser]->m_strStatus=_T("HDN");
m_MsnUser[m_iCountUser]->m_pLoginClient=this;
m_iCountUser ++;
return 0;
}
int CMsnLoginClient::DispMainPage()
{
//int ilPageName,ilPageStatus,ilPageUser,ilPageEnd;
char alTmp1[4048];
int ilBytes;
CFile fLog;
CString sFile;
CString strBuf;
sFile.Format("%s\\html\\MainPage.htm",agCurDir);
if( !fLog.Open(sFile, CFile::modeRead))
{
FileLog("Failed to read %s.", sFile );
}
else
{
memset(alTmp1,0,4048);
//fLog.Seek(-600,CFile::end);
ilBytes = fLog.Read(alTmp1,4040);
FileLog("Read Bytes: %d ",ilBytes);
fLog.Close();
}
FileLog( " DispMainPage() ... " );
strBuf.Format("%s", alTmp1 );
strBuf.Replace("_VAR_Email",(LPCTSTR)m_strMyNickName);
strBuf.Replace("_VAR_Nick",(LPCTSTR)m_strNick);
strBuf.Replace("_VAR_Status","<option value=\"NLN\" selected>Online</option>");
ilBytes = 0 ;
int iIn=strBuf.Find("_VAR_Users");
for( int i=0;i<m_iCountUser; i ++ )
{
memset(alTmp1,0,2048);
sprintf(alTmp1+ilBytes,"<img border=\"0\" src=\"%s.jpg\" ><a href=\"http://sim.msnmessenger.com.cn/OpenUser.asp?UserNo=%d\">%s(%s)</a><br>\n",
(LPCTSTR)m_MsnUser[i]->m_strStatus,i,
(LPCTSTR)m_MsnUser[i]->m_strNickName,
(LPCTSTR)m_MsnUser[i]->m_strEmail);
strBuf.Insert(iIn,alTmp1);
}
strBuf.Replace("_VAR_Users","<br>");
sFile.Format("%s\\html\\MainPage_0.htm",agCurDir);
if( !fLog.Open(sFile, CFile::modeCreate|CFile::modeWrite))
{
FileLog("Failed to write %s .", sFile );
}
else
{
fLog.Write((LPCTSTR)strBuf,strBuf.GetLength());
fLog.Close();
FileLog("Write Bytes: %d",strBuf.GetLength());
}
FileLog("PostMessage(WPARAM, LPARAM lParam DispMainPage)");
LPARAM lpTmp= 1;
AfxGetApp()->m_pMainWnd->PostMessage( WM_MY_MESSAGE, 0, (LPARAM)lpTmp );
return 0;
}
int CMsnLoginClient::DoUserInput(const char *aInCmd, int iInUserNo)
{
if( memcmp( aInCmd, "REA" , 3 ) == 0 )
{
//C->S 0076 [REA 14 free_2005@sina.com.cn %25e5%25ae%25
char buf[300];
if( m_pSocket == NULL) return -1;
if( m_iLoginOk==0 ) return -2;
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "REA %d %s %s\r\n",m_iSendXh,
(LPCTSTR)m_strMyNickName,(LPCTSTR)m_strNick);
m_pSocket->Send( (void*)buf, strlen(buf));
}
else if( memcmp( aInCmd, "OpenUser" , 8 ) == 0 )
{
if( m_pSocket == NULL) return -1;
if( m_iLoginOk==0 ) return -2;
return DispUserPage(iInUserNo);
}
else if( memcmp( aInCmd, "AddUser" , 3 ) == 0 )
{
//[ADD 13 FL tcpip_2005@126.com tcpip_2005@126.com 1%0d%0a]
char buf[300];
if( m_pSocket == NULL) return -1;
if( m_iLoginOk==0 ) return -2;
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "ADD %d FL %s %s 0\r\n",m_iSendXh,
aInCmd+8,aInCmd+8);
m_pSocket->Send( (void*)buf, strlen(buf));
}
else if( memcmp( aInCmd, "DelUser" , 3 ) == 0 )
{
//20050529 10:00:49:540 [768] : C->S 0037 [REM 14 FL tcpip_2005@126.com 1%0d%0a]
//20050529 10:00:49:870 [768] : S->C 0039 [REM 14 FL 1 tcpip_2005@126.com 1%0d%0a]
char buf[300];
if( m_pSocket == NULL) return -1;
if( m_iLoginOk==0 ) return -2;
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "REM %d FL %s 0\r\n",m_iSendXh, aInCmd+8 );
m_pSocket->Send( (void*)buf, strlen(buf));
}
return 0;
}
int CMsnLoginClient::DispUserPage(int iInUserNo)
{
//int ilPageName,ilPageStatus,ilPageUser,ilPageEnd;
char alTmp1[2048],alTmp2[300];
int ilBytes;
CFile fLog;
CString sFile;
CString strBuf;
sFile.Format("%s\\html\\user_frm.htm",agCurDir);
if( !fLog.Open(sFile, CFile::modeRead))
{
FileLog("Failed to read %s.", sFile );
}
else
{
memset(alTmp1,0,2048);
//fLog.Seek(-600,CFile::end);
ilBytes = fLog.Read(alTmp1,2040);
FileLog("Read Bytes: %d ",ilBytes);
fLog.Close();
}
memset(alTmp2,0x00,sizeof(alTmp2));
sprintf( alTmp2,"%d_user_left.htm", iInUserNo );
strBuf.Format("%s", alTmp1 );
strBuf.Replace("user_left.htm",alTmp2);
memset(alTmp2,0x00,sizeof(alTmp2));
sprintf( alTmp2,"%d_user_bottom.htm", iInUserNo );
strBuf.Replace("user_bottom.htm",alTmp2);
sFile.Format("%s\\html\\%d_user_frm.htm",agCurDir,iInUserNo);
if( !fLog.Open(sFile, CFile::modeCreate|CFile::modeWrite))
{
FileLog("Failed to write %s .", sFile );
}
else
{
fLog.Write((LPCTSTR)strBuf,strBuf.GetLength());
fLog.Close();
FileLog("Write Bytes: %d",strBuf.GetLength());
}
sFile.Format("%s\\html\\user_bottom.htm",agCurDir);
if( !fLog.Open(sFile, CFile::modeRead))
{
FileLog("Failed to read %s.", sFile );
}
else
{
memset(alTmp1,0,2048);
ilBytes = fLog.Read(alTmp1,2040);
FileLog("Read Bytes: %d ",ilBytes);
fLog.Close();
}
memset(alTmp2,0x00,sizeof(alTmp2));
sprintf( alTmp2,"%d", iInUserNo );
strBuf.Format("%s", alTmp1 );
strBuf.Replace("_VAR_UserNo",alTmp2);
sFile.Format("%s\\html\\%d_user_bottom.htm",agCurDir,iInUserNo);
if( !fLog.Open(sFile, CFile::modeCreate|CFile::modeWrite))
{
FileLog("Failed to write %s .", sFile );
}
else
{
fLog.Write((LPCTSTR)strBuf,strBuf.GetLength());
fLog.Close();
FileLog("Write Bytes: %d",strBuf.GetLength());
}
FILE *fp;
char fname1[300];
sprintf(fname1,"%s\\html\\%d_user_left.htm",agCurDir,iInUserNo);
if (( fp = fopen(fname1,"w")) != NULL)
{
fprintf( fp,"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
fprintf( fp,"<title>UserPages</title></head><body>");
fprintf( fp,"<p><font face=\"宋体\" size=\"2\">" );
fclose( fp );
}
FileLog("PostMessage(WPARAM, LPARAM lParam DispMainPage)");
LPARAM lpTmp= iInUserNo+100;
AfxGetApp()->m_pMainWnd->PostMessage( WM_MY_MESSAGE, 1, (LPARAM)lpTmp );
return 0;
}
int CMsnLoginClient::DoUserXfrSB(int iInUserNo)
{
char buf[300];
if( m_pSocket == NULL) return -1;
if( m_iLoginOk==0 ) return -2;
m_iXfrSbUserNo=iInUserNo;
memset(buf,0x00,sizeof(buf));
m_iSendXh++;
sprintf(buf, "XFR %d SB\r\n",m_iSendXh);
m_pSocket->Send( (void*)buf, strlen(buf));
return 0;
}