示例#1
0
文件: html.c 项目: aabc/blists
static int html_send(struct buffer *dst)
{
	if (html_flags & HTML_BODY)
		buffer_appends(dst, footer);

	if (dst->error) {
		buffer_free(dst);
		return html_error(NULL);
	}

	write_loop(STDOUT_FILENO, dst->start, dst->ptr - dst->start);
	buffer_free(dst);

	return 0;
}
int main (int argc, char *argv[])
{
	char err_msg[256];
	long file_size = 0;
	long file_begin = 0, file_end = 0;

	// Get multipart file data separator
	char separator[MAX_SEPARATOR_LEN];

	html_header();

	if (get_content_separator(separator, sizeof(separator), &file_size) < 0)
	{
		html_error(RFC_ERROR);
		return -1;
	}

	// Get multipart file name
	char *filename = getenv("UPLOAD_FILENAME");
	if (filename == NULL)
	{
		html_error(RFC_ERROR);
		return -1;
	}

	// Wait until file is completely uploaded
	int tries = 0;
	while (tries>5)
	{
		struct stat filestat;
		if (stat(filename, &filestat)>0)
		{
			if (filestat.st_size >= file_size) // Size ok?
				break;
		}

		sleep(1000);
		tries++;
	}

	// Open file
	FILE *fd = fopen(filename, "r");
	if (fd == NULL)
	{
		html_error(RFC_ERROR);
		return -1;
	}

	// Parse parameters
	parameter_t *params;
	if (read_parameters(fd, separator, &params)<0)
	{
		fclose(fd);
		html_error(RFC_ERROR);
		return -1;
	}

	fclose(fd);

	// Find parameter containing NVRAM reset flag
	parameter_t *find = find_parameter(params, "reset_rwfs");
	int reset_rwfs = 0;
	if (find != NULL)
	{
		if (find->value != NULL)
			reset_rwfs = (strcasecmp(find->value, "on")==0);
	}

	// Find parameter containing file
	find = find_parameter(params, "filename");
	if (find != NULL)
	{
		// Check if parameter is correct
		if (find->content_type == NULL)
		{
			html_error(RFC_ERROR);
			return -1;
		}
		if (!check_binary_content_type(find->content_type))
		{
			sprintf(err_msg, "Unsupported content-type for binary data: %s", find->content_type);
			html_error(err_msg);
			return -1;
		}

		file_begin = find->start_pos;
		file_end   = find->end_pos;
	}
	else
	{
		html_error("No firmware binary file");
		return -1;
	}

	release_parameters(params);

	// check image size
	if (!check(filename, (int)file_begin, (int)(file_end - file_begin), err_msg))
	{
		html_error(err_msg);
		return -1;
	}

	// firmware update timeouts
	// examination, reset NVRAM if needed
	// start web timer and crash rwfs BEFORE flash destroy
	if (reset_rwfs)
	{
		system("/bin/mtd_write erase RW-FS > /dev/null");
		html_success(18*(IMAGE1_SIZE/0x100000) + 60);
	} else
		html_success(18*(IMAGE1_SIZE/0x100000) + 50);

	// flash write
	if (mtd_write_firmware(filename, (int)file_begin, (file_end - file_begin)) == -1)
		return -1;

	sleep (3);
	reboot(RB_AUTOBOOT);
	return 0;
}
/*
 * SubVar
 *
 * This function substitutes variables designated by the leading separator (sep)
 * with values with matching names.  The string buffer will be realloced in 512-byte
 * chunks if needed.
 */
char *SubVar(char sep, char *var_name, char *var_value, char *oldbuf, int *bufsize) {
    char *buf, *s, *t;
    int l, n;
    char o;
    char *local_buf;
#if MSQL
	char *u;
	int i,j;
    int numi, numj;
    char temp[1024];
    m_result *result=NULL;
    m_row record;
    int new_connection=0;
#endif
 
    l=strlen(oldbuf);
    n=strlen(var_value);
 
    if(l-(int)strlen(var_name)-1+(int)strlen(var_value) >(int)(*bufsize-1)) {

#if DEBUG
        fprintf(fperr,"Query buffer re-allocated\n");
        fflush(fperr);
#endif

        buf=realloc(oldbuf,*bufsize+((n>512)?n:512));
        *bufsize+=((n>512)?n:512);
    } else buf=oldbuf;
 
    /* I don't trust strdup() on many systems */
    local_buf=malloc(*bufsize*sizeof(char));
    strcpy(local_buf,buf);
 
    if(!local_buf) {
        html_error("FI Error","Unable to allocate memory");
        exit(-1);
    }
    s=local_buf;
 
    while(*s!='\0') {
        if(*s==sep) {
            if(s>local_buf && *(s-1)=='\\') {  /* Allow for escaping the separator */
                s++;
                continue;
            }
            n=strcspn(s+1," ,()'\n:;.{}[]-+=~!@#$%^*<>\"");
 
            t=s+n+1;
            o=*t;
            *t='\0';
            if(!strcasecmp(var_name,s+1)) {
                *s='\0';
#if MSQL
                if(!strcasecmp(var_name,"sql")) {
                    /* looking for database name */
 
#if DEBUG
                    fprintf(fperr,"var_name=[%s], var_value=[%s]\n",var_name,var_value);
                    fflush(fperr);
#endif
 
                    u=strchr(var_value,' ');
                    if(u) {
                        *u='\0';
                        if(strlen(current_db)==0) {
                            if(dbsock==-1) {
                                dbsock=msqlConnect(NULL);
                                new_connection=1;
                            }
                            if(msqlSelectDB(dbsock,var_value)<0) {
                                strcpy(temp,msqlErrMsg);
                                current_db[0]='\0';
                            } else {
                                strcpy(current_db,var_value);
                            }
                        }
                        if(msqlQuery(dbsock,u+1)<0) strcpy(temp,msqlErrMsg);
                        else {
                            temp[0]='\0';
                            msqlStoreResult(result);
                            numi=msqlNumRows(result);
                            for(i=0;i<numi;i++) {
                                if(strlen(before_record)) strcat(temp,before_record);
                                msqlDataSeek(result,i);
                                numj=msqlNumFields(result);
                                record=msqlFetchRow(result);
                                for(j=0;j<numj;j++) {
                                    if(strlen(before_element)) strcat(temp,before_element);
                                    strcat(temp,record[j]);
                                    if(strlen(after_element)) strcat(temp,after_element);
                                }
                                if(strlen(after_record)) strcat(temp,after_record);
                            }
                            msqlFreeResult(result);
                            if(new_connection) {
                                msqlClose(dbsock);
                                dbsock=-1;
                                current_db[0]='\0';
                            }
                        }
                    }
                }
#endif
                sprintf(buf,"%s%s%c%s",local_buf,var_value,o,t+1);
                strcpy(local_buf,buf);
            } else {
                *t=o;
            }
            s=t+1;
        } else s++;
    }
    free(local_buf);
    return(buf);
}
示例#4
0
int oversight_main(int argc,char **argv,int send_content_type_header)
{
    int result=0;
    int done=0;

    g_start_clock = time(NULL);
    assert(sizeof(long long) >= 8);

    init_view();
    adjust_path();

    char *q=getenv("QUERY_STRING");
    char *sp=getenv("SCRIPT_NAME");

    char *p;
    char *req;
    if (q && (p = delimited_substring(q,"&",REMOTE_VOD_PREFIX2,"=",1,0)) != NULL) {

        gaya_auto_load(p+strlen(REMOTE_VOD_PREFIX2)+1);
        done=1;

    } else if (q && strstr(q,YAMJ_PREFIX2)) {

        g_query=parse_query_string(q,g_query);
        //req = url_decode(q+strlen(YAMJ_PREFIX2));
        req = url_decode(query_val("yamj"));
        yamj_xml(req);
        FREE(req);
        done=1;
    } else if (sp && (req=strstr(sp,YAMJ_PREFIX)) != NULL) {
        // If oversight script is launched as /oversight/yamj/xxxxx.xml
        // then use xxxxxx.xml as a yamj xml request.
        // This is to allow for Apache ModAlias to serve static images whilst calling oversight for CGI
        // The rewrite rules should be 
        //    ScriptAliasMatch ^/oversight/yamj/(.*).xml  /share/Apps/oversight/oversight.cgi
        //    AliasMatch ^/oversight/yamj/banner_(.*jpg) /oversight/db/global/_b/ovs_$1
        //    AliasMatch ^/oversight/yamj/fanart_(.*jpg) /oversight/db/global/_fa/ovs_$1
        //    AliasMatch ^/oversight/yamj/poster_(.*jpg) /oversight/db/global/_J/ovs_$1
        //    AliasMatch ^/oversight/yamj/thumb_(.*).jpg  /oversight/db/global/_J/ovs_$1.thumb.jpg
        //    AliasMatch ^/oversight/yamj/boxset_(.*).jpg  /oversight/db/global/_J/ovs_$1.thumb.boxset.jpg`
        //
        req += strlen(YAMJ_PREFIX);
        yamj_xml(req);
        done=1;

    } else if (q == NULL || strchr(q,'=') == NULL ) {

        if (argc > 1 ) {

            if ( argv[1] && *argv[1] && argv[2] == NULL && util_starts_with(argv[1],YAMJ_PREFIX) ) {
                char *req = url_decode(argv[1]+strlen(YAMJ_PREFIX));
                yamj_xml(req);
                FREE(req);
                done=1;

            } else if ( argv[1] && *argv[1] && argv[2] == NULL && strchr(argv[1],'=') == NULL) {
                // Single argument passed.
                //
                char *path = url_decode(argv[1]);
                char *dot = strrchr(path,'.');
                if (dot < path) dot = strchr(path,'\0');
                int result = 0;

                fprintf(stderr,"path=[%s]",path);

                // should really use file command or magic number to determine file type

                if (dot && STRCMP(dot,".png") == 0 ) {

                    result = cat(CONTENT_TYPE"image/png",path);

                } else if (dot &&  STRCMP(dot,".jpg") == 0 ) {

                    result = cat(CONTENT_TYPE"image/jpeg",path);

                } else if (dot &&  STRCMP(dot,".gif") == 0) {

                    result = cat(CONTENT_TYPE"image/gif",path);

                } else if (dot &&  (STRCMP(dot,".swf") == 0 || STRCMP(dot,".phf" ) == 0) ) {

                    result = cat(CONTENT_TYPE"application/x-shockwave-flash",path);

                } else if (browsing_from_lan() ) {

                    if (is_dir(path)) {

                        // load_configs(); // load configs so we can use file_to_url() functions 
                        result = ls(path);
                    } else {
                        int exists = is_file(path);

                        char *all_headers = NULL;
                        char *content_headers = NULL;

                        if (exists) {
                            if (strstr(path,".tar.gz") || strcmp(dot,".tgz") == 0) {

                                ovs_asprintf(&content_headers,"%s%s\n%s%s",
                                        CONTENT_TYPE,"application/x-tar",CONTENT_ENC,"gzip");

                            } else if (strcmp(dot,".gz") == 0 ) {

                                ovs_asprintf(&content_headers,"%s%s\n%s%s",
                                        CONTENT_TYPE,"application/x-gzip",CONTENT_ENC,"identity");

                            } else if (strcmp(dot,".html") == 0 ) {

                                ovs_asprintf(&content_headers,"%s%s",
                                        CONTENT_TYPE,"text/html;charset=utf-8");

                            } else {
                                ovs_asprintf(&content_headers,"%s%s",
                                        CONTENT_TYPE,"text/plain;charset=utf-8");
                            }
                        } else {
                            // .gz.txt is a fake extension added by the ls command to view log.gz inline without browser downloading.
                            if (strstr(path,".gz.txt")) {
                                ovs_asprintf(&content_headers,"%s%s\n%s%s",
                                        CONTENT_TYPE,"text/plain;charset=utf-8", CONTENT_ENC,"gzip");
                                // remove .txt to get real zip file.
                                // .txt is needed so a certain browser displays inline. (might be other ways)
                                *dot = '\0';
                            } else {
                                // 404 error would be here
                            }
                        }
                        ovs_asprintf(&all_headers,"%s\n%s%ld",content_headers,CONTENT_LENGTH,file_size(path));
                        FREE(content_headers);
                        result = cat(all_headers,path);
                        FREE(all_headers);

                    }
                }
                FREE(path);
                fflush(stdout);
                done=1;
            }
        }
    }



    if (!done) {
        if (send_content_type_header) {
            printf("Content-Type: text/html; charset=utf-8\n\n");

            start_page("CGI");
        } else {
            start_page("WGET");
        }

        html_log_level_set(2);

        load_configs();
        //html_hashtable_dump(0,"settings",g_nmt_settings);

        long log_level;
        if (config_check_long(g_oversight_config,"ovs_log_level",&log_level)) {
            html_log_level_set(log_level);
        }

        html_comment("Appdir= [%s]",appDir());

        //array_unittest();
        //util_unittest();
        //config_unittest();

        g_query = string_string_hashtable("g_query2",16);

        html_comment("default query ... ");
        add_default_html_parameters(g_query);
        html_hashtable_dump(0,"prequery",g_query);

        html_comment("read query ... ");
        g_query=parse_query_string(getenv("QUERY_STRING"),g_query);
        html_hashtable_dump(0,"query",g_query);

        html_comment("read post ... ");

        struct hashtable *post=read_post_data(getenv("TEMP_FILE"));
        html_hashtable_dump(0,"post",g_query);
        
        html_comment("merge query and post data");
        merge_hashtables(g_query,post,1); // post is destroyed

        html_hashtable_dump(0,"query final",g_query);


#if 0
        html_comment("utf8len expect 2 = %d",utf8len("a"));
        html_comment("utf8len expect 2 = %d",utf8len("à€€a"));
        html_comment("utf8len expect 2 = %d",utf8len("ü€€€€€a"));
        html_comment("utf8cmp_char 0 = %d",utf8cmp_char("ü€€€€€a","ü€€€€€b"));
        html_comment("utf8cmp_char !0 = %d",utf8cmp_char("ü€€€€€a","ü€€€€€a"));
        html_comment("utf8cmp_char 0 = %d",utf8cmp_char("a","a"));
        html_comment("utf8cmp_char !0 = %d",utf8cmp_char("a","b"));
        html_comment("utf8cmp_char !0 = %d",utf8cmp_char("ü€€€€€a","üa"));
        html_comment("utf8cmp_char !0 = %d",utf8cmp_char("a","ü€€€€€a"));
        Abet *a = abet_create("abcdefghijklmnopqrstuvwxyz");
        html_comment("inc a %d",abet_letter_inc_or_add(a,"a",1));
        html_comment("inc a %d",abet_letter_inc_or_add(a,"a",1));
        html_comment("inc z %d",abet_letter_inc_or_add(a,"z",1));
        html_comment("inc 4 %d",abet_letter_inc_or_add(a,"4",1));
        html_comment("inc 5 %d",abet_letter_inc_or_add(a,"5",1));
        html_comment("inc 5 %d",abet_letter_inc_or_add(a,"5",1));
        html_comment("inc 6* %d",abet_letter_inc_or_add(a,"6",0));
        html_comment("inc 7* %d",abet_letter_inc_or_add(a,"7",0));
        html_comment("inc a %d",abet_letter_inc_or_add(a,"a",1));
        abet_free(a);
#endif


        config_read_dimensions(1);

        HTML_LOG(0,"Begin Actions");
        do_actions();
       
        ViewMode *view;

        DbSortedRows *sortedRows = NULL;


        while(1) {
            view=get_view_mode(1);  
            HTML_LOG(0,"view mode = [%s]",view->name);

            // If movie view but all ids have been removed , then move up
            if (view == VIEW_MOVIE && !*query_val(QUERY_PARAM_IDLIST)) {
                query_pop();
                view=get_view_mode(1);  
            }

            sortedRows = get_sorted_rows_from_params();
            dump_all_rows("sorted",sortedRows->num_rows,sortedRows->rows);

            // If it's not a tv/movie detail or boxset view then break
            if (view == VIEW_MENU ||  view == VIEW_ADMIN ) {
                break;
            }

            // Found some data - as we are on a smaller view - filter it
            if (sortedRows->num_rows && sortedRows->num_rows < 50 ) {
                int new_num = sortedRows->num_rows;
                int max_new = sortedRows->num_rows;
                DbItem **new_list = filter_page_items(0,sortedRows->num_rows,sortedRows->rows,max_new,&new_num);
                FREE(sortedRows->rows);
                sortedRows->rows = new_list;
                sortedRows->num_rows=new_num;
            }
            if (sortedRows->num_rows) break;

            // No data found in this view - try to return to the previous view.
            query_pop();
            // Adjust config - 
            // TODO Change the config structure to reload more efficiently.
            //reload_configs();

            config_read_dimensions(1);

            // Now refetch all data again with new parameters.
            sorted_rows_free_all(sortedRows);
            HTML_LOG(0,"reparsing database");
        }

        // Remove and store the last navigation cell. eg if user clicked on cell 12 this is passed in 
        // the URL as @i=12. The url that returns to this page then has i=12. If we have returned to this
        // page we must remove i=12 from the query so that it is not passed to the new urls created for this 
        // page.
        set_selected_item();

        char *skin_name=get_skin_name();


        if (strchr(skin_name,'/') || *skin_name == '.' || !*skin_name ) {

            html_error("Invalid skin name[%s]",skin_name);

        } else {

            playlist_open();
            //exp_test();

            if (view->view_class == VIEW_CLASS_ADMIN) {

                setPermissions();
                display_admin(sortedRows);

            } else {

                char *template = query_val(QUERY_PARAM_TEMPLATE_NAME);
                if (EMPTY_STR(template)) {
示例#5
0
文件: html.c 项目: aabc/blists
int html_month_index(const char *list, unsigned int y, unsigned int m)
{
	unsigned int d, n, aday, dp;
	int fd;
	idx_msgnum_t mn[32], mp, count, total;
	struct buffer dst;
	int first; /* first message of this month */
	off_t size, size_n;
	struct idx_message *msgp = NULL, *msg = NULL;
	int prev = 0, next = 0;

	if (y < MIN_YEAR || y > MAX_YEAR ||
	    m < 1 || m > 12)
		return html_error("Invalid date");
	aday = ((y - MIN_YEAR) * 12 + (m - 1)) * 31;

	fd = idx_open(list);
	if (fd < 0)
		return html_error(errno == ENOENT ?
		    "No such mailing list" : NULL);

	if (!idx_read_aday_ok(fd, aday, mn, sizeof(mn))) {
		idx_close(fd);
		return html_error("Index error");
	}

	/* quickly calculate how many messages we have in this month */
	total = 0;
	first = 0;
	mp = mn[0];
	for (d = 1; d <= 31; d++) {
		if (!mn[d])
			continue;
		if (mp > 0) {
			/* Remember index of first message */
			if (first == 0)
				first = mp;
			count = (mn[d] > 0) ? mn[d] - mp : -mn[d];
			if (count <= 0) {
				buffer_free(&dst);
				idx_close(fd);
				return html_error(NULL);
			}
			total += count;
		}
		mp = mn[d];
	}
	/* have messages, allocate and read them */
	if (total && first) {
		off_t got;
		off_t idx_offset;

		first--;
		size = total * sizeof(struct idx_message);
		idx_offset = IDX2MSG(first);
		/* we need to read prev and next messages too */
		if (first) {
			size += sizeof(struct idx_message);
			idx_offset -= sizeof(struct idx_message);
			prev = 1;
		}
		size_n = size + sizeof(struct idx_message);

		if (!(msgp = malloc(size_n)) ||
		    (got = idx_read(fd, idx_offset, msgp, size_n)) == -1 ||
		    (got != size && got != size_n)) {
			idx_close(fd);
			free(msgp);
			return html_error("Index error");
		}
		msg = msgp + prev;
		next = (got == size_n) ? total + prev : 0;
	}

	if (idx_close(fd) || buffer_init(&dst, 0)) {
		free(msgp);
		return html_error(NULL);
	}

	buffer_appends(&dst, "\n");

	if (html_flags & HTML_HEADER) {
		buffer_appends(&dst, "<title>");
		buffer_appends_html(&dst, list);
		buffer_appendf(&dst, " mailing list - %u/%02u</title>\n", y, m);
	}

	if (html_flags & HTML_BODY) {
		if (prev) {
			buffer_appends(&dst, "<a href=\"");
			buffer_appendf(&dst, "../../%u/%02u/\">[&lt;prev month]</a> ",
			    MIN_YEAR + msgp[0].y, msgp[0].m);

		}
		if (next) {
			buffer_appends(&dst, "<a href=\"");
			buffer_appendf(&dst, "../../%u/%02u/\">[next month&gt;]</a> ",
			    MIN_YEAR + msgp[next].y, msgp[next].m);

		}
		buffer_appends(&dst,
		    "<a href=\"..\">[year]</a>"
		    " <a href=\"../..\">[list]</a>\n");

		buffer_appends(&dst, "<p><h2>");
		buffer_appends_html(&dst, list);
		buffer_appendf(&dst, " mailing list - %u/%02u</h2>\n", y, m);

		if (!total || !msg) {
			buffer_free(&dst);
			free(msgp);
			return html_error("No messages for this month");
		}

		html_output_month_cal(&dst, mn, y, m, L_DAILY);

		total = 0;
		dp = 0;
		mp = mn[0];
		for (d = 1; d <= 31; d++) {
			if (!mn[d])
				continue;
			if (mp > 0) {
				if (mn[d] > 0)
					count = mn[d] - mp;
				else
					count = -mn[d];
				if (count <= 0) {
					buffer_free(&dst);
					free(msgp);
					return html_error(NULL);
				}
				if (!total)
					buffer_appends(&dst, "<p>Messages by day:\n<p>\n");
				total += count;

				buffer_appendf(&dst, "<b>%s %u</b> "
				    "(<a href=\"%02u/\">%u message%s</a>)<br>\n"
				    "<ul>\n",
				    month_name[m - 1], dp + 1,
				    dp + 1, count, count == 1 ? "" : "s");

				int maxn = count;
				if (count >= MAX_SHORT_MSG_LIST)
					maxn = MAX_SHORT_MSG_LIST;
				if (count > MAX_SHORT_MSG_LIST)
					maxn--;

				for (n = 1; n <= maxn; n++) {
					buffer_appendf(&dst, "<li><a href=\"%02u/%u\">", dp + 1, n);
					output_strings(&dst, msg++, 1);
					buffer_appends(&dst, "\n");
				}
				msg += count - maxn;
				if (count > MAX_SHORT_MSG_LIST)
					buffer_appendf(&dst, "<li><a href=\"%02u/\">%u more messages</a>\n", d, count - maxn);
				buffer_appends(&dst, "</ul>\n");
			}
			mp = mn[d];
			dp = d;
		}

		if (total)
			buffer_appendf(&dst, "<p>%u message%s\n", total, total == 1 ? "" : "s");
		else
			buffer_appends(&dst, "<p>No messages\n");
	}

	free(msgp);

	return html_send(&dst);
}
示例#6
0
文件: html.c 项目: aabc/blists
int html_day_index(const char *list, unsigned int y, unsigned int m, unsigned int d)
{
	unsigned int aday;
	off_t idx_offset;
	off_t size, size_n;
	int fd, error, got;
	idx_msgnum_t mx[2]; /* today, next day */
	struct buffer dst;
	struct idx_message *mp;
	int prev; /* have prev message = 1 */
	int count; /* how many messages in this month */
	int next; /* flag & index to next message */

	if (y < MIN_YEAR || y > MAX_YEAR ||
	    m < 1 || m > 12 ||
	    d < 1 || d > 31)
		return html_error("Invalid date");
	aday = YMD2ADAY(y - MIN_YEAR, m, d);

	fd = idx_open(list);
	if (fd < 0)
		return html_error(errno == ENOENT ?
		    "No such mailing list" : NULL);
	/* read two consecutive aday entries
	 * will need them to determine message count for this day */
	error = !idx_read_aday_ok(fd, aday, &mx, sizeof(mx));
	if (error || mx[0] < 1 || mx[0] >= MAX_MAILBOX_MESSAGES) {
		idx_close(fd);
		return html_error((error || mx[0] > 0) ? NULL : "No messages"
		    " for this day");
	}
	if (mx[1] > 0)
		count = mx[1] - mx[0];
	else
		count = -mx[1];
	size = count * sizeof(struct idx_message);
	idx_offset = IDX2MSG(mx[0] - 1);
	if (mx[0] > 1) {
		/* read one more entry for Prev day quick link */
		size += sizeof(struct idx_message);
		idx_offset -= sizeof(struct idx_message);
		prev = 1;
	} else {
		prev = 0;
	}

	/* read one more entry for Next day quick link */
	size_n = size + sizeof(struct idx_message);

	if (!(mp = malloc(size_n)) ||
	    (got = idx_read(fd, idx_offset, mp, size_n)) == -1 ||
	    (got != size && got != size_n)) {
		idx_close(fd);
		free(mp);
		return html_error("Index error");
	}
	next = (got == size_n) ? count + prev : 0;

	if (idx_close(fd) || error || buffer_init(&dst, 0)) {
		free(mp);
		return html_error(NULL);
	}

	buffer_appends(&dst, "\n");

	if (html_flags & HTML_HEADER) {
		buffer_appends(&dst, "<title>");
		buffer_appends_html(&dst, list);
		buffer_appendf(&dst, " mailing list - %u/%02u/%02u</title>\n", y, m, d);
	}

	if (html_flags & HTML_BODY) {
		int i;
		if (prev) {
			buffer_appends(&dst, "<a href=\"");
			buffer_appendf(&dst, "../../../%u/%02u/%02u/\">[&lt;prev day]</a> ",
			    MIN_YEAR + mp[0].y, mp[0].m, mp[0].d);
		}
		if (next) {
			buffer_appends(&dst, "<a href=\"");
			buffer_appendf(&dst, "../../../%u/%02u/%02u/\">[next day&gt;]</a> ",
			    MIN_YEAR + mp[next].y, mp[next].m, mp[next].d);
		}
		buffer_appends(&dst,
		    "<a href=\"..\">[month]</a>"
		    " <a href=\"../..\">[year]</a>"
		    " <a href=\"../../..\">[list]</a>\n");

		buffer_appends(&dst, "<p><h2>");
		buffer_appends_html(&dst, list);
		buffer_appendf(&dst, " mailing list - %u/%02u/%02u</h2>\n", y, m, d);

		if (count)
			buffer_appends(&dst, "<ul>\n");
		for (i = 0; i < count; i++) {
			struct idx_message *msg = mp + i + prev;

			buffer_appendf(&dst, "<li><a href=\"%u\">", i + 1);
			output_strings(&dst, msg, 1);
			buffer_appends(&dst, "\n");
		}
		if (count)
			buffer_appends(&dst, "</ul>\n");

		buffer_appendf(&dst, "<p>%u message%s\n", count, count == 1 ? "" : "s");
	}

	free(mp);

	return html_send(&dst);
}
示例#7
0
文件: html.c 项目: aabc/blists
int html_attachment(const char *list, unsigned int y, unsigned int m, unsigned int d, unsigned int n, unsigned int a)
{
	unsigned int aday;
	char *list_file;
	off_t idx_offset;
	int fd, error, got, trunc;
	idx_msgnum_t m1, m1r;
	struct idx_message idx_msg;
	idx_off_t offset;
	idx_size_t size;
	struct buffer src, dst;
	struct mime_ctx mime;
	char *body, *bend;

	if (y < MIN_YEAR || y > MAX_YEAR ||
	    m < 1 || m > 12 ||
	    d < 1 || d > 31 ||
	    n < 1 || n > 999999)
		return html_error("Invalid date or message number");
	aday = YMD2ADAY(y - MIN_YEAR, m, d);

	list_file = concat(MAIL_SPOOL_PATH "/", list, NULL);
	if (!list_file)
		return html_error(NULL);

	fd = idx_open(list);
	if (fd < 0) {
		error = errno;
		free(list_file);
		return html_error(error == ENOENT ?
		    "No such mailing list" : (error == ESRCH ?
		    "Index needs rebuild" : NULL));
	}

	error = !idx_read_aday_ok(fd, aday, &m1, sizeof(m1));
	if (error || m1 < 1 || m1 >= MAX_MAILBOX_MESSAGES) {
		idx_close(fd);
		free(list_file);
		return html_error((error || m1 > 0) ? NULL : "No such message");
	}
	m1r = m1 + n - 2; /* both m1 and n are 1-based; m1r is 0-based */
	idx_offset = IDX2MSG(m1r);
	got = idx_read(fd, idx_offset, &idx_msg, sizeof(idx_msg));
	if (got != sizeof(idx_msg))
		error = 1;

	if (idx_close(fd) || error) {
		free(list_file);
		return html_error(got ? NULL : "No such message");
	}

	if (y - MIN_YEAR != idx_msg.y ||
	    m != idx_msg.m ||
	    d != idx_msg.d) {
		free(list_file);
		return html_error("No such message");
	}

	offset = idx_msg.offset;
	size = idx_msg.size;

	trunc = size > MAX_MESSAGE_SIZE;
	if (trunc)
		size = MAX_MESSAGE_SIZE;
	if (buffer_init(&src, size)) {
		free(list_file);
		return html_error(NULL);
	}
	fd = open(list_file, O_RDONLY);
	free(list_file);
	if (fd < 0) {
		buffer_free(&src);
		return html_error("mbox open error");
	}
	error =
	    lseek(fd, offset, SEEK_SET) != offset ||
	    read_loop(fd, src.start, size) != size;
	if (close(fd) || error || mime_init(&mime, &src)) {
		buffer_free(&src);
		return html_error("mbox read error");
	}
	if (buffer_init(&dst, size)) {
		buffer_free(&src);
		mime_free(&mime);
		return html_error(NULL);
	}

	body = NULL;
	while (src.end - src.ptr > 9 && *src.ptr != '\n') {
		switch (*src.ptr) {
		case 'C':
		case 'c':
			mime_decode_header(&mime);
			continue;
		}
		mime_skip_header(&mime);
	}
	if (src.ptr >= src.end) {
		buffer_free(&src);
		buffer_free(&dst);
		mime_free(&mime);
		return html_error(NULL);
	}
	if (*src.ptr == '\n')
		body = ++src.ptr;

	const char *error_msg = "Attachment not found";
	unsigned int attachment_count = 0;
	if (a)
	do {
		if (mime.entities->boundary) {
			body = mime_next_body_part(&mime);
			if (!body || body >= src.end)
				break;
			body = mime_next_body(&mime);
		}
		if (mime.entities->boundary || !is_attachment(&mime) || ++attachment_count != a)
			body = NULL;
		if (!body) {
			bend = mime_skip_body(&mime);
			if (!bend)
				break;
			continue;
		}

		int text = !strncasecmp(mime.entities->type, "text/", 5);
		if (text) {
			buffer_appends(&dst, "Content-Type: text/plain");
			if (mime.entities->charset && enc_allowed_charset(mime.entities->charset))
				buffer_appendf(&dst, "; charset=%s", mime.entities->charset);
			buffer_appendc(&dst, '\n');
		} else {
			buffer_appends(&dst, "Content-Type: application/octet-stream\n");
		}
		buffer_appendf(&dst, "Content-Disposition: %s; filename=\"", text ? "inline" : "attachment");
		buffer_append_filename(&dst, mime.entities->filename, text);
		buffer_appends(&dst, "\"\n");

		body = mime_decode_body(&mime, RECODE_NO, &bend);
		if (trunc && (!body || bend >= src.end)) {
			error_msg = "Attachment is truncated";
			break;
		}

		buffer_appendf(&dst, "Content-Length: %llu\n\n", (unsigned long long)(mime.dst.ptr - body));
		buffer_append(&dst, body, mime.dst.ptr - body);
		error_msg = NULL;
		break;
	} while (bend < src.end && mime.entities);

	buffer_free(&src);

	if (error_msg || mime.dst.error || dst.error) {
		mime_free(&mime);
		buffer_free(&dst);
		return html_error(error_msg);
	}

	mime_free(&mime);

	return html_send(&dst);
}
示例#8
0
文件: html.c 项目: aabc/blists
int html_message(const char *list, unsigned int y, unsigned int m, unsigned int d, unsigned int n)
{
	unsigned int aday, n0, n2;
	char *list_file;
	off_t idx_offset;
	int fd, error, got, trunc, prev, next;
	idx_msgnum_t m0, m1, m1r;
	struct idx_message idx_msg[3];
	idx_off_t offset;
	idx_size_t size;
	struct buffer src, dst;
	struct mime_ctx mime;
	char *p, *q, *date, *from, *to, *cc, *subject, *body, *bend;

	if (y < MIN_YEAR || y > MAX_YEAR ||
	    m < 1 || m > 12 ||
	    d < 1 || d > 31 ||
	    n < 1 || n > 999999)
		return html_error("Invalid date or message number");
	aday = YMD2ADAY(y - MIN_YEAR, m, d);

	list_file = concat(MAIL_SPOOL_PATH "/", list, NULL);
	if (!list_file)
		return html_error(NULL);

	fd = idx_open(list);
	if (fd < 0) {
		error = errno;
		free(list_file);
		return html_error(error == ENOENT ?
		    "No such mailing list" : (error == ESRCH ?
		    "Index needs rebuild" : NULL));
	}

	error = !idx_read_aday_ok(fd, aday, &m1, sizeof(m1));
	if (error || m1 < 1 || m1 >= MAX_MAILBOX_MESSAGES) {
		idx_close(fd);
		free(list_file);
		return html_error((error || m1 > 0) ? NULL : "No such message");
	}
	m1r = m1 + n - (1 + 1); /* both m1 and n are 1-based; m1r is 0-based */
	idx_offset = IDX2MSG(m1r);
	prev = next = 1;
	if (m1r >= 1) {
		idx_offset -= sizeof(idx_msg[0]);
		got = idx_read(fd, idx_offset, &idx_msg, sizeof(idx_msg));
		if (got != sizeof(idx_msg)) {
			error = got != sizeof(idx_msg[0]) * 2;
			idx_msg[2] = idx_msg[1];
			next = 0;
		}
	} else {
		prev = 0;
		got = idx_read(fd, idx_offset, &idx_msg[1], sizeof(idx_msg[1]) * 2);
		if (got != sizeof(idx_msg[1]) * 2) {
			error = got != sizeof(idx_msg[1]);
			idx_msg[2] = idx_msg[1];
			next = 0;
		}
		idx_msg[0] = idx_msg[1];
	}

	n0 = n - 1;
	if (!n0 && prev && !error) {
		aday = YMD2ADAY(idx_msg[0].y, idx_msg[0].m, idx_msg[0].d);
		error = !idx_read_aday_ok(fd, aday, &m0, sizeof(m0));
		if (m1 > m0)
			n0 = m1 - m0;
		else
			error = 1;
	}

	if (idx_close(fd) || error) {
		free(list_file);
		return html_error(got ? NULL : "No such message");
	}

	n2 = n + 1;
	if (idx_msg[2].y != idx_msg[1].y ||
	    idx_msg[2].m != m || idx_msg[2].d != d)
		n2 = 1;

	if (y - MIN_YEAR != idx_msg[1].y ||
	    m != idx_msg[1].m || d != idx_msg[1].d) {
		free(list_file);
		return html_error("No such message");
	}

	offset = idx_msg[1].offset;
	size = idx_msg[1].size;

	trunc = size > MAX_MESSAGE_SIZE;
	if (trunc)
		size = MAX_MESSAGE_SIZE;
	if (buffer_init(&src, size)) {
		free(list_file);
		return html_error(NULL);
	}
	fd = open(list_file, O_RDONLY);
	free(list_file);
	if (fd < 0) {
		buffer_free(&src);
		return html_error("mbox open error");
	}
	error =
	    lseek(fd, offset, SEEK_SET) != offset ||
	    read_loop(fd, src.start, size) != size;
	if (close(fd) || error || mime_init(&mime, &src)) {
		buffer_free(&src);
		return html_error("mbox read error");
	}
	if (buffer_init(&dst, size)) {
		buffer_free(&src);
		mime_free(&mime);
		return html_error(NULL);
	}

	date = from = to = cc = subject = body = NULL;
	while (src.end - src.ptr > 9 && *src.ptr != '\n') {
		switch (*src.ptr) {
		case 'D':
		case 'd':
			if (!strncasecmp(src.ptr, "Date:", 5)) {
				date = mime_decode_header(&mime);
				continue;
			}
			break;
		case 'F':
		case 'f':
			if (!strncasecmp(src.ptr, "From:", 5)) {
				from = mime_decode_header(&mime);
				continue;
			}
			break;
		case 'T':
		case 't':
			if (!strncasecmp(src.ptr, "To:", 3)) {
				to = mime_decode_header(&mime);
				continue;
			}
			break;
		case 'S':
		case 's':
			if (!strncasecmp(src.ptr, "Subject:", 8)) {
				subject = mime_decode_header(&mime);
				continue;
			}
			break;
		case 'C':
		case 'c':
			if (!strncasecmp(src.ptr, "CC:", 3))
				cc = mime_decode_header(&mime);
			else
				mime_decode_header(&mime);
			continue;
		}
		mime_skip_header(&mime);
	}
	if (src.ptr >= src.end) {
		buffer_free(&src);
		buffer_free(&dst);
		mime_free(&mime);
		return html_error(NULL);
	}
	if (*src.ptr == '\n')
		body = ++src.ptr;

	if ((p = subject)) {
		while ((p = strchr(p, '['))) {
			if (strncasecmp(++p, list, strlen(list)))
				continue;
			q = p + strlen(list);
			if (*q != ']')
				continue;
			if (*++q == ' ') q++;
			memmove(--p, q, strlen(q) + 1);
		}
	}

	buffer_appends(&dst, "\n");

	if (html_flags & HTML_HEADER) {
		buffer_appends(&dst, "<title>");
		buffer_appends_html(&dst, list);
		if (subject && strlen(subject) > 9) {
			buffer_appends(&dst, " - ");
			buffer_appends_html(&dst, subject + 9);
		}
		buffer_appends(&dst, "</title>\n");
		if (html_flags & HTML_CENSOR)
			buffer_appends(&dst, "<meta name=\"robots\" content=\"noindex\">\n");
	}

	if (html_flags & HTML_BODY) {
		unsigned int attachment_count = 0;

		if (prev) {
			buffer_appends(&dst, "<a href=\"");
			if (n == 1)
				buffer_appendf(&dst, "../../../%u/%02u/%02u/",
				    MIN_YEAR + idx_msg[0].y, idx_msg[0].m, idx_msg[0].d);
			buffer_appendf(&dst, "%u\">[&lt;prev]</a> ", n0);
		}
		if (next) {
			buffer_appends(&dst, "<a href=\"");
			if (n2 == 1)
				buffer_appendf(&dst, "../../../%u/%02u/%02u/",
				    MIN_YEAR + idx_msg[2].y, idx_msg[2].m, idx_msg[2].d);
			buffer_appendf(&dst, "%u\">[next&gt;]</a> ", n2);
		}
		if (idx_msg[1].t.pn) {
			buffer_appends(&dst, "<a href=\"");
			if (idx_msg[1].t.py != idx_msg[1].y ||
			    idx_msg[1].t.pm != idx_msg[1].m ||
			    idx_msg[1].t.pd != idx_msg[1].d)
				buffer_appendf(&dst, "../../../%u/%02u/%02u/",
				    MIN_YEAR + idx_msg[1].t.py, idx_msg[1].t.pm, idx_msg[1].t.pd);
			buffer_appendf(&dst, "%u\">[&lt;thread-prev]</a> ", idx_msg[1].t.pn);
		}
		if (idx_msg[1].t.nn) {
			buffer_appends(&dst, "<a href=\"");
			if (idx_msg[1].t.ny != idx_msg[1].y ||
			    idx_msg[1].t.nm != idx_msg[1].m ||
			    idx_msg[1].t.nd != idx_msg[1].d)
				buffer_appendf(&dst, "../../../%u/%02u/%02u/",
				    MIN_YEAR + idx_msg[1].t.ny, idx_msg[1].t.nm, idx_msg[1].t.nd);
			buffer_appendf(&dst, "%u\">[thread-next&gt;]</a> ", idx_msg[1].t.nn);
		}
		buffer_appends(&dst,
		    "<a href=\".\">[day]</a>"
		    " <a href=\"..\">[month]</a>"
		    " <a href=\"../..\">[year]</a>"
		    " <a href=\"../../..\">[list]</a>\n");

		buffer_appends(&dst, "<pre style=\"white-space: pre-wrap\">\n");
		if (date)
			buffer_append_header(&dst, date);
		if (from)
			buffer_append_header(&dst, from);
		if (to)
			buffer_append_header(&dst, to);
		if (cc)
			buffer_append_header(&dst, cc);
		if (subject)
			buffer_append_header(&dst, subject);

		if (!(html_flags & HTML_CENSOR))
		do {
			if (mime.entities->boundary) {
				body = mime_next_body_part(&mime);
				if (!body || body >= src.end)
					break;
				body = mime_next_body(&mime);
			}
			if (mime.entities->boundary)
				body = NULL;
			if (!body) {
				bend = mime_skip_body(&mime);
				if (!bend)
					break;
				continue;
			}

			/* mime_decode_body() will break mime vars, so,
			 * remember them now */
			char *filename = mime.entities->filename;
			char *type = mime.entities->type;
			const int isattachment = is_attachment(&mime);
			const int isinline = is_inline(&mime);
			int skip = 0;

			body = mime_decode_body(&mime, isattachment ? RECODE_NO : RECODE_YES, &bend);
			if (!body)
				break;
			if (bend >= src.end)
				skip = trunc;
			bend = src.ptr;
			if (!skip && isattachment) {
				int text = !strncasecmp(type, "text/", 5);
				attachment_count++;
				buffer_appendf(&dst, "\n<span style=\"font-family: times;\"><strong>"
				    "%s attachment \"</strong><a href=\"%u/%u\"%s>",
				    text ? "View" : "Download", n, attachment_count,
				    text ? "" :  " rel=\"nofollow\" download");
				if (filename)
					buffer_appends_html(&dst, filename);
				buffer_appends(&dst, "</a><strong>\" of type \"</strong>");
				buffer_appends_html(&dst, type);
				buffer_appends(&dst, "<strong>\"");
				if (body)
					buffer_appendf(&dst, " (%llu bytes)", (unsigned long long)(mime.dst.ptr - body));
				buffer_appends(&dst, "</strong></span>\n");
				continue;
			} else if (!isinline) {
				skip = 1;
			} else {
				skip = 0; /* do not skip non-attachments */
			}
			if (skip) {
				buffer_appends(&dst, "\n<span style=\"font-family: times;\"><strong>"
				    "Content of type \"</strong>");
				buffer_appends_html(&dst, type);
				buffer_appends(&dst, "<strong>\" skipped</strong></span>\n");
				continue;
			}
			/* inline */
			buffer_appendc(&dst, '\n');
			buffer_append_html_generic(&dst, body, mime.dst.ptr - body, 0, 1);
			mime.dst.ptr = body;
		} while (bend < src.end && mime.entities);

		if ((html_flags & HTML_CENSOR) || trunc)
			buffer_appendf(&dst, "\n<span style=\"font-family: times;\"><strong>"
			    "Content %s</strong></span>\n", (html_flags & HTML_CENSOR) ? "removed" : "truncated");

		buffer_appends(&dst, "</pre>\n");
	}

	buffer_free(&src);

	if (mime.dst.error || dst.error) {
		mime_free(&mime);
		buffer_free(&dst);
		return html_error(NULL);
	}

	mime_free(&mime);

	return html_send(&dst);
}
示例#9
0
文件: html.c 项目: aabc/blists
int html_year_index(const char *list, unsigned int y)
{
	unsigned int min_y, max_y, m, d, aday, rday;
	int fd;
	idx_msgnum_t *mn, count;
	size_t mn_size;
	struct buffer dst;

	aday = 0;
	mn_size = (N_ADAY + 1) * sizeof(idx_msgnum_t);
	min_y = MIN_YEAR;
	max_y = MAX_YEAR;
	if (y) {
		if (y < min_y || y > max_y)
			return html_error("Invalid date");
		aday = (y - min_y) * (12 * 31);
		mn_size = (12 * 31 + 1) * sizeof(idx_msgnum_t);
		min_y = max_y = y;
	}

	fd = idx_open(list);
	if (fd < 0)
		return html_error(errno == ENOENT ? "No such mailing list" : NULL);

	if (!(mn = malloc(mn_size)) ||
	    !idx_read_aday_ok(fd, aday, mn, mn_size)) {
		idx_close(fd);
		free(mn);
		return html_error(NULL);
	}

	/* find first and next index for Prev and Next year links */
	int first = 0;
	int lastn = 0;
	rday = YMD2ADAY(min_y - MIN_YEAR, 1, 1) - aday;
	unsigned int eday = YMD2ADAY(max_y - MIN_YEAR + 1, 1, 1) - aday;
	int sanity = 0;
	for (; rday < eday; rday++) {
		if (mn[rday] > 0) {
			if (!first)
				first = mn[rday];
			if (mn[rday + 1] <= 0)
				lastn = mn[rday] + -mn[rday + 1];
			else
				lastn = mn[rday + 1];
			/* sanity check of index */
			if (lastn <= mn[rday] ||
			    ((mn[rday] != sanity) && sanity)) {
				buffer_free(&dst);
				free(mn);
				return html_error("Index corrupt");
			}
			sanity = lastn;
		}
	}
	int prev = 0;
	int next = 0;
	if (first || lastn) {
		struct idx_message msg;
		off_t size = sizeof(struct idx_message);
		if (first > 1) {
			if (!idx_read_msg_ok(fd, first - 2, &msg, size)) {
				free(mn);
				return html_error("Index error");
			}
			prev = MIN_YEAR + msg.y;
		}
		if (lastn > 1) {
			if (idx_read_msg_ok(fd, lastn, &msg, size))
				next = MIN_YEAR + msg.y;
		}
	}

	/* read Recent messages */
	struct idx_message *msg = NULL;
	int recent_count = 0;
	int i;
	if (min_y != max_y && lastn > 1) {
		int recent_offset = 0;
		recent_count = MAX_RECENT_MSG_LIST;
		if ((lastn - 1) < recent_count)
			recent_count = lastn - 1;
		size_t size = recent_count * sizeof(struct idx_message);
		recent_offset = lastn - recent_count;
		if (!(msg = malloc(size)) ||
		    !idx_read_msg_ok(fd, recent_offset - 1, msg, size))
			recent_count = 0;

		/* resolve to message number in the day and cache in offset field */
		rday = YMD2ADAY(min_y - MIN_YEAR, 1, 1) - aday;
		i = 0;
		for (; rday < eday; rday++) {
			if (mn[rday] > 0 && recent_offset >= mn[rday]) {
				count = aday_count(&mn[rday]);
				while (recent_offset < (mn[rday] + count)) {
					msg[i].offset = recent_offset - mn[rday] + 1;
					recent_offset++;
					i++;
					if (i > recent_count)
						break;
				}
			}
		}
	}
	if (idx_close(fd) || buffer_init(&dst, 0)) {
		free(msg);
		free(mn);
		return html_error(NULL);
	}

	buffer_appends(&dst, "\n");

	if (html_flags & HTML_HEADER) {
		buffer_appends(&dst, "<title>");
		buffer_appends_html(&dst, list);
		buffer_appends(&dst, " mailing list");
		if (min_y == max_y)
			buffer_appendf(&dst, " - %u", y);
		buffer_appends(&dst, "</title>\n");
	}

	if (html_flags & HTML_BODY) {
		if (prev)
			buffer_appendf(&dst, "<a href=\"../%u/\">[prev year]</a>\n", prev);
		if (next)
			buffer_appendf(&dst, "<a href=\"../%u/\">[next year]</a>\n", next);
		if (min_y == max_y)
			buffer_appends(&dst, "<a href=\"..\">[list]</a>\n");

		buffer_appends(&dst, "<p><h2>");
		buffer_appends_html(&dst, list);
		buffer_appends(&dst, " mailing list");
		if (min_y == max_y)
			buffer_appendf(&dst, " - %u", y);
		buffer_appends(&dst, "</h2>\n");

		idx_msgnum_t total = 0, monthly_total[12];
		/* output short year-o-month index */
		int o_header = 0;
		int o_year = 0;
		int o_month = -1;
		for (y = max_y; y >= min_y; y--) {
			rday = YMD2ADAY(y - MIN_YEAR, 1, 1) - aday;
			for (m = 1; m <= 12; m++) {
				monthly_total[m - 1] = 0;
				for (d = 1; d <= 31; d++, rday++) {
					if (mn[rday] <= 0)
						continue;
					if (mn[rday + 1] > 0)
						count = mn[rday + 1] - mn[rday];
					else
						count = -mn[rday + 1];
					monthly_total[m - 1] += count;
				}
				if (!monthly_total[m - 1])
					continue;
				if (!o_header) {
					buffer_appends(&dst,
					    "\n<table border=0 "
					    "class=cal_brief><tr><th>"
					    "<th>Jan<th>Feb<th>Mar"
					    "<th>Apr<th>May<th>Jun"
					    "<th>Jul<th>Aug<th>Sep"
					    "<th>Oct<th>Nov<th>Dec\n");
					o_header++;
				}
				if (o_year != y) {
					if (o_month >= 0) {
						for (o_month++; o_month <= 12; o_month++)
							buffer_appends(&dst, "<td>&nbsp;");
					}
					buffer_appendf(&dst, "\n<tr><td>");
					if (min_y != max_y)
						buffer_appendf(&dst, "<a href=\"%u/\">", y);
					buffer_appendf(&dst, "<b>%4u</b>", y);
					if (min_y != max_y)
						buffer_appends(&dst, "</a>");
					o_year = y;
					o_month = 0;
				}
				for (o_month++; o_month < m; o_month++)
					buffer_appends(&dst, "<td>&nbsp;");
				buffer_appendf(&dst, "<td><a href=\"");
				if (min_y != max_y)
					buffer_appendf(&dst, "%u/", y);
				buffer_appendf(&dst, "%02u/\">%u</a>", m, monthly_total[m - 1]);
				o_month = m;
				total += monthly_total[m - 1];
			}
		}
		if (o_header) {
			if (o_year) {
				for (o_month++; o_month <= 12; o_month++)
					buffer_appends(&dst, "<td>&nbsp;");
			}
			buffer_appends(&dst, "\n</table>\n");
		}

		/* output Recent messages */
		if (msg && recent_count) {
			buffer_appends(&dst, "<br>Recent messages:<br>\n<ul>\n");
			for (i = recent_count - 1; i >= 0; i--) {
				buffer_appendf(&dst,
				    "<li>%04u/%02u/%02u #%u: <a href=\"%04u/%02u/%02u/%u\">\n",
				    msg[i].y + MIN_YEAR, msg[i].m, msg[i].d, (int)msg[i].offset,
				    msg[i].y + MIN_YEAR, msg[i].m, msg[i].d, (int)msg[i].offset);
				output_strings(&dst, &msg[i], 1);
				buffer_appends(&dst, "\n");
			}
			buffer_appends(&dst, "</ul>\n");
		}

		free(msg); msg = NULL;

		/* output monthly calendars */
		if (min_y == max_y) {
			y = min_y;
			buffer_appends(&dst, "\n<p>\n<table border=0 class=cal_big>");
			for (m = 1; m <= 12; m++) {
				rday = YMD2ADAY(y - MIN_YEAR, m, 1) - aday;
				if (m % 3 == 1) {
					unsigned int n;
					buffer_appends(&dst, "\n<tr>");
					for (n = m; n < m + 3; n++) {
						if (monthly_total[n - 1])
							buffer_appendf(&dst, "<th><a href=\"%02u/\">%s</a>",
							    n, month_name[n - 1]);
						else
							buffer_appendf(&dst, "<th>%s", month_name[n - 1]);
					}
					buffer_appends(&dst, "\n<tr>");
				}

				buffer_appends(&dst, "<td valign=\"top\">");
				html_output_month_cal(&dst, &mn[rday], y, m, L_MONTHLY);
			}
			buffer_appends(&dst, "</table>");
		}

		if (total)
			buffer_appendf(&dst, "<p>%u message%s\n", total, total == 1 ? "" : "s");
		else
			buffer_appends(&dst, "<p>No messages\n");
	} /* HTML_BODY */

	free(msg);
	free(mn);

	return html_send(&dst);
}