| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hzPop3Acc::Collect | (hzVect<hzString>&,) |
Declared in file: hzMailer.h
Defined in file : hzPop3.cpp
Function Logic:
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 ;
}