Pop3.cpp
资源名称:POP3SMTP.rar [点击查看]
上传用户:pyhyhg
上传日期:2022-08-11
资源大小:56k
文件大小:6k
源码类别:
Email客户端
开发平台:
Visual C++
- // Pop31.cpp: implementation of the CPop3 class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Pop3Base4.h"
- #include "POP3SMTPDlg.h"
- #include "Pop3.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- #define MAX_BUFF 20000
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CPop3::CPop3()
- {
- state=FIRST;
- error="Not ed to serverrn";
- delAfterRead=FALSE;
- }
- CPop3::~CPop3()
- {
- }
- void CPop3::OnReceive(int err)
- {
- if(err==0) //if no error
- {
- char buff[MAX_BUFF];
- int rec=Receive(buff,MAX_BUFF); //receive data
- buff[rec]=NULL;
- lastMsg=buff;
- ParseMsg(); //parse data
- }
- else
- {
- error="Error while receiving!rn";
- ((DLG)m_pWnd)->Dispatch(S_CLOSE);
- }
- }
- void CPop3::GetLastMsg(CString &s)
- {
- s=lastMsg;
- }
- void CPop3::SetProp(CString u, CString p)
- {
- user=u;
- pass=p;
- }
- void CPop3::ParseMsg()
- {
- CString s;
- strstream str;
- string check;
- str<<(LPCSTR)lastMsg;
- str>>check;
- if(check=="-ERR") //if there is an error
- {
- error="从服务器收到 -ERR:"+lastMsg;
- Close(); //dis and stop
- return;
- }
- switch(state) //what should we do next?
- {
- case FIRST: //if we are already ed
- msgs.clear();
- ((DLG)m_pWnd)->Dispatch(S_RECEIVE); //send message to parent
- s.Format("user %s%c%c",user,13,10);
- Send((LPCSTR)s,s.GetLength()); //send user id
- state=USER;
- break;
- case USER:
- ((DLG)m_pWnd)->Dispatch(S_RECEIVE);
- s.Format("pass %s%c%c",pass,13,10);
- Send((LPCSTR)s,s.GetLength()); //now send password
- state=PASS;
- break;
- case PASS:
- ((DLG)m_pWnd)->Dispatch(S_RECEIVE);
- s.Format("stat%c%c",13,10);
- Send((LPCSTR)s,s.GetLength()); //now send stat request
- state=STAT;
- break;
- case STAT:
- {
- string s1;
- str.seekg(0);
- str>>s1>>numMsg>>sizeMsg; //obtain number and size of messages
- flush(str);
- ((DLG)m_pWnd)->Dispatch(S_GETNUMMSGS);
- ((DLG)m_pWnd)->Dispatch(S_GETSIZEMSGS);
- if(numMsg>0) //if there are any messages then send retr command
- {
- state=RETR;
- s.Format("retr 1%c%c",13,10);
- retrMsg=1;
- MESSAGEPROP prop;
- prop.msgSize=0;
- prop.retrSize=0;
- prop.text="";
- msgs.push_back(prop);
- Send((LPCSTR)s,s.GetLength()); //send retr request
- }
- else //if not, dis
- {
- error="没有新的信件!rn";
- Close();
- }
- }
- break;
- case RETR:
- {
- if(msgs[retrMsg-1].msgSize==0) //if it's first data then skip +OK
- {
- string temp;
- str.seekg(0);
- str>>temp>>msgs[retrMsg-1].msgSize; //and obtain size of message
- }
- msgs[retrMsg-1].text+=lastMsg; //append data to messagetext
- msgs[retrMsg-1].retrSize+=lastMsg.GetLength(); //add data size to retr size
- if(msgs[retrMsg-1].retrSize>=msgs[retrMsg-1].msgSize) //if we've got whole message
- { //check if there are any other messages
- if(retrMsg<numMsg) //if yes
- {
- MESSAGEPROP prop;
- prop.msgSize=0;
- prop.retrSize=0;
- prop.text="";
- msgs.push_back(prop);
- retrMsg++;
- s.Format("retr %d%c%c",retrMsg,13,10); //request another
- Send((LPCSTR)s,s.GetLength());
- }
- else
- {
- //we've got now all messages
- if(delAfterRead && numMsg>0) //we want to delete them from server
- {
- state=DELE;
- delMsg=1;
- s.Format("dele %d%c%c",delMsg,13,10);
- Send((LPCSTR)s,s.GetLength());
- }
- else //leave them and dis
- {
- state=ENDRETR;
- ((DLG)m_pWnd)->Dispatch(S_ENDRETR);
- error="Session endedrn";
- s.Format("quit%c%c",13,10);
- Send((LPCSTR)s,s.GetLength());
- Close();
- }
- }
- }
- }break;
- case DELE:
- {
- //delete another message
- if(delMsg<numMsg)
- {
- delMsg++;
- s.Format("dele %d%c%c",delMsg,13,10);
- Send((LPCSTR)s,s.GetLength());
- }
- else //no more messages - dis
- {
- ((DLG)m_pWnd)->Dispatch(S_ENDRETR);
- state=GOON;
- error="Deleted all messagesrn";
- s.Format("quit%c%c",13,10);
- Send((LPCSTR)s,s.GetLength());
- Close();
- }
- }
- break;
- case GOON: //default
- default:
- ((DLG)m_pWnd)->Dispatch(S_RECEIVE);
- break;
- }
- }
- void CPop3::Close()
- {
- CString str;
- str.Format("quit%c%c",13,10);
- Send((LPCSTR)str,str.GetLength());
- ((DLG)m_pWnd)->Dispatch(S_CLOSE);
- state=FIRST;
- CAsyncSocket::Close();
- error="Not ed to serverrn";
- }
- CString CPop3::GetError()
- {
- return error;
- }
- int CPop3::GetNumMsg()
- {
- return numMsg;
- }
- int CPop3::GetSizeMsg()
- {
- return sizeMsg;
- }
- int CPop3::GetRetrMsgNum()
- {
- return msgs.size();
- }
- CString CPop3::GetMsg(int i)
- {
- return msgs[i].text;
- }
- CString CPop3::GetMsgSubject(int i)
- {
- int where=msgs[i].text.Find("Subject:");
- CString ret;
- ReadLn(where,msgs[i].text,ret);
- return ret;
- }
- void CPop3::DelAfterRead(BOOL del)
- {
- delAfterRead=del;
- }
- CString CPop3::GetMsgBody(int i)
- {
- CString ret;
- int where=msgs[i].text.Find("rnrn");
- if(where!=-1)
- where+=4;
- else where=0;
- ret=msgs[i].text.Right(msgs[i].text.GetLength()-where);
- ret=ret.Left(ret.GetLength()-3);
- return ret;
- }
- CString CPop3::GetMsgStuff(int i)
- {
- CString ret;
- int where=msgs[i].text.Find("From:");
- ReadLn(where,msgs[i].text,ret);
- ret+="rn";
- where=msgs[i].text.Find("To:");
- ReadLn(where,msgs[i].text,ret);
- ret+="rn";
- where=msgs[i].text.Find("Date:");
- ReadLn(where,msgs[i].text,ret);
- ret+="rn";
- ret+=GetMsgSubject(i);
- ret+="rn";
- return ret;
- }
- void CPop3::ReadLn(int index,CString src, CString &dst)
- {
- CString comp;
- comp=src[index];
- while(comp!="r")
- {
- dst+=comp;
- comp=src[++index];
- }
- }
- void CPop3::WriteMsg(int i, CString name)
- {
- CStdioFile file(name,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
- file.Write((LPCSTR)msgs[i].text,msgs[i].text.GetLength());
- file.Close();
- }