static char * parse_mail(char * userid, int filetime, int mode, struct attach_link **attach_link_list) { if(mode != ARTICLE_PARSE_WITHOUT_ANSICOLOR && mode != ARTICLE_PARSE_WITH_ANSICOLOR) return NULL; if(!userid || filetime<=0) return NULL; char path[STRLEN]; snprintf(path, STRLEN, "mail/%c/%s/M.%d.A", mytoupper(userid[0]), userid, filetime); FILE *article_stream = fopen(path, "r"); if(!article_stream) return NULL; FILE *mem_stream, *html_stream; char *mem_buf, *html_buf, buf[512], attach_link[256], *tmp_buf, *attach_filename; size_t mem_buf_len, html_buf_len, attach_file_size; int attach_no = 0; mem_stream = open_memstream(&mem_buf, &mem_buf_len); fseek(article_stream, 0, SEEK_SET); keepoldheader(article_stream, SKIPHEADER); while(1) { if(fgets(buf, 500, article_stream) == 0) break; // RAW 模式下跳过 qmd if(mode == ARTICLE_PARSE_WITHOUT_ANSICOLOR && (strncmp(buf, "--\n", 3)==0)) break; // 附件处理 if(!strncmp(buf, "begin 644", 10)) { // TODO 老方式暂不实现 fflush(mem_stream); fclose(mem_stream); free(mem_buf); return NULL; } else if(checkbinaryattach(buf, article_stream, &attach_file_size)) { attach_no++; attach_filename = buf + 18; fprintf(mem_stream, "#attach %s\n", attach_filename); memset(attach_link, 0, 256); snprintf(attach_link, 256, "/api/attach/get?mid=%d&pos=%d&attname=%s", filetime, -4+(int)ftell(article_stream), attach_filename); add_attach_link(attach_link_list, attach_link, attach_file_size); fseek(article_stream, attach_file_size, SEEK_CUR); continue; } // 常规字符处理 if(mode == ARTICLE_PARSE_WITHOUT_ANSICOLOR && strchr(buf, '\033')!=NULL) { tmp_buf = strdup(buf); while(strchr(tmp_buf, '\033') != NULL) tmp_buf = string_replace(tmp_buf, "\033", "[ESC]"); fprintf(mem_stream, "%s", tmp_buf); free(tmp_buf); tmp_buf = NULL; } else { fprintf(mem_stream, "%s", buf[0]==0 ? "" : buf); } } fflush(mem_stream); fclose(article_stream); char *utf_content; if(mode == ARTICLE_PARSE_WITHOUT_ANSICOLOR) { if(strlen(mem_buf)==0) { utf_content = strdup(""); } else { utf_content = (char *)malloc(3*mem_buf_len); memset(utf_content, 0, 3*mem_buf_len); g2u(mem_buf, mem_buf_len, utf_content, 3*mem_buf_len); } } else { html_stream = open_memstream(&html_buf, &html_buf_len); fseek(mem_stream, 0, SEEK_SET); fprintf(html_stream, "<article>\n"); aha_convert(mem_stream, html_stream); fprintf(html_stream, "</article>"); fflush(html_stream); fclose(html_stream); utf_content = (char*)malloc(3*html_buf_len); memset(utf_content, 0, 3*html_buf_len); g2u(html_buf, html_buf_len, utf_content, 3*html_buf_len); free(html_buf); } // 释放资源 fclose(mem_stream); free(mem_buf); return utf_content; }
FILE * filetohtml(char *from, char *to, int no, int isdir, int depth) { FILE *fr; FILE *fw; char buf[512], *ext, *name=NULL; int pic, i, len, isa, base64; char dir[512]; fr = fopen(from, "r"); if (fr == NULL) return NULL; fw = fopen(to, "w"); if (fw == NULL) { fclose(fr); return NULL; } fprintf(fw, "<html>\r\n<head>\r\n<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>\r\n</head>\r\n<body>\r\n"); while (fgets(buf, sizeof (buf), fr) != NULL) { base64 = isa = 0; if (!strncmp(buf, "begin 644 ", 10)) { isa = 1; base64 = 1; name = buf + 10; } else if (checkbinaryattach(buf, fr, &len)) { isa = 1; base64 = 0; name = buf + 18; } if (isa) { strcpy(dir, to); ext = strrchr(dir, '.'); if (ext != NULL) *ext = 0; switch (getattach(fr, buf, name, dir, base64, len, 0)) { case 0: if ((ext = strrchr(name, '.')) != NULL) { if (strcasecmp(ext, ".bmp") && strcasecmp(ext, ".jpg") && strcasecmp(ext, ".gif") && strcasecmp(ext, ".jpeg")) pic = 0; else pic = 1; } else pic = 0; if (pic) fprintf (fw, "附图:\r\n<img src=\"%d/%s\"></img>\r\n", no, name); else fprintf(fw, "附件:\r\n<a href=\"%d/%s\">%s</a>\r\n", no, name, name); break; case -1: case -11: fprintf(fw, "这是一个附件,但是无法建立目录!\r\n"); break; case -2: case -12: fprintf(fw, "这是一个附件,但是文件名过长!\r\n"); break; case -3: fprintf(fw, "这是一个附件,但是解码失败!\r\n"); break; } } else htmlline(fw, buf); } fprintf(fw, "<center><a href=javascript:history.go(-1)>返回</a>"); if (no > 1) fprintf(fw, " <a href=./%d%s>上一项</a>", no - 1, isdir ? "/index.html" : ".html"); fprintf(fw, " <a href=./index.html>回到目录</a> <a href="); for (i = 1; i < depth; i++) fprintf(fw, "../"); fprintf(fw, "./index.html>首页</a>"); fclose(fr); return fw; }