/*****************************************************************************
 *
 * File ..................: tosser/pack.c
 * Purpose ...............: Pack mail
 * Last modification date : 13-Nov-1999
 * Memory leaks check ....: 20-Feb-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_ARCHIVE

#include "../include/libs.h"
#include "../include/structs.h"
#include "../include/records.h"
#include "../common/common.h"
#include "../dbase/dbftn.h"
#include "../dbase/dbnode.h"
#include "putil.h"
#include "pack.h"


extern	int do_quiet;			/* Quiet flag		     */


/*
 *  Pack queued arcmail mail for a node. If the node is locked, the mail won't
 *  be packed, and the queue stays as it is. The mail will then be packed
 *  on a next run.
 */
int pack_queue(char *name)
{
	FILE		*fp;
	faddr		noden;
	fidoaddr	nodenr;
	char		flavor, nr, oldnr, maxnr;
	char		srcfile[128], *arcfile, *pktfile;
	int		Attach, fage;
	long		fsize;
	time_t		Now;

	sprintf(srcfile, "%s", name);

	/*
	 * Get the nodenumber from the filename
	 */
	noden.domain = NULL;
	noden.name   = NULL;
	noden.zone   = atoi(strtok(name, "."));
	noden.net    = atoi(strtok(NULL, "."));
	noden.node   = atoi(strtok(NULL, "."));
	noden.point  = atoi(strtok(NULL, "."));
	if (SearchFidonet(noden.zone))
		noden.domain = xstrcpy(fidonet.domain);

	memset(&nodenr, 0, sizeof(nodenr));
	nodenr.zone  = noden.zone;
	nodenr.net   = noden.net;
	nodenr.node  = noden.node;
	nodenr.point = noden.point;
	sprintf(nodenr.domain, "%s", noden.domain);

	if (!SearchNode(nodenr, nodenr.point)) {
		WriteError("Downlink %s not found", aka2str(nodenr));
		return FALSE;
	}

	/*
	 * If we route via another aka, change everything.
	 */
	if (nodes.RouteVia.zone) {
		Syslog('p', "Route Via %s", aka2str(nodes.RouteVia));
		noden.zone   = nodes.RouteVia.zone;
		noden.net    = nodes.RouteVia.net;
		noden.node   = nodes.RouteVia.node;
		noden.point  = nodes.RouteVia.point;
		sprintf(noden.domain, "%s", nodes.RouteVia.domain);
	}

	Syslog('+', "Pack ARCmail for %s, via %s", aka2str(nodenr), ascfnode(&noden, 0x1f));

	if (!do_quiet) {
		printf("\rAdding ARCmail for %s                      ", ascfnode(&noden, 0x1f));
		fflush(stdout);
	}

	if (getarchiver((char *)"ZIP")) {
		flavor = 'f';
		if (nodes.Crash)
			flavor = 'c';
		if (nodes.Hold)
			flavor = 'h';
	} else {
		WriteError("Archiver ZIP not found");
		return FALSE;
	}

	/*
	 * Generate ARCmail filename and .PKT filename,
	 */
	arcfile = calloc(128, sizeof(char));
	sprintf(arcfile, "%s", arcname(&noden, nodes.Aka[0].zone, nodes.ARCmailCompat));
	pktfile = calloc(40, sizeof(char));
	sprintf(pktfile, "%08lx.pkt", sequencer());

	if (nodelock(&noden)) {
		WriteError("Node %s lock error", ascfnode(&noden, 0x1f));
		free(arcfile);
		free(pktfile);
		return FALSE;
	}

	if (rename(srcfile, pktfile)) {
		WriteError("$Can't rename %s to %s", srcfile, pktfile);
		free(arcfile);
		free(pktfile);
		nodeulock(&noden);
		return FALSE;
	}

	/*
	 * Add zero word at the end of the .pkt file
	 */
	if ((fp = fopen(pktfile, "a+")) == NULL) {
		WriteError("$Can't open %s", pktfile);
		free(arcfile);
		free(pktfile);
		nodeulock(&noden);
		return FALSE;
	}
	putc('\0', fp);
	putc('\0', fp);
	fclose(fp);

	/*
	 *  Check the size of the existing archive if there is a size limit.
	 *  Change to new archive names if the existing is too large.
	 *  If the archive size is zero, it's an already sent archive, the
	 *  number will be bumped also.
	 *  If the archive is older then 6 days, the name is also bumped.
	 *  Do this until we find a new name or if the last digit is a '9' or 'z'.
	 *  Purge archives older then toss_days.
	 */
	nr = oldnr = '0';
	Now = time(NULL);
	if (nodes.ARCmailAlpha)
		maxnr = 'z';
	else
		maxnr = '9';
	Attach = FALSE;

	for (;;) {
		fsize = file_size(arcfile);
		fage  = (int)((Now - file_time(arcfile)) / 86400);
//		Syslog('p', "Check.1 %s len=%d age=%d", arcfile, fsize, fage);

		if (fsize == -1L) {
//			Syslog('p', "File doesn't exist, using this one");
			Attach = TRUE;
			break;
		}

		if (fsize == 0L) {
			if ((fage > 6) && (nr < maxnr)) {
				Syslog('p', "Truncated ARCmail %s is older then 6 days, removing", arcfile);
				unlink(arcfile);
				fsize = -1L;
				Attach = TRUE;
				break;
			}
			Syslog('p', "ARCmail %s len=0, bumping name", arcfile);
			nr++;
			if (nr == ('9' +1))
				nr = 'a';
			arcfile[strlen(arcfile) -1] = nr;
		} else if (CFG.maxarcsize && (fsize > (CFG.maxarcsize * 1024)) && (nr < maxnr)) {
			Syslog('p', "ARCmail %s too big, bumping name", arcfile);
			nr++;
			if (nr == ('9' +1))
				nr = 'a';
			arcfile[strlen(arcfile) -1] = nr;
		}

		fsize = file_size(arcfile);
		fage  = (int)((Now - file_time(arcfile)) / 86400);
//		Syslog('p', "Check.2 %s len=%d age=%d", arcfile, fsize, fage);

        	if ((fsize > 0L) && (fage > 6) && (nr < maxnr)) {
			Syslog('p', "ARCmail %s too old, bumping name", arcfile);
			nr++;
			if (nr == ('9' +1))
				nr = 'a';
			arcfile[strlen(arcfile) -1] = nr;
		}

		if (oldnr == nr)
			break;
		else
			oldnr = nr;
	}

	fsize = file_size(arcfile);

	/*
	 * Most archivers don't like adding to a truncated file, so we
	 * delete the truncated file before archiving.
	 */
	if (fsize == 0) {
		// Should not come here anymore.
		WriteError("IS THIS REAL? If so blame Michiel.");
		unlink(arcfile);
	}

	if (execute(archiver.marc, arcfile, pktfile, (char *)"/dev/null", (char *)"/dev/null", (char *)"/dev/null") == 0)
		unlink(pktfile);

	/*
	 * Attach file to .flo
	 */
	if (Attach)
		attach(noden, arcfile, TFS, flavor);

	nodeulock(&noden);
	free(arcfile);
	free(pktfile);
	return TRUE;
}



/*
 *  Add queued unpacked mail for a node. If the node is locked, the mail
 *  stays in the queue.
 */
int add_queue(char *name)
{
	faddr		noden;
	char		flavor;
	char		srcfile[128], *outfile;
	char		*buf;
	FILE		*inf, *ouf;
	int		bread;

	sprintf(srcfile, "%s", name);

	/*
	 * Get the nodenumber from the filename
	 */
	noden.domain = NULL;
	noden.name   = NULL;
	noden.zone   = atoi(strtok(name, "."));
	noden.net    = atoi(strtok(NULL, "."));
	noden.node   = atoi(strtok(NULL, "."));
	noden.point  = atoi(strtok(NULL, "."));
	if (SearchFidonet(noden.zone))
		noden.domain = xstrcpy(fidonet.domain);

	Syslog('+', "Add Netmail for %s", ascfnode(&noden, 0x1f));
	if (!do_quiet) {
		printf("\rAdding Netmail for %s                      ", ascfnode(&noden, 0x1f));
		fflush(stdout);
	}

	outfile = calloc(128, sizeof(char));
	if (strstr(srcfile, ".ccc"))
		flavor = 'c';
	else if (strstr(srcfile, "hhh"))
		flavor = 'h';
	else
		flavor = 'f';
	sprintf(outfile, "%s", pktname(&noden, flavor));
	Syslog('p', "Outfile: %s", outfile);

	if (nodelock(&noden)) {
		WriteError("Node %s lock error", ascfnode(&noden, 0x1f));
		free(outfile);
		return FALSE;
	}

	/*
	 *  Now we must see if there is already mail in the outbound.
	 *  If that's the case, we must skip the .pkt header from the queue
	 *  because there is already a .pkt header also, append to the
	 *  outbound 2 bytes before the end of file, this is the zero word.
	 */
	if ((inf = fopen(srcfile, "r")) != NULL) {
		if (access(outfile, R_OK) == -1) {
			ouf = fopen(outfile, "w");  /* create new  */
			Syslog('p', "Create new %s", outfile);
		} else {
			ouf = fopen(outfile, "r+"); /* open R/W    */
			fseek(ouf, -2, SEEK_END);   /* b4 0 word   */
			fseek(inf, 58, SEEK_SET);   /* skip header */
			Syslog('p', "Append to %s", outfile);
		}
		if (ouf != NULL) {
			buf = malloc(16384);

			do {
				bread = fread(buf, 1, 16384, inf);
				fwrite(buf, 1, bread, ouf);
			} while (bread);

			free(buf);
			putc('\0', ouf);
			putc('\0', ouf);
			fclose(ouf);
			fclose(inf);
			unlink(srcfile);
		} else {
			WriteError("$Can't open %s", outfile);
			fclose(inf);
		}
	}
	
	nodeulock(&noden);
	free(outfile);
	return TRUE;
}



/*
 *  Pack mailqueue file(s) in the $MBSE_ROOT/tmp directory.
 */
void packmail()
{
	char		*temp;
	struct	dirent	*de;
	DIR		*dp;

	IsDoing("Packing mail");
	if (!do_quiet) {
		colour(9, 0);
		printf("Packing mail\n");
		colour(3, 0);
	}

	temp = calloc(129, sizeof(char));
	sprintf(temp, "%s/tmp", getenv("MBSE_ROOT"));

	if (chdir(temp) == -1) {
		WriteError("$Error chdir to %s", temp);
		free(temp);
		return;
	}

	if ((dp = opendir(temp)) == NULL) {
		WriteError("$Error opendir %s", temp);
		free(temp);
		return;
	}

	/*
	 * Scan the $MBSE_ROOT/tmp directory for .qqq or .nnn files
	 */
	while ((de = readdir(dp))) {
		if (strstr(de->d_name, ".qqq"))
			pack_queue(de->d_name);
		if (strstr(de->d_name, ".nnn") || 
		    strstr(de->d_name, ".ccc") || 
		    strstr(de->d_name, ".hhh"))
			add_queue(de->d_name);
	}

	closedir(dp);
	free(temp);

	if (!do_quiet) {
		printf("\r                                              \r");
		fflush(stdout);
	}
}



