/*
 *	Copyright (C) 1988, 1989  TGV, Incorporated
 *	Deliver mail into MultiNet's mailer
 *
 */
#include "address.h"
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <sjcdef.h>
#include <descrip.h>
#include "vax-mm.h"
#ifdef	MULTINET

#define  FILENAMESIZE  256	/* max characters in file name (including '\0') */
#define  MAXLINE       5122	/* normal size of line buffers */

static int Queueit();
static char *Ascii_Date();
char *MM_geterror0(void);

/*
 *	Deliver Net mail
 */
int MultiNet_Deliver_Net_Mail(Address,Header,Header_Length,Text,Text_Length)
register struct Address *Address;
char *Header;
int   Header_Length;
char *Text;
int   Text_Length;
{
	FILE *fp;
	char scrfil[FILENAMESIZE];
	char from[MAXLINE], temp[MAXLINE];
	char *j, *cp;
	int i, Status;

	/*
	 *  Open the output file
	 */

	sprintf(scrfil,"MULTINET_SPOOL:SMTP-MM-MAIL.%08.8X_%lx",getpid(),time(0));
	enable_image_privilege();
	if ((i = creat(scrfil, 0600)) < 0) {
	    printf("%s : %s\n", scrfil, MM_geterror0());
	    disable_image_privilege();
	    return(0);
	}
	if (!(fp=fdopen(i, "w"))) {
	    close(i);
	    printf("%s : %s\n",scrfil, MM_geterror0());
	    disable_image_privilege();
	    return(0);
	}

	/*
	 *  Find the from address
	 */
	for (i = Header_Length, cp=Header, j=temp; i > 0; ) {
	    if (imatch(cp,0,"Sender:")) {
		for (cp += skipbl(cp,8) ; *cp != '\n' ; *j++ = *cp++);
		*j = '\0';
		break;
	    }
	    while(--i > 0) if (*cp++ == '\n') break;
	}
    	if (j == temp) {
	    for (i = Header_Length, cp=Header, j=temp; i > 0; ) {
	    	if (imatch(cp,0,"From:")) {
		    for (cp += skipbl(cp,6) ; *cp != '\n' ; *j++ = *cp++);
		    *j = '\0';
		    break;
	    	}
	    	while(--i > 0) if (*cp++ == '\n') break;
    	    }
	}
    	just_address(from, temp);
	(void) fprintf(fp,"MAIL FROM:<%s>\n",from);

	/*
	 *  Write each destination to the output file
	 */
	for( ; Address ; Address = Address->Next_Address) {
	    if(Address->Type == ADDRESS_NETWORK) {
		if (Address->Host)
		  fprintf(fp,"RCPT TO:<%s@%s>\n",Address->User,Address->Host);
		else
		  fprintf(fp,"RCPT TO:%s\n",Address->User);
	    } else {
		fprintf(fp,"RCPT TO:<%s>\n",Address->User);
	    }
	}
	fprintf(fp, "ARRIVAL_TIME: %s\n",Ascii_Date());
	(void) fflush(fp);
	write(fileno(fp),Header,Header_Length);
	write(fileno(fp),Text,Text_Length);
	(void) fclose(fp);

	Status = Queueit(scrfil, temp);
	if (!Status)
	    printf("Can't submit mail, message lost: %s\n",MM_geterror0());
	else
	    printf("%s\n", temp);

	fflush(stdout);
	disable_image_privilege();

	if (!Status)
	    return(0);
	else
	    return(1);
}

static int Queueit(file, Result)
char *file, *Result;
{
	unsigned short iosb[4];
	int ResultLen=0;
	unsigned long status, SYS$SNDJBCW();

/*
 *	Submit this file to the special symbiont
 *	Build the itemlist first.
 */

	struct SJCLST {
	    short len;
	    short item;
	    char *buf;
	    short *lenbuf;
	} itmlst[] = {19, SJC$_QUEUE, "MULTINET_SMTP_QUEUE", 0,
		      strlen(file), SJC$_FILE_SPECIFICATION, file, 0,
		      0, SJC$_DELETE_FILE, 0, 0,
		      MAXLINE-1, SJC$_JOB_STATUS_OUTPUT, Result, (short *) &ResultLen,
		      0,0};

/*
 *	Call $SNDJBCW to submit it, and print the resulting status line.
 *	Then, we're done.
 */
	status = SYS$SNDJBCW(0,SJC$_ENTER_FILE,0,itmlst,iosb,0,0);
	if (status&1) status=iosb[0];
	if (!(status&1)) {
	    (void) unlink(file);
	    if ((status & 0xffff8000) == 0x8000) status |= 0x40000;
	    vaxc$errno = status;
	    errno = EVMSERR;
	    return(0);
	} else {
	    Result[ResultLen] = '\0';
	    return(1);
	}
}

static char *Ascii_Date()
{
	static char Date[21];
	struct dsc$descriptor descr;

	/*
	 *	Set the Date string
	 */
	descr.dsc$w_length = 20;
	descr.dsc$a_pointer = Date;
	(void) SYS$ASCTIM(&descr.dsc$w_length,&descr,0,0);
	Date[descr.dsc$w_length] = '\0';
	return(Date);
}
#endif	MULTINET
