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

📄 pop3check.cpp

📁 一个邮件监控程序
💻 CPP
字号:
/*

  Pop3Check.cpp


  Realiza el chequeo de emails de cuenta pop3.

  Autor: Sergio Scarnatto

*/

#include "stdafx.h"
#include "Gniazdo.h"
#include "MailMonitorDlg.h"
#include "Pop3Check.h"
#include "AMMimeUtils.h"

#include <atlbase.h>

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

#define MAX_BUFF 20000
#define MAX_TOPLINES_EMAIL 0

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPop3Check::CPop3Check()
{
  state = FIRST;
  error = "There is no connection with the server.";
  fromUltMensaje = "";
  bufmail = "";
  newmsgs = 0;
  numMsg = 0;
}

CPop3Check::~CPop3Check()
{
}

void CPop3Check::OnReceive(int err)
{
  if (err == 0)
  {
    char buff[MAX_BUFF];
    int rec = Receive(buff, MAX_BUFF - 1); 
    buff[rec] = NULL;
    lastMsg = buff;
    ParseMsg();
  }
  else
  {
    error = "Error receiving data!";
    ((DLG)m_pWnd)->Dispatch(S_CLOSE);
  }
}

void CPop3Check::SetProp(CString u, CString p)
{
  user = u;
  pass = p;
}

void CPop3Check::ParseMsg()
{
  CString s;
  strstream str;
  string check;
  str <<(LPCSTR)lastMsg;
  str >> check;

  if (check == "-ERR") 
  {
    error = "Error -ERR from server :"+lastMsg;
    Close(); 
    return;
  }
  switch (state) 
  {
    case FIRST: 
      fromUltMensaje = "";
      newmsgs =0;
      numnewmsg = 0;
      ((DLG)m_pWnd)->Dispatch(S_RECEIVE); 
      s.Format("user %s%c%c", user, 13, 10);
      Send((LPCSTR)s, s.GetLength()); 
      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()); 
      state = PASS;
      break;
    
    case PASS:
      ((DLG)m_pWnd)->Dispatch(S_RECEIVE);
      s.Format("stat%c%c", 13, 10);
      Send((LPCSTR)s, s.GetLength()); 
      state = STAT; 
      break;
    
    case STAT:
      {
        ud->initRegMsgs();
        string s1;
        str.seekg(0);
        str >> s1 >> numMsg >> sizeMsg; 
        flush(str);
        ((DLG)m_pWnd)->Dispatch(S_GETNUMMSGS);
        ((DLG)m_pWnd)->Dispatch(S_GETSIZEMSGS);
        if (numMsg>0) 
        {
          state = UIDL;
          s.Format("uidl 1%c%c", 13, 10);
          bufmail = "";
          retrMsg = 1;
          Send((LPCSTR)s, s.GetLength()); 
        }
        else 
        {
          error = "No new messages.";
          Close();
        }
      }
      break;
    case UIDL:
      {
        string s1, s2;
        int p1;
        CString uidl;
        str.seekg(0);
        str >> s1 >> p1 >> s2;        
        uidl = s2.data();
        if (ud->IsNewMail(uidl))
        {
          newmsgs += 1;
          numnewmsg = retrMsg;
        }
        // Verifica si hay m醩 mensajes
        if (retrMsg < numMsg) 
        {
          retrMsg++;
          state = UIDL;
          s.Format("uidl %d%c%c", retrMsg, 13, 10); // pide el siguiente mensaje
          Send((LPCSTR)s, s.GetLength());
        }
        else
        {
          if (newmsgs == 1)
          {
            state = RETR;
            s.Format("top %d %d%c%c", numnewmsg, MAX_TOPLINES_EMAIL, 13, 10); 
            bufmail = "";
            Send((LPCSTR)s, s.GetLength()); 
          }
          else
          {
            state = ENDRETR;
            ((DLG)m_pWnd)->Dispatch(S_ENDRETR);
            error = "Session closed";
            s.Format("quit%c%c", 13, 10);
            Send((LPCSTR)s, s.GetLength());
            Close();
          }
        }
      }
      break;
    
    case RETR:
      {  
        bufmail += lastMsg;
      
        int where = bufmail.Find("From:");
        if (where >= 0 && where + 80 < bufmail.GetLength())
        {
          fromUltMensaje = "";
          ReadLn(where + 5, bufmail, fromUltMensaje);

          char *sfrom = (char*)calloc(fromUltMensaje.GetLength()+1, sizeof(char));
          strcpy(sfrom, fromUltMensaje);
          fromUltMensaje = MimeDecodeMailHeaderField(sfrom);
          free(sfrom);

          state = ENDRETR;
          ((DLG)m_pWnd)->Dispatch(S_ENDRETR);
          error = "Session closed";
          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 CPop3Check::Close()
{
  if (state == ENDRETR)
    ud->removeRegMsgs();

  CString str;
  str.Format("quit%c%c", 13, 10);
  Send((LPCSTR)str, str.GetLength());
  state = FIRST;
  CAsyncSocket::Close();
  ((DLG)m_pWnd)->Dispatch(S_CLOSE);
  error = "There is no connection with the server.";
}

void CPop3Check::JustClose()
{
  state = FIRST;
  CAsyncSocket::Close();
}

CString CPop3Check::GetError()
{
  return error;
}

int CPop3Check::GetNumMsg()
{
  return numMsg;
}

void CPop3Check::ReadLn(int index, CString src, CString &dst)
{
  CString comp;
  comp = src[index];
  while (comp != "\r")
  {
    dst += comp;
    comp = src[++index];
  }
}

int CPop3Check::NewMessages()
{
  return newmsgs;
}

// setea nombre del servidor
void CPop3Check::SetNomServer(CString nombre)
{
  this->nomserver = nombre;
}

// recupera el from del mensaje nuevo
CString CPop3Check::GetFromUltMensaje()
{
  return this->fromUltMensaje;
}

// setea la clase de persistencia
void CPop3Check::SetUserData(userData * vUD)
{
  this->ud = vUD;  
}

⌨️ 快捷键说明

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