/*****************************************************************************
 *
 * File ..................: mbcico/ftnmsg.c
 * Purpose ...............: Fidonet mailer
 * Last modification date : 14-Aug-1999
 *
 *****************************************************************************
 * Copyright (C) 1997-1999
 *   
 * Michiel Broek		FIDO:		2:2801/16
 * Beekmansbos 10		Internet:	mbroek@ux123.pttnwb.nl
 * 1971 BV IJmuiden
 * the Netherlands
 *
 * This file is part of MBSE BBS.
 *
 * This BBS is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * MBSE BBS is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MBSE BBS; see the file COPYING.  If not, write to the Free
 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *****************************************************************************/

#include "../include/libs.h"
#include "../include/structs.h"
#include "../common/common.h"
#include "ftnmsg.h"
#include "packet.h"



static char *months[] = {
(char *)"Jan",(char *)"Feb",(char *)"Mar",	
(char *)"Apr",(char *)"May",(char *)"Jun",
(char *)"Jul",(char *)"Aug",(char *)"Sep",
(char *)"Oct",(char *)"Nov",(char *)"Dec"
};


char *ftndate(time_t);
char *ftndate(time_t t)
{
	static char	buf[32];
	struct tm	*ptm;

	ptm = localtime(&t);
	if (ptm->tm_sec > 59)
		ptm->tm_sec = 59;

	sprintf(buf,"%02d %s %02d  %02d:%02d:%02d",ptm->tm_mday,
		months[ptm->tm_mon], ptm->tm_year%100,
		ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
	return buf;
}



FILE *ftnmsghdr(ftnmsg *fmsg, FILE *pkt, faddr *route, char flavor)
{
	unsigned char	buffer[0x0e];
	time_t		Now;
	faddr		*from;

	if ((strlen(fmsg->to->name) > 36) || 
	    (strlen(fmsg->from->name) > 36) || 
	    (strlen(fmsg->subj) > 72))
		return NULL;

	if (route == NULL) 
		route = fmsg->to;
	pkt = openpkt(pkt, route, flavor);
	if (pkt == NULL) 
		return NULL;

	if (fmsg->area)
		from = bestaka_s(fmsg->to);
	else
		from = fmsg->from;

	memset(&buffer, 0, sizeof(buffer));
	buffer[0x00] = 2;
	buffer[0x02] = (from->node & 0x00ff);
	buffer[0x03] = (from->node & 0xff00) >> 8;
	buffer[0x04] = (fmsg->to->node & 0x00ff);
	buffer[0x05] = (fmsg->to->node & 0xff00) >> 8;
	buffer[0x06] = (from->net & 0x00ff);
	buffer[0x07] = (from->net & 0xff00) >> 8;
	buffer[0x08] = (fmsg->to->net & 0x00ff);
	buffer[0x09] = (fmsg->to->net & 0xff00) >> 8;
	buffer[0x0a] = (fmsg->flags & 0x00ff);
	buffer[0x0b] = (fmsg->flags & 0xff00) >> 8;
	fwrite(buffer, 1, sizeof(buffer), pkt);

	fprintf(pkt, "%s%c", ftndate(fmsg->date), '\0');
	fprintf(pkt, "%s%c", fmsg->to->name?fmsg->to->name:(char *)"Sysop", '\0');
	fprintf(pkt, "%s%c", fmsg->from->name?fmsg->from->name:(char *)"Sysop", '\0');
	fprintf(pkt, "%s%c", fmsg->subj?fmsg->subj:(char *)"<none>", '\0');

	if (fmsg->area) {
		fprintf(pkt, "AREA:%s\r", fmsg->area);
	} else {
		if (fmsg->to->point)
			fprintf(pkt, "\1TOPT %u\r", fmsg->to->point);
		if (fmsg->from->point)
			fprintf(pkt, "\1FMPT %u\r", fmsg->from->point);
		fprintf(pkt, "\1INTL %u:%u/%u %u:%u/%u\r",
			fmsg->to->zone,
			fmsg->to->net,
			fmsg->to->node,
			fmsg->from->zone,
			fmsg->from->net,
			fmsg->from->node
			);
	}

	if (fmsg->msgid_s) 
		fprintf(pkt, "\1MSGID: %s\r", fmsg->msgid_s);
	else if (fmsg->msgid_a) 
		fprintf(pkt, "\1MSGID: %s %08lx\r",
			fmsg->msgid_a,
			fmsg->msgid_n);

	if (fmsg->reply_s) 
		fprintf(pkt, "\1REPLY: %s\r", fmsg->reply_s);
	else if (fmsg->reply_a)
		fprintf(pkt, "\1REPLY: %s %08lx\r",
			fmsg->reply_a,
			fmsg->reply_n);

	Now = time(NULL) - (gmt_offset((time_t)0) * 60);
	fprintf(pkt, "\001PID: MBSE BBS %s\r", PIDVersion);
	fprintf(pkt, "\001TZUTC: %s\r", gmtoffset(Now));

	if (ferror(pkt)) 
		return NULL;
	else 
		return pkt;
}


