/* PR-QWK - QWK Importer for UMS
 * Copyright (C) 1998 J.Ross Nicoll
 *
 * This program 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
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <exec/types.h>
#include <utility/tagitem.h>

#include <proto/dos.h>
#include <proto/ums.h>

#define CATCOMP_NUMBERS

#include "locale.h"
#include "pr-qwk.h"

static struct TagItem MailTags[] = {
	UMSTAG_WFromName, 0,
	UMSTAG_WToName, 0,
	UMSTAG_WSubject, 0,
	UMSTAG_WGroup, 0,
	UMSTAG_WFolder, 0,
	UMSTAG_WMsgText, 0,
	TAG_DONE, 0
};

#define Prototype extern
#include "PR-QWK-protos.h"
Prototype BOOL doMailBulletins(VOID);
Prototype BOOL doShowBulletins(VOID);
Prototype BOOL MailFile(STRPTR);
Prototype BOOL ShowFile(STRPTR);
Prototype VOID OutputFiles(VOID);

BOOL doMailBulletins(VOID)
{
	BOOL Success;
	TEXT parsedPattern[36];

	if(ParsePatternNoCase("BLT-#[0-9].#[0-9]", parsedPattern, 36) == -1) return(FALSE);

	Success = Examine(TempDirLock, FileInfoBlock);
	if(Success != FALSE)
	{
		Success = ExNext(TempDirLock, FileInfoBlock);
		while(Success == DOSTRUE)
		{
			if(MatchPatternNoCase(parsedPattern, FileInfoBlock -> fib_FileName) == TRUE)
			{
				MailFile(FileInfoBlock -> fib_FileName);
			}

			Success = ExNext(TempDirLock, FileInfoBlock);
		}

		return(TRUE);
	}
	else
	{
		HandleIOError(IOMSG_EXAMINE);
	}						

	return(FALSE);
}

/* ---------------------------------------------------------------------- */

BOOL doShowBulletins(VOID)
{
	BOOL Success;
	TEXT parsedPattern[36];

	if(ParsePatternNoCase("BLT-#[0-9].#[0-9]", parsedPattern, 36) == -1) return(FALSE);

	Success = Examine(TempDirLock, FileInfoBlock);
	if(Success != FALSE)
	{
		Success = ExNext(TempDirLock, FileInfoBlock);
		while(Success == DOSTRUE)
		{
			if(MatchPatternNoCase(parsedPattern, FileInfoBlock -> fib_FileName) == TRUE)
			{
				ShowFile(FileInfoBlock -> fib_FileName);
			}

			Success = ExNext(TempDirLock, FileInfoBlock);
		}

		return(TRUE);
	}
	else
	{
		HandleIOError(IOMSG_EXAMINE);
	}

	return(FALSE);
}

/* ---------------------------------------------------------------------- */

BOOL MailFile(STRPTR Filename)
{
	BPTR file;
	STRPTR buffer;
	ULONG allocation;
	ULONG errorChk;

	file = Open(Filename, MODE_OLDFILE);
	if(file != NULL)
	{
		ExamineFH(file, FileInfoBlock);
		allocation = (FileInfoBlock -> fib_Size + 1);
		buffer = AllocPoolVec(allocation);
		if(buffer != NULL)
		{
			*(buffer + FileInfoBlock -> fib_Size) = 0;
			errorChk = Read(file, buffer, FileInfoBlock -> fib_Size);
			if(errorChk == FileInfoBlock -> fib_Size)
			{
				Close(file);

				MailTags[2].ti_Data = (ULONG)Filename;
				MailTags[5].ti_Data = (ULONG)buffer;
				UMSWriteMsg(Account, MailTags);
				FreePoolVec(buffer);

				return(TRUE);
			}
			else
			{
				HandleIOError(IOMSG_READ);
			}
		}
		else
		{
			HandleMemoryError(allocation, MISCMSG_FILE_BUFFER);
		}
		Close(file);
	}
	else
	{
		if(ShowDebug != 0) HandleIOError(IOMSG_OPEN);
	}


	return(FALSE);
}

/* ---------------------------------------------------------------------- */

BOOL ShowFile(STRPTR Filename)
{
	BPTR file;
	TEXT buffer[1025];
	UWORD readIn;

	file = Open(Filename, MODE_OLDFILE);
	if(file != NULL)
	{
		SPrintF(FormatBuffer, GetString(INFOMSG_START_FILE), Filename);
		PutStr(FormatBuffer);

		do
		{
			readIn = Read(file, buffer, 1024);
			buffer[readIn] = 0;
			PutStr(buffer);
		}while(readIn == 1024);

		PutC('\n');
		Flush(OutputHandle);

		Close(file);

		return(TRUE);
	}
	else
	{
		if(ShowDebug != 0)
		{
			HandleIOError(IOMSG_OPEN);
		}
	}

	return(FALSE);
}

/* ---------------------------------------------------------------------- */

VOID OutputFiles(VOID)
{
	MailTags[0].ti_Data = (ULONG)User;
	if(MailTo == NULL)
	{
		MailTags[1].ti_Data = (ULONG)ControlData.Username;
	}
	else
	{
		MailTags[1].ti_Data = (ULONG)MailTo;
	}
	MailTags[4].ti_Data = (ULONG)MailFolder;

	PutStr(GetString(INFOMSG_MAILING_FILES));
	if(MailWelcome == TRUE) MailFile(ControlData.WelcomeFile);
	if(MailNews == TRUE) MailFile(ControlData.NewsFile);
	if(MailBulletins == TRUE) doMailBulletins();
	if(MailNewFiles == TRUE) MailFile("NEWFILES.DAT");
	if(MailGoodbye == TRUE) MailFile(ControlData.GoodbyeFile);

	PutStr(GetString(INFOMSG_SHOWING_FILES));
	if(ShowWelcome == TRUE) ShowFile(ControlData.WelcomeFile);
	if(ShowNews == TRUE) ShowFile(ControlData.NewsFile);
	if(ShowBulletins == TRUE) doShowBulletins();
	if(ShowNewFiles == TRUE) ShowFile("NEWFILES.DAT");
	if(ShowGoodbye == TRUE) ShowFile(ControlData.GoodbyeFile);

	return();
}
