📄 parse.c
字号:
if (bp) free(bp); bp=next; }#endif headp = NULL; } else { /* ...and this type is not a prefered one. Thus, we * shall ignore it completely! */ disposition = DISP_IGNORE;#if 0 printf("%s is to be ignored\n", type);#endif } } if (!strcasecmp(type, "text/plain") || (!alternativeparser && !strcasecmp(type, "text/html")) ) { /* * text or inlined html follows */ /* default is just plain 7/8 bit */ if(ENCODE_UNSET == decode) decode = ENCODE_NORMAL; if (!strcasecmp(type, "text/html")) content = CONTENT_HTML; else content = CONTENT_TEXT; continue; } else if (!strncasecmp(type, "message/rfc822", 14)) { /* ** Here comes an attached mail! This can be ugly, ** since the attached mail may very well itself ** contain attached binaries, or why not another ** attached mail? :-) ** ** We need to store the current boundary separator ** in order to get it back when we're done parsing ** this particular mail, since each attached mail ** will have its own boundary separator that *might* ** be used. */#if 0 /* removed 2001-02-07, this is old leftovers from when I * picked this code out of hypermail */ bp = addbody(bp, &lp, "<P><STRONG>attached mail follows:</STRONG><HR>", BODY_HTMLIZED | bodyflags);#endif bodyflags |= BODY_ATTACHED; isinheader = 2; continue; } else if (strncasecmp(type, "multipart/", 10)) { /* ** This is not a multipart and not text */ struct body *fnamep=NULL; char acomment[256]; char attachname[129]; /* listed attachment name */ char checkpath[256]; /* uniqueness path */ char *fname = NULL; /* attachment filename */ char nameisuniq=FALSE; /* use the name included ?*/ char *file = NULL; fname = strstr(ptr, "name="); if (NULL == fname) { /* ** Name of file not specified in the ** Content-Type header. See if the ** Content-Disposition header exists and ** contains the info. */ for (fnamep = head;fnamep;fnamep=fnamep->next) { if(!fnamep->header) continue; if (!strncasecmp(fnamep->line,"Content-Disposition:", 20)) { if ((fname = strstr(fnamep->line, "filename=")) != NULL) { sscanf(fname+10, "%128[^\"]",attachname); fname = attachname; } } } } else { sscanf(fname+6, "%128[^\"]", attachname); fname = attachname; }#if 0 sprintf(line, "** %s\n", fname?fname:"attachment"); bp = addbody(bp,&lp,line,BODY_HTMLIZED|bodyflags);#endif /* don't save this */ disposition = DISP_IGNORE; content = CONTENT_IGNORE; continue; } else { /* ** Find the first boundary separator */ boundary=strcasestr(ptr, "boundary="); if (boundary) { boundary=strchr(boundary, '='); if (boundary) { boundary++; while (isspace(*boundary)) boundary++; if ('\"' == *boundary) { sscanf(++boundary, "%[^\"]", boundbuffer); } else sscanf(boundary, "%s", boundbuffer); boundary = boundbuffer; } while (fgets(line, MAXLINE, fp)) { if (!strncmp(line, "--", 2) && !strncmp(line+2, boundbuffer, strlen(boundbuffer))) { break; } } /* ** This stores the boundary string in a stack ** of strings: */ boundp = bound(boundp, boundbuffer); /* printf("set new boundary: %s\n", boundp->line); */ /* ** We set ourselves, "back in header" since there is ** gonna come MIME headers now after the separator */ isinheader = 1; /* Daniel Stenberg started adding the * "multipart/alternative" parser 13th of July * 1998! We check if this is a 'multipart/ * alternative' header, in which case we need to * treat it very special. */ if(!strncasecmp(&ptr[10], "alternative", 11)) { /* It *is* an alternative session! Alternative ** means there will be X parts with the same text ** using different content-types. We are supposed ** to take the most prefered format of the ones ** used and only output that one. MIME defines ** the order of the texts to start with pure text ** and then continue with more and more obscure ** formats. (well, it doesn't use those terms but ** that's what it means! ;-)) */ /* How "we" are gonna deal with them: ** ** We create a "spare" linked list body for the ** very first part. Since the first part is ** defined to be the most readable, we save that ** in case no content-type present is prefered! ** ** We skip all parts that are not prefered. All ** prefered parts found will replace the first ** one that is saved. When we reach the end of ** the alternatives, we will use the last saved ** one as prefered. */ savealternative = TRUE;#if 0 printf("SAVEALTERNATIVE: yes\n");#endif } } else boundary = NULL; } } else if (!strncasecmp(head->line, "Content-Transfer-Encoding:", 26)) { char *ptr=head->line+26; head->parsedheader= TRUE; while (isspace(*ptr)) ptr++; if (!strncasecmp(ptr, "QUOTED-PRINTABLE", 16)) { decode = ENCODE_QP; } else if (!strncasecmp(ptr, "BASE64", 6)) { decode = ENCODE_BASE64; } else if (!strncasecmp(ptr, "8BIT", 4)) { decode = ENCODE_NORMAL; } else if (!strncasecmp(ptr, "7BIT", 4)) { decode = ENCODE_NORMAL; } else if (!strncasecmp(ptr, "x-uue", 5)) { decode = ENCODE_UUENCODE; if (uudecode(fp, line, line, NULL, TRUE)) /* ** oh gee, we failed this is chaos */ break; } else { /* this is an unknown format, we use default decoding */ char code[64]; sscanf(ptr, "%63s", code); sprintf(line, " ('%s')\n", code); bp = addbody(bp, &lp, line, BODY_HTMLIZED|bodyflags); } #if 0 printf("DECODE set to %d\n", decode);#endif } } if (savealternative) { /* let's remember 'bp' and 'lp' */ origbp=bp; origlp=lp; alternativeparser = TRUE; /* restart on a new list: */ lp=bp=NULL; } headp = lp; /* start at this point next time */ } else {#if 0 decodeRFC2047(line, MAXLINE);#endif bp = addbody(bp, &lp, line, BODY_HEADER|bodyflags); } } else { /* decode MIME complient gibberish */ char newbuffer[MAXLINE]; char *data; int datalen=-1; /* -1 means use strlen to get length */ if (Mime_B) { if (boundp && !strncmp(line, "--", 2) && !strncmp(line+2, boundp->line, strlen(boundp->line))) { /* right at this point, we have another part coming up */ isinheader = 1; /* back on a kind-of-header */#if 0 printf("hit %s\n", line);#endif if (!strncmp(line+2+strlen(boundp->line), "--", 2)) { bp = addbody(bp,&lp,"\n",BODY_HTMLIZED|bodyflags); isinheader = 0; /* no header, the ending boundary can't have any describing headers */#if 0 printf("End boundary %s\n", line);#endif boundp = bound(boundp, NULL); if (!boundp) { bodyflags &= ~BODY_ATTACHED; } if(alternativeparser) { struct body *next; /* we no longer have alternatives */ alternativeparser = FALSE;#if 0 printf("We DUMP an old alternative\n");#endif while(bp) { origbp = addbody(origbp, &origlp, bp->line, (bp->header?BODY_HEADER:0)| (bp->html?BODY_HTMLIZED:0)| (bp->attached?BODY_ATTACHED:0) ); next= bp->next; free(bp->line); free(bp); bp=next; } bp = origbp; lp = origlp; headp= NULL; }#if 0 if (boundp) printf("back %s\n", boundp->line); else printf("back to NONE\n");#endif } if (-1 != binfile) { close(binfile); binfile=-1; } continue; } } switch ( decode ) { case ENCODE_QP: mdecodeQP(fp, line, &data, &datalen); break; case ENCODE_BASE64: base64Decode(line, newbuffer, &datalen); data = newbuffer; break; case ENCODE_UUENCODE: uudecode(NULL, line, newbuffer, &datalen, FALSE); data = newbuffer; break; case ENCODE_NORMAL: case ENCODE_UNSET: data = line; break; default: /* we have no clue! */ data = NULL; break; }#if 0 printf("LINE %s\n", data);#endif if (data) { if ((content == CONTENT_TEXT) || (content==CONTENT_HTML)) { if (decode > ENCODE_MULTILINED) { /* ** This can be more than one resulting line, ** as the decoded the string may look like: "#!/bin/sh\r\n\r\nhelp() {\r\n echo 'Usage: difftree" */ char *p=data; char *n; char store;#if 0 printf("decode type %d\n", decode);#endif while ((n = strchr(p, '\n'))) { store = n[1]; n[1]=0;#if 0 printf("UNFOLDED %s", p);#endif bp = addbody(bp, &lp, p, (content==CONTENT_HTML? BODY_HTMLIZED:0)| (multilinenoend?BODY_CONTINUE:0)| bodyflags); multilinenoend = FALSE; /* full line pushed */ n[1]=store; p = n+1; } if (strlen(p)) { /* ** This line doesn't really end here, ** we will get another line soon that ** should get appended! */#if 0 printf("CONTINUE %s\n", p);#endif bp = addbody(bp, &lp, p, (content==CONTENT_HTML? BODY_HTMLIZED:0)| (multilinenoend?BODY_CONTINUE:0)| bodyflags); /* ** We want the next line to get appended to this! */ multilinenoend = TRUE; } } else { bp = addbody(bp, &lp, data, (content==CONTENT_HTML? BODY_HTMLIZED:0) | bodyflags ); }#if 0 printf("ALIVE?\n");#endif } else if (content == CONTENT_BINARY) { if (-1 != binfile) { if (datalen < 0) datalen = strlen(data); /*fwrite(data, datalen, 1, binfile); */ write(binfile, data, datalen); /*bp = addbody(bp, "file contents");*/ } } if (ENCODE_QP == decode) free(data); /* this was allocatd by mdecodeQP() */ } } } if (!isinheader || readone) { while (rmlastlines(bp)); num++; } fclose(fp); /* can we clean up a bit please... */ if (boundp != NULL) { if (boundp->line) free(boundp->line); free(boundp); } return bp;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -