/*
  
Title:	MsgEd
  
File:	writmail.c
  
Author:	Jim Nutt
	
Copr:	1988 by Jim Nutt
  
Description:
  
writes messages back to disk.
	
Revision History:
  
	0.00	28 jan 1988	first draft
	
Support Files:
  
	msged.h
  
*/

#define TEXTLEN 128

#include "msged.h"

int     writemsg(m, n)
	MSG     m;
	int     n;

{
	char    path[PATHLEN];
	LINE   *l;
	FILE   *fp;
        char    corigin[TEXTLEN];
	int 	i;
	int	newtear = 1;

	if (!messages[n])
                return (FALSE);

        if (!override) {
                memset(corigin,0,sizeof corigin);
                sprintf(path,"%s\\origin",arealist[area].path);

                if ((fp = fopen(path,"rt")) != NULL) {
                        fgets(corigin,sizeof(corigin),fp);
                        fclose(fp);
                }
                else if (origin != NULL)
                        strcpy(corigin,origin);
                else
                        strcpy(corigin,username);
        }
        else
                strcpy(corigin,origin);

	sprintf(path, "%s\\%d.MSG", arealist[area].path, n);

	if ((fp = fopen(path, "wb")) == NULL)
		return (FALSE);

	m.header.dest_net = m.to.net;
	m.header.dest = m.to.node;

	/*
	 * we really only want to remap crash mail if a point...  the boss
	 * node can (and probably will) handle remapping the rest 
	 */

        if ((!m.header.crash) && (m.from.point) && (pointnet != 0)) {
		m.from.net = pointnet;
		m.from.node = m.from.point;
		m.from.point = 0;
	}

	m.header.orig_net = m.from.net;
	m.header.orig = m.from.node;

	m.header.local = 1;

	/*
	 * don't zone gate a message if it's going somewhere in your own zone
	 */

	if ((thisnode.zone != m.to.zone) && (!m.header.crash) && (arealist[area].netmail) && ((gate & GZONES) == GZONES)) {
                m.header.dest_net = thisnode.zone;
		m.header.dest = m.to.zone;
	}

	if (((m.to.domain != NULL) && (m.from.domain != NULL)) &&
	     (strcmp(m.to.domain,m.from.domain)) && ((gate & GDOMAINS) == GDOMAINS))
	     	for (i = 0; i < domains; i++)
	     		if (strcmp(domain_list[i].domain,m.to.domain) == 0) {
	     			m.header.dest = domain_list[i].node;
	     			m.header.dest_net = domain_list[i].net;
	     		}

	fwrite(&(m.header), sizeof(MSGHEADER), 1, fp);

	if (m.to.point)
		fprintf(fp, "\01TOPT %d\r\n", m.to.point);

	if (m.from.point)
		fprintf(fp, "\01FMPT %d\r\n", m.from.point);

        if ((m.from.zone != m.to.zone) && (arealist[area].netmail))
		fprintf(fp, "\01INTL %d:%d/%d %d:%d/%d\r\n",
			m.to.zone, m.to.net, m.to.node,
			m.from.zone, m.from.net, m.from.node);

	if (((m.to.domain != NULL) && (m.from.domain != NULL)) &&
	     (strcmp(m.to.domain,m.from.domain)))
		fprintf(fp, "\01DOMAIN %s %d:%d/%d %s %d:%d/%d\r\n",
			strupr(m.to.domain),m.to.zone, m.to.net, m.to.node,
			strupr(m.from.domain),m.from.zone, m.from.net, m.from.node);

	l = msgbuf.first;
	while (l != NULL) {
		char   *s = l->text;

		if (l->text == NULL)
			break;

		if (newtear)
			newtear = strncmp(s,"--- ",4);

		while (*s) {
			if (*s == '\n') {
				fputc('\r', fp);
				fputc('\n', fp);
			}
			else
				fputc(*s, fp);
			s++;
                }

		if ((strchr(l->text,'\n') == NULL) && softcr) {
			fputc(' ',fp);
			fputc(0x8d,fp);
		}

		l = l->next;

	}

	fputs("\r\n",fp);
	if ((tearline && arealist[area].echomail) && newtear) {
		fputs("\r\n--- msged " VERSION "\r\n", fp);
		fprintf(fp," * Origin: %s (",corigin);
                if (thisnode.domain != NULL)
                        fprintf(fp,"%s ",thisnode.domain);
                fprintf(fp,"%d:%d/%d",thisnode.zone,thisnode.net,thisnode.node);
		if (thisnode.point != 0)
			fprintf(fp,".%d",thisnode.point);
		fputs(")\r\n",fp);
	}
	fputc(0, fp);
        fclose(fp);
        arealist[area].new = 1;
	return (TRUE);
}

int     writeheader(m, n)
	MSGHEADER m;
	int     n;

{
	char    path[PATHLEN];
        FILE   *fp;

	if (!messages[n])
		return (FALSE);

	sprintf(path, "%s\\%d.MSG", arealist[area].path, n);

        if ((fp = fopen(path, "r+b")) == NULL)
		return (FALSE);

        fseek(fp,0l,SEEK_SET);
        fwrite(&m, (unsigned) sizeof(MSGHEADER), 1, fp);

	fclose(fp);
	return (TRUE);
}
