📄 pop3_client.c
字号:
return;
}
if(pop3_client.state == POP3C_TOP0_GET) {
/* Offer the e-mail to sender */
if((pop3_client.curmsgtotlen + 100 )> pop3_client.curmsghlen) {
if(pop3_client.curmsgtotlen < pop3_client.curmsghlen)
i = pop3c_msgoffer(pop3_client.curmsgindex, 0, &pop3_client.from[0], &pop3_client.subject[0]);
else
i = pop3c_msgoffer(pop3_client.curmsgindex, pop3_client.curmsgtotlen - pop3_client.curmsghlen, &pop3_client.from[0], &pop3_client.subject[0]);
if( i == (-2) ) {
/* User want's the mail to be deleted directly */
pop3c_senddele(pop3_client.curmsgindex);
pop3c_changestate(POP3C_DELE_SENT);
DEBUGOUT("POP3C deleting the e-mail\r\n");
return;
}
if( i == (-1) ) {
/* User want's the mail to be left on server without reading it */
/* So goto next one */
pop3c_changestate(POP3C_STAT_GET);
return;
}
if( i >= 0 ) {
/* User wants to read and delete the mail normally */
pop3c_sendretr(pop3_client.curmsgindex);
pop3c_changestate(POP3C_RETR_SENT);
DEBUGOUT("POP3C reading the e-mail\r\n");
return;
}
return;
}
/* The mail is somehow corrupted, just delete it */
pop3c_senddele(pop3_client.curmsgindex);
pop3c_changestate(POP3C_DELE_SENT);
DEBUGOUT("POP3C deleting CORRUPTED e-mail\r\n");
return;
}
if(pop3_client.state == POP3C_MESSAGE_RECEIVED) {
/* Delete the readed message */
pop3c_senddele(pop3_client.curmsgindex);
pop3c_changestate(POP3C_DELE_SENT);
DEBUGOUT("POP3C deleting readed e-mail\r\n");
return;
}
if(pop3_client.state == POP3C_DELE_ACKED) {
/* Goto next one */
pop3c_changestate(POP3C_STAT_GET);
return;
}
if(pop3_client.state == POP3C_QUIT_ACKED) {
/* Try to close TCP */
if(tcp_close(pop3_client.sochandle) >= 0) {
pop3c_changestate(POP3C_CLOSED);
pop3c_allok();
DEBUGOUT("POP3C connection closed OK\r\n");
return;
}
/* Close is not accepted by TCP. See if timeout */
if(check_timer(pop3_client.tmrhandle) == 0) {
/* Use brute force */
(void)tcp_abort(pop3_client.sochandle);
pop3c_changestate(POP3C_CLOSED);
pop3c_allok();
DEBUGOUT("POP3C connection closed by ABORT\r\n");
return;
}
/* Keep trying untill timeout */
return;
}
return;
}
void pop3c_senduser (void)
{
INT8 i;
UINT8* buf;
/* Fill TCP Tx buffer with "USER " and use callback function */
/* pop3c_getusername in order to get the username */
/* that combined "USER username" to POP3 server */
buf = &net_buf[TCP_APP_OFFSET];
*buf++ = 'U';
*buf++ = 'S';
*buf++ = 'E';
*buf++ = 'R';
*buf++ = ' ';
i = pop3c_getusername(buf);
if(i < 0)
return;
buf += i;
/* Insert >CRLF */
*buf++ = '\r';
*buf = '\n';
(void)tcp_send(pop3_client.sochandle, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, i + 7);
}
void pop3c_sendpassword (void)
{
INT8 i;
UINT8* buf;
/* Fill TCP Tx buffer with "PASS " and use callback function */
/* pop3c_getpassword in order to get the password */
/* that combined "PASS password" to POP3 server */
buf = &net_buf[TCP_APP_OFFSET];
*buf++ = 'P';
*buf++ = 'A';
*buf++ = 'S';
*buf++ = 'S';
*buf++ = ' ';
i = pop3c_getpassword(buf);
if(i < 0)
return;
buf += i;
/* Insert >CRLF */
*buf++ = '\r';
*buf = '\n';
(void)tcp_send(pop3_client.sochandle, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, i + 7);
}
void pop3c_sendstat (void)
{
UINT8* buf;
/* Fill TCP Tx buffer with "STAT\r\n" and send it to POP3 server */
buf = &net_buf[TCP_APP_OFFSET];
*buf++ = 'S';
*buf++ = 'T';
*buf++ = 'A';
*buf++ = 'T';
*buf++ = '\r';
*buf = '\n';
(void)tcp_send(pop3_client.sochandle, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, 6);
}
void pop3c_sendlist (UINT16 msgnbr)
{
UINT8* buf;
INT16 i;
/* Ask LIST of given message number in order to get the total len of it */
buf = &net_buf[TCP_APP_OFFSET];
*buf++ = 'L';
*buf++ = 'I';
*buf++ = 'S';
*buf++ = 'T';
*buf++ = ' ';
(void)__ltoa((UINT32)msgnbr, buf);
i = __strlen(buf,40);
if(i<0)
return;
buf += i;
*buf++ = '\r';
*buf = '\n';
(void)tcp_send(pop3_client.sochandle, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, i + 7);
}
void pop3c_sendtop (UINT16 msgnbr)
{
UINT8* buf;
INT16 i;
/* Ask TOP msgnbr 0 in order to get the header of msg */
buf = &net_buf[TCP_APP_OFFSET];
*buf++ = 'T';
*buf++ = 'O';
*buf++ = 'P';
*buf++ = ' ';
(void)__ltoa( (UINT32)msgnbr, buf);
i = __strlen(buf,40);
if(i<0)
return;
buf += i;
*buf++ = ' ';
*buf++ = '0';
*buf++ = '\r';
*buf = '\n';
(void)tcp_send(pop3_client.sochandle, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, i + 8);
}
void pop3c_sendretr (UINT16 msgnbr)
{
UINT8* buf;
INT16 i;
/* Ask RETR of given message number in order to get the message */
buf = &net_buf[TCP_APP_OFFSET];
*buf++ = 'R';
*buf++ = 'E';
*buf++ = 'T';
*buf++ = 'R';
*buf++ = ' ';
(void)__ltoa( (UINT32)msgnbr, buf);
i = __strlen(buf,40);
if(i<0)
return;
buf += i;
*buf++ = '\r';
*buf = '\n';
(void)tcp_send(pop3_client.sochandle, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, i + 7);
}
void pop3c_senddele (UINT16 msgnbr)
{
UINT8* buf;
INT16 i;
/* Ask DELE of given message number in order to delete it */
buf = &net_buf[TCP_APP_OFFSET];
*buf++ = 'D';
*buf++ = 'E';
*buf++ = 'L';
*buf++ = 'E';
*buf++ = ' ';
(void)__ltoa( (UINT32)msgnbr, buf);
i = __strlen(buf,40);
if(i<0)
return;
buf += i;
*buf++ = '\r';
*buf = '\n';
(void)tcp_send(pop3_client.sochandle, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, i + 7);
}
void pop3c_sendquit (void)
{
UINT8* buf;
/* Fill TCP Tx buffer with "QUIT\r\n" and send it to POP3 server */
buf = &net_buf[TCP_APP_OFFSET];
*buf++ = 'Q';
*buf++ = 'U';
*buf++ = 'I';
*buf++ = 'T';
*buf++ = '\r';
*buf = '\n';
(void)tcp_send(pop3_client.sochandle, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, 6);
}
INT16 pop3c_parsestat (void)
{
/* Parse total number of messages */
UINT8 i;
UINT8 ch;
if( RECEIVE_NETWORK_B() != '+')
return(-1);
if( __tolower(RECEIVE_NETWORK_B()) != 'o')
return(-1);
if( __tolower(RECEIVE_NETWORK_B()) != 'k')
return(-1);
if( RECEIVE_NETWORK_B() != ' ')
return(-1);
pop3_client.msgtotal = 0;
for(i=0; i<5; i++) {
ch = RECEIVE_NETWORK_B();
if(ch == ' ')
return(1);
if( isnumeric(ch) == 0)
return(-1);
ch = asciitohex(ch);
pop3_client.msgtotal *= 10;
pop3_client.msgtotal += ch;
}
/* If we get there the number of messages is too big */
return(-1);
}
INT16 pop3c_parselist (void)
{
/* Parse total length of current message */
UINT8 i;
UINT8 ch;
if( RECEIVE_NETWORK_B() != '+')
return(-1);
if( __tolower(RECEIVE_NETWORK_B()) != 'o')
return(-1);
if( __tolower(RECEIVE_NETWORK_B()) != 'k')
return(-1);
if( RECEIVE_NETWORK_B() != ' ')
return(-1);
/* Receive untill next space */
for(i=0; i<5; i++) {
ch = RECEIVE_NETWORK_B();
if( ch == ' ')
break;
}
/* Space not found? */
if( ch != ' ')
return(-1);
pop3_client.curmsgtotlen = 0;
for(i=0; i<9; i++) {
ch = RECEIVE_NETWORK_B();
if(ch == '\r')
return(1);
if(ch == '\n')
return(1);
if( isnumeric(ch) == 0)
return(-1);
ch = asciitohex(ch);
pop3_client.curmsgtotlen *= 10;
pop3_client.curmsgtotlen += ch;
}
/* If all OK we are NOT here */
return(-1);
}
void pop3c_changestate (UINT8 nstate)
{
init_timer(pop3_client.tmrhandle, POP3C_TOUT*TIMERTIC);
pop3_client.state = nstate;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -