#include "hypermail.h" #include "setup.h" #include "struct.h" #include "threadprint.h" #include "printfile.h" #include "print.h" static void format_thread_info(FILE *, struct emailinfo *, int, int *, struct emailinfo *, FILE *, int, bool); static int finish_thread_levels(FILE **, int, int, int *, FILE **, char **, char **, int, struct emailinfo *, struct emailinfo *, char *, FILE *); static void finish_thread_file(FILE *, struct emailinfo *, char *); /* Define this to make it print a whole lot of debug output to stdout: */ /* #define DEBUG_THREAD */ #define MAXSTACK 200 /* A counter to know how many open li elements we have */ static int num_open_li[MAXSTACK + 1]; /* ** If year and/or month are != -1, only messages within the specified time ** period will be printed. */ void print_all_threads(FILE *fp, int year, int month, struct emailinfo *email) { int level = 0; int newlevel; int i; int prev = -1; int hide_level = 0; int thread_file_depth = (year == -1 && month == -1 ? set_thread_file_depth : 0); static int reply_list_count = 0; int stack[MAXSTACK + 1]; /* should be dynamic - this will do for now */ FILE *fp_stack[MAXSTACK + 1]; int num_replies[MAXSTACK + 1]; char *filename_stack[MAXSTACK + 1]; char *subject_stack[MAXSTACK + 1]; struct emailsubdir *subdir = email ? email->subdir : NULL; struct emailinfo *last_email; FILE *fp_body = NULL; char *filenameb = NULL; int threadnum = 0; bool is_first = TRUE; struct reply *rp = threadlist; last_email = rp->data; if (!last_email && rp->msgnum == -1 && set_files_by_thread) { progerr("files_by_thread error start with rp->msgnum == -1"); } for (i = 0; i <= MAXSTACK; i++) num_replies[i] = num_open_li[i] = 0; while (rp != NULL) { #if DEBUG_THREAD fprintf(stderr, "print_all_threads: message %d prev %d level %d\n", rp->msgnum, prev, level); #endif if (rp->msgnum == -1) { level = finish_thread_levels(&fp, level, 0, num_replies, fp_stack, filename_stack, subject_stack, thread_file_depth, email, last_email, filenameb, fp_body); filenameb = NULL; rp = rp->next; continue; } else if(level == 0 && subdir && rp->data->subdir != subdir) { rp = rp->next; continue; } #if DEBUG_THREAD fprintf(stderr, "print_all_threads: %d: %s\n", rp->msgnum, rp->data->name); #endif if (prev == -1) { level = finish_thread_levels(&fp, level, 0, num_replies, fp_stack, filename_stack, subject_stack, thread_file_depth, email, rp->data, filenameb, fp_body); filenameb = NULL; stack[level] = rp->msgnum; } else if (hide_level) { ; /* don't change level */ } else if (rp->frommsgnum == prev) { if (level < MAXSTACK) level++; else fprintf(stderr, "thread level too deep - sticking at %d\n", MAXSTACK); stack[level] = rp->msgnum; num_replies[level] = 0; if (!set_indextable) { if (level < set_thrdlevels) { if (level > thread_file_depth) { fprintf(fp, "\n"); if (num_open_li[level] != 0) { fprintf(*fp, ""); num_open_li[level]--; } fprintf (*fp, ""); printfooter(*fp, ihtmlfooterfile, set_label, set_dir, subject_stack[level], filename, TRUE); fclose(*fp); *fp = fp_stack[level - 1]; if (num_replies[level]) { fprintf(*fp, "\n", filename_stack[level], num_replies[level]); if (chmod(filename, set_filemode) == -1) { snprintf(errmsg, sizeof(errmsg), "Couldn't chmod \"%s\" to %o.", filename, set_filemode); progerr(errmsg); } num_open_li[level]++; } else remove(filename); free(filename_stack[level]); free(filename); } } else { if (num_open_li[level] != 0) { fprintf(*fp, ""); num_open_li[level]--; } } level--; } } else { level = newlevel; } return level; }