// MSN.cpp : implementation file
//
#include "stdafx.h"
#include "zx.h"
#include "MSN.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMSN dialog
CMSN::CMSN(CWnd* pParent /*=NULL*/)
: CDialog(CMSN::IDD, pParent)
{
//{{AFX_DATA_INIT(CMSN)
//}}AFX_DATA_INIT
}
void CMSN::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMSN)
DDX_Control(pDX, IDC_STATUSLIST, m_stalistctr);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMSN, CDialog)
//{{AFX_MSG_MAP(CMSN)
ON_WM_TIMER()
ON_WM_SYSCOMMAND()
ON_NOTIFY(NM_DBLCLK, IDC_USERLIST, OnDblclkUserlist)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CONNECTOK,OnConnectOK)
ON_MESSAGE(WM_RECEIVEOK,OnReceiveOK)
ON_MESSAGE(WM_PWDWRONG,OnPWDWrong)
ON_MESSAGE(WM_LOGINOK,OnLoginOK)
ON_MESSAGE(WM_ENDCHAT,OnEndChat)
ON_MESSAGE(WM_STARTCHAT,OnStartChat)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMSN message handlers
void CMSN::OnOK()
{
// TODO: Add extra validation here
(CButton *)GetDlgItem(IDOK)->EnableWindow(FALSE);
try
{
Stop();
}
catch (...)
{
}
Ini();
(CButton *)GetDlgItem(IDOK)->EnableWindow(TRUE);
//CDialog::OnOK();
}
BOOL CMSN::Start() //由输入的用户名密码启动msn机器人
{
if(msnsocket.Create()==FALSE)
{
return FALSE;
}
if(msnsocket.Connect(msnserver,serverport)==FALSE)
{
if(GetLastError()==WSAEWOULDBLOCK)
{
WriteStatus("wait...");
}
else
{
return FALSE;
}
}
return TRUE;
}
BOOL CMSN::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_stalistctr.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_FLATSB|LVS_EX_GRIDLINES);
m_stalistctr.InsertColumn(0,_T("Status"),LVCFMT_LEFT,1000);
msnstate=startini;
SetTimer(1,4000,NULL);
SetTimer(2,30000,NULL);
int * timeout=new int;
*timeout=1;
msnsocket.SetSockOpt(SO_RCVTIMEO,(void *)timeout,sizeof(int));
GetWindowRect(&MSNrectsize);
m_stalistctr.GetWindowRect(&stalistoldrect);
ScreenToClient(&stalistoldrect);//将控件大小转换为在对话框中的区域坐标
Ini();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CMSN::Ini()
{
msnsocket.msnHwnd=GetSafeHwnd();
msnsocket.TrID=0;
msnsocket.lastTrID=-1;
serverport=1863;
msnserver=_T("messenger.hotmail.com");
// m_stalistctr.DeleteAllItems();
chatinguser="";
//////////////////////////////////////////////////////////////////////////
RECT ProRect=GetStaListLen();
pProCtr=new CProgressCtrl;
pProCtr->Create(WS_VISIBLE,ProRect,this,IDC_MSNPROGRESS);
pProCtr->SetRange32(0,9);
pProCtr->SetPos(msnsocket.TrID);
pProCtr->ShowWindow(SW_SHOWNORMAL);
pUserListCtr=new CMyListCtr;
pUserListCtr->Create(WS_VISIBLE,ProRect,this,IDC_USERLIST);
pUserListCtr->ModifyStyle(0,LVS_REPORT);
pUserListCtr->ModifyStyle(LVS_SORTASCENDING|LVS_SORTDESCENDING,0);
pUserListCtr->SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_FLATSB|LVS_EX_GRIDLINES);
pUserListCtr->InsertColumn(LISTUSERNAME,_T("User Name"),LVCFMT_LEFT,200);
pUserListCtr->InsertColumn(LISTNICKNAME,_T("Nickname"),LVCFMT_LEFT,200);
pUserListCtr->DeleteAllItems();
pUserListCtr->ShowWindow(SW_HIDE);
pUserImglist=new CImageList;
pUserImglist->Create(16, 16, ILC_MASK | ILC_COLORDDB, 1, 100);
pUserListCtr->SetImageList(pUserImglist, LVSIL_SMALL);
//////////////////////////////////////////////////////////////////////////
for(int i=0;i<chatvector.size();i++)
{
try
{
delete chatvector[i];
}
catch(...)
{
}
}
chatvector.clear();
PNGAsk=0;
if(Start()==TRUE)
{
msnstate=login;
return TRUE;
}
else
{
msnstate=inifaile;
return FALSE;
}
}
void CMSN::OnCancel()
{
// TODO: Add extra cleanup here
Stop();
Quit();
CDialog::OnCancel();
}
void CMSN::SetUserInfo(CString user,CString pwd)
{
m_user=user;
m_pwd=pwd;
}
void CMSN::WriteStatus(CString Status)
{
Status.TrimRight("\r\n");
m_stalistctr.InsertItem(0,Status);
}
LRESULT CMSN::OnConnectOK(WPARAM wparam,LPARAM lparam)
{
WriteStatus("connect ok!");
pProCtr->SetPos(msnsocket.TrID);
Send("VER "+msnsocket.AddTrID()+" MSNP9 MSNP8 CVR0\r\n");
return 0;
}
LRESULT CMSN::OnReceiveOK(WPARAM wparam,LPARAM lparam)
{
if(msnsocket.recstrlist.IsEmpty()==TRUE)
{
return 0;
}
CString recstr=msnsocket.recstrlist.RemoveHead();
WriteStatus(rectitle+recstr);
Engine(recstr);
return 0;
}
void CMSN::Send(CString sendstr)
{
WriteStatus(sendtitle+sendstr);
msnsocket.Send((LPCSTR)sendstr,sendstr.GetLength());
}
void CMSN::Engine(CString instr)
{
CString temp=instr;
temp.MakeUpper();
if(temp.Left(3)=="VER")
{
Send("CVR "+msnsocket.AddTrID()+" 0x0804 winnt 5.1 i386 MSNMSGR 6.0.0602 MSMSGS "+m_user+"\r\n");
pProCtr->SetPos(msnsocket.TrID);
}
else if(temp.Left(3)=="CVR")
{
Send("USR "+msnsocket.AddTrID()+" TWN I "+m_user+"\r\n");
pProCtr->SetPos(msnsocket.TrID);
}
else if(temp.Left(3)=="XFR")
{
//"XFR 3 NS 207.46.106.72:1863 0 207.46.104.20:1863\r\n"
//"XFR 12 SB 207.46.108.79:1863 CKI 17334300.1065769901.10583\r\n"
if(temp.Find("NS")!=-1)
{
int pos1=temp.Find("NS");
int pos2=temp.Find(":");
int pos3=temp.Find(" 0 ");
msnserver=temp.Mid(pos1+3,pos2-pos1-3);
CString serverportstr;
serverportstr=temp.Mid(pos2+1,pos3-pos2-1);
msnsocket.Close();
Start();
pProCtr->SetPos(msnsocket.TrID);
}
else if(temp.Find("SB")!=-1)
{
GetStringLeft(temp);
GetStringLeft(temp);
GetStringLeft(temp);
CString servername=GetStringLeft(temp);
int pos=servername.Find(":");
CString port=servername.Mid(pos+1);
servername=servername.Mid(0,pos);
GetStringLeft(temp);
CString SBcheckstr=temp;
SBcheckstr.TrimRight("\r\n");
if (chatinguser!="")
{
CMSNChat * msnchat=GetChatWin(chatinguser);
msnchat->SetUserInfo(m_user,m_pwd,GetSafeHwnd());
msnchat->SetSendInfo(SBcheckstr,chatinguser,servername,atoi(port));
msnchat->ChatTo();
}
}
}
else if(temp.Left(3)=="USR")
{
CString usrinfo;
int pos=temp.Find(" S ");
if(pos==-1)
{
pos=temp.Find("OK");
if(pos==-1)
{
}
else
{
Send("SYN "+msnsocket.AddTrID()+" 0 \r\n");
}
}
else
{
usrinfo=instr.Mid(pos+3);
CString key;
SSLCheck(usrinfo,key);
if(key!="")
{
Send("USR "+msnsocket.AddTrID()+" TWN S "+key+"\r\n");
}
}
pProCtr->SetPos(msnsocket.TrID);
}
else if(temp.Left(3)=="SYN")
{
Send("CHG "+msnsocket.AddTrID()+" NLN 268435492 \r\n");
pProCtr->SetPos(msnsocket.TrID);
}
else if(temp.Left(3)=="CHG")
{
PostMessage(WM_LOGINOK);
}
else if(temp.Left(3)=="RNG")
{
//RNG 17334300 207.46.108.79:1863 CKI 1065769901.21116 yxu68@hotmail.com \302\240\345\276\220\346\263\263\r\n
CString chatuser;
GetStringLeft(temp);
GetStringLeft(temp);
GetStringLeft(temp);
GetStringLeft(temp);
GetStringLeft(temp);
chatuser=GetStringLeft(temp);
chatuser.MakeLower();
CMSNChat * msnchat=GetChatWin(chatuser);
msnchat->SetUserInfo(m_user,m_pwd,GetSafeHwnd());
msnchat->AddRing(instr);
/*for(int i=0;i<chatvector.size();i++)
{
if(chatvector[i]->m_chatuser==chatuser)
{
chatvector[i]->SetUserInfo(m_user,m_pwd,GetSafeHwnd());
chatvector[i]->AddRing(instr);
return;
}
}
CMSNChat * msnchat;
msnchat=new CMSNChat;
msnchat->SetUserInfo(m_user,m_pwd,GetSafeHwnd());
vector<CMSNChat *> * p=(vector<CMSNChat *> *)&chatvector;
msnchat->Create(IDD_CHATDIALOG);
RECT rec