/*
    mod_bbs Multi Thread BBS Engine
    Copyright (C)2003 1000GEX Project 1000gex@1000gex.net
*/

static int bbs_read(bbs_t *bbs,request_rec *r)
{
    int i = 0;
    int lines = 0;
    int no = 0;
    int sep_num = 0;
    int size = 0;
    char *data[RES_MAX];
    char *search;
    char *buffer;
    char *subject;
    char *from;
    char *mail;
    char *date;
    char *message;
    char *path;
    char *dat_file;
    char *from_string;
    FILE *fp;
    res_t *res;
    
    res = ap_pcalloc(r->pool, sizeof(res_t));
    buffer = ap_pcalloc(r->pool, BBS_BUFFER_SIZE + 1);
    
    /* 入力データの判定 */
    if(bbs_read_check(bbs, r) < 0){
        error_html(r,"リクエストの情報が不正です");
        return -1;
    }
    
    /* testファイルの存在するディレクトリまでのパスを生成 */
    path = ap_pstrdup(r->pool, r->filename);
    path[safe_strlen(path) - 5] = '\0';
    


#if DEBUG
    ap_rprintf(r,"bbs_read:filename=%s<br>\n", r->filename);
    ap_rprintf(r,"bbs_read:path=%s<br>\n", path);
    ap_rprintf(r,"bbs_read:dat_file=%s<br>\n", dat_file);
    ap_rprintf(r,"bbs=%s key=%s opt=%s",bbs->bbs,bbs->key,bbs->opt);
#endif

    /* datファイルへのパスを生成 */
    dat_file = ap_psprintf(r->pool,"%s/%s/dat/%s.dat", path, bbs->bbs, bbs->key);

    /* datファイルの読み出し */
    if((fp = ap_pfopen(r->pool, dat_file, "r")) == NULL){
        error_html(r,"datファイルのオープンに失敗しました");
        return -1;
    }
    while(fgets(buffer,BBS_BUFFER_SIZE,fp) != NULL){
        data[lines] = ap_pstrdup(r->pool,buffer);
        size += safe_strlen(buffer);
        lines++;
        if(lines > RES_MAX){
            error_html(r,"datファイルが長すぎます");
            return -1;
        }
        //ap_rprintf(r,"%s<br>\n",buffer);
    }
    ap_pfclose(r->pool, fp);
    
    res = ap_restoken(r,data[0]);
    if(safe_strlen(res->mail) == 0){
        from_string = ap_psprintf(r->pool,"<FONT color=green><B>%s</B></FONT>", res->from);
    }
    else{
        from_string = ap_psprintf(r->pool,"<A href=\"mailto:%s\"><B>%s</B></A>", res->mail, res->from);
    }
    ap_rprintf(r,
                "<HTML><HEAD>"
                "<TITLE>%s mod_bbs</TITLE>"
                "</HEAD>"
                "<BODY bgcolor=#EFEFEF>\n"
                "<A href=\"../\">■掲示板に戻る■</A>"
                "<BR><FONT color=red size=+1>%s</FONT><BR>\n"
                "<DL>\n"
                ,res->subject
                ,res->subject
            );
    ap_rprintf(r,
                "<DT>%d ：%s　"
                "%s"
                "<DD>%s"
                "<br><br>\n"
                ,i + 1
                ,from_string
                ,res->date
                ,res->message
            );
    /* 読み出したデータを分解/整形してHTML出力 */
    for(i=1;i<lines;i++){
        res = ap_restoken(r,data[i]);
        if(safe_strlen(res->mail) == 0){
            from_string = ap_psprintf(r->pool,"<FONT color=green><B>%s</B></FONT>", res->from);
        }
        else{
            from_string = ap_psprintf(r->pool,"<A href=\"mailto:%s\"><B>%s</B></A>", res->mail, res->from);
        }
        ap_rprintf(r,
                    "<DT>%d ：%s　"
                    "%s"
                    "<DD>%s"
                    "<br><br>\n"
                    ,i + 1
                    ,from_string
                    ,res->date
                    ,res->message
                );
    }
    ap_rprintf(r,
                "</Dl><FONT face=Arial color=red><B>%d KB</B></FONT>"
                "<FONT size=2>"
                "<B> [ Apache+GCC "
                "<FONT size=4 color=tomato>世界最速マルチスレッド掲示板</FONT>"
                "　<A href=\"http://1000gex.net\" target=_blank>1000GEX Project</A>"
                " ]</B></FONT>"
                "<HR><CENTER>"
                "<A href=\"PATH/read.cgi/%s/%s/634-\">"
                "新着レスの表\示"
                "</A></CENTER><HR>"
                "<FORM action=../../../bbs.cgi method=post>"
                "<INPUT type=submit value=書き込む name=submit>"
                " 名前： <INPUT size=19 name=FROM>"
                " E-mail<FONT size=1> (省略可) </FONT>: "
                "<INPUT size=19 name=mail>"
                "<BR><TEXTAREA name=MESSAGE rows=5 wrap=off cols=70></TEXTAREA>"
                "<INPUT type=hidden value=%s name=bbs>"
                "<INPUT type=hidden value=%s name=key>"
                "<INPUT type=hidden value=1072962521 name=time>"
                "</FORM>"
                "mod_bbs Ver " BBS_VERSION
                "</BODY></HTML>"
                ,size / 1024
                ,bbs->bbs
                ,bbs->key
                ,bbs->bbs
                ,bbs->key
                );
    return 0;
}


res_t *ap_restoken(request_rec *r, char *string)
{
    int i = 0;
    int count = 0;
    int num = 0;
    res_t *res;
    
    res = ap_pcalloc(r->pool, sizeof(res_t));
    if(string == NULL){
        return res;
    }
    
    num = safe_strlen(string) - 2;
    res->from = string;
    for(i=0;i<num;i++){
        if( (string[0] == '<') && (string[1] == '>') ){
            switch(count){
                case 0:res->mail = string + 2;break;
                case 1:res->date = string + 2;break;
                case 2:res->message = string + 2;break;
                case 3:res->subject = string + 2;break;
            }
                string[0] = '\0';
                string[1] = '\0';
                count++;
        }
        string++;
    }
    return res;
}



static int bbs_read_check(bbs_t *bbs,request_rec *r)
{
    char *path = r->parsed_uri.path;
    char *token;
    char *temp[ READ_PATH_MAX ];
    int i = 0;
    int test_position = 0;
    int opt_flag = 0;

#if DEBUG
    ap_rprintf(r,"bbs_read_chack:path=%s<br>",path);
#endif

    while(*path && (temp[i] = ap_getword_nc(r->pool, &path, '/'))){
        if(i > READ_PATH_MAX){
            return -1;
        }
        if(!strcasecmp(temp[i],"test")){
            test_position = i;
        }
        i++;
    }

    if( (test_position == 0) || ((test_position + 3) > (i - 1)) ){
    
#if DEBUG
        ap_rprintf(r,"リクエストが変 pos=%d i=%d", test_position, i);
#endif

        return -1;
    }
    if( (test_position + 2) < (i - 1)){
        opt_flag = 1;
    }
    bbs->cgi = temp[test_position + 1];
    bbs->bbs = temp[test_position + 2];
    bbs->key = temp[test_position + 3];
    if(opt_flag){
        bbs->opt = temp[test_position + 4];
    }
    else{
        bbs->opt = '\0';
    }
    return 0;
}
