Return TypeFunction nameArguments
hzEcodehzPop3Acc::Collect(hzVect<hzString>&,)

Declared in file: hzMailer.h
Defined in file : hzPop3.cpp

Function Logic:

0:START 1:hzChain::Clear 2:!m_Repos 3:Return E_NOINIT 4:hzTcpClient::ConnectStd rc 5:rc!=E_OK 6:Err2Txt 7:Return rc 8:hzTcpClient::SetSendTimeout rc 9:rc!=E_OK 10:Err2Txt 11:Return rc 12:hzTcpClient::SetRecvTimeout rc 13:rc!=E_OK 14:Err2Txt 15:Return rc 16:hzChain::Clear hzTcpClient::Recv rc 17:rc!=E_OK 18:Err2Txt 19:Goto done 20:S zi 21:zi!=+OK 22:rc Err2Txt 23:Goto done 24:hzChain::Clear hzChain::Printf S hzTcpClient::Send rc 25:rc!=E_OK 26:Err2Txt 27:Goto done 28:hzChain::Clear hzTcpClient::Recv rc 29:rc!=E_OK 30:Err2Txt 31:Goto done 32:S zi 33:zi!=+OK 34:rc S 35:Goto done 36:hzChain::Clear hzChain::Printf S hzTcpClient::Send rc 37:rc!=E_OK 38:Err2Txt rc 39:Goto done 40:hzChain::Clear hzTcpClient::Recv rc 41:rc!=E_OK 42:Err2Txt 43:Goto done 44:S zi 45:zi!=+OK 46:S rc 47:Goto done 48:hzChain::Clear hzChain::Printf S hzTcpClient::Send rc 49:rc!=E_OK 50:Err2Txt 51:Goto done 52:hzChain::Clear 53:; 54:hzTcpClient::Recv rc 55:rc!=E_OK 56:Err2Txt 57:Goto done 58:hzChain::Size zi hzChain::Size zi 59:zi==\r\n.\r\n 60:S zi 61:zi!=+OK 62:rc Err2Txt 63:Goto done 64:!zi.eof()&&*zi!=(char)10; 65:items 66:!zi.eof(); 67:!zi.eof()&&IsDigit(*zi); 68:hzChain::AddByte hzChain::Size 69:!word.Size() 70:S atoi mailId hzChain::Clear 71:!zi.eof(); 72:*zi==(char)13 73:items 74:*zi==(char)10 75:hzChain::AddByte 76:S hzChain::Clear hzSet::Exists 77:!m_Already.Exists(S) 78:hzMapS::Insert hzChain::Iter::eof 79:zi.eof() 80:nIndex 81:hzMapS::GetKey mailId hzMapS::GetObj S nIndex 82:rc==E_OK&&nIndex 83:hzMapS::GetKey mailId hzMapS::GetObj S hzChain::Clear hzChain::Printf hzTcpClient::Send rc 84:rc!=E_OK 85:hzChain::Clear 86:; 87:hzTcpClient::Recv rc 88:rc!=E_OK 89:hzChain::Size zi hzChain::Size zi 90:zi==\r\n.\r\n 91:hzChain::Clear zi 92:zi!=+OK 93:rc S 94:!zi.eof()&&*zi!=(char)10; 95:*zi!=(char)10 96:!zi.eof(); 97:*zi==(char)13 98:zi==\r\n.\r\n 99:zi hzChain::Iter::eof 100:zi.eof() 101:zi 102:hzChain::AddByte 103:path ofstream::open ofstream::fail 104:os.fail() 105:items close ofstream::clear hzVect::Add 106:close items hzTcpClient::Close 107:Return rc

Function body:

hzEcode hzPop3Acc::Collect (hzVect<hzString>& messages)
{
   _hzfunc("hzPop3Acc::Collect") ;
   hzMapS<int32_t,hzString>    temp ;
   ofstream        os ;
   hzTcpClient     P ;
   chIter          zi ;
   hzChain         Z ;
   hzChain         word ;
   hzChain         msg ;
   hzString        S ;
   hzString        path ;
   uint32_t        mailId ;
   uint32_t        nIndex ;
   hzEcode         rc ;
   m_Error.Clear() ;
   if (!m_Repos)
   {
       threadLog("POP3 Account not initialized\n") ;
       return E_NOINIT ;
   }
   rc = P.ConnectStd(m_Server, 110);
   if (rc != E_OK)
   {
       threadLog("Cannot connect to email server %s (error=%s)\n", *m_Server, Err2Txt(rc)) ;
       return rc ;
   }
   rc = P.SetSendTimeout(30);
   if (rc != E_OK)
   {
       threadLog("Could not set send_timeout on connection to POP3 server (error=%s)\n", Err2Txt(rc)) ;
       return rc ;
   }
   rc = P.SetRecvTimeout(30);
   if (rc != E_OK)
   {
       threadLog("Could not set recv_timeout on connection to POP3 server (error=%s)\n", Err2Txt(rc)) ;
       return rc ;
   }
   Z.Clear() ;
   rc = P.Recv(Z) ;
   if (rc != E_OK)
   {
       threadLog("Cannot recv server hello (error=%s)\n", Err2Txt(rc)) ;
       goto done ;
   }
   S = Z ;
   threadLog("Server: [%s]\n", *S) ;
   zi = Z ;
   if (zi != "+OK")
   {
       rc = E_PROTOCOL ;
       threadLog("Expected +OK as hello from server. (error=%s)\n", Err2Txt(rc)) ;
       goto done ;
   }
   Z.Clear() ;
   Z.Printf("USER %s\r\n", *m_Username) ;
   S = Z ;
   threadLog("Client: [%s]\n", *S) ;
   rc = P.Send(Z) ;
   if (rc != E_OK)
   {
       threadLog("Cannot send username to email server (error=%s)\n", Err2Txt(rc)) ;
       goto done ;
   }
   Z.Clear() ;
   rc = P.Recv(Z) ;
   if (rc != E_OK)
   {
       threadLog("Cannot recv response to username from email server (error=%s)\n", Err2Txt(rc)) ;
       goto done ;
   }
   S = Z ;
   threadLog("Server: [%s]\n", *S) ;
   zi = Z ;
   if (zi != "+OK")
   {
       rc = E_PROTOCOL ;
       S = Z ;
       threadLog("Expected +OK response to username (got=%s)\n", *S) ;
       goto done ;
   }
   Z.Clear() ;
   Z.Printf("PASS %s\r\n", *m_Password) ;
   S = Z ;
   threadLog("Client: [%s]\n", *S) ;
   rc = P.Send(Z) ;
   if (rc != E_OK)
   {
       threadLog("Cannot send password to email server (error=%s)\n", Err2Txt(rc)) ;
       rc = E_PROTOCOL ;
       goto done ;
   }
   Z.Clear() ;
   rc = P.Recv(Z) ;
   if (rc != E_OK)
   {
       threadLog("Cannot recv response to username from email server (error=%s)\n", Err2Txt(rc)) ;
       goto done ;
   }
   S = Z ;
   threadLog("Server: [%s]\n", *S) ;
   zi = Z ;
   if (zi != "+OK")
   {
       S = Z ;
       threadLog("Expected +OK to password. (got=%s)\n", *S) ;
       rc = E_PROTOCOL ;
       goto done ;
   }
   Z.Clear() ;
   Z.Printf("UIDL\r\n") ;
   S = Z ;
   threadLog("Client: [%s]\n", *S) ;
   rc = P.Send(Z) ;
   if (rc != E_OK)
   {
       threadLog("Cannot send UIDL command to email server (error=%s)\n", Err2Txt(rc)) ;
       goto done ;
   }
   Z.Clear() ;
   for (;;)
   {
       rc = P.Recv(Z) ;
       if (rc != E_OK)
       {
           threadLog("Cannot recv response to UIDL from email server (error=%s)\n", Err2Txt(rc)) ;
           goto done ;
       }
       threadLog("Server - Response to UIDL of %d bytes\n", Z.Size()) ;
       zi = Z ;
       zi += (Z.Size() - 5);
       if (zi == "\r\n.\r\n")
           break ;
   }
   S = Z ;
   threadLog("Server: [%s]\n", *S) ;
   zi = Z ;
   if (zi != "+OK")
   {
       rc = E_PROTOCOL ;
       threadLog("Expected +OK response to UIDL (error=%s)\n", Err2Txt(rc)) ;
       goto done ;
   }
   for (; !zi.eof() && *zi != CHAR_NL ; zi++) ;
   zi++ ;
   for (; !zi.eof() ; zi++)
   {
       for (; !zi.eof() && IsDigit(*zi) ; zi++)
           word.AddByte(*zi) ;
       if (!word.Size())
           break ;
       S = word ;
       mailId = atoi(*S) ;
       word.Clear() ;
       for (zi++ ; !zi.eof() ; zi++)
       {
           if (*zi == CHAR_CR)
               zi++ ;
           if (*zi == CHAR_NL)
               break ;
           word.AddByte(*zi) ;
       }
       S = word ;
       word.Clear() ;
       if (!m_Already.Exists(S))
           temp.Insert(mailId, S) ;
       if (zi.eof())
           break ;
   }
   for (nIndex = 0; nIndex < temp.Count() ; nIndex++)
   {
       mailId = temp.GetKey(nIndex) ;
       S = temp.GetObj(nIndex) ;
       threadLog("Mailbox has %d %d (%s)\n", nIndex, mailId, *S) ;
   }
   for (nIndex = 0; rc == E_OK && nIndex < temp.Count() ; nIndex++)
   {
       mailId = temp.GetKey(nIndex) ;
       S = temp.GetObj(nIndex) ;
       Z.Clear() ;
       Z.Printf("RETR %d\r\n", mailId) ;
       rc = P.Send(Z) ;
       if (rc != E_OK)
           { threadLog("Cannot send RETR command to email server\n") ; break ; }
       Z.Clear() ;
       for (;;)
       {
           rc = P.Recv(Z) ;
           if (rc != E_OK)
               { threadLog("Cannot recv response to RETR command\n") ; break ; }
           threadLog("Server - Message of %d bytes\n", Z.Size()) ;
           zi = Z ;
           zi += (Z.Size() - 5);
           if (zi == "\r\n.\r\n")
               break ;
       }
       msg.Clear() ;
       zi = Z ;
       if (zi != "+OK")
       {
           rc = E_OK ;
           S = Z ;
           threadLog("Expected +OK in response to RETR command (got=%s)\n", *S) ;
           continue ;
       }
       for (zi += 3; !zi.eof() && *zi != CHAR_NL ; zi++) ;
       if (*zi != CHAR_NL)
       {
           threadLog("Malformed +OK response to RETR command\n") ;
           continue ;
       }
       for (zi++ ; !zi.eof() ; zi++)
       {
           if (*zi == CHAR_CR)
           {
               if (zi == "\r\n.\r\n")
               {
                   zi += 5;
                   if (zi.eof())
                       break ;
                   zi -= 5;
               }
           }
           msg.AddByte(*zi) ;
       }
       path = m_Repos + "/" + S ;
       os.open(*path) ;
       if (os.fail())
           threadLog("Cannot open file %s for writting\n", *path) ;
       else
       {
           threadLog("Writing message file %s\n", *path) ;
           os << msg ;
           os.close() ;
           os.clear() ;
           messages.Add(S) ;
       }
   }
   os.close() ;
done:
   P.Close() ;
   return rc ;
}