- #include "StdAfx.h"
- #include "Pop3Message.h"
- #include "MIMECode.h"
- #include "Base64.h"
- CPop3Message::CPop3Message(void)
- {
- }
- CPop3Message::~CPop3Message(void)
- {
- }
- ///<summary>
- /// pass the mail header to this object
- ///</summary>
- void CPop3Message::SetTopMailContent(LPCTSTR pszMail)
- {
- m_strData = pszMail;
- DecodeHeader();
- }
- ///<summary>
- /// pass all the mail to this object
- ///</summary>
- ///<param name = pszMail>
- /// the content of the mail
- ///</param>
- void CPop3Message::SetMailContent(IN LPCTSTR pszMail)
- {
- m_strData = pszMail;
- ExtractMailContent();
- }
- ///<summary>
- /// override the operator
- ///</summary>
- BOOL CPop3Message::operator ==(const CPop3Message& cMsg)
- {
- return ((cMsg.GetMailSubject() == m_strSubject)
- && (cMsg.GetMailFrom() == m_strFrom)
- && (cMsg.GetMailDate() == m_strDate));
- }
- ///<summary>
- /// parse the mail header
- ///</summary>
- void CPop3Message::DecodeHeader()
- {
- int nStartpos = -1;
- int nEndpos = -1;
- CString strSearchFor;
- //We can assume that there's a CR/LF before each of the tags, as the servers insert
- //Received: lines on top of the mail while transporting the mail
- strSearchFor="rnFrom: ";
- nStartpos = m_strData.Find(strSearchFor);
- if (nStartpos < 0)
- {
- m_strFrom.Empty();
- }
- else
- {
- nEndpos = m_strData.Mid(nStartpos + strSearchFor.GetLength()).Find("rn");
- m_strFrom = m_strData.Mid(nStartpos + strSearchFor.GetLength(), nEndpos);
- ParseFrom();
- }
- strSearchFor = "rnDate:";
- nStartpos = m_strData.Find(strSearchFor);
- if (nStartpos < 0)
- {
- m_strDate.Empty();
- }
- else
- {
- nEndpos = m_strData.Mid(nStartpos + strSearchFor.GetLength()).Find("rn");
- m_strDate = m_strData.Mid(nStartpos + strSearchFor.GetLength(), nEndpos);
- ParseTime();
- }
- strSearchFor = "rnSubject:";
- nStartpos = m_strData.Find(strSearchFor);
- if (nStartpos < 0)
- {
- m_strSubject.Empty();
- }
- else
- {
- nEndpos = m_strData.Mid(nStartpos + strSearchFor.GetLength()).Find("rn");
- m_strSubject = m_strData.Mid(nStartpos + strSearchFor.GetLength(), nEndpos);
- ParseSubject();
- }
- }
- ///<summary>
- /// parse the mail to extract the mail content
- ///</summary>
- void CPop3Message::ExtractMailContent()
- {
- int nPos = -1;
- nPos = m_strData.Find("Content-Type: text/plain");
- if (-1 == nPos)
- {
- //only process the text part now
- return;
- }
- m_strData = m_strData.Right(m_strData.GetLength() - nPos - 24);
- nPos = -1;
- nPos = m_strData.Find("Content-Transfer-Encoding: ");
- if (-1 == nPos)
- {
- return;
- }
- m_strData = m_strData.Right(m_strData.GetLength() - nPos - 27);
- nPos = -1;
- nPos = m_strData.Find("7bit");
- if (0 == nPos)
- {
- m_strData = m_strData.Right(m_strData.GetLength() - nPos - 8);//7bitrnrn
- nPos = -1;
- nPos = m_strData.Find("--");
- if (-1 != nPos)
- {
- m_strContent = m_strData.Left(nPos);
- }
- return;
- }
- nPos = -1;
- nPos = m_strData.Find("base64");
- if (0 == nPos)
- {
- m_strData = m_strData.Right(m_strData.GetLength() - nPos - 10);//base64rnrn
- nPos = -1;
- nPos = m_strData.Find("--");
- if (-1 != nPos)
- {
- CString strContent;
- strContent = m_strData.Left(nPos);
- CBase64 base64;
- m_strContent = base64.Decode(strContent);
- }
- }
- }
- ///<summary>
- /// acquire the sender of the mail
- ///</summary>
- CString CPop3Message::GetMailFrom()const
- {
- return m_strFrom;
- }
- ///<summary>
- /// acquire the subject of the mail
- ///</summary>
- CString CPop3Message::GetMailSubject()const
- {
- return m_strSubject;
- }
- ///<summary>
- /// acquire the Date of the mail
- ///</summary>
- CString CPop3Message::GetMailDate()const
- {
- return m_strDate;
- }
- ///<summary>
- /// acquire the mail content
- ///</summary>
- CString CPop3Message::GetMailContent()const
- {
- return m_strContent;
- }
- ///<summary>
- /// parse the time format to give a more proper one to show
- ///</summary>
- void CPop3Message::ParseTime()
- {
- //we assume that the time format is like Wed, 15 Oct 2008 14:08:40 +0800
- CString strTmp1, strTmp2;
- int nPos = -1;
- nPos = m_strDate.Find(',');
- if (nPos > 0)
- {
- strTmp1 = m_strDate.Left(nPos);
- strTmp1.Trim();
- strTmp1 = ConvertWeekToChinese(strTmp1);
- m_strDate = m_strDate.Right(m_strDate.GetLength() - nPos - 2);
- }
- else
- {
- return;
- }
- nPos = -1;
- nPos = m_strDate.Find(' ');
- if (nPos > 0)
- {
- strTmp2 = m_strDate.Left(nPos);
- m_strDate = m_strDate.Right(m_strDate.GetLength() - nPos - 1);
- strTmp2 += _T("日");
- strTmp2 += strTmp1;
- }
- else
- {
- return;
- }
- nPos = -1;
- nPos = m_strDate.Find(' ');
- if (nPos > 0)
- {
- strTmp1 = m_strDate.Left(nPos);
- strTmp1 = ConvertMonthToChinese(strTmp1);
- m_strDate = m_strDate.Right(m_strDate.GetLength() - nPos - 1);
- strTmp1 += strTmp2;
- }
- else
- {
- return;
- }
- nPos = -1;
- nPos = m_strDate.Find(' ');
- if (nPos > 0)
- {
- strTmp2 = m_strDate.Left(nPos);
- m_strDate = m_strDate.Right(m_strDate.GetLength() - nPos - 1);
- strTmp2 += _T("年");
- strTmp2 += strTmp1;
- }
- else
- {
- return;
- }
- nPos = -1;
- nPos = m_strDate.Find(' ');
- if (nPos > 0)
- {
- strTmp1 = m_strDate.Left(nPos);
- m_strDate = m_strDate.Right(m_strDate.GetLength() - nPos - 1);
- strTmp2 += strTmp1;
- m_strDate = strTmp2;
- }
- else
- {
- return;
- }
- }
- ///<summary>
- /// convert the English name referred to week into Chinese
- ///</summary>
- CString CPop3Message::ConvertWeekToChinese(IN const CString& strEng)
- {
- int nPos = -1;
- nPos = strEng.Find("Mon");
- if (nPos >= 0)
- {
- return _T(" (星期一) ");
- }
- nPos = -1;
- nPos = strEng.Find("Tue");
- if (nPos >= 0)
- {
- return _T(" (星期二) ");
- }
- nPos = -1;
- nPos = strEng.Find("Wed");
- if (nPos >= 0)
- {
- return _T(" (星期三) ");
- }
- nPos = -1;
- nPos = strEng.Find("Thu");
- if (nPos >= 0)
- {
- return _T(" (星期四) ");
- }
- nPos = -1;
- nPos = strEng.Find("Fri");
- if (nPos >= 0)
- {
- return _T(" (星期五) ");
- }
- nPos = -1;
- nPos = strEng.Find("Sat");
- if (nPos >= 0)
- {
- return _T(" (星期六) ");
- }
- nPos = -1;
- nPos = strEng.Find("Sun");
- if (nPos >= 0)
- {
- return _T(" (星期日) ");
- }
- ASSERT(FALSE);
- return _T(" (未知) ");
- }
- ///<summary>
- /// convert the English name referred to month into Chinese
- ///</summary>
- CString CPop3Message::ConvertMonthToChinese(IN const CString& strEng)
- {
- if (_T("Jan") == strEng)
- {
- return _T("1月");
- }
- else if (_T("Feb") == strEng)
- {
- return _T("2月");
- }
- else if (_T("Mar") == strEng)
- {
- return _T("3月");
- }
- else if (_T("Apr") == strEng)
- {
- return _T("4月");
- }
- else if (_T("May") == strEng)
- {
- return _T("5月");
- }
- else if (_T("Jun") == strEng)
- {
- return _T("6月");
- }
- else if (_T("Jul") == strEng)
- {
- return _T("7月");
- }
- else if (_T("Aug") == strEng)
- {
- return _T("8月");
- }
- else if (_T("Sep") == strEng)
- {
- return _T("9月");
- }
- else if (_T("Oct") == strEng)
- {
- return _T("10月");
- }
- else if (_T("Nov") == strEng)
- {
- return _T("11月");
- }
- else if (_T("Dec") == strEng)
- {
- return _T("12月");
- }
- ASSERT(FALSE);
- return _T(" (未知)");
- }
- ///<summary>
- /// parse the "From" part
- ///</summary>
- void CPop3Message::ParseFrom()
- {
- int nPos = -1;
- nPos = m_strFrom.Find('<');
- if (nPos > 0)
- {
- int nEndPos = -1;
- nEndPos = m_strFrom.Find('>');
- m_strFrom = m_strFrom.Mid(nPos + 1, nEndPos - nPos - 1);
- }
- }
- ///<summary>
- /// parse the "Subject" part
- ///</summary>
- void CPop3Message::ParseSubject()
- {
- CString strHead;
- strHead = m_strSubject.Left(2);
- if (strHead != _T("=?"))
- {
- return;
- }
- m_strSubject = m_strSubject.Right(m_strSubject.GetLength() - 2);//delete the "=?"
- int nPos = -1;
- nPos = m_strSubject.Find('?');
- if (nPos > 0)
- {
- m_strSubject = m_strSubject.Right(m_strSubject.GetLength() - nPos - 1);
- nPos = -1;
- nPos = m_strSubject.Find('?');
- if (nPos > 0)
- {
- m_strSubject = m_strSubject.Right(m_strSubject.GetLength() - nPos - 1);
- m_strSubject = m_strSubject.Left(m_strSubject.GetLength() - 2);//delete the "?="
- CBase64 base64;
- m_strSubject = base64.Decode(m_strSubject);
- }
- }
- }