/***************
*
* g:\exe\txl\src\txllog2.c
*/
#include "txl.h"


#define BYTE(x) *((unsigned char *)(x))
#define BIT_CHK(x,f) ((int)(BYTE(x+((f)>>3)) >> 7-((f) & 0x07)) & 1)

void get_forum(int type)
{
	char *dmyptr;

	log_pname[0] = NUL;
	if (type == 1) {
		dmyptr = log_bufptr - 49;
		while (*dmyptr > ' ') {
			dmyptr--;
		}
		if (strncmp("＜フォーラム体験＆質問コーナー＞", dmyptr+1, 32)) {
			sscanf(dmyptr, "%16s", log_fname);
		}
		else {
			strcpy(log_fname ,"BEGIN");
		}
	}
	if (type == 2) {
		dmyptr = log_bufptr + 1;
		sscanf(dmyptr, "%16s LIB:%[0-9]", log_fname, log_libstr);
	}
}
void get_libno(int type)
{
	char *dmyptr;

	if ((type == 1) || (type == 2)) {
		dmyptr = log_bufptr + 1;
	}
	sscanf(dmyptr, "%[0-9]", log_libstr);
}

void get_titlestr()
{
	char *tmp;
	char *env;
	int i, ktype = CT_ANK;
	int r1, r2, r3, r = 0, end = 1;

	tmp = log_bufptr;

/* 暫定 */
	while (end) {
		end = 0;
		while (r1 = matchstr("re:", tmp)) {
			end = 1;
			tmp += 3;
			r += 1;
		}
		while (r2 = matchstr("re$%:", tmp)) {
			end = 1;
			if (*(tmp + 3) > '0') {
				r += *(tmp + 3) - '0';
			}
			tmp += 5;
		}
		while (r3 = matchstr("re$%%:", tmp)) {
			end = 1;
			r += atoi(tmp + 3);
			tmp += 6;
		}
		while (r3 = matchstr("re$%%%:", tmp)) {
			end = 1;
			r += atoi(tmp + 3);
			tmp += 7;
		}
	}

	end = 1;
	if ((env = getenv("TXLRES")) != NULL) {
		if (matchstr("re$'%'d:", env) && r) {
			end = 0;
			sprintf(log_titlestr, env, r+1);
		}
	}
	if (end) {
		if (strncmp(tmp, log_resstr, strlen(log_resstr))) {
			strcpy(log_titlestr, log_resstr);
		}
	}
	strncat(log_titlestr, tmp, 41 - strlen(log_titlestr));

	tmp = strchr(log_titlestr, 0x0d);
	if (tmp != NULL) {
		*tmp = NUL;
	}
	else {
		if (strlen(log_titlestr) > 40) {
/*			*(jstradv(log_titlestr, btom(log_titlestr, 38))) = NUL; */
			for ( i = 0; i < 41; i++) {
				if (*(log_titlestr + i) == NUL) {
					break;
				}
				ktype = chkctype(*(log_titlestr+i), ktype);
			}
			if (i > 40) {
				if (ktype != CT_KJ2) {
					*(log_titlestr+i-1) = NUL;
				}
				else {
					*(log_titlestr+i-2) = NUL;
				}
			}
		}
	}

}

void del_tailspace(char *str, int len)
{
	int i;
	for (i = len; i > 0; i--) {
		if (*(str + i) == 0x20) {
			*(str + i) = NUL;
		}
		else if ((*(str + i) == 0x40) && 
				(*(str + i - 1) == 0x81)) { /* 全角スペース	*/
			i--;
			*(str + i) = NUL;
		}
		else {
			break;
		}
	}
}

void meschk(int style)
{
	if (style == 3) {
		sscanf(log_bufptr, "%[0-9]", log_reply);
		log_bufptr += 10;
		strncpy(log_resp, log_bufptr, 8);
		strncpy(log_rhandle, log_bufptr + 10, 16);
		del_tailspace(log_rhandle, 15);
#if 0
		for (i = 15; i > 0; i--) {
			if (*(log_rhandle + i) == 0x20) {
				*(log_rhandle + i) = NUL;
			}
			else if ((*(log_rhandle + i) == 0x40) && 
					(*(log_rhandle + i - 1) == 0x81)) { /* 全角スペース	*/
				i--;
				*(log_rhandle + i) = NUL;
			}
			else {
				break;
			}
		}
#endif
		log_bufptr += 28;
		get_titlestr();
		if ((log_bufptr = strchr(log_bufptr, 0x0a)) == NULL) {
			fprintf(fpmes, "Error:find EOF before title+1 line");
			Exit(1);
		}
		log_bufptr += 2;
		sscanf(log_bufptr, "%2d", &log_mesno);
	}
	else if (style == 2) {
		log_bufptr += 2;
		sscanf(log_bufptr, "%s  MES(%d)", log_fname, &log_mesno);
		if (log_mesno == 0) {
			sscanf(log_bufptr, "MES(%d)", &log_mesno);
		}
		*log_reply = NUL;
		*log_titlestr = NUL;
	}
	else if (style == 1) {
		sscanf(log_bufptr, "%2d", &log_mesno);
		*log_reply = NUL;
		*log_titlestr = NUL;
	}
	else if (style == 8) {
		sscanf(log_bufptr, "%[0-9]", log_reply);
		log_bufptr += 12;
		strncpy(log_resp, log_bufptr, 8);
		strncpy(log_rhandle, log_bufptr + 10, 16);
		del_tailspace(log_rhandle, 15);
#if 0
		for (i = 15; i > 0; i--) {
			if (*(log_rhandle + i) == 0x20) {
				*(log_rhandle + i) = NUL;
			}
			else if ((*(log_rhandle + i) == 0x40) && 
					(*(log_rhandle + i - 1) == 0x81)) { /* 全角スペース	*/
				i--;
				*(log_rhandle + i) = NUL;
			}
			else {
				break;
			}
		}
#endif
		log_bufptr += 27;
		get_titlestr();
		if ((log_bufptr = strchr(log_bufptr, 0x0a)) == NULL) {
			fprintf(fpmes, "Error:find EOF before title+1 line");
			Exit(1);
		}
		log_bufptr += 2;
		sscanf(log_bufptr, "%2d", &log_mesno);
	}
	if (log_pname[0] == NUL) {
		fprintf(fpmes, "forum=%s;mes=%d;reply=%s;resp=%s;handle=%s;title=%s\n"
			, log_fname, log_mesno, log_reply, log_resp, log_rhandle,
				log_titlestr);
	}
	else {
		fprintf(fpmes, "patio=%s;mes=%d;reply=%s;resp=%s;handle=%s;title=%s\n"
			, log_pname, log_mesno, log_reply, log_resp, log_rhandle,
				log_titlestr);
	}

}

char *setdataexp(char kind)
{
	static char expbuf[4];

	switch (kind) {
	case 'B':
		strcpy(expbuf, "LZH");
		break;
	case 'T':
		strcpy(expbuf, "ISH");
		break;
	case 'N':
		strcpy(expbuf, "NPB");
		break;
	default:
		strcpy(expbuf, "DAT");
	}
	return (expbuf);
}

void get_downname(void)
{
	int i=0, j=0, type=CT_ANK, dmy=0;
	char *tmp = log_bufptr;

	if (*tmp == '[') {
		while ((*tmp != ']') && (*tmp != '\n')) tmp++;
		if (*tmp == ']') tmp++;
	}
	for (i = 0; ((j < 8) && (*(tmp + i) != '.') &&
			(*(tmp + i) != '\n')); i++) {
		if (BIT_CHK(filechr, *(tmp + i)) ||
				((type = chkctype(*(tmp + i), type)) != CT_ANK)) {
			*(log_downname + j) = *(tmp + i);
			j++;
		}
	}
	if (*(tmp+i) == '.') {
		*(log_downname+j) = '.';
		j++;
		tmp += i;
		for (i = 1; (*(tmp + i) != '.') &&
				(*(tmp+i)!='\n'); i++) {
			if (i == 1) {
				while (*(tmp + i) == ' ') {
					dmy++;
					i++;
				}
			}
			if (BIT_CHK(filechr, *(tmp+i)) ||
					((type = chkctype((*tmp + i), type)) != CT_ANK)) {
				*(log_downname + j) = *(tmp + i);
				j++;
			}
			if ((i - dmy) >= 3) {
				break;
			}
		}
	}

	if (*log_downname == 'n') {
		*log_downname = 'N';
	}
}

void dlibchk(int style)
{
/*
 250  PFG02442 92/04/05   25408   25 B TXF136E .EXE ﾃｷｽﾄﾌｧｲﾙ ﾌｫｰﾏｯﾀ Ver1.36e
*/
	char exchr = NUL;
	char *tmpptr;

	if (style == 5) { /* for 週間FTOWNS/週間FYUI */
		log_bufptr += 3;
		sscanf(log_bufptr, "%4d", &log_downno);
		log_bufptr += 4;
	}
	else if (style == 4) {
		sscanf(log_bufptr, "%4d", &log_downno);
		strncpy(log_resp, log_bufptr + 6, 8);
		log_resp[8] = NUL;

		exchr = *(log_bufptr+37);
		log_bufptr += 39;
	}
	else if ((style == 19) || (style == 20)) {
		log_bufptr += style - 18;
		sscanf(log_bufptr, "%4d", &log_downno);
		strncpy(log_resp, log_bufptr + 6, 8);
		log_resp[8] = NUL;
		exchr = *(log_bufptr+32);
		log_bufptr += 34;
	}

	get_downname();

	strcpy(log_docfile, log_downname);
	if (strchr(log_docfile, '.')) {
		strcpy(strchr(log_docfile, '.'), ".GGG");
	}
	else {
		if (sprintf(log_downname, "LIB%d_%d.%s",
			atoi(log_libstr), log_downno, setdataexp(exchr)) > 12) {
			sprintf(log_downname, "%d_%d.%s",atoi(log_libstr), log_downno,
				setdataexp(exchr));
			sprintf(log_docfile, "%d_%d.GGG",atoi(log_libstr), log_downno);
		}
		else {
			sprintf(log_docfile, "LIB%d_%d.GGG",atoi(log_libstr), log_downno);
		}

	}
	if (style == 5) { /* for 週間FTOWNS/週間FYUI */
		if ((tmpptr = strstr(log_bufptr, "掲載者−")) != NULL) {
			strncpy(log_resp, tmpptr+8, 8);
			log_resp[8] = NUL;
		}
	}
	fprintf(fpmes, "forum=%s;libno=%s;datano=%d;resp=%s;docfile=%s;datafile=%s;\n"
	,log_fname, log_libstr, log_downno, log_resp, log_docfile, log_downname);

}

void get_forum_overrun(void)
{
	int cline = 0;
	char *pstr;

	for (pstr = sfstr[0]; *pstr != NUL; log_bufptr++) {
		if ((*pstr == *log_bufptr) || (*pstr == '*')) {
			pstr++;
		}
		else {
			pstr = sfstr[0];
		}
		if (*log_bufptr == 0x0a) {
			cline++;
			if (cline > 100) break;
		}
	}
	if (*pstr==NUL) get_forum(1);
}

void ffsutopchk(int style)
{
/*
09/08 [FMR]      INT12HFM.LZH SCAN/CLEANをFMで動かすTSR   SDI00618  (#5-26)
*/
	int lib;

	log_bufptr += 17;
	get_downname();
	strcpy(log_docfile, log_downname);
	if (strchr(log_docfile, '.')) {
		strcpy(strchr(log_docfile, '.'), ".GGG");
	}
	log_bufptr += 41;
	strncpy(log_resp, log_bufptr, 8);
	log_resp[8] = NUL;
	log_bufptr += 9;
	if (*(log_bufptr+1) == '#') {
		sscanf(log_bufptr, "(#%d-%d)", &lib, &log_downno);
	}
	else {
		sscanf(log_bufptr, "(%d-%d)", &lib, &log_downno);
	}

#if 0
	/* 複数フォーラムの可能性がある場合 */
	get_forum_overrun();

	fprintf(fpmes, "forum=%s;libno=%d;datano=%d;resp=%s;docfile=%s;datafile=%s;\n"
		,log_fname, lib, log_downno, log_resp, log_docfile, log_downname);
#else
	fprintf(fpmes, "forum=FFSU;libno=%d;datano=%d;resp=%s;docfile=%s;datafile=%s;\n"
		,lib, log_downno, log_resp, log_docfile, log_downname);
#endif
}

#if 0
void ffmtopchk(int style)
{
/*
12/12[TOWNS] ぼむぼむ                 (DL 5-433)
*/
	int i, lib, len;
	char *tmpptr, target[16], str[4]="(DL";

	tmpptr = str;
	for (i = 0; i < 80; i++) {
		if (*log_bufptr != *tmpptr) {
			tmpptr = str;
			log_bufptr++;
			if (*log_bufptr == 0x0a) {
				break;
			}
		}
		else {
			tmpptr++;
			log_bufptr++;
			if (*tmpptr == NUL) break;
		}
	}
	if (*tmpptr == NUL) {
		sscanf(log_bufptr + 1, "%2d-%4d", &lib, &log_downno);
		len = sprintf(target, "DL%d_%d.lzh", lib, log_downno);
		strcpy(log_downname, (target + (len > 12 ? 2 : 0)));
		strcpy(log_docfile, log_downname);
		strcpy(strchr(log_docfile, '.'), ".GGG");
	}
	else {
		fprintf(fpmes, "Error:Fatal System Error,cannot continue.\n");
		Exit(1);
	}

	get_forum_overrun();

	fprintf(fpmes, "forum=%s;libno=%d;datano=%d;resp=;docfile=%s;datafile=%s;\n"
		,log_fname, lib, log_downno, log_docfile, log_downname);
}
#endif

void fravtopchk()
{
/*
08/31 Tue DL04 #349 ぴー              祝！　ＦＲＡＶ
09/08 DL05 #142 しゅばるつしると  TIFF:『攻撃隊軌道待機』
*/
	int lib, len;
	char target[16];

	log_bufptr += 8;
	sscanf(log_bufptr, "%2d #%4d", &lib, &log_downno);
	len = sprintf(target, "DL%d_%d.lzh", lib, log_downno);
	strcpy(log_downname, (target + (len > 12 ? 2 : 0)));
	strcpy(log_docfile, log_downname);
	strcpy(strchr(log_docfile, '.'), ".GGG");

#if 0
	/* 複数フォーラムの可能性がある場合 */
	get_forum_overrun();

	fprintf(fpmes, "forum=%s;libno=%d;datano=%d;resp=;docfile=%s;datafile=%s;\n"
		,log_fname, lib, log_downno, log_docfile, log_downname);
#else
	fprintf(fpmes, "forum=FRAV;libno=%d;datano=%d;resp=;docfile=%s;datafile=%s;\n"
		,lib, log_downno, log_docfile, log_downname);
#endif

}

