📄 mime.c
字号:
mmessage->attachtype[attachnr]=cpystr("application/occet-stream"); } } else { mmessage->attachtype[attachnr]=cpystr("application/occet-stream"); } tmps3=(char *)calloc(strlen(tmpat->filename),sizeof(char)); tmps3=strrchr(tmpat->filename,'/'); mmessage->attachname[attachnr]=(char *)calloc (strlen(tmps3),sizeof(char)); if (tmps3) { tmps3++; mmessage->attachname[attachnr]=cpystr(tmps3); } fread (mmessage->attach[attachnr], sizeof (char), st->st_size, fd); mmessage->attach[attachnr]=mime_enc_base64(mmessage->attach[attachnr],st->st_size,0); fclose (fd); } else { printf("cannot open file %s for reading",tmpat->filename); } attachnr++; } else { /* * We only want to add attachments if it's not the body text. * This means that it's the second plus attachment as the body * counts as the first one on a multi-part. */ if( seekCntr ) {#if DEBUG > 4 printf( "Adding an attachment that is not associated with a file.\n" ) ; printf( "Attachment text is '%s'.\n", tmpat -> data ) ;#endif /* * Now, nclude the attachment name for easy reference. We'll just * base the name on the assumption that it's an rfc822 message. */ mmessage -> attachname[attachnr] = strdup( "rfc822.txt" ) ; /* * We hit here if there is an attachment but no files, just text. * More than likely, it's a message text body that needs to copied as an * attachment. This really means that it still needs to be base64 encoded. */#if 0 // This was leaking memory because of the strdup() call mmessage -> attach[attachnr] = mime_enc_base64( tmpat -> data, strlen(tmpat -> data), 0 ) ) ;#else mmessage -> attach[attachnr] = strdup ( tmpat -> data ) ;#endif /* okay, set the MIME type for the attachment, which is whatever the MIME type is */ if( tmpat -> type ) mmessage -> attachtype[attachnr] = strdup( tmpat -> type ) ; else mmessage -> attachtype[attachnr] = strdup( "text/plain" ) ; /* Increment the attachment counter. */ attachnr++; } } seekCntr++ ; lseek = lseek -> next ; } } tmpat=((AFS *)atlst->data); /* Looks like the old logic leaked memory like the Titanic does water! */ mmessage -> body = strdup( tmpat -> data ) ; /* Fix up the attachment count */ mmessage -> attach_count = seekCntr ; return mmessage ;}char *mime_encode (char *from, char *replyto, char *org, char *to, char *cc, char *bcc, char *subject, char *timestamp, Llist *atlst, int returnreceipt, char *priority) { char *os_info=generate_os_info (); char *lang=lang_getlang(); AFS *tmpat=NULL; int enc_type, nread; MimeType *mtype; char *tmps1=(char *)calloc(1024, sizeof(char)); char *tmps2; char *tmps3; char *tmps4; char *tmps5=(char *)calloc(1024, sizeof(char)); char *tmphead=NULL; char *tmpbody; char *boundary=NULL; char *head=NULL; char *body=NULL; char *ret=NULL; FILE *fd; Llist *lseek=NULL; struct stat *st; int hsize, atmemb=llist_length (atlst);//size, i;/* assemble the header */ hsize=strlen (from)+8+ strlen (org)+16+ strlen (replyto)+8+ strlen (to)+8+ strlen (cc)+8+ strlen (bcc)+8+ strlen (subject)+10+ strlen (timestamp)+8+ 800; // for good mesure /* I see no reason we need to reinvent the wheel here!! Duh! */#ifdef DEBUG printf ("mime_encode: Assembling the header\n");#endif head=(char *)calloc(hsize+2, sizeof(char)); addtext (head, "Date: ", 6); addtext (head, timestamp, strlen(timestamp)); addtext (head, "\r\n", 2); addtext (head, "From: ", 6); addtext (head, from, strlen(from)); addtext (head, "\r\n", 2); if (returnreceipt!=0) { addtext (head, "Disposition-Notification-To: ", 29); addtext (head, from, strlen(from)); addtext (head, "\r\n", 2); } if (replyto[0]!='\0') { addtext (head, "Reply-To: ", 10); addtext (head, replyto, strlen(replyto)); addtext (head, "\r\n", 2); } if (org[0]!='\0') { addtext (head, "Organization: ", 14); addtext (head, org, strlen(org)); addtext (head, "\r\n", 2); } addtext (head, "X-Mailer: TradeClient ", 23); snprintf (tmps1, 1022, "%s [%s] %s", VERSION, lang, os_info); addtext (head, tmps1, strlen(tmps1)); addtext (head, "\r\n", 2); addtext (head, "X-Priority: ", 12); snprintf (tmps5, 1022, "%s", priority); addtext (head, tmps5, strlen(tmps5)); addtext (head, "\r\n", 2); addtext (head, "X-MSMail-Priority: ", 20); snprintf (tmps5, 1022, "%s", priority); addtext (head, tmps5, strlen(tmps5)); addtext (head, "\r\n", 2); addtext (head, "MIME-Version: 1.0", 17); addtext (head, "\r\n", 2); addtext (head, "To: ", 4); addtext (head, to, strlen(to)); addtext (head, "\r\n", 2); if (cc[0]!='\0') { addtext (head, "Cc: ", 4); addtext (head, cc, strlen(cc)); addtext (head, "\r\n", 2); } if (bcc[0]!='\0') { addtext (head, "Bcc: ", 5); addtext (head, bcc, strlen(bcc)); addtext (head, "\r\n", 2); } if (subject[0]!='\0') { addtext (head, "Subject: ", 9); tmps2=rm_crlf (subject); addtext (head, tmps2, strlen(tmps2)); free (tmps2); addtext (head, "\r\n", 2); }#ifdef DEBUG printf ("mime_encode: Start of processing attachments\n");#endif if (atmemb<2) {#ifdef DEBUG printf ("mime_encode: Found only one attachment (probly the body)\n");#endif if (atlst) tmpat=((AFS *)atlst->data); if (tmpat) { tmphead=(char *)calloc (600, sizeof(char)); if (tmpat->filename) { tmps2=strrchr (tmpat->filename,'.'); if (tmps2) tmps2++; mtype=mime_type_search_ext (tmps2); fd=fopen (tmpat->filename, "r"); tmps2=(char *)calloc (4100, sizeof(char)); nread=fread (tmps2, sizeof(char), 4096, fd); enc_type=encode_with (tmps2, nread); free (tmps2); st=(struct stat *)calloc (1, sizeof(struct stat)); stat (tmpat->filename, st); tmps2=(char *)calloc (st->st_size+2, sizeof(char)); fseek (fd, 0, SEEK_SET); fread (tmps2, sizeof (char), st->st_size, fd); fclose (fd); addtext (head, "Content-Type: ", 14); if (mtype!=NULL) { addtext (head, mtype->type, strlen(mtype->type)); } else { if (enc_type==ENC_BASE64) { addtext (head, "application/octet-stream", 24); } else { addtext (head, "text/plain", 10); } } tmps3=strrchr(tmpat->filename,'/'); if (tmps3) tmps3++; else tmps3=tmpat->filename; addtext (head, ";\r\n\tname=\"", 10); addtext (tmphead, tmps3, strlen (tmps3)); addtext (head, "\"\r\n", 3); switch (enc_type) { case ENC_BASE64: addtext (head, "Content-Transfer-Encoding: Base64", 33); addtext (head, "\r\n", 2); body=mime_enc_base64 (tmps2, st->st_size, 0); free (tmps2); break; case ENC_QP: addtext (head, "Content-Transfer-Encoding: Quoted-printable", 43); addtext (head, "\r\n", 2); body=mime_enc_qp (tmps2, st->st_size, 0); free (tmps2); break; case ENC_NONE: default: addtext (head, "Content-Transfer-Encoding: 7bit", 31); addtext (head, "\r\n", 2); body=tmps2; tmps2=NULL; break; } addtext (head, "Content-Disposition: attachment;\r\n\tfilename=\"", 45); addtext (tmphead, tmps3, strlen (tmps3)); addtext (head, "\"\r\n", 3); free (st); } else { addtext (head, "Content-Type: ", 14); addtext (head, tmpat->type, strlen(tmpat->type)); addtext (head, "; charset=us-ascii", 18); addtext (head, "\r\n", 2); if (strncmp (tmpat->type,"message/rfc822", 14)) { switch (encode_with (tmpat->data, strlen(tmpat->data))) { case ENC_BASE64: addtext (head, "Content-Transfer-Encoding: Base64", 33); addtext (head, "\r\n", 2); body=mime_enc_base64 (tmpat->data, strlen(tmpat->data), 0); break; case ENC_QP: addtext (head, "Content-Transfer-Encoding: Quoted-printable", 43); addtext (head, "\r\n", 2); body=mime_enc_qp (tmpat->data, strlen(tmpat->data), 0); break; case ENC_NONE: default: addtext (head, "Content-Transfer-Encoding: 7bit", 31); addtext (head, "\r\n", 2); body=strdup(tmpat->data); break; } } else { addtext (head, "Content-Transfer-Encoding: 7bit", 31); addtext (head, "\r\n", 2); tmps2=tmpat->data; if (*tmps2=='>') { while (*tmps2!='\n') { tmps2++; } tmps2++; } body=strdup (tmps2); tmps2=NULL; } } } else { body=(char *)calloc (2, sizeof(char)); } addtext (head, "\r\n", 2); } else {#ifdef DEBUG printf ("mime_encode: Found %d attachments\n", atmemb);#endif //seems to be a fairly random number boundary=generate_boundary (((unsigned int)(hash_string(to)+ hash_string(subject)+time(NULL))/4), 24); addtext (head, "Content-Type: multipart/mixed;\r\n\tboundary=\"", 43); addtext (head, boundary, strlen(boundary)); addtext (head, "\"\r\n", 3); body=(char *)calloc (400, sizeof(char)); addtext (body, "\r\n\This message has been encoding in MIME format. If you are reading this,then your email client does not support this format. As such, it isrecommended that you contact your system administrator to find out how toobtain a MIME compliant email client or contact the message originator andrequest this email be resent in a format that is supported by your email client.\\r\n\r\n", 376); lseek=atlst;#ifdef DEBUG printf ("mime_encode: Finished header, attaching rest of message\n");#endif while (lseek) { tmphead=(char *)calloc (600, sizeof(char)); tmpat=((AFS *)lseek->data); if (tmpat->filename) { tmps2=strrchr (tmpat->filename,'.'); if (tmps2) tmps2++; mtype=mime_type_search_ext (tmps2);#ifdef DEBUG printf ("mime_encode: Attachment is a file '%s', opening for reading.\n", tmpat->filename);#endif fd=fopen (tmpat->filename, "r"); tmps2=(char *)calloc (4100, sizeof(char)); if (!fd) break; nread=fread (tmps2, sizeof(char), 4096, fd);#ifdef DEBUG printf ("mime_encode: Checking for the encodeing method we should use\n");#endif enc_type=encode_with (tmps2, nread); free (tmps2); st=(struct stat *)calloc (1, sizeof(struct stat)); stat (tmpat->filename, st); tmps2=(char *)calloc (st->st_size+2, sizeof(char)); fseek (fd, 0, SEEK_SET); fread (tmps2, sizeof (char), st->st_size, fd); fclose (fd); addtext (tmphead, "Content-Type: ", 14); if (mtype!=NULL) { addtext (tmphead, mtype->type, strlen(mtype->type)); } else { if (enc_type==ENC_BASE64) { addtext (tmphead, "application/octet-stream", 24); } else { addtext (tmphead, "text/plain", 10); } } tmps3=strrchr(tmpat->filename,'/'); if (tmps3) tmps3++; else tmps3=tmpat->filename; addtext (tmphead, ";\r\n\tname=\"", 10); addtext (tmphead, tmps3, strlen (tmps3)); addtext (tmphead, "\"\r\n", 3);#ifdef DEBUG printf ("mime_encode: Encodeing..."); fflush (stdout);#endif switch (enc_type) { case ENC_BASE64: addtext (tmphead, "Content-Transfer-Encoding: Base64", 33); addtext (tmphead, "\r\n", 2); tmpbody=mime_enc_base64 (tmps2, st->st_size, 0); free (tmps2); break; case ENC_QP: addtext (tmphead, "Content-Transfer-Encoding: Quoted-printable", 43); addtext (tmphead, "\r\n", 2); tmpbody=mime_enc_qp (tmps2, st->st_size, 0); free (tmps2); break; case ENC_NONE: default: addtext (tmphead, "Content-Transfer-Encoding: 7bit", 31); addtext (tmphead, "\r\n", 2); tmpbody=tmps2; tmps2=NULL; break; }#ifdef DEBUG printf ("done.\n");#endif addtext (tmphead, "Content-Disposition: attachment;\r\n\tfilename=\"", 45); addtext (tmphead, tmps3, strlen (tmps3)); addtext (tmphead, "\"\r\n", 3); free (st); } else { if (strncmp (tmpat->type,"message/rfc822", 14)!=0) { addtext (tmphead, "Content-Type: ", 14); addtext (tmphead, tmpat->type, strlen(tmpat->type)); addtext (tmphead, "; charset=us-ascii", 18); addtext (tmphead, "\r\n", 2); switch (encode_with (tmpat->data, strlen(tmpat->data))) { case ENC_BASE64: addtext (tmphead, "Content-Transfer-Encoding: Base64", 33); addtext (tmphead, "\r\n", 2); tmpbody=mime_enc_base64 (tmpat->data, strlen(tmpat->data), 0); break; case ENC_QP: addtext (tmphead, "Content-Transfer-Encoding: Quoted-printable", 43); addtext (tmphead, "\r\n", 2); tmpbody=mime_enc_qp (tmpat->data, strlen(tmpat->data), 0); break; case ENC_NONE: default: addtext (tmphead, "Content-Transfer-Encoding: 7bit", 31); addtext (tmphead, "\r\n", 2); tmpbody=strdup(tmpat->data); break; } } else { addtext (tmphead, "Content-Type: ", 14); addtext (tmphead, tmpat->type, strlen(tmpat->type)); addtext (tmphead, "; charset=us-ascii", 18); addtext (tmphead, "\r\n", 2); addtext (head, "Content-Transfer-Encoding: 7bit", 31); addtext (head, "\r\n", 2); tmps2=tmpat->data; if (*tmps2=='>') { while (*tmps2!='\n') { tmps2++; } tmps2++; } tmpbody=strdup (tmps2); tmps2=NULL; } } addtext (tmphead, "\r\n", 2); tmps3=(char *)calloc(strlen(tmphead)+strlen(tmpbody)+4, sizeof(char)); addtext (tmps3, tmphead, strlen(tmphead)); addtext (tmps3, tmpbody, strlen(tmpbody)); addtext (tmps3, "\r\n", 2); free (tmphead); free (tmpbody); tmps4=(char *)calloc (strlen(tmps3)+strlen(body)+ strlen(boundary)+8, sizeof(char)); addtext (tmps4, body, strlen(body)); addtext (tmps4, "--", 2); addtext (tmps4, boundary, strlen(boundary)); addtext (tmps4, "\r\n", 2); addtext (tmps4, tmps3, strlen(tmps3)); free (tmps3); free (body); body=tmps4; lseek=lseek->next; } tmps4=(char *)calloc (strlen(body)+strlen(boundary)+8, sizeof(char)); addtext (tmps4, body, strlen(body)); addtext (tmps4, "--", 2); addtext (tmps4, boundary, strlen(boundary)); addtext (tmps4, "--\r\n", 4); free (body); body=tmps4; } ret=(char *)calloc(strlen(head)+strlen(body)+4, sizeof(char)); addtext (ret, head, strlen(head)); addtext (ret, body, strlen(body)); addtext (ret, "\r\n", 2);#ifdef DEBUG printf( "The header is:\n%s\n\n", head ) ; printf( "The body is:\n%s\n\n", body ) ; printf( "The total message is:\n%s\n\n", ret ) ;#endif if (head) free (head); if (body) free (body);#if 0 if (lang) free (lang);#endif if (os_info) free (os_info); if (tmps1) free (tmps1); if (boundary) free (boundary); if (atmemb<2) free (tmphead); return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -