⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mime.c

📁 一个功能全面的电子邮件客户端
💻 C
📖 第 1 页 / 共 4 页
字号:
		}	}	ret=(char *)calloc(*bpos+1, sizeof(char));	memmove (ret, tmpret, *bpos+1);	free (tmpret);	return ret;}char *mime_dec_qp (char *data, unsigned long *bpos) {	int len=strlen(data);	int i,c1,c2;	char *tmpret=(char *)calloc(len+2, sizeof(char));	char *ret;	char *tmps=rm_cr(data);	*bpos=0;	for (i=0; i<len; i++) {		c1=tmps[i];		if (c1=='=') {			i++;			c1=tmps[i];			if (c1!='\n') {				c1=index_qp[(unsigned char)c1];				i++;				c2=tmps[i];				c2=index_qp[(unsigned char)c2];// need global option to strip CR's for now, lets leave them alown//				if ( ((c1<<4) | c2) != '\r') {					tmpret[*bpos]=((c1<<4) | c2);					*bpos+=1;//				}			}		} else {			tmpret[*bpos]=c1;			*bpos+=1;		}	}	ret=(char *)calloc(*bpos+1, sizeof(char));	memmove (ret, tmpret, *bpos);	free (tmpret);	free (tmps);	return ret;}char *mime_enc_base64 (unsigned char *data, unsigned long len, unsigned long maxlen) {	int c1,c2,c3, cpl=0;	unsigned long dpos=0, rpos=0;	char *tmpret=(char *)calloc(len*3, sizeof(char));	char *ret=NULL;#ifdef getnc#undef getnc#endif#define getnc(p)	p[dpos];dpos++;#define putnc(p, c) p[rpos]=c;rpos++;	if ((maxlen!=0)&&(maxlen<73))		return NULL;	for (dpos=0; dpos<=len;) {		c1=getnc(data);		c2=getnc(data);		putnc(tmpret, b64[c1>>2])		if (dpos>len) {			putnc(tmpret, b64[((c1&0x3)<<4)])			putnc(tmpret, '=')			putnc(tmpret, '=')		} else {			c3=getnc(data);			if (dpos>len) {				putnc(tmpret, b64[((c1&0x3)<<4) | ((c2&0xF0)>>4)])				putnc(tmpret, b64[((c2&0xF)<<2)])				putnc(tmpret, '=')			} else {				putnc(tmpret, b64[((c1&0x3)<<4) | ((c2&0xF0)>>4)])				putnc(tmpret, b64[((c2&0xF)<<2) | ((c3&0xC0)>>6)])				putnc(tmpret, b64[(c3&0x3F)])			}		}		cpl+=4;		if (cpl>71) {			putnc(tmpret, '\r')			putnc(tmpret, '\n')/*   FIXME: might want to check for max length so the message can be broken   into smaller chunks*/			cpl=0;		}	}	if (cpl) {		putnc(tmpret, '\r')		putnc(tmpret, '\n')	}	ret=(char *)calloc(rpos+2, sizeof(char));	memmove (ret, tmpret, rpos);	free (tmpret);	return ret;}char *mime_enc_qp (char *data, unsigned long len, unsigned long maxlen) {	int c, cpl=0;	unsigned long dpos=0, rpos=0;	char *tmpret=(char *)calloc(len*3, sizeof(char));	char *ret=NULL;#ifdef getnc#undef getnc#endif#ifdef putnc#undef putnc#endif#define getnc(p)	p[dpos];dpos++;#define putnc(p, c) p[rpos]=c;rpos++;	if ((maxlen!=0)&&(maxlen<73))		return NULL;	for (dpos=0; dpos<=len;) {		c=getnc(data);		if (dpos>len) break;		if (index_no_enc[c-1]==NA) {			putnc (tmpret, '=')			putnc (tmpret, hextable[(c&0xF0)>>4])			putnc (tmpret, hextable[c&0xF])			cpl+=2;		} else {			putnc(tmpret, c)		}		cpl++;		if (c=='\n') cpl=0;		if (cpl>71) {			putnc(tmpret, '=')			putnc(tmpret, '\r')			putnc(tmpret, '\n')/*   FIXME: might want to check for max length so the message can be broken   into smaller chunks*/			cpl=0;		}	}	if (cpl) {		putnc(tmpret, '\r')		putnc(tmpret, '\n')	}	ret=(char *)calloc(rpos+2, sizeof(char));	memmove (ret, tmpret, rpos);	free (tmpret);	return ret;}void message_save_body (GtkWidget *widget, gpointer data) {	char *body, *head ;	char *message=((char *)data);	unsigned long hsize = message_hsize (message);	unsigned long msize = strlen( message ) ;	Header_Info *hinfo=NULL;	char *tmps=NULL, *sdata=NULL;	unsigned long slen;	head = body = NULL ;	head=(char *)calloc(hsize+2, sizeof(char));	body=(char *)calloc((msize - hsize) + 2, sizeof(char));	memmove (head, message, hsize);	memmove (body, message+hsize, msize - hsize);	hinfo=parse_header (head);	switch (mime_enc_type (hinfo->content_t_e)) {		case ENC_BASE64:			sdata=mime_dec_base64 (body, &slen);			break;		case ENC_QP:			sdata=mime_dec_qp (body, &slen);			break;		case ENC_NONE:			sdata=strdup (body);			slen=strlen (body);			break;	}	if (hinfo->content_d) {		tmps=get_filename (hinfo->content_d);	} else {		tmps=strdup ("no-name");	}	save_string (sdata, slen, tmps);	if (widget) gtk_widget_destroy (lookup_widget (widget, "yesno"));	free (tmps);	free (head);	free (body);	destroy_hinfo (hinfo);	free (message);}void message_use_ext_prog (GtkWidget *widget, gpointer data) {	char *message=((char *)data);	unsigned long hsize=message_hsize (message);	char *head=(char *)calloc(hsize+2, sizeof(char));	char *body=(char *)calloc(strlen (message)-hsize+2, sizeof(char));	char *file=NULL, *tmps=NULL, *sdata=NULL, *tmps2=NULL;	Header_Info *hinfo=NULL;	MimeType *mtype;	int fd;	unsigned long slen;	memmove (head, message, hsize);	memmove (body, message+hsize, strlen (message)-hsize);	hinfo=parse_header (head);	mtype=mime_type_search_type (hinfo->content_type);	switch (mime_enc_type (hinfo->content_t_e)) {		case ENC_BASE64:			sdata=mime_dec_base64 (body, &slen);			break;		case ENC_QP:			sdata=mime_dec_qp (body, &slen);			break;		case ENC_NONE:			sdata=strdup (body);			slen=strlen (body);			break;	}	/* Only do this if we have a content disposition and it's NOT "inline". */	if( (hinfo -> content_d) && 	    (strncmp( "inline", hinfo -> content_d, 6) != 0) ) {		tmps=get_filename (hinfo->content_d);	} else {		if (hinfo->content_type) {			if (mtype->ext) {				tmps2=cut_string (mtype->ext, ',', 1, 1);				if (tmps2[0]=='\0') {					free (tmps2);					tmps2=strdup(mtype->ext);				}				tmps=(char *)calloc (255, sizeof(char));				snprintf (tmps, 255, "%d.%s", (int)time(NULL), tmps2);			} else {				tmps=(char *)calloc (255, sizeof(char));				snprintf (tmps, 255, "%d", (int)time(NULL));			}		} else {			tmps=(char *)calloc (255, sizeof(char));			snprintf (tmps, 255, "%d", (int)time(NULL));		}	}	if (tmps2) free (tmps2);	if( tmps ) {	  file=(char *)calloc (strlen(tmps)+20, sizeof(char));	  mkdir ("/tmp/tradeclient/", S_IREAD | S_IWRITE | S_IEXEC);	  snprintf (file, strlen(tmps)+20, "/tmp/tradeclient/%s", tmps);	  free (tmps);	  fd=open (file, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);	  write (fd, sdata, slen);	  close (fd);	  tmps=(char *)calloc(strlen(mtype->handle)+strlen(file)+20, sizeof(char));	  snprintf (tmps, strlen(mtype->handle)+strlen(file)+20, "%s", mtype->handle);	  substitute (tmps, "%f", file);	  	  tmps2=strdup (tmps);	  snprintf (tmps, strlen(mtype->handle)+strlen(file)+20, "%s", tmps2);	  spawn_with_status(tmps, 0);	  free (tmps);	  free (tmps2);	}	if( widget ) gtk_widget_destroy( lookup_widget (widget, "yesno") ) ;	free( head ) ;	free( body ) ;	free( file ) ;	free( sdata ) ;	destroy_hinfo( hinfo ) ;	free ( message ) ;}void message_view (GtkWidget *widget, gpointer data) {	char *message=((char *)data);	unsigned long hsize=message_hsize (message);	char *head=(char *)calloc(hsize+2, sizeof(char));	Header_Info *hinfo=NULL;	memmove (head, message, hsize);	hinfo=parse_header (head);	free (head);	if (strncasecmp (hinfo->content_type, "message/rfc822", 14)==0) {		message_window_new_stream (message);	} else {		create_docsource_stream (message);	}	hinfo_destroy (hinfo);	free (message);}/* Modified by Charles Hornberger <charlie@tradeclient.org> * Attempts to fix MIME boundary bug, and deal with mangled MIME boundaries. */char *mime_process (char *message, Attachment *att, int popup) {  Header_Info *hinfo, *tmphinfo;  int hsize=message_hsize (message);  int tmplen,bsize;  MimeType *mtype;  Attachment *attseek=att, *tmpatt;  unsigned long slen=0;  int digest=FALSE;  char *head=(char *)calloc(hsize+1, sizeof(char));  char *body=NULL;  char *boundary=NULL;  char *ret=NULL;  char *rethead=NULL;  char *retbody=NULL;  char *tmps=NULL;  char *msg=NULL;  char *tmp1=NULL, *tmp2=NULL, *tmp3l, *tmphead=NULL;  GtkWidget *yesno;  char *virusMsg = "";/*  char *virusMsg = _("This message has been modified in transit or is invalid.  This may be\n" \		     "because an attachment contained a virus which has been incorrectly\n" \		     "removed by a virus scanner or the message originator wanted to send\n" \		     "a message with the intent of crashing your mail client (a.k.a \"a mail-bomb\").\n" \		     "The original message body, if any, is included below:\n\n\n") ;*/#if DEBUG > 9  struct timeval tv1, tv2;  float tf1,tf2;  gettimeofday (&tv1, NULL);#endif#ifdef DMALLOC  dmalloc_verify( 0 ) ;#endif  memmove (head, message, hsize);  hinfo=parse_header (head);  /* if we can't parse the header, give up now */  if (hinfo==NULL) {#if DEBUG > 9    gettimeofday (&tv2, NULL);    tf1=(tv1.tv_sec&0x000000ff)+((float)tv1.tv_usec/(float)1000000);    tf2=(tv2.tv_sec&0x000000ff)+((float)tv2.tv_usec/(float)1000000);    printf ("takes %f seconds for mime_process\n", (tf2-tf1));#endif    free (head);    return NULL;  }    rethead=header_displayable (head);  /* if we can't determine the content type, simply return the message */  if (hinfo->content_type==NULL) {    retbody=(char *)calloc(strlen (message)+2, sizeof(char));    memmove (retbody, message+hsize, strlen (message)-hsize);    if (rethead) {      ret=(char *)calloc(strlen (retbody)+strlen (rethead)+2, sizeof(char));      memmove (ret, rethead, strlen (rethead));      memmove (ret+strlen (rethead), retbody, strlen (retbody));      free (rethead);    } else {      ret=(char *)calloc(strlen (retbody)+2, sizeof(char));      memmove (ret, retbody, strlen (retbody));    }#if DEBUG > 9    gettimeofday (&tv2, NULL);    tf1=(tv1.tv_sec&0x000000ff)+((float)tv1.tv_usec/(float)1000000);    tf2=(tv2.tv_sec&0x000000ff)+((float)tv2.tv_usec/(float)1000000);    printf ("takes %f seconds for mime_process\n", (tf2-tf1));#endif    destroy_hinfo (hinfo);    free (retbody);    free (head);    return ret;  }      /*   * FIXME: this line below needs to search the entire content_type and be   * case insentive.      */#ifdef DMALLOC      dmalloc_verify( 0 ) ;#endif      if (strncasecmp (hinfo->content_type, "multipart", 9)==0) {	if (strncasecmp (hinfo->content_type, "multipart/digest", 16)==0)	  digest=TRUE;	boundary=get_boundary (hinfo->content_type);	/* This is really an error, just not sure what to do with it at this time! */    if(boundary==NULL) {      free (head);      free (rethead);      return NULL ;    }    bsize=strlen(boundary);    /* copy the message, minus the head, into 'body' */	body=(char *)calloc(strlen (message)-hsize+2, sizeof(char));	memmove (body, message+hsize, strlen (message)-hsize);    /* look for the boundary */	tmp1=strstr (body, boundary);    /* if we found it, advance our 'tmp1' pointer past it and any newlines */	if( tmp1 ) {	  tmp1+=bsize;	  if (digest==FALSE) {	    if ((*tmp1=='\n')||(*tmp1=='\r')) tmp1++;	    if ((*tmp1=='\n')||(*tmp1=='\r')) tmp1++;	  } else {	    for (;((*tmp1=='\n') || (*tmp1=='\r'));tmp1++) {}	  }    }    /* otherwise, simply return the body and our 'virus message' */    else {	  tmp1 = malloc( strlen(body) + strlen(virusMsg) + 1 ) ;	  strcpy( tmp1, virusMsg ) ;	  strcat( tmp1, body ) ;	  free( body ) ;      free( head) ;      free( rethead) ;	  return tmp1 ;	}	tmplen=message_hsize (tmp1);	tmphead=(char *)calloc(tmplen+2, sizeof(char));	memmove (tmphead, tmp1, tmplen);	tmphinfo=parse_header (tmphead);	if (tmphead) free (tmphead);	if (((mime_handle (tmphinfo->content_type)==MIMEHANDLE_INT) ||	     (strncasecmp (tmphinfo->content_type, "multipart/alternative", 21)==0)) &&	    (digest==FALSE)) {#ifdef DMALLOC	  dmalloc_verify( 0 ) ;#endif	  if (strncasecmp (tmphinfo->content_type, "multipart/alternative", 21)==0) {        /* look for the next boundary */	    tmp2=strstr (tmp1, boundary);        /* if we didn't find another boundary, return what's left of the body */        if(tmp2==NULL) {          tmp2 = malloc( strlen(body) + strlen(virusMsg) + 1 );          strcpy( tmp2, virusMsg );          strcat( tmp2, body );          free( body );          free( head );          free( rethead );          destroy_hinfo( tmphinfo );          return tmp2 ;        }        /* back up to the _beginning_ of the boundary string */	    tmp3l=tmp2;        while( *tmp3l != '\n' ) tmp3l-- ;	    tmps=(char *)calloc(tmp3l-tmp1+2, sizeof(char));	    memmove (tmps, tmp1, tmp3l-tmp1);	    retbody=mime_process (tmps, attseek, 0);	    attseek=((Attachment *)attseek->next);	    attseek->offset+=(tmp1-body)+hsize;	    tmp2+=bsize;	    tmp1=tmp2;	    free (tmps);      }      else {	    tmp1+=tmplen;        /* look for the next boundary */	    tmp2=strstr (tmp1, boundary);        /* if we didn't find another boundary, return what's left of the body */        if(tmp2==NULL) {          tmp2 = malloc( strlen(body) + strlen(virusMsg) + 1 );          strcpy( tmp2, virusMsg );          strcat( tmp2, body );          free( body );          free( head );          free( rethead );          destroy_hinfo( tmphinfo );          return tmp2 ;        }        /* back up to the _beginning_ of the boundary string */        tmp3l=tmp2;	    while( *tmp3l != '\n' ) tmp3l-- ;	    tmps=(char *)calloc(abs(tmp1-tmp3l)+2, sizeof(char));	    memmove( tmps, tmp1, abs(tmp1-tmp3l) ) ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -