📄 email.cpp
字号:
#include <stdio.h>#include <string.h>#include <ctype.h>#include "Email.h"//检查信息头,是否符合逻辑Cemail::Cemail(){ //初始化存放边界的堆栈空间 m_icurrent=0 ; for(int i=0;i<MAX_LOOP;i++) m_pszline[i]=new char[1024]; m_phv=new Chv(); m_phv->init("FROM,TO,CC,BCC,SUBJECT,DATE,CONTENT-TYPE,CONTENT-TRANSFER-ENCODING"); }Cemail::~Cemail(){ for(int i=0;i<MAX_LOOP;i++) delete m_pszline[i]; }void Cemail::clear(){ m_icurrent=0 ; m_phv->clear(); }void Cemail::clearField(){ m_phv->clear(); }int Cemail::availField(){ return m_phv->gettotavail();}int Cemail::getEncodingType(){ char *pval; pval=m_phv->getValue("Content-Transfer-Encoding"); //printf("get encode type=[%s]\n",pval); if(pval!=NULL) { return Cval::getEncodingType(pval); } return encode_8bit;}char *Cemail::getHv(char *name){ char *pval; pval=m_phv->getValue(name); //printf("get encode type=[%s]\n",pval); return pval;}//初始化用户地址bool Cemail::initAdd(char *key){//setMailAdd if(m_phv->getValue(key,m_szAdd)) { m_pCurAdd=strtok(m_szAdd,","); return true; } else { m_pCurAdd=NULL; return false; }} bool Cemail::getNextAdd(char *sz_name,int len_name,char *sz_add,int len_add){ char szone[1024]; char *pone; if(m_pCurAdd!=NULL) { strcpy(szone,m_pCurAdd); //这里要判断一下被拷贝串的长度 pone=strchr(szone,'<'); if(pone==NULL) { strcpy(sz_name," "); if(strlen(szone)<len_add) strcpy(sz_add,szone); else { memcpy(sz_add,szone,len_add-1); sz_add[len_add-1]=0; } } else { int len=pone-szone; if(len>=0) { if(len>=len_name) len=len_name-1; memcpy(sz_name,szone,len); sz_name[len]=0; } len=strlen(pone)-2; if(len>0) { if(len>=len_add) len=len_add-1; memcpy(sz_add,pone+1,len); sz_add[len]=0; } } #ifdef DEBUG printf("name=[%s]add=[%s]\n",sz_name,sz_add); #endif for(int i=0;i<strlen(sz_add);i++) sz_add[i]=tolower(sz_add[i]); m_pCurAdd=NULL; m_pCurAdd=strtok(NULL,","); return true; } return false;}bool Cemail::getContentType(char *sz_contenttype){ char szline[1024]; char szone[1024]; char *pval=szline; if(m_phv->getValue("Content-Type",szline)) { if(Cval::getNextAtom(pval,szone)) { strcpy(sz_contenttype,szone); return true; } } return false; }bool Cemail::getCharset(char *sz_charset){ char szline[1024]; char szone[1024]; char *pval=szline; if(m_phv->getValue("Content-Type",szline)) { if(Cval::getNextAtom(pval,szone)) { //strcpy(m_MailText.sz_contenttype,szone); while(Cval::getNextAtom(pval,szone)) { if(strcasecmp(szone,"charset")==0) { if(Cval::getNextAtom(pval,szone)) { if(*szone=='=') { if(Cval::getNextAtom(pval,szone)) { strcpy(sz_charset,szone); return true; } } } } } } } return false;}bool Cemail::getFileName(char *sz_buf){ char *pval; pval=m_phv->getValue("Content-Type"); if(pval==NULL) { return false; } while(Cval::getNextAtom(pval,sz_buf)) { if(strcasecmp(sz_buf,"name")==0) { if( Cval::getNextAtom(pval,sz_buf)) { if(*sz_buf=='=') { if( Cval::getNextAtom(pval,sz_buf)) { return true; } } } return false; } } return false ;}//int Cemail::getMsgType(){ char *pval; char sz_buf[1024]; //pval=m_phv->getValue(m_Emailm_EmailField,"Content-Type"); pval=m_phv->getValue("Content-Type"); if(pval==NULL) return type_text; if(Cval::getNextAtom(pval,sz_buf)) { strtok(sz_buf,"/"); if(!strcasecmp(sz_buf,"TEXT")) { return type_text; } if(!strcasecmp(sz_buf,"IMAGE")) { return type_image; } if(!strcasecmp(sz_buf,"AUDIO")) { return type_audio; } if(!strcasecmp(sz_buf,"VIDEO")) { return type_video; } if(!strcasecmp(sz_buf,"APPLICATION")) { return type_application; } if(!strcasecmp(sz_buf,"MESSAGE")) { return type_message; } if(!strcasecmp(sz_buf,"MULTIPART")) { return type_multipart; } } return type_err;}bool Cemail::getStackBoudary(char *szbd){ if(m_icurrent>=1 && m_icurrent<=MAX_LOOP) { strcpy(szbd,m_pszline[m_icurrent-1]); return true; } else return false;}void Cemail::pushBoudary(char *szbd){ printf("m_icurrent=%d\n",m_icurrent); assert(m_icurrent>=0 && m_icurrent<MAX_LOOP); strcpy(m_pszline[m_icurrent],szbd); m_icurrent++;}int Cemail::popBoudary(){ if(m_icurrent<=0 ) return -1; m_icurrent--; return m_icurrent;}bool Cemail::getBoudary(char *szbd){ char *pval; char sz_buf[1024]; //printf("pos ContentType\n"); pval=m_phv->getValue("Content-Type"); if(pval==NULL) return false; //printf("content-type=[%s]\n",pval); while(Cval::getNextAtom(pval,sz_buf)) { if(strcasecmp(sz_buf,"boundary")==0) { if( Cval::getNextAtom(pval,sz_buf)) { if(*sz_buf=='=') { if( Cval::getNextAtom(pval,sz_buf)) { strcpy(szbd,"--"); strncpy(szbd+2,sz_buf+1,strlen(sz_buf)-2); szbd[strlen(sz_buf)]=0; return true; } /*strcpy(szbd,"--"); strcpy(szbd+2,pval); return true; */ } } return false; } } return false;}//multipart //流定位到当前边界//从栈获取边界//bool Cemail::profHeader(Cmimestream *pmimestream){ char szline[1024]; while(1) { //m_phv->clear();//0705 m_phv->clear("Content-Type"); m_phv->setValues(pmimestream); if(getMsgType()==type_multipart) { char szbd[128]; if(getBoudary(szbd)) { pushBoudary(szbd); if(pmimestream->gotoMarker(szbd)) { pmimestream->gotoNextLine(); continue; } else return false; } else {//no boudary para //err processing; return false; } } else return true; }}//处理完一个邮件体后分析,定位下一个邮件的边界/* -1:error 0: >0:*/int Cemail::profBoundary(Cmimestream *pmimestream){ char szline[1024]; if(m_icurrent==0) { pmimestream->getLine(szline);//这是边界 return 0; } while(1) { pmimestream->getLine(szline);//这是边界 if(Cval::isEndBoudary(szline)) {//是结束边界 //#ifdef DEBUG printf("this is EndBD\n"); //#endif if(popBoudary()==0) {//本封邮件结束,去结尾 if(pmimestream->getEndEmailBodyPos()==NULL) return -1; pmimestream->getLine(szline);//去掉结束行 //#ifdef DEBUG printf("EndBD is ignored \n"); return 0; //#endif //while(getL) } else {//使用上一层边界 char szbd[128]; getStackBoudary(szbd); char *pebody=pmimestream->gotoMarker(szbd); if(pebody==NULL) return -1; else continue;//这里要循环处理,所以下面不返回,而是继续判断这个边界是不结束 } } else {//上一层边界开始, return 1; } } }#ifdef DEBUGvoid Cemail::showHeader(){ char szbuf[1024]; memset(szbuf,0,sizeof(szbuf)); if(m_phv->getValue("From",szbuf)!=NULL) { printf("=[%s]",szbuf); } memset(szbuf,0,sizeof(szbuf)); if(m_phv->getValue("To",szbuf)!=NULL) { printf("=[%s]",szbuf); } memset(szbuf,0,sizeof(szbuf)); if(m_phv->getValue("Cc",szbuf)!=NULL) { printf("=[%s]",szbuf); } memset(szbuf,0,sizeof(szbuf)); if(m_phv->getValue("Date",szbuf)!=NULL) { printf("=[%s]",szbuf); } memset(szbuf,0,sizeof(szbuf)); if(m_phv->getValue("Subject",szbuf)!=NULL) { printf("=[%s]",szbuf); } memset(szbuf,0,sizeof(szbuf)); if(m_phv->getValue("Content-Transfer-Encoding",szbuf)!=NULL) { printf("=[%s]",szbuf); } memset(szbuf,0,sizeof(szbuf)); if(m_phv->getValue("Content-Type",szbuf)!=NULL) { printf("=[%s]",szbuf); }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -