/***************
*
* g:\exe\txl\src\txllog2.c
*/
#include "txl.h"

/* 0x8146 == '：' */
#define W_COLON		(0x8146)
/* 0x8183 == '＜' / LTS = less than sign */
#define W_LTS		(0x8183)

int get_hpstyle(char *id, int org)
{
	int style = org;
	FILE *fp;
	char *token;
	char *buf = logstr_mlist;	/* 80バイト---使い回し(^^;) */
	unsigned char atmp[2] = {0,0};
	int ret;

	if ((fp = fopen(txfsetfile, "rt")) == NULL) {
		return (style);	/* myforum.logは存在しない */
	}
	while (!feof(fp)) {
		fgets(buf, 80, fp);
		if ((token = strchr(buf, '\n')) != NULL) {
			*token = NUL;
		}
		if (*buf == '%') {
			if (strncmp(id, buf + 1, 8) == 0) {
				token = strchr(buf+9, '=');
				if (token != NULL) {
					ret = sscanf(token+1, "%d,%2c", &style, atmp);
					if (ret >= 2) {
						if (atmp[1]) {
							log_hpchr = atmp[0] << 8 | atmp[1];
						}
						else {
							log_hpchr = atmp[0];
						}
					}
					style &= 1;		/* 第0ビット以外無視 */
				}
				break;
			}
		}
	}
	return (style);
}

void log_sethptitle()
{
	int i, type = CT_ANK;
	unsigned char sepchr[3] = {0, 0, 0};
	char *tmp;

	if ((log_hpchr & 0x0ff00) == 0) {
		sepchr[0] = log_hpchr & 0x0ff;
	}
	else {
		sepchr[0] = (log_hpchr >> 8) & 0x0ff;
		sepchr[1] = log_hpchr & 0x0ff;
	}

	tmp = strchr(log_bufptr, 0x0d);
	if (tmp != NULL) {
		*tmp = NUL;			/* ログファイルに傷を付ける(行末をNULに) */
	}

	if ((tmp = jstrchr(log_bufptr, log_hpchr)) != NULL) {
		*tmp = NUL;		/* ログファイルに傷を付ける(境界をNULに) */
		tmp++;
		if (log_hpchr & 0x0ff00) {
			tmp++;
		}
		if (log_hpstyle == 1) {
			strcpy(log_rhandle, log_bufptr);	/* 前半からﾊﾝﾄﾞﾙ取得 */
			log_bufptr = tmp;
		}
		else {
			strcpy(log_rhandle, tmp);			/* 後半からﾊﾝﾄﾞﾙ取得 */
		}
	}
	get_titlestr();
	if (log_hpstyle == 1) {
		sprintf(line1, "%s%s%s", log_handle, sepchr, log_titlestr);
	}
	else {
		sprintf(line1, "%s%s%s", log_titlestr, sepchr, log_handle);
	}

	for (i = 0; i < 41; i++) {
		if (*(line1 + i) == NUL) {
			break;
		}
		type = chkctype(*(line1 + i), type);
	}
	if (i > 40) {
		if (type != CT_KJ2) {
			*(line1 + 40) = NUL;
		}
		else {
			*(line1 + 39) = NUL;
		}
	}
	strcpy(log_titlestr, line1);
}

void hpchk()
{
	char *handle;

	if ((handle = getenv("TXFUSER")) != NULL) {
		strncpy(log_handle, handle, 16);
	}

	/*  option for CATBEE  */
	if (jstrstr(log_handle, "猫飛") != NULL) {
		strcpy(log_handle, "CATBEE");
	}
	/*  End of CATBEE option  */

	strncpy(log_reply, log_bufptr, 3);
	log_reply[3] = 0;
	log_bufptr += 23;
	strncpy(log_resp, log_bufptr, 8);
	log_resp[8] = 0;
	log_bufptr += 14;
	log_hpstyle = get_hpstyle(log_idstr, log_hpstyle);

	if (log_hpchr == 0) {
		if (log_hpstyle == 1) {
			log_hpchr = W_COLON;
		}
		else {
			log_hpchr = W_LTS;
		}
	}

	if (log_hpstyle >= 0) {
		log_sethptitle();
	}
	else {
		get_titlestr();
	}

	fprintf(fpmes, "hp=%s;reply=%s;resp=%s;handle=%s;title=%s\n",
			log_idstr, log_reply, log_resp, log_rhandle, 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 */
		strncpy(log_rhandle, log_bufptr + 4, 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 += 22;
		strncpy(log_idstr, log_bufptr, 8);
		log_idstr[8] = NUL;
	}
	else { /* mail from PC-VAN */
/*
10  -              PCV>***%%%%% %%/%% %%:%%
15  -                 PCV>***%%%%%      %%/%%/%% %%:%%
↑ｽﾀｲﾙ             ~~~
*/
		log_bufptr += 19;
		if (style == 15) {
			log_bufptr += 3;
		}
		strncpy(log_idstr, log_bufptr, 12);
		log_idstr[12] = NUL;
	}

	if ((log_bufptr = strchr(log_bufptr, 0x0a)) == NULL) {
		fprintf(fpmes, "Error:find EOF before title+1 line");
		Exit(1);
	}
	log_bufptr += 11;
	get_titlestr();
	fprintf(fpmes, "mail=%s;handle=%s;title=%s\n", log_idstr, log_rhandle, log_titlestr);

}

void widegatechk()
{
/*
-------------------------------------------------------------------------------
 4  WIDE GATE         WID00100  03/22 09:19
    題名：MAIL from WIDE for FAPX

Date: Mon, 22 Mar 93 08:56:23 JST
From: PFG02442@niftyserve.or.jp
From: Kouichi Shiono <shiono@cms20x.niftyserve.or.jp>
From: oza@cmlab.densi.kansai-u.ac.jp (Kouji Ozaki)
From: goichi <goichi@joho.densi.kansai-u.ac.jp>
From: morikawa@sunkist.tis.co.jp (Masahiko Morikawa )
From: Kazutaka Suzuki <suzuki@iecs.kansai-u.ac.jp>
From: takada@enzan.ee.kansai-u.ac.jp (Hideo Takada)
From: joo@cia.ncl.omron.co.jp (Naoki Joo)
From: tatsu@nxenzan.ee.kansai-u.ac.jp (Tatsuaki Yunoki)
From: naka@cms101.niftyserve.or.jp (Tomohiko Nakatani)
From: 荒木　聖 <HAD01045@niftyserve.or.jp>
From: Kazutaka Suzuki (鈴木  和貴) <suzuki@iecs.kansai-u.ac.jp>
From: "maruu(Ushioda)" <PEA03643@niftyserve.or.jp>
To: PEE01244@niftyserve.or.jp
Cc: PDB02377@niftyserve.or.jp
-------------------------------------------------------------------------------
*/

	int i;
	char *tmpptr, *tmpptr2, *inetid;
	if ((log_bufptr = strchr(log_bufptr, 0x0a)) == NULL) {
		fprintf(fpmes, "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) {
			fprintf(fpmes, "Error:find EOF before title+%d line", i);
			Exit(1);
		}
	}
	log_bufptr += 7;
	inetid = malloc(80);
	if (inetid == NULL) {
		fprintf(stderr, "TXL: out of memory(widegatechk)\n");
		Exit(1);
	}
	tmpptr = inetid;

	for (i = 0; ((*(tmpptr++) = *(log_bufptr++)) != '\r') && (i < 80); i++);
	inetid[i] = NUL;
	while (strchr(inetid, ' ') != NULL) {
		if ((tmpptr = jstrchr(inetid, '@')) == NULL) {
			break;		/* anything illegal!! */
		}
		if ((tmpptr2 = strchr(tmpptr, ' ')) != NULL) {
			*tmpptr2 = NUL;
		}
		else {
			while ((*tmpptr != ' ') && (*tmpptr != '<')) tmpptr--;
			tmpptr2 = inetid;
			do {
				*(tmpptr2++) = *(++tmpptr);
			} while ((*tmpptr != NUL) && (*tmpptr != '>'));
			if (*(--tmpptr2) == '>') {
				*tmpptr2 = NUL;
			}
		}
	}

	fprintf(fpmes, "wpnc=%s;title=%s\n", inetid, log_titlestr);
	free(inetid);
}

void maillistchk()
{
/*
  1   **********　　　04/08  B NEWSARA.LZH 未だできんんんんんんん      ILDFTINC
*/
	strncpy(log_rhandle, log_bufptr + 6, 16);
	del_tailspace(log_rhandle, 15);
	log_bufptr += 31;
	get_titlestr();
	log_titlestr[40] = NUL;
	del_tailspace(log_titlestr, 39);
	log_bufptr += 40;
	strncpy(log_idstr, log_bufptr, 8);
	log_idstr[8] = NUL;
	fprintf(fpmes, "mailid=%s;handle=%s;title=%s\n", log_idstr, log_rhandle, log_titlestr);
}

void fgalchk()
{
/* 特殊処理が必要なフォーラム
  LM FGALM    AN FGALIAN
  W1 FGALSW1  W2 FGALSW2
  AY FGALRAY
AG  3  126 HCD00632 94/02/28   27059 B IZUMI012.LZH MAG:リレハンメルの女の子
*/
	int lib;
	char tail[4];

	strncpy(tail, log_bufptr, 2);
	tail[2] = NUL;
	switch (*(unsigned int *)tail) {
#ifdef LSI_C
	case 'ML':
#else
	case 0x4d4c:
#endif
		strcpy(tail, "M");
		break;
#ifdef LSI_C
	case 'NA':
#else
	case 0x4e41:
#endif
		strcpy(tail, "IAN");
		break;
#ifdef LSI_C
	case '1W':
#else
	case 0x3157:
#endif
		strcpy(tail, "SW1");
		break;
#ifdef LSI_C
	case '2W':
#else
	case 0x3257:
#endif
		strcpy(tail, "SW2");
		break;
#ifdef LSI_C
	case 'YA':
#else
	case 0x5941:
#endif
		strcpy(tail, "RAY");
		break;
	}
	sscanf(log_bufptr+2, "%d %d", &lib, &log_downno);
	strncpy(log_resp, log_bufptr + 11, 8);
	log_resp[8] = NUL;

	log_bufptr += 39;
	get_downname();
	strcpy(log_docfile, log_downname);
	if (strchr(log_docfile, '.')) {
		strcpy(strchr(log_docfile, '.'), ".GGG");
	}
	fprintf(fpmes, "forum=FGAL%s;libno=%d;datano=%d;resp=%s;docfile=%s;datafile=%s;\n"
	,tail, lib, log_downno, log_resp, log_docfile, log_downname);
}

