/*****************************************************************************
 *
 * File ..................: bbs/msgutil.c
 * Purpose ...............: Utilities for message handling.
 * Last modification date : 08-Mar-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.
 *****************************************************************************/


#define	DB_MSGAREA
#define	BS_USERS

#include "../include/libs.h"
#include "../include/mbse.h"
#include "../include/structs.h"
#include "../include/records.h"
#include "../common/common.h"
#include "../msgbase/msgtext.h"
#include "../msgbase/msg.h"
#include "oneline.h"
#include "msgutil.h"



int	BaseWrite = FALSE;


/*
 *  Open specified message base for read or write.
 */
int Open_Msgbase(char *Base, int Mode)
{
	BaseWrite = FALSE;

	if (!Msg_Open(Base))
		return FALSE;

	if (Mode != 'w')
		return TRUE;

	if (!Msg_Lock(30L)) {
		Msg_Close();
		return FALSE;
	}

	BaseWrite = TRUE;
	return TRUE;
}



/*
 *  Close current messagebase.
 */
void Close_Msgbase()
{
	if (BaseWrite) {
		Msg_UnLock();
		BaseWrite = FALSE;
	}
	Msg_Close();
}



void Add_Headkludges(faddr *dest, int IsReply)
{
	char		*temp;
	unsigned long	crc = -1;
	time_t		tt;

	temp = calloc(128, sizeof(char));

	switch (msgs.Type) {
		case LOCALMAIL:	Msg.Localmail = TRUE;
				break;

		case NETMAIL:	Msg.Netmail = TRUE;
				sprintf(Msg.FromAddress, "%s", aka2str(msgs.Aka));
				sprintf(Msg.ToAddress, "%s", ascfnode(dest, 0x1f));

				if (msgs.Aka.point) {
					sprintf(temp, "\001FMPT %d", msgs.Aka.point);
					MsgText_Add2(temp);
				}

				if (dest->point) {
					sprintf(temp, "\001TOPT %d", dest->point);
					MsgText_Add2(temp);
				}

				sprintf(temp, "\001INTL: %d:%d/%d %d:%d/%d", dest->zone, dest->net, dest->node, msgs.Aka.zone, msgs.Aka.net, msgs.Aka.node);
				MsgText_Add2(temp);

				break;

		case ECHOMAIL:	Msg.Echomail = TRUE;
				sprintf(Msg.FromAddress, "%s", aka2str(msgs.Aka));
				break;

		case NEWS:	Msg.News = TRUE;
				break;

		case EMAIL:	Msg.Email = TRUE;
				break;
	}

	sprintf(temp, "\001MSGID: %s %08lx", aka2str(msgs.Aka), sequencer());
	MsgText_Add2(temp);
	Msg.MsgIdCRC = upd_crc32(temp, crc, strlen(temp));

	if (IsReply) {
		sprintf(temp, "\001REPLY: %s", Msg.Replyid);
		MsgText_Add2(temp);
		crc = -1;
		Msg.ReplyCRC = upd_crc32(temp, crc, strlen(temp));
	}

	sprintf(temp, "\001PID: MBSE BBS %s", PIDVersion);
	MsgText_Add2(temp);
	(void)time(&tt);
	sprintf(temp, "\001TZUTC: %s", gmtoffset(tt));
	MsgText_Add2(temp);

	free(temp);
}



/*
 *  Add bottom message kludges. The flag Quote is false if this is called
 *  from Offline Reader, the user then may or may have not added a quote.
 */
void Add_Footkludges(int Quote)
{
	char	*temp;
	char	*aka;

	temp = calloc(128, sizeof(char));
	aka  = calloc(32,  sizeof(char));

	if (msgs.Quotes && Quote) {
		Syslog('m', "  Add quote");
		MsgText_Add2((char *)"");
		sprintf(temp, "... %s", Oneliner_Get());
		MsgText_Add2(temp);
		MsgText_Add2((char *)"");
	}

	sprintf(temp, "--- MBSE BBS v%s (Linux)", MBSEVersion);
	MsgText_Add2(temp);

	if (msgs.Type == ECHOMAIL) {
		/* RANDOM ORIGIN LINES IMPLEMENTEREN */
		if (msgs.Aka.point)
			sprintf(aka, "(%d:%d/%d.%d)", msgs.Aka.zone, msgs.Aka.net, msgs.Aka.node, msgs.Aka.point);
		else
			sprintf(aka, "(%d:%d/%d)", msgs.Aka.zone, msgs.Aka.net, msgs.Aka.node);

		if (strlen(msgs.Origin))
			sprintf(temp, " * Origin: %s %s", msgs.Origin, aka);
		else
			sprintf(temp, " * Origin: %s %s", CFG.origin, aka);
		MsgText_Add2(temp);
	}

	free(aka);
	free(temp);
}



