📄 attachments.cpp
字号:
#include "attachments.h"#include "fullpipe.h"void showattachment(char mailfile[], int mailnum, int attachnum){ FILE * mailbox; char data[500]; int count = 0; int attachcount = 0; char header[500]; int attachfound = 0; passwd *info; mailbox = fopen(mailfile, "r"); if (mailbox == NULL) { error("showattachment():can't open data for read."); return; } if (bothlock(mailbox, 'r')!=0) { fclose(mailbox); error("showattachment():Error locking mailstore"); return; } info=getpwnam(globaluser); while (fgets(data, 500, mailbox) != NULL) { if (strncmp(data, "From ", 5)==0) { count++; if (count > mailnum) break; } if (count == mailnum) //email found { if (strncmp(data, "begin ", 6)==0) { attachcount++; } if (attachcount == attachnum) { snprintf(header, 500, "content-type: %s\n\n", guessmime(data)); attachfound = 1; int pipe[2]; char uudecodepath[500]; //not really needed char rdata[100]; //attach data snprintf(uudecodepath, 500, "/usr/bin/uudecode"); char *arg[4]; arg[0] = "uudecode"; arg[1] = "-o"; arg[2] = "/dev/stdout"; arg[3] = '\0'; //make sure file handles are flushed to prevent duplicate //output from buffers. fflush(stdout); fflush(stdin); if (fullpipe_arg(uudecodepath, arg, pipe)) { error("showattachment(): Pipe error"); return; } int forkresult; forkresult = fork(); if (forkresult == (pid_t)-1) //error { error("showattachment(): fork error"); } if (forkresult == (pid_t)0) //child writes { do { write(pipe[1], data, (size_t)(strlen(data))); if (strncmp(data, "end", 3)==0) { break; } } while (fgets(data, 500, mailbox) != NULL); close(pipe[1]); exit(0); //kill child } else //parent reads { close(pipe[1]); //prevents blocking attachcount++; //need to end fprintf(stdout, "%s", header); fflush(stdout); int size; while ((size = read(pipe[0], rdata, 99))>0) { write(1, rdata, size); } close(pipe[0]); } } } } bothunlock(mailbox); fclose(mailbox); if (attachfound == 0) { error("showattachment(): No attachment found"); }}void mshowattachment(char mailfile[], int mailnum, int attachnum){ int number; //email to read passwd *info; FILE *mailstore; char filename[400]; char data[500]; char contenttype[400]; int count = 0; int attachcount = 0; int offofheader = 0; //mail header int extenddata = 0; //multi line headers int domarkold = 1; int onmimeheader = 0; int needsdecoding = 0; char answer[500]; char boundary[300]; boundary[0] = '-'; //sets up boundary boundary[1] = '-'; // " " " strncpy(contenttype, "text/plain\n", 399); mailstore = fopen(mailfile, "r"); if (mailstore == NULL) { error("showinbox():ERROR while tring to read mail file"); return; } if (bothlock(mailstore, 'r')!=0) { fclose(mailstore); error("showinbox():Error while locking mail file."); return; } while (fgets(data, 499, mailstore) != NULL) { if (strncmp(data, "From ", 5)==0) { count++; } if (count == mailnum) { if (data[0] == '\n') offofheader = 1; if (offofheader == 0) //in header { if (finddata(data, "boundary=", answer) != NULL) { int o = 0; if (answer[0] == '"') o++; //checks for" strncpy(boundary+2, answer+o, 298); for (o = 0; o<299; o++) { if (boundary[o] == '\n' || boundary[o] == '"' || boundary[o] == '\r' || boundary[o] == '\0') { boundary[o] = '\0'; break; } } } } else //out of header, in the email body in mime header { if (strncmp(data, boundary,strlen(boundary))==0) { onmimeheader = 1; attachcount++; } if (attachcount == attachnum) { if (onmimeheader && strncasecmp(data, "Content-Type: ", 14) == 0) { strncpy(contenttype, data+14, 399); } if (onmimeheader && strncasecmp(data, "Content-Transfer-Encoding: base64",33)==0) { needsdecoding = 1; } if (onmimeheader == 0) { int pipe[2]; char mimepath[500]; //not really needed char rdata[100]; //attach data char *arg[4]; if (needsdecoding) { snprintf(mimepath, 500, "/usr/bin/mimencode"); arg[0] = "mimencode"; arg[1] = "-u"; arg[2] = '\0'; } else { snprintf(mimepath, 500, "/bin/cat"); arg[0] = "cat"; arg[1] = '\0'; } //make sure file handles are flushed to prevent duplicate //output from buffers. fflush(stdout); fflush(stdin); if (fullpipe_arg(mimepath, arg, pipe)) { error("showattachment(): Pipe error"); return; } int forkresult; forkresult = fork(); if (forkresult == (pid_t)-1) //error { error("showattachment(): fork error"); } if (forkresult == (pid_t)0) //child writes { do { if (strncmp(data, boundary,strlen(boundary))==0) { break; } write(pipe[1], data, (size_t)(strlen(data))); } while (fgets(data, 500,mailstore) != NULL); close(pipe[1]); exit(0); //kill child } else //parent reads { close(pipe[1]); //prevents blocking attachcount++; //need to end int size; fflush(stdout); while ((size = read(pipe[0], rdata, 99))>0) { write(1, rdata, size); } close(pipe[0]); } } if (onmimeheader && strncmp(data, "\n", 1) == 0) { cout << "Content-Type: "; cout << contenttype << endl; //contenttype has a return at end //already. onmimeheader = 0; } } } }//attach count == num } //while bothunlock(mailstore); fclose(mailstore); return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -