📄 pop3_client.c
字号:
continue;
}
if( ch == ' ') /* Remove spaces */
continue;
/* Buffer already full for this line? */
if(pop3_client.charsinheaderbuf > 8)
continue;
/* End of header? (parsing of that is not absolutely correct) */
/* We detect it from CRLF. or LFCR. or CR. or LF. */
/* the correct indication being CRLF.CRLF */
if( (ch == '.') && (pop3_client.charsinheaderbuf == 0) ) {
/* Remove CRLF.CRLF from header length */
if( pop3_client.curmsghlen >= 5 )
pop3_client.curmsghlen -= 5;
pop3c_changestate(POP3C_TOP0_GET);
break;
}
ch = __tolower(ch);
pop3_client.headerbuf[pop3_client.charsinheaderbuf] = ch;
pop3_client.charsinheaderbuf++;
pop3_client.headerbuf[pop3_client.charsinheaderbuf] = '\0';
/* Is it 'from:' ? */
if(bufsearch(&pop3_client.headerbuf[0],pop3_client.charsinheaderbuf, (BYTE*)("from:")) == 0) {
/* Continue imidiately to read sender */
pop3_client.from[0] = '\0';
pop3_client.charsinheaderbuf = 0;
pop3c_changestate(POP3C_RECEIVING_HDR_FROM);
continue;
}
/* Is it 'subject:' ? */
if(bufsearch(&pop3_client.headerbuf[0],pop3_client.charsinheaderbuf,(BYTE*)("subject:")) == 0) {
/* Continue imidiately to read subject */
pop3_client.subject[0] = '\0';
pop3_client.charsinheaderbuf = 0;
pop3c_changestate(POP3C_RECEIVING_HDR_SUBJ);
continue;
}
} /* of RECEIVING_HEADER */
if( pop3_client.state == POP3C_RECEIVING_HDR_FROM) {
if( ch == ' '){ /* Remove spaces */
if(pop3_client.charsinheaderbuf == 0)
continue;
}
if( ch == '\r') {
pop3_client.charsinheaderbuf = 0;
pop3c_changestate(POP3C_RECEIVING_HEADER);
continue;
}
if( ch == '\n') {
pop3_client.charsinheaderbuf = 0;
pop3c_changestate(POP3C_RECEIVING_HEADER);
continue;
}
/* Store it */
pop3_client.from[pop3_client.charsinheaderbuf] = ch;
pop3_client.charsinheaderbuf++;
pop3_client.from[pop3_client.charsinheaderbuf] = '\0';
if(pop3_client.charsinheaderbuf >= POP3C_SENDERMAXLEN) {
/* The buffer is exeeded */
/* Mark it corrupted */
pop3_client.from[0] = '\0';
pop3c_changestate(POP3C_RECEIVING_HEADER);
continue;
}
} /* of RECEIVING_HDR_FROM */
if( pop3_client.state == POP3C_RECEIVING_HDR_SUBJ) {
if( ch == ' '){ /* Remove spaces */
if(pop3_client.charsinheaderbuf == 0)
continue;
}
if( ch == '\r') {
pop3_client.charsinheaderbuf = 0;
pop3c_changestate(POP3C_RECEIVING_HEADER);
continue;
}
if( ch == '\n') {
pop3_client.charsinheaderbuf = 0;
pop3c_changestate(POP3C_RECEIVING_HEADER);
continue;
}
/* Store it */
pop3_client.subject[pop3_client.charsinheaderbuf] = ch;
pop3_client.charsinheaderbuf++;
pop3_client.subject[pop3_client.charsinheaderbuf] = '\0';
if(pop3_client.charsinheaderbuf >= POP3C_SUBJECTMAXLEN) {
/* The buffer is exeeded */
/* Mark it corrupted */
pop3_client.subject[0] = '\0';
pop3c_changestate(POP3C_RECEIVING_HEADER);
continue;
}
} /* of RECEIVING_HDR_SUBJ */
}
break;
case POP3C_RETR_SENT:
if(cmd == POP3C_OK) {
DEBUGOUT("RETR +OK by POP3 server\r\n");
/* Continue imidiately to receive message */
pop3c_changestate(POP3C_RECEIVING_MSG_HEADER);
} else
break;
case POP3C_RECEIVING_MSG_HEADER:
/* Try to find empty row to detect the start of actual message */
match = 0;
NETWORK_RECEIVE_INITIALIZE(received_tcp_packet.buf_index);
for(i=0; i < (UINT16)par1; i++) {
ch = RECEIVE_NETWORK_B();
if(match == 0) {
if( (ch == '\r') || (ch == '\n') )
match++;
else
match = 0;
continue;
}
if(match == 1) {
if( (ch == '\r') || (ch == '\n'))
match++;
else
match = 0;
continue;
}
if(match == 2) {
if( (ch == '\r') || (ch == '\n'))
match++;
else
match = 0;
continue;
}
if(match == 3) {
if( (ch == '\r') || (ch == '\n')) {
match++;
/* Continue to read the actual msg */
par1 -= (i + 1);
pop3c_changestate(POP3C_RECEIVING_MSG);
break;
} else
match = 0;
continue;
}
}
/* If we don't find the end of header we will timeout */
/* on pop3c_run so no error handling here */
if( pop3_client.state != POP3C_RECEIVING_MSG)
break;
end_detect = 0;
case POP3C_RECEIVING_MSG:
/* Search is this packet end of msg and do not give */
/* CRLF.CRLF to application */
for(i=0; i < (UINT16)par1; i++) {
ch = RECEIVE_NETWORK_B();
pop3c_data(ch);
if( (ch == '\r') && (end_detect != 3))
end_detect = 0;
if( end_detect == 0 ) {
if(ch == '\r')
end_detect++;
else
end_detect = 0;
continue;
}
if( end_detect == 1 ){
if(ch == '\n')
end_detect++;
else
end_detect = 0;
continue;
}
if( end_detect == 2 ){
if(ch == '.')
end_detect++;
else
end_detect = 0;
continue;
}
if( end_detect == 3 ){
if(ch == '\r')
end_detect++;
else
end_detect = 0;
continue;
}
if( end_detect == 4 ){
if(ch == '\n') {
pop3c_changestate(POP3C_MESSAGE_RECEIVED);
return(1);
}
else
end_detect = 0;
continue;
}
}
break;
case POP3C_DELE_SENT:
if(cmd == POP3C_OK) {
DEBUGOUT("DELE +OK by POP3 server\r\n");
pop3c_changestate(POP3C_DELE_ACKED);
return(1);
}
break;
case POP3C_QUIT_SENT:
if(cmd == POP3C_OK) {
DEBUGOUT("QUIT +OK by POP3 server\r\n");
pop3c_changestate(POP3C_QUIT_ACKED);
return(1);
}
break;
default:
break;
}
return(1);
case TCP_EVENT_REGENERATE:
/* Send last packet again */
DEBUGOUT("POP3C is regenerating...\r\n");
switch (pop3_client.state) {
case POP3C_USERNAME_SENT:
pop3c_senduser();
return(1);
case POP3C_PASSWORD_SENT:
pop3c_sendpassword();
return(1);
case POP3C_STAT_SENT:
pop3c_sendstat();
return(1);
case POP3C_LIST_SENT:
pop3c_sendlist(pop3_client.curmsgindex);
return(1);
case POP3C_TOP0_SENT:
pop3c_sendtop(0);
return(1);
case POP3C_RETR_SENT:
pop3c_sendretr(pop3_client.curmsgindex);
return(1);
case POP3C_DELE_SENT:
pop3c_senddele(pop3_client.curmsgindex);
return(1);
case POP3C_QUIT_SENT:
pop3c_sendquit();
return(1);
default:
return(-1);
}
break;
default:
return(-1);
}
return(-1);
}
/********************************************************************************
Function: pop3c_run
Parameters: void
Return val: void
Date: 11.9.2002
Desc: This function should be called periodically from main loop
in order to run pop3 client 'thread'.
*********************************************************************************/
void pop3c_run (void)
{
INT16 i;
/* On that function we can send data when called by main loop */
if( pop3c_init_done == 0 )
return;
if( pop3_client.state < POP3C_OPEN_REQUESTED)
return;
/* Is there timeout of some sort? */
if(check_timer(pop3_client.tmrhandle) == 0) {
/* Yep */
(void)tcp_abort(pop3_client.sochandle);
pop3c_changestate(POP3C_CLOSED);
/* Make user callback */
pop3c_error();
return;
}
if( pop3_client.state == POP3C_OPEN_REQUESTED) {
/* We are on this state because user has requested connection */
/* but connection is not yet opened. */
/* Try to get TCP stack to accept our connection request */
(void)tcp_abort(pop3_client.sochandle); /* Release old connection */
if(tcp_connect(pop3_client.sochandle, pop3_client.remip, pop3_client.remport, 0) >= 0)
pop3c_changestate(POP3C_CONNECTIONOPEN_SENT);
return;
}
if( tcp_getstate(pop3_client.sochandle) != TCP_STATE_CONNECTED ) {
return;
}
if( tcp_checksend(pop3_client.sochandle) < 0 )
return;
/* It's connected and no unacked data so try to send */
if(pop3_client.state == POP3C_SERVER_READY) {
/* Send USER */
pop3c_senduser();
pop3c_changestate(POP3C_USERNAME_SENT);
DEBUGOUT("POP3C USER packet sent\r\n");
return;
}
if(pop3_client.state == POP3C_USERNAME_ACKED) {
/* Send PASS */
pop3c_sendpassword();
pop3c_changestate(POP3C_PASSWORD_SENT);
DEBUGOUT("POP3C PASS packet sent\r\n");
return;
}
if(pop3_client.state == POP3C_PASSWORD_ACKED) {
/* Send STAT */
pop3c_sendstat();
pop3c_changestate(POP3C_STAT_SENT);
DEBUGOUT("POP3C STAT packet sent\r\n");
return;
}
if(pop3_client.state == POP3C_STAT_GET) {
/* Still messages? */
if( pop3_client.curmsgindex < pop3_client.msgtotal ) {
pop3_client.curmsgindex++;
pop3c_sendlist(pop3_client.curmsgindex);
pop3c_changestate(POP3C_LIST_SENT);
DEBUGOUT("POP3C LIST packet sent\r\n");
return;
}
/* End of messages */
pop3c_sendquit();
pop3c_changestate(POP3C_QUIT_SENT);
DEBUGOUT("POP3C QUIT packet sent\r\n");
return;
}
if(pop3_client.state == POP3C_LIST_GET) {
/* Now we have the whole length of current message. Receive body */
pop3c_sendtop(pop3_client.curmsgindex);
pop3c_changestate(POP3C_TOP0_SENT);
DEBUGOUT("POP3C TOP packet sent\r\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -