/***************
*
* g:\exe\txf\src\txflog2.c
*/
#include "txf.h"


#define BYTE(x) *((unsigned char *)(x))
#define BIT_CHK(x,f) ((int)(BYTE(x+(f/8)) >> 7-f%8) & 1)

void get_forum(int type)
{
	char *dmyptr;

	log_pname[0] = NUL;
	if (type == 1) {
		dmyptr = log_bufptr - 49;
		while (*dmyptr > ' ') {
			dmyptr--;
		}
	}
	if (type == 2) {
		dmyptr = log_bufptr + 1;
	}
	sscanf(dmyptr, "%16s", log_fname);
}
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;
			tmp += 5;
			if (*(tmp + 3) > '0') {
				r += *(tmp + 3) - '0';
			}
		}
		while (r3 = matchstr("re$%%:", tmp)) {
			end = 1;
			tmp += 6;
			if (*(tmp + 4) > '0') {
				if (*(tmp + 3) > '0') {
					r += (*(tmp + 3) - '0') * 10;
				}
				r += *(tmp + 4) - '0';
			}
		}
	}

	end = 1;
	if ((env = getenv("TXFRES")) != NULL) {
		if (matchstr(env,"re'*'%'d:")) {
			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;
				}
			}
		}
	}
	if (viewmode > 9) {
		fprintf(stderr, "Info:TITLE:%s\n", log_titlestr);
	}

}

void meschk(int style)
{

	if (style == 3) {
		sscanf(log_bufptr, "%[0-9]", log_reply);
		log_bufptr += 10;
		strncpy(log_resp, log_bufptr, 8);
		log_bufptr += 28;
		get_titlestr();
		if ((log_bufptr = strchr(log_bufptr, 0x0a)) == NULL) {
			printf("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);
		log_bufptr += 27;
		get_titlestr();
		if ((log_bufptr = strchr(log_bufptr, 0x0a)) == NULL) {
			printf("Error:find EOF before title+1 line");
			exit(1);
		}
		log_bufptr += 2;
		sscanf(log_bufptr, "%2d", &log_mesno);
	}
	if (log_pname[0] == NULL) {
		printf("forum=%s;mes=%d;reply=%s;resp=%s;title=%s\n"
			, log_fname, log_mesno, log_reply, log_resp, log_titlestr);
	}
	else {
		printf("patio=%s;mes=%d;reply=%s;resp=%s;title=%s\n"
			, log_pname, log_mesno, log_reply, log_resp, 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;
			}
		}
	}

}

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;
	}

	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;
		}
	}
	printf("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();

	printf("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
	printf("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 {
		printf("Error:Fatal System Error,cannot continue.\n");
		exit(1);
	}

	get_forum_overrun();

	printf("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();

	printf("forum=%s;libno=%d;datano=%d;resp=;docfile=%s;datafile=%s;\n"
		,log_fname, lib, log_downno, log_docfile, log_downname);
#else
	printf("forum=FRAV;libno=%d;datano=%d;resp=;docfile=%s;datafile=%s;\n"
		,lib, log_downno, log_docfile, log_downname);
#endif

}

void hpchk()
{
	int i, type = CT_ANK;
	char *handle, *tmp;
	char tmptitle[64];

	if ((handle = getenv("TXFUSER")) != NULL) {
		strncpy(log_handle, handle, 16);
	}
#if 0
	else if ((handle = getenv("USER") != NULL) {
		strncpy(log_handle, handle, 16);
	}
#endif

	/*  option for CATBEE  */
	if (jstrstr(log_handle, "猫飛") != NULL) {
		strcpy(log_handle, "CATBEE");
	}
	/*  End of CATBEE option  */

	log_bufptr += 23;
	strncpy(log_resp, log_bufptr, 8);
	log_bufptr += 14;
	if (log_hpstyle == 1) {
		tmp = log_bufptr;
		if (((tmp = jstrchr(tmp, ':')) != NULL) && (tmp < strchr(log_bufptr, 0x0d))) {
			log_bufptr = tmp + 1;
		}
	}

	get_titlestr();

	if (*log_handle != NUL) {
		if (log_hpstyle == 1) {
			if (sprintf(tmptitle, "%s:%s", log_handle, log_titlestr) > 64) {
				printf("Error:Fatal System Error,cannot continue.\n");
				exit(1);
			}
			strncpy(log_titlestr, tmptitle, 42);
		}
		else {
			if (jstrchr(log_titlestr, '＜') != NULL) {
				*(jstrchr(log_titlestr, '＜')) = NUL;
			}
			if (strlen(log_titlestr) < (38-chkctype(*log_handle, CT_ANK))) {
				strcat(log_titlestr, "＜");
				strncat(log_titlestr, log_handle, 41 - strlen(log_titlestr));
			}
		}
	}

	for (i = 0; i < 41; i++) {
		if (*(log_titlestr + i) == NUL) {
			break;
		}
		type = chkctype(*(log_titlestr + i), type);
	}
	if (i > 40) {
		if (type != CT_KJ2) {
			*(log_titlestr + 40) = NUL;
		}
		else {
			*(log_titlestr + 39) = NUL;
		}
	}
	if (viewmode > 9) {
		fprintf(stderr, "Info:TITLE:%s\n", log_titlestr);
	}

	printf("hp=%s;resp=%s;title=%s\n", log_idstr, log_resp, log_titlestr);

}

void mailchk(int style)
{
/*
 2  山田　弘明　　　  PEE01244  05/17 10:21
 2  山田　弘明　　　  PEE01244          93/05/17 10:21
    題名：FAPX406B.LZH
 6  -              PCV>ZZZ99999 04/01 07:29
    題名：PC-VANからのテストだよ〜ん
*/
	if ((style == 7)||(style == 13)) { /* 標準mail or newmail */
		log_bufptr += 22;
		strncpy(log_idstr, log_bufptr, 8);
		log_idstr[8] = NUL;
	}
	else { /* mail from PC-VAN */
		log_bufptr += 19;
		strncpy(log_idstr, log_bufptr, 12);
		log_idstr[12] = NUL;
	}

	if ((log_bufptr = strchr(log_bufptr, 0x0a)) == NULL) {
		printf("Error:find EOF before title+1 line");
		exit(1);
	}
	log_bufptr += 11;
	get_titlestr();
	printf("mail=%s;title=%s\n", log_idstr, log_titlestr);

}

void widegatechk()
{
/*
-------------------------------------------------------------------------------
 4  WIDE GATE         WID00100  03/22 09:19
    題名：MAIL from WAID for FAPX

Date: Mon, 22 Mar 93 08:56:23 JST
From: nakatani@me.fan.fujitsu.co.jp
To: PEE01244@niftyserve.or.jp
Cc: PDB02377@niftyserve.or.jp
-------------------------------------------------------------------------------
*/

	int i;
	char *tmpptr;
	if ((log_bufptr = strchr(log_bufptr, 0x0a)) == NULL) {
		printf("Error:find EOF before title+1 line");
		exit(1);
	}
	log_bufptr += 11;
	get_titlestr();
	for (i = 2; i < 5; i++) {
		if ((log_bufptr = strchr(log_bufptr+1, 0x0a)) == NULL) {
			printf("Error:find EOF before title+%d line", i);
			exit(1);
		}
	}
	log_bufptr += 7;
	tmpptr = wcfile;
	for (i = 0; ((*(tmpptr++) = *(log_bufptr++)) != '\r') && (i < 80); i++);
	wcfile[i] = NUL;

	printf("wpnc=%s;title=%s\n", wcfile, log_titlestr);
	/* IDが長いので、wildcard用バッファを使用 */
}

