📄 pop3.c
字号:
/* pop3 protocol for uC/IP
* $Id: pop3.c,v 1.1.1.1 2004/12/22 10:02:09 zyu Exp $
* Copyright (C)2002 Jiang Wei.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <..\app\email\email.h>
#include <..\app\email\emlfunc.h>
#include <..\app\email\emldb.h>
//#include "asixmail.h"
#include "nettcp.h"
#include "domain.h"
//#include "nettypes.h"
#define SAVEINBOX 0x1
#define MAXMAILLEN 16000
#define POP3_PORT 110
char *pop3server;
short pop3port;
char *pop3user;
char *pop3pass;
int (*newmail)(char *user, char *data);
//struct mail_head *gethead(char *)
char buf[16384];
//char buf1[3072];
//unsigned int bufsize = 16384;
int bufsize = 16384;
extern int mail_finish;
enum {
STAT_OK = 0,
STAT_IDLE,
STAT_USER,
STAT_PASS,
STAT_STAT,
STAT_LIST,
STAT_RETR,
STAT_DELE,
STAT_QUIT,
STAT_ERROR = -1,
};
char *pop3_cmd[] = {
"USER ",
"PASS ",
"STAT",
"RETR ",
"DELE ",
"QUIT",
"LIST ",
};
char *pop3_reply[] = {
"-ERR",
"+OK",
};
char *str_crnl = "\r\n";
struct mail_head {
char *from;
char *to;
char *subject;
char *date;
char flag;
char index;
char *text;
};
void getmhead(char *);
void mailrip(char *);
char *readline(char *);
char *getdate(char *);
int do_pop3(int st)
{
int s, i, nmail;
int m = 0;
int n = 0;
int connect;
struct sockaddr_in pop3ad;
int pop3_stat;
int mailcount = 1;
// int mailcount = 20;
int maildel,maillen;
// int ret;
int value;
char *ptr;
char *mailpointer;
char popstring[50];
int control = 1;
pop3server = net_server_stru.pop3_srv;
SetWindowText( st, "正在进行DNS解析...", NULL );
while(1) {
pop3ad.sin_addr.s_addr = resolve(pop3server);
m++;
if(pop3ad.sin_addr.s_addr) break;
if(m == 5) {
SetWindowText( st, "DNS解析失败,请重试...", NULL );
delay(1000);
mail_finish = 1;
return -2;
}
}
SetWindowText( st, "DNS解析成功...", NULL );
delay(1000);
if ((s = tcpOpen()) < 0)
return s;
pop3port = POP3_PORT;
pop3user = net_server_stru.pop3_user;
pop3pass = net_server_stru.pop3_pass;
pop3ad.sin_family = AF_INET;
pop3ad.sin_port = htons(pop3port);
SetWindowText( st, "正在连接POP3服务器...", NULL );
do{
connect = tcpConnectJiffy(s, &pop3ad, 0, 300);
++n;
if(n == 5) {
SetWindowText( st, "连接POP3服务器失败,请重试...", NULL );
delay(1000);
mail_finish = 1;
return -3;
}
} while(connect != 0);
delay(1000);
SetWindowText( st, "连接POP3服务器成功...", NULL );
memset(buf,0,sizeof(buf));
pop3_stat = STAT_IDLE;
// while ((i = tcpRead(s, buf, bufsize)) >= 0) {
while ((i = tcpReadJiffy(s,buf,bufsize,1200)) >=0) {
switch (pop3_stat) {
case STAT_IDLE:
if (strncmp(buf, pop3_reply[1], strlen(pop3_reply[1]))) {
pop3_stat = STAT_ERROR;
break;
}
if (i < bufsize) {
strcpy(buf, pop3_cmd[0]);
strcat(buf, pop3user);
strcat(buf, str_crnl);
++pop3_stat;
break;
}
break;
case STAT_USER:
if (strncmp(buf, pop3_reply[1], strlen(pop3_reply[1]))) {
pop3_stat = STAT_ERROR;
break;
}
if (i < bufsize) {
strcpy(buf, pop3_cmd[1]);
strcat(buf, pop3pass);
strcat(buf, str_crnl);
++pop3_stat;
break;
}
break;
case STAT_PASS:
if (strncmp(buf, pop3_reply[1], strlen(pop3_reply[1]))) {
pop3_stat = STAT_ERROR;
break;
}
if (i < bufsize) {
strcpy(buf, pop3_cmd[2]);
strcat(buf, str_crnl);
++pop3_stat;
//++pop3_stat;
break;
}
break;
case STAT_STAT:
if (strncmp(buf, pop3_reply[1], strlen(pop3_reply[1]))) {
pop3_stat = STAT_ERROR;
break;
}
if(control == 1) {
i = strlen(pop3_reply[1]) + 1;
nmail = 0;
nmail = atoi(buf + i);
--control;
}
if (mailcount <= nmail) {
strcpy(buf, pop3_cmd[6]);
sprintf(buf+5,"%d",mailcount);
strcat(buf, str_crnl);
++pop3_stat;
break;
} else {
strcpy(buf, pop3_cmd[5]);
strcat(buf, str_crnl);
pop3_stat = STAT_QUIT;
break;
}
break;
case STAT_LIST:
if (strncmp(buf, pop3_reply[1], strlen(pop3_reply[1]))) {
pop3_stat = STAT_ERROR;
break;
}
buf[i] = '\0';
mailpointer = buf + i;
//value = tcpRead(s,mailpointer,bufsize);
value = tcpReadJiffy(s, mailpointer, bufsize,1200);
mailpointer += value;
ptr = strchr(buf+4,' ');
maillen = atoi(ptr+1);
if(maillen > MAXMAILLEN) {
sprintf(popstring,"第%d封邮件太大,无法接收",mailcount);
SetWindowText( st, popstring, NULL );
delay(1000);
++mailcount;
if (mailcount <= nmail) {
strcpy(buf, pop3_cmd[6]);
sprintf(buf+5,"%d",mailcount);
strcat(buf, str_crnl);
//--pop3_stat;
break;
} else {
strcpy(buf, pop3_cmd[4]);
strcat(buf, "1");
strcat(buf, str_crnl);
pop3_stat += 2;
maildel = 2;
break;
}
}else {
sprintf(popstring,"接收第%d封邮件(共%d封)",mailcount,nmail);
SetWindowText( st, popstring, NULL );
strcpy(buf, pop3_cmd[3]);
sprintf(buf+5,"%d",mailcount);
strcat(buf, str_crnl);
++pop3_stat;
break;
}
break;
case STAT_RETR:
if (strncmp(buf, pop3_reply[1], strlen(pop3_reply[1]))) {
pop3_stat = STAT_ERROR;
break;
}
buf[i] = '\0';
mailpointer = buf + i;
//value = tcpRead(s,mailpointer,bufsize);
value = tcpReadJiffy(s, mailpointer, bufsize,1200);
mailpointer += value;
while(value) {
value = tcpReadJiffy(s, mailpointer, bufsize,1200);
mailpointer += value;
if(strstr(buf,"\r\n.\r\n"))
break;
}
getmhead(buf);
++mailcount;
if (mailcount <= nmail) {
strcpy(buf, pop3_cmd[6]);
sprintf(buf+5,"%d",mailcount);
strcat(buf, str_crnl);
--pop3_stat;
break;
} else {
strcpy(buf, pop3_cmd[4]);
strcat(buf, "1");
strcat(buf, str_crnl);
++pop3_stat;
maildel = 2;
break;
}
break;
case STAT_DELE:
if (strncmp(buf, pop3_reply[1], strlen(pop3_reply[1]))) {
pop3_stat = STAT_ERROR;
break;
}
if (maildel <= nmail) {
strcpy(buf, pop3_cmd[4]);
sprintf(buf+5,"%d",maildel);
strcat(buf, str_crnl);
++maildel;
break;
} else {
strcpy(buf, pop3_cmd[5]);
strcat(buf, str_crnl);
++pop3_stat;
break;
}
break;
case STAT_QUIT:
if (strncmp(buf, pop3_reply[1], strlen(pop3_reply[1]))) {
pop3_stat = STAT_ERROR;
break;
}
if (i < bufsize) {
pop3_stat = STAT_OK;
break;
}
break;
}
if (pop3_stat > 0) {
tcpWrite(s, buf, strlen(buf));
if(pop3_stat == STAT_RETR) {
memset(buf,0,sizeof(buf));
}
}
else break;
}
tcpClose(s);
mail_finish = 1;
if (pop3_stat < 0) return -1;
else return 0;
}
void getmhead(char *buffer)
{
EMAIL_STRU mailhead;
// EMAIL_STRU *mailpoint = &mailhead;
int i = 0;
char *ptr;
char *ptr1;
char *buf = buffer;
// char temp[20];
// char dategroup[20];
// char from[50];//; = "From:";
// char to[50];// = "To:";
// char time[100];// = "Date:";
// char subject[100];// = "Subject:";
if((ptr = strstr(buf,"From:"))!=NULL) {
ptr += 5;
if(*ptr == ' ')
ptr++;
mailhead.addresser = ptr;
}
else if((ptr = strstr(buf,"Return-Path:"))!=NULL) {
ptr += 12;
if(*ptr == ' ')
ptr++;
mailhead.addresser = ptr;
}else
mailhead.addresser = NULL;
if((ptr = strstr(buf,"To:"))!=NULL) {
ptr += 3;
if(*ptr == ' ')
ptr++;
// if((ptr1 = strchr(ptr,'<'))!=NULL)
// ptr = ++ptr1;
mailhead.addressee = ptr;
}else if((ptr = strstr(buf,"Delivered-To:"))!=NULL) {
ptr += 13;
if(*ptr == ' ')
ptr++;
// if((ptr1 = strchr(ptr,'<'))!=NULL)
// ptr = ++ptr1;
mailhead.addressee = ptr;
}else
mailhead.addressee = NULL;
if((ptr = strstr(buf,"Subject:"))!=NULL) {
ptr += 8;
if(*ptr == ' ')
ptr++;
//if((ptr1 = strchr(ptr,"<"))!=NULL)
// ptr = ++ptr1;
mailhead.subject = ptr;
}else
mailhead.subject = NULL;
if((ptr = strstr(buf,"Date:"))!=NULL) {
ptr += 5;
mailhead.time = ptr;
}else if((ptr = strstr(buf,"Message-Id:"))!=NULL) {
ptr += 13;
mailhead.time = ptr;
}else
mailhead.time = NULL;
if((ptr = strstr(buf,"\r\n\r\n"))!=NULL) {
ptr += 4;
mailhead.content = ptr;
}
else
mailhead.content = "无内容";
if(ptr = strstr(mailhead.content,"\r\n.\r\n"))
*ptr = '\0';
// mailrip(mailhead.content);
// strcat((char *)from,readline(mailhead.addresser));
// mailrip(from);
// mailhead.addresser = from;
if(mailhead.addresser != NULL) {
sprintf(mailhead.addresser,"%s",readline(mailhead.addresser));
mailrip(mailhead.addresser);
ptr = mailhead.addresser;
if((ptr1 = strchr(ptr,'<'))!=NULL)
ptr = ++ptr1;
mailhead.addresser = ptr;
}else {
mailhead.addresser = "无";
}
if(mailhead.addressee != NULL) {
sprintf(mailhead.addressee,"%s",readline(mailhead.addressee));
mailrip(mailhead.addressee);
ptr = mailhead.addressee;
if((ptr1 = strchr(ptr,'<'))!=NULL)
ptr = ++ptr1;
mailhead.addressee = ptr;
}else{
mailhead.addressee = "无";
}
// strcat((char *)to,readline(mailhead.addressee));
// mailrip(to);
// mailhead.addressee = to;
if(mailhead.subject != NULL) {
sprintf(mailhead.subject,"%s",readline(mailhead.subject));
mailrip(mailhead.subject);
}else{
mailhead.subject = "无主题";
}
// strcat((char *)subject,readline(mailhead.subject));
// mailrip(subject);
// mailhead.subject = subject;
if(mailhead.time != NULL) {
sprintf(mailhead.time,"%s",readline(mailhead.time));
mailrip(mailhead.time);
if(ptr = strstr(mailhead.time,"."))
*ptr = '\0';
else
mailhead.time = getdate(mailhead.time);
}else {
mailhead.time = "20021107190037";
}
// strcat((char *)time,readline(mailhead.time));
// mailrip(time);
// ptr = strstr(mailhead.time,".");
// *ptr = '\0';
// mailhead.time = time;
mailhead.type = "1";
mailhead.box = "0";
TransCreateEmail(&mailhead,0);
Lfree(mailhead.time);
//return &mailhead;
}
void mailrip(char *s)
{
char *cp;
if((cp = strchr(s,'>')) != NULL)
*cp = '\0';
if((cp = strchr(s,'\n')) != NULL)
*cp = '\0';
if((cp = strchr(s,'\r')) != NULL)
*cp = '\0';
}
char *readline(char *bufptr)
{
static char buffer[259]; /* we define the buffer as a static array 11/26/1999 */
register char *buffer2;
register int i=0;
buffer2=bufptr;
if (buffer2==NULL||*buffer2=='\0') {
buffer[0]='\0';
return buffer;
}
while( (*buffer2!='\n') && ( i<256 ) ){/* we add the boundary check here. 11/26/1999*/
buffer[i]=*buffer2;
buffer2++;
i++;
}
buffer[i++]='\n';
buffer[i++]='\0';
return buffer;
}
char *getdate(char *dateptr)
{
int i = 0;
char *ptr;
char temp[20];
// char dategroup[20];
char *dategroup;
char *month[] = {
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
};
char *monthdig[] = {
"01","02","03","04","05","06","07","08","09","10","11","12"
};
ptr = dateptr;
dategroup = (char *)Lmalloc(20*sizeof(char));
for(;;) {
if(isdigit(*ptr)) {
temp[i] = *ptr;
++i;
}
ptr++;
if(*ptr == '\0')
break;
}
temp[i] = '\0';
ptr = &temp[2];
for(i=0;i<4;i++) {
dategroup[i] = *ptr;
ptr++;
}
dategroup[4] = '\0';
for(i=0;i<12;i++) {
if((ptr = strstr(dateptr,month[i]))!=NULL)
break;
}
monthdig[12] = "00";
strcat(dategroup,monthdig[i]);
ptr = temp;
for(i=0;i<2;i++) {
dategroup[i+6] = *ptr;
ptr++;
}
dategroup[8] = '\0';
ptr = &temp[6];
strcat(dategroup,ptr);
dategroup[14] = '\0';
return dategroup;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -