/*
**  MBMAIL.C - 6/21/87 The message file stuff, except for forwarding.
*/

/*
**  History of Modifiy - By A.Yamada(JA3JWI)
**
**  6. Add editing BID                                                                                      88/05/04
**  5. Reter quick when tnc is D/C (get_title())                87/12/15
**  4. Support /EX for end of tx.                               87/12/08
**  3. Automaticaly add Bulletin-Id, when single "$" read       87/12/03
**  2. Show all of information of message, when edit it         87/11/28
**  1. Implement from PC-DOS                                    87/11/16
*/

#include "mb.h"
#include "mbexp.h"
#if SHARE							/* AY-880601 */
#include <errno.h>
#include <sys/locking.h>
#endif

#if !SWAP_NO						/* AY-880920 */
#include "mbswap.h"
extern AT_SWAP	*at_swaproot;
extern TO_SWAP	*to_swaproot;
extern DISARP	*disarproot;
#endif

extern int	have_header;			/* AY-880910 */
extern int  in_dofilecmd;			/* AY-880303 */
extern char rfwd, rfwdok;           /* AY-1 */
extern char pathlist[];             /* AY-871026 */
extern int  errno;                  /* AY-880101 */

int	mayhave_bid;					/* AY-880910 */

char  *mbfile, *mbbfile, *msgdir, *bidprompt, *bid_toolong;
char  *to_file;                   /* AY-880103 */
char  *at_file;                   /* AY-880103 */
char  *mailer;                    /* AY-880107 */
word  kill_after_m;

char  *ufwd;  /* Calls with unread msgs, for BT */
char  *bfwd;  /* Calls with unread msgs, for forwarding */

short ufwdm, ufwdn, bfwdm, bfwdn;
int  showexp;                /* AY-871102 */
int  check_kanji_title;		 /* AY-880228 */
char  *mm[num_mm];
char wpcall[ln_call];
extern char *hfwdfile ;          /* File name of H-FWD define file */

#if !DOMAIN_NO						/* AY-880901 */
char domain[RECSIZE - sizeof(word) - (BIDSIZ + 5)];
#endif

MAIL_HDR  *mfhs;
MSG_HDR   *tmmhs;
MSG_EXT   *mmes;

int mfl, mflb;

wt_mmhs()
{
	if (port->mmhs->rn)               /* AY-871004 */
		return (write_rec (mfl, port->mmhs->rn, (char *)port->mmhs));
	else
		return (0);                     /* AY-871004 */
}

/*
 *  General permission check: sysop or TO or FROM.
 */

permit()
{
	if (port->user->options & u_sysop) return true;

	if (matchn(port->user->call, port->mmhs->to, ln_call)) return true;
	if (matchn(port->user->call, port->mmhs->from, ln_call)) return true;
	return false;
}

/*
 *  Return true if user has kill permission for current message.
 */

kpermit()
{
	if (permit()) return true;
	return ((port->mmhs->type is 'T') and (port->opt2 is 'T'));
}

/*
 *  Return true if user has read permission for current message.
 */

rpermit()
{
	if (permit()) return true;

#if !EXPFWD_NO	/* AY-880930 */
	if (port->mmhs->stat & m_chk)
		return false;
#endif

/*
 *	Is this mail has Kanji title? (AY-880228)
 */
	if (!port->mmhs->unu3 || check_kanji_title)
		return (port->mmhs->type isnt 'P');
	else
		return false;
}

/*
 *  Clean the mail file.
 *  Force drain of buffers, update of directory item.
 */

clnmsg()
{
	close(mfl);
	mfl = open(mbfile, O_RDWR | O_BINARY);
}

/*
 *  Close the mail file.
 */

clsmsg()
{
	close (mfl);
}

/*
 *  Open the mail file.
 */

opnmsg()
{
/*
**  Allocate space for the mail file records.
*/

	mfhs	=  (MAIL_HDR *) malloc(sizeof(MAIL_HDR));
	tmmhs =  (MSG_HDR *)	malloc(sizeof(MSG_HDR));
	mmes	=  (MSG_EXT *)	malloc(sizeof(MSG_EXT));

/*
**  Allocate the "who has mail" lists.
*/

	ufwd = (char *) malloc (ln_call * ufwdm);
	bfwd = (char *) malloc (ln_call * bfwdm);
	if (bfwd is NULL) errall();

/*
**  Open the file. If it does not exist, make one.
*/

	if ((mfl = open(mbfile, O_RDWR | O_BINARY)) < 0)
	{
		mfl = open(mbfile, O_CREAT | O_RDWR | O_BINARY, pmode);
		if (mfl < 0) { 
			nofile(mbfile); 
			exit(1); 
		}
		inithdr();
		mfhs->next_msg = 1;
		write_rec(mfl, 0, (char *)mfhs);
	}

/*
**  Read the mail file header.
*/

	read_rec(mfl, 0, (char *)mfhs);

/*
**  If older version mail file, update it.
*/

	if (mfhs->version < mb_version) updmsg();

/*
**  Set flag to force build of forwarding lists.
*/

	s_param setbit s_mchange;
}

/*
**  Initialize the mail file header.
*/

inithdr()
{
	mfhs->next	  = 1;
	mfhs->first   = 0;
	mfhs->last	  = 0;
	mfhs->ffree   = 0;
	mfhs->lfree   = 0;
	mfhs->version = mb_version;
	mfhs->free	  = 0;
	mfhs->count   = 0;
	mfhs->unt_msg = 1;

	curtim();
	strncpy(mfhs->date, l_date, ln_date);
	strncpy(mfhs->time, l_time, ln_time);
	fill(mfhs->unused, '\0', 99);
}

/*
**  Update mail file to current version.
*/

updmsg()
{
	register word i;

	sprintf(port->line, "Updating message file from version %d to version %d\n",
	    mfhs->version, mb_version);
	outstr(port->line);

	for (i = mfhs->first; i; i = port->mmhs->next)
	{
		read_rec(mfl, i, (char *)port->mmhs);

		if (mfhs->version < 3) remnl(port->mmhs->title);

		if (mfhs->version < 4)
		{
			port->mmhs->ext = 0;
			switch (port->mmhs->stat)
			{
			case 'F' : 
				port->mmhs->stat = m_fwd;  
				break;
			case 'N' : 
				port->mmhs->stat = 0;      
				break;
			case 'Y' : 
				port->mmhs->stat = m_read; 
				break;
#if !EXPFWD_NO	/* AY-880930 */
			case 'C' :
				port->mmhs->stat = m_chk;
				break;
			case 'K' :
				port->mmhs->stat = m_known;
				break;
			case 'U' :
				port->mmhs->stat = m_unk;
				break;
#endif
			}
		}

		if (mfhs->version < 6)
		{
			port->mmhs->next = port->mmhs->ext;
			port->mmhs->prev = port->mmhs->unu3;
			port->mmhs->ext  = port->mmhs->unu2;
		}

		if (mfhs->version < 7) port->mmhs->rn = i;

		wt_mmhs();
	}
	mfhs->version = mb_version;
	write_rec(mfl, 0, (char *)mfhs);
	clnmsg();
}

#if	!GMR_NO
/*
**  GM or GR command: backup the mail file.
*/

untmsg()
{
register word inhdr, mh, pmh;

	if (!mfhs->count) { 
		port->msg = mm[11]; 
		return; 
	}
	prtx(mm[12]);

/*
 *  Lock cuurent first.
 */
	DBGMSG((stderr, "untmsg()\n\r"));
	DB_LOCK(mfl);	/* AY-881108 */

/*
 *  Close current.
 */
#if !(XENIX|UNIX|BSD)	/* AY-881108 */
	close(mfl);
#endif

/*
 *  Delete backup.
 */
	unlink(mbbfile);

/*
**  Copy current to backup.
*/
#if !(XENIX|UNIX|BSD) 	/* AY-881108 */
	if (samedir(mbfile, mbbfile)) rename(mbfile, mbbfile); 
	else copy(mbfile, mbbfile);
#else
	copy(mbfile, mbbfile);
#endif

/*
**  Delete current.
*/
#if !(XENIX|UNIX|BSD)	/* AY-881108 */
	unlink(mbfile);
#endif

/*
**  Open new current and backup.
*/
#if !(XENIX|UNIX|BSD)	/* AY-881108 */
	mfl  = open(mbfile,  (O_CREAT|O_RDWR|O_BINARY), pmode);
#else
	lseek(mfl, 0L, 0);
#endif
	mflb = open(mbbfile, (O_RDONLY|O_BINARY));

	read_rec(mflb, 0, (char *)mfhs);
	inhdr = mfhs->first;
	inithdr();

	if (port->opt2 is 'R') {
		if (port->flds is 1) mfhs->next_msg = 1;
		else mfhs->next_msg = atoi(port->fld[1]);
	}

	mh = getrec();
	mfhs->first = mh; 
	pmh = 0;

/*
**  For each message header ...
*/

	while(inhdr) {

/*
**  Read the header.
*/

		if (!(read_rec(mflb, inhdr, (char *)port->mmhs)))
			break; /* AY-870920 */

/*
**  If there is a header extension item, read it.
*/

		if (port->mmhs->ext) {
			read_rec(mflb, port->mmhs->ext, (char *)mmes);
			port->mmhs->ext = getrec();
		}

/*
**  If there is a BID, read it.   AY-20.
*/
		msgbid->bid[0] = '\0';
		if (port->mmhs->unu2) {
			read_rec(mflb, port->mmhs->unu2, (char *)msgbid);
			port->mmhs->unu2 = getrec();
		}

/*
**  Tell the sysop about this message.
*/

		sprintf(port->line, "Hdr %6u Size %6u # %6u Read %6u BID: %s\n",
					inhdr,
					port->mmhs->size,
					port->mmhs->number,
					port->mmhs->read,
					msgbid->bid);
		if (port->dev is p_console)	/* AY-881020 */
			outstr(port->line);

/*
**  Point to next header.
*/

		inhdr = port->mmhs->next;

/*
**  If renumbering messages, renumber this message.
*/

		if (port->opt2 is 'R') {
			sprintf(port->line, "%s%u", msgdir, port->mmhs->number);
			sprintf(port->cmd,  "%s%u", msgdir, mfhs->next_msg);
			rename(port->line, port->cmd);
			port->mmhs->number = mfhs->next_msg++;
		}

/*
**  If there is a header extension item, write it.
*/

		if (port->mmhs->ext) {
			mmes->next = 0;
			write_rec(mfl, port->mmhs->ext, (char *)mmes);
		}

/*
**  If there is a BID, write it.   AY-20.
*/

		if (port->mmhs->unu2) {
			msgbid->next = 0;
			write_rec(mfl, port->mmhs->unu2, (char *)msgbid);
		}

/*
** Write the header.
*/

		mfhs->count++;
		port->mmhs->prev = pmh;
		if (port->mmhs->next) port->mmhs->next = getrec();
		port->mmhs->rn = mh;
		wt_mmhs();
		pmh = mh;
		mh = port->mmhs->next;
	}

/*
**  Write the file header.
*/

	mfhs->last = pmh;
	mfhs->unt_msg = mfhs->next_msg;
	write_rec(mfl, 0, (char *)mfhs);
	close(mflb);
	clnmsg();
/*
 *  Unlock current.  AY-881108
 */
	DB_UNLOCK(mfl);
}
#endif

/*
 *  Find a message given it's number.
 */

findmsg(nr)
word nr;
{
	register word i;

	for (i = mfhs->last; i; i = port->mmhs->prev)
	{
		read_rec(mfl, i, (char *)port->mmhs);
		if (port->mmhs->number is nr) return true;
		if (port->mmhs->number < nr)  return false;
	}
	return false;
}

/*
 *  Print headers of unread messages for this user.
 */

newmsg()
{
	if (findfwd(port->user->call, ufwd, ufwdn) or
	    findfwd(port->user->call, bfwd, bfwdn))
	{
		prtx(mm[2]);
		if (!(port->user->options & u_expert)) /* AY-1 */
		{
			port->opt2 = '\0';
			port->flds = 1;
			lstmsg();
		}
	}
}

/*
**	XL command, Expanded listing message
*/
#if !XLIST_NO	/* AY-880827 */
explist()
{
char option[10], tempfile[30];

	strcpy(option, "a t");
	if (port->flds > 1) {
		switch (*port->fld[1]) {
		case '*': strcpy(option, ""); break;
                case '@': case 'A' : strcpy(option, "a"); break;
		case 'T': strcpy(option, "t"); break;
		case 'F': strcpy(option, "f"); break;
		}
	}

	tmpnam(tempfile);
	sprintf(tmp->scr, "mbxls %s > %s", option, tempfile);
	if (system(tmp->scr) == 0)
#if (XENIX|UNIX|BSD)
		{ flow_yes(); dload(tempfile); flow_no(); unlink(tempfile); }
#else
		{ dload(tempfile); unlink(tempfile); }
#endif
	else
		{ perror(tempfile); fprintf(stderr, "\r"); }
}
#endif

static word cnt; /* AY-880830 */

/*
 *  L command, list messages.
 */

word latest, latest2;	/* AY-881006 */

lstmsg()
{
char *tmpnam();

register short done, ok;
word h, found, more, from;
#if	(XENIX|UNIX|BSD)	/* AY-880308 */
char *name, call[80], *regcmp(), *regex();
#endif

	SETMFHS();
	latest2 = 0;
/*
**	Have I show the Expanded item?
*/
	showexp = 0;
	if (port->flds > 1)
	if (*port->fld[port->flds - 1] == ';') { showexp = 1; port->flds -= 1; }


/*
**  First set up arguments.
*/

	cnt = found = 0; 
	more = 0; 
	done = false;
	from = mfhs->next_msg;				/* AY-880317 */
	switch(port->opt2) {
	case '@' :
	case '<' :
	case '>' :	
		pcall(tcall, port->fld[1]);
#if	(XENIX|UNIX|BSD)	/* AY-880317 */
		unbl(call, port->fld[1], 79);
		strupr(call);
		if (call[0] != '^') {
			sprintf(tmp->scr, "^%s", call);
			strcpy(call, tmp->scr);
		}
		if (call[strlen(call) - 1] != '$')
			strcat(call, "$");
		name = regcmp(call, 0);
#endif
		break;
	case '\0': 
		break;
	case ' ' : 
		if (port->flds is 1) more = port->user->msg_number;
	default  : 
		if (port->flds >= 2)	/* AY-880317 */
		/*default  : if (port->flds is 2)*/
		{
			if (!num(port->fld[1])) {
				port->msg = mwhat; 
				return; 
			}
			more = atoi(port->fld[1]);
			if (port->flds >= 3) {		/* AY-880317 */
				if (!num(port->fld[2])) {
					port->msg = mwhat; 
					return; 
				}
				from = atoi(port->fld[2]);
				if (from < more) {
					found = more; 
					more = from;
					from = found; 
					found = 0;
				}
			}
		}
	}


	/*
 *  Log it, unless is initial login check for msgs.
 */

	if (port->opt2)
	{
		sprintf(port->line, "%u", more);
		log('M', 'L', port->opt2, port->line);
	}

	/*
 *  Paw through the messages and show what requested.
 */

#if (XENIX|UNIX|BSD)    /* AY-880716 */
	flow_yes();
#endif

	for (h = mfhs->last; h /*and !done*/; h = port->mmhs->prev)
	{
		read_rec(mfl, h, (char *)port->mmhs);
		if (port->opt2 is 'L')
			done = (found is more);
		else
			done = ((port->mmhs->number < more) or (port->mmhs->number > from));
		/*	done = (port->mmhs->number < more); */

		ok = !done;

/*
 *  Does this user have read permission for this message?
 */

		if (ok) ok = rpermit();

/*
 *  Is this message wanted?
 */
		if (ok)
			switch(port->opt2) {
			case '\0': 
				ok = matchn(port->user->call, port->mmhs->to, ln_call); 
				break;
#if !EXPFWD_NO	/* AY-880930 */
			case 'C' :
				ok = (port->mmhs->stat & m_chk);
				break;
			case 'K' :
				ok = (port->mmhs->stat & m_known);
				break;
			case 'U' :
				ok = (port->mmhs->stat & m_unk);
				break;
#endif
			case 'F' : 
				ok = (port->mmhs->stat & m_fwd); 
				break;
			case 'L' : 
				break;
			case 'M' :
				ok = matchn(port->user->call, port->mmhs->to, ln_call);
				break;

			case 'Y' : 
				ok = (port->mmhs->stat & m_read); 
				break;
			case '@' :
#if	(XENIX|UNIX|BSD)
				unbl(call, port->mmhs->bbs, ln_call);
				ok = (regex(name, call) != NULL); 
				break;
#else
/*                             ok = matchn(tcall, port->mmhs->bbs, ln_call);  */
                               ok = wcm (tcall,port->mmhs->bbs) ; /* change  5/13   */
				break;
#endif
			case '<' :
#if	(XENIX|UNIX|BSD)
				unbl(call, port->mmhs->from, ln_call);
				ok = (regex(name, call) != NULL); 
				break;
#else
/*                             ok = matchn(tcall, port->mmhs->from, ln_call);  */
                               ok = wcm (tcall, port->mmhs->from) ; /* change 5/13  */
				break;
#endif
			case '>' :
#if	(XENIX|UNIX|BSD)
				unbl(call, port->mmhs->to, ln_call);
				ok = (regex(name, call) != NULL); 
				break;
#else
/*                             ok = matchn(tcall, port->mmhs->to, ln_call);      */
                               ok = wcm (tcall, port->mmhs->to) ;  /* change 5/13   */
				break;
#endif
			case '$' : 
				ok = (port->mmhs->unu2); 
				break; /* AY-870929 */
			default  :
				ok = ((port->mmhs->type is port->opt2) or (port->opt2 is ' '));
			}
		if (ok) {
			prthdr(!found++);
			latest = port->mmhs->number;	/* AY-881006 */
			if (!latest2)
				latest2 = latest;	/* AY-881007 */
		}

                if (cnt > 21)  /* AY-880830 & Nov12,89 */
		{
                    if (isatty (1) and isatty(0))     /* MAY.5,1990 */
                    {
			prtx(pausemsg);
			outstr("\n");
			getdat();
			if (port->mode & gone)
			{
#if (XENIX|UNIX|BSD) /* AY-880716 */
				flow_no();
#endif
				return;
			}

			switch(port->line[0]) {	/* AY-880710 */
			case 'Q':
			case 'q':
			case 'A':
			case 'a':
#if (XENIX|UNIX|BSD)	/* AY-880716 */
				flow_no();
#endif
				return;
			default:
				cnt = 0; 
				break;
			}
                    }else cnt = 0 ;       /* Nov12,89  */
		}
	}
#if (XENIX|UNIX|BSD)	/* AY-880716 */
	flow_no();
#endif
	if (!found) port->msg = mfind;
}

/*
 *  Create the header line.
 */

makehdr()             /* AY-871007 */
{
register char st, st1;
#if !DOMAIN_NO	/* AY-881126 */
register char st2;
	st2 = ' ';
#endif

	st = 'N'; 
	st1 = ' ';
	if (port->mmhs->stat & m_read) { 
		st = 'Y'; 
		st1 = '@'; 
	}
	if (port->mmhs->stat & m_fwd)  { 
		st = 'F'; 
		st1 = '@'; 
	}

#if !DOMAIN_NO	/* AY-880901 */
	if (port->mmhs->unu2) {
		read_rec(mfl, port->mmhs->unu2, (char *)msgbid);
		if (msgbid->bid[0]) st = '$';

		if (msgbid->domain[0]) {
			st2 = '%';
			if (st != '$') st1 = ' ';
		}
	}
	else
		st1 = ' ';
#else
	if (port->mmhs->unu2)
		st = '$';
	else
		st1 = ' ';
#endif

#if !EXPFWD_NO	/* AY-880930 */
	if (port->mmhs->stat & m_chk)
		st = 'C';
	if (port->mmhs->stat & m_known)
		st = 'K';
	if (port->mmhs->stat & m_unk)
		st = 'U';
#endif

	sprintf(tmp->scr,"%5u %c%c %5u %6.6s %6.6s %c%6.6s%c%4.4s/%4.4s %s\n",
	    port->mmhs->number, port->mmhs->type, st, port->mmhs->size,
	    port->mmhs->to, port->mmhs->from, st1, port->mmhs->bbs, st2,
	    port->mmhs->date + 2, port->mmhs->time, port->mmhs->title);
	if (strlen(tmp->scr) > 80) strcpy((tmp->scr + 78), "\n"); /* AY-880125 */
}

/*
 *  Print a message header.
 */

prthdr(tit)
int tit;
{
register short i;
#if !DOMAIN_NO	/* AY-880901 */
char to[ln_call + 1];
#endif

	if (tit)
	{
		prtx(mm[3]);
		cnt++;			/* AY-880830 */
	}
	makehdr();
	outstr(tmp->scr);
	cnt++;				/* AY-880830 */

	msgbid->bid[0] = msgbid->domain[0] = '\0'; 	/* AY-880901 */
#if !DOMAIN_NO	/* AY-880901 */
	if (port->mmhs->unu2)	/* AY-880901 */
		read_rec(mfl, port->mmhs->unu2, (char *)msgbid);

	if (showexp)
	if (msgbid->domain[0])
	{
		unbl(to, port->mmhs->to, ln_call);
		sprintf(tmp->scr, " Address: <%s@%s>\n", to, msgbid->domain);
		outstr(tmp->scr);
		cnt++;
	}
#else
	if (port->mmhs->unu2)	/* AY-880901 */
		read_rec(mfl, port->mmhs->unu2, (char *)msgbid);
#endif

	if (port->mmhs->ext and showexp)     /* AY-871102 */
	{
		read_rec(mfl, port->mmhs->ext, (char *)mmes);
		if (msgbid->bid[0])              /* AY-871009 */
		{
			outstr("   Bul:");
			sprintf(tmp->scr, " [%s]", msgbid->bid);
			outstr(tmp->scr);
		}   /* End of AY-871009*/
		else
			outstr("    Cc:");
		for (i = 0; i < mmes->count; i++)
		{
			if (mmes->flag[i]) outchar(' '); 
			else outstr(" *");
			unbl(tmp->scr, mmes->call[i], ln_call);  /* AY-871012 */
			strlwr(tmp->scr);
			outnb(tmp->scr, ln_call);
		}
		cnt++;								/* AY-880830 */
		outchar('\n');
		return;
	}

	if (port->mmhs->unu2 and showexp) /* AY-871102 */
	if (msgbid->bid[0] && !port->mmhs->ext) {
		sprintf(tmp->scr, "   Bid: [%s]\n", msgbid->bid);
		if (port->mmhs->stat & m_read) *(tmp->scr + 7) = '*';
		if (port->mmhs->stat & m_fwd)  *(tmp->scr + 7) = '*';
		outstr(tmp->scr);
		cnt++;						/* AY-880830 */
	}
}

/*
**  Parse the [@ bbs] and [< from] fields of a command.
*/

#if !DOMAIN_NO	/* AY-880901 */
atfrom(i)
int i;
{
register char *ptr;
char *strchr();

	if (*port->fld[i] is '@') {
		if (ptr = strchr(port->fld[i + 1], '.')) {
			strncpy(domain, port->fld[i + 1], sizeof(domain));
			*ptr = '\0';
			pcall(port->mmhs->bbs,  port->fld[i + 1]);
			*ptr = '.';
                        if (iscall (port->mmhs->bbs)) hfwd();  /* JUN.2,91 */
		}
		else
			pcall(port->mmhs->bbs,  port->fld[i + 1]);
	} else

	if (*port->fld[i] is '<')
		pcall(port->mmhs->from, port->fld[i + 1]);
}
#else
atfrom(i)
int i;
{
	if	  (*port->fld[i] is '@') pcall(port->mmhs->bbs,  port->fld[i + 1]);
	else if (*port->fld[i] is '<') pcall(port->mmhs->from, port->fld[i + 1]);
}
#endif
/*
 *      replace @BBS if H-address is match.     June 2,1991
 */
hfwd()
/*  hfwdfile =defiend in CONFIG.MBX
 *  port->mmhs->bbs = @bbs of MAIL.DAT
 *  domain   = address of MAIL.DAT
 */

{
        FILE *hfw ; int i, j ;
        char buf [80], repbbs [10] ;

        if (hfwdfile == NULL ) return ;         /* No. hfwd.mbx in config.mbx */
        if ((hfw = fopen (hfwdfile, "r") ) == NULL) return ; /* No file */

        while (fgets (buf,80,hfw) ) {           /* READ HFWD.MBX file   */
                if (iseof(buf)) break ;         /* end if *** EOF */
                if ( buf[0] == '#') continue ;  /* skip comment line */
                unbl (buf,buf,80) ;             /* set "\0" after string */
                uc (buf);                       /* TO Upper char     */
                if ( buf[0] == '.') {
                        for ( i= strlen(buf)-1, j= strlen(domain)-1 ; i > 0; i--,j--)
                                       if (buf[i] != domain[j] ) break ;
                        if (buf[i] == domain[j] ) { /* H-address Match */
                                pcall(port->mmhs->bbs,repbbs) ;
                                break ;         /* end of WHILE */
                        }else  continue ;       /* skip no-match Hadress */
                }
                pcall (repbbs,buf) ;
        }
        fclose (hfw) ;
}

#if	!DUPMSG_NO
/*
 *  CM command: duplicate a message.
 */

dupmsg()
{
register int i;  /* AY-60 */

	DBGMSG((stderr, "dupmsg()\n\r"));
	DB_LOCK(mfl);
	SETMFHS();

	fill((char *)msgbid, '\0', sizeof(MSG_BID));
#if !DOMAIN_NO
    domain[0] =
#endif
	bidname[0] = msgbid->bid[0] = '\0';

	if (!findmsg(atoi(port->fld[2]))) { 
		port->msg = mnmsg; 
		return; 
	}
	port->opt1 = 'S';
/* Del Bid Copy   Feb.22,90
        if (port->mmhs->unu2)                      AY-880830
	{
		read_rec(mfl, port->mmhs->unu2, (char *)msgbid);
		unbl(bidname, msgbid->bid, BIDSIZ);
	}
*/
	prthdr(true);
	pcall(port->mmhs->to, port->fld[1]);
	fill(port->mmhs->bbs, ' ', ln_call);

	if (port->flds >= 3)  /* AY-60 */
		for (i = 2; port->flds > i+1; i++) atfrom(i);

        if (*port->mmhs->bbs isnt ' ')        /* Feb.22,90 */
                if (!iscall(port->mmhs->bbs)){
                        port->msg = mwhat ;
                        return ;
        }

	sprintf(port->cmd,  "%s%u", msgdir, port->mmhs->number);
	sprintf(port->line, "%s%u", msgdir, mfhs->next_msg);
	copy(port->cmd, port->line);
	cremsg();
	DBGMSG((stderr, "dupmsg()\n\r"));
	DB_UNLOCK(mfl);
}
#endif

#if	!FILMSG_NO
/*
 *  F command: copy a message to a file.
 */

filmsg()
{
	register char *p;
	register short hdr, append;
	register FILE *out;

	DB_LOCK(mfl);	/* AY-881117 */
	SETMFHS();

	if (!findmsg(atoi(port->fld[1]))) { 
		port->msg = mnmsg; 
		DB_UNLOCK(mfl);	/* AY-881117 */
		return; 
	}

	hdr = false;
	append = false;

	if (port->flds is 4) for (p = port->fld[3]; *p; p++) switch (*p)
	{
	case 'A' : 
		append = true; 
		break;
	case 'H' : 
		hdr    = true; 
		break;
	default: 
		;
	}

	if (port->opt2 is ' ') strcpy(port->line, port->fld[2]);
	else if (getdir(port->fld[2]) is NULL) { 
		port->msg = mndir; 
		DB_UNLOCK(mfl);
		return; 
	}

	if (append)
		out = fopen (port->line, "a"); 
	else {
		if ((out = fopen (port->line, "r")) isnt NULL) { 
			fclose(out); 
			port->msg = mexst; 
			DB_UNLOCK(mfl);
			return; 
		}
		out = fopen (port->line, "w");
	}

	if (out is NULL) { 
		port->msg = mcant; 
		DB_UNLOCK(mfl);
		return; 
	}

	sprintf(tmp->scr, "%s %s", port->fld[1], port->line);
	log('M', 'D', port->opt2, tmp->scr);

	prthdr(true);

	if (hdr) fputs(tmp->scr, out);

	sprintf(port->cmd, "%s%u", msgdir, port->mmhs->number);
	if ((port->fl = fopen(port->cmd, "r")) is NULL) { 
		fclose(out); 
		nofile(port->cmd); 
		DB_UNLOCK(mfl);
		return; 
	}

	while (fgets(tmp->scr, scrmax, port->fl) isnt NULL)
		fputs(tmp->scr, out);

	fclose(port->fl);
	fclose(out);
	DB_UNLOCK(mfl);
}
#endif

/*
 *  "Display the message text" utility.
 */

dloadm()
{
#if SHARE	/* AY-880722 */
MSG_HDR tmpmmhs;
#endif

	sprintf(port->line, "%u", port->mmhs->number);
	log('M', 'R', port->opt2, port->line);

	if (port->opt1 is 'V' or port->opt2 is 'H')
		prthdr(true);    /* AY-880308 */

	sprintf(port->line, "%s%u", msgdir, port->mmhs->number);
	if ((port->fl = fopen(port->line, "r")) is 0) { 
		nofile(port->line); 
		return; 
	}

	if ((port->opt1 isnt 'V') and (port->opt2 isnt 'H'))  /* AY-880308 */
		mmh(port->fl, true);
	else {
		pgst(NULL);
		pgck();   /* First line of header */
		pgck();   /* Second line of header */
	}

	while(fgets(tmp->scr, scrmax, port->fl) isnt NULL) {
		if (isbreak()) break;
		outstr(tmp->scr);
		if (pgck() is 'Q') break;
	}
	fclose (port->fl);

	if (port->mode & gone) return; /* AY-871008 */
	port->mmhs->read++;          /* AY-871007 */
	if (matchn(port->user->call, port->mmhs->to, ln_call)) {
		port->mmhs->stat setbit m_read;
		s_param setbit s_mchange;
	}
#if SHARE	/* AY-880722 */
	DBGMSG((stderr, "dloadm()\n\r"));
	DB_LOCK(mfl);
	read_rec(mfl, port->mmhs->prev, (char *)&tmpmmhs);
	read_rec(mfl, tmpmmhs.next, (char *)&tmpmmhs);
	if ((tmpmmhs.rn is port->mmhs->rn) and
		(tmpmmhs.next is port->mmhs->next) and
		(tmpmmhs.prev is port->mmhs->prev)) {
                        wt_mmhs();
	}

	DBGMSG((stderr, "dloadm()\n\r"));
	DB_UNLOCK(mfl);
#else
	wt_mmhs();
#endif
}

/*
 *  RM command: read my messages.
 */

rdmsgm()
{
	register short found;
	register word h;
        register char t;

	SETMFHS();

	found = false;
	for (h = mfhs->first; h; h = port->mmhs->next)
	{
		read_rec(mfl, h, (char *)port->mmhs);
#if !EXPFWD_NO	/* AY-880930 */
		if (!(port->mmhs->stat & (m_read | m_fwd | m_chk)) and
#else
		if (!(port->mmhs->stat & (m_read | m_fwd)) and
#endif
		    matchn(port->user->call, port->mmhs->to, ln_call)) {
#if (XENIX|UNIX|BSD)	/* AY-880714 */
			flow_yes();
#endif
			dloadm();
#if (XENIX|UNIX|BSD)	/* AY-880714 */
			flow_no();
#endif
			found = true;
                        pgdn() ;
                  /*
                        if (port->mode is local and isatty(1))
                        {
                                prtx (pausemsg) ;
                                t =inchar() ; outchar ('\n') ;
                                if ((t is 'Q') or (t is 'q')) break ;
                        }
                  */
                }
	}
	if (!found) port->msg = mfind;
}

/*
 *  R command: read one message.
 */

rdmsg()
{       
	if (!num(port->fld[1])) { 
		port->msg = mwhat; 
		return; 
	}
	if (!findmsg(atoi(port->fld[1]))) { 
		port->msg = mnmsg; 
		return; 
	}
	if (!rpermit()) { 
		port->msg = mprot; 
		return; 
	}

#if (XENIX|UNIX|BSD)	/* AY-880714 */
	flow_yes();
#endif
	dloadm();
#if (XENIX|UNIX|BSD)	/* AY-880714 */
	flow_no();
#endif
}

/*
 *  Read the message, look for the last bbs header.
 *  Set the @ field of the current message to the "at bbs" in that header.
 */

findat()
{
	register short i;

	fill(port->mmhs->bbs, ' ', ln_call);
	sprintf(port->line, "%s%u", msgdir, port->mmhs->number);
	if ((port->fl = fopen(port->line, "r")) is 0) { 
		nofile(port->line); 
		return; 
	}

	while(fgets(port->line, linelen, port->fl) isnt NULL) {
		parse_head();
		if (!port->flds) {
			fclose(port->fl);
			return;
		} else {
			for (i = 0; i < port->flds; i++)
				if (*port->fld[i] is '@') pcall(port->mmhs->bbs, port->fld[i] + 1);
		}
	}
	fclose (port->fl);
}

/*
 *  KF, KM, KY commands, kill multiple messages.
 */

klmsgm()
{
	register word h, next;
	register short found, ok;

	if (port->flds is 2) pcall(tcall, port->fld[1]);

	SETMFHS();

	found = false;
	for (h = mfhs->first; h; h = next) {
		read_rec(mfl, h, (char *)port->mmhs);
		next = port->mmhs->next;

		ok = kpermit();

		if (ok) if (port->flds is 2)
           /*             ok = matchn(tcall, port->mmhs->to, ln_call); */
                        ok = wcm (tcall, port->mmhs->to) ;

		if (ok)
			switch(port->opt2) {
#if !EXPFWD_NO	/* AY-880930 */
			case 'C' :
				ok = (port->mmhs->stat & m_chk);
				break;
			case 'K' :
				ok = (port->mmhs->stat & m_known);
				break;
			case 'U' :
				ok = (port->mmhs->stat & m_unk);
				break;
#endif
			case 'F' : 
				ok = (port->mmhs->stat & m_fwd); 
				break;
			case 'M' :
				ok = ((port->mmhs->stat & m_read) and
				    matchn(port->user->call, port->mmhs->to, ln_call));
				break;
			case 'Y' : 
				ok = (port->mmhs->stat & m_read); 
				break;
			}

		if (ok) {
			sprintf(port->line, "%u", port->mmhs->number);
			log('M', 'K', port->opt2, port->line);
			prtx(mm[13]);
			found = true; 
			kmsg();
		}
	}
	if (!found) port->msg = mfind;
}

/*
 *  K command, kill a message.
 */

klmsg()
{
	register short dosvc;

	if (!num(port->fld[1]))	    { 
		port->msg = mwhat; 
		return; 
	}
	if (!findmsg(atoi(port->fld[1]))) { 
		port->msg = mnmsg; 
		return; 
	}
	if (!kpermit())		    { 
		port->msg = mprot; 
		return; 
	}

	log('M', 'K', port->opt2, port->fld[1]);

	dosvc = (port->opt2 is 'T') and
	    (port->mmhs->type is 'T') and
	    (s_param & s_svc);

	if (dosvc) findat();

/*
 *  Kill the message.
 */

	kmsg();

/*
 *  If this is KT of T type message, and we do service messages,
 *  save what cremsg needs for service message.
 */

	if (dosvc) {
		port->opt1 = 'S';
		strncpy(port->mmhs->to, port->mmhs->from, ln_call);
		strncpy(port->mmhs->from, cport->user->call, ln_call);
		port->mmhs->type = 'S';

		sprintf(port->line, "%s%u", msgdir, mfhs->next_msg);
		port->fl = fopen(port->line, "w");
		fprintf(port->fl, "Msg %5u taken from %6.6s by %6.6s at %4.4s on %6.6s\n",
		    port->mmhs->number, cport->user->call, port->user->call, l_time, l_date);
		fclose(port->fl);

		filesize = 56;
		cremsg();
	}
	port->msg = mdone;
}

/*
 *  Kill message utility.
 *  Used by "kill multiple" "kill single", and forwarding.
 *  Header must be read in.
 */

kmsg()
{
	register PORTS *p;

	if (!port->mmhs->rn) return;

#if SHARE
	DBGMSG((stderr, "kmsg()\n\r"));
	DB_LOCK(mfl);
	SETMFHS();
#endif

/*
**  Dispose of the file.
*/

	sprintf(port->line, "%s%u", msgdir, port->mmhs->number);
	unlink(port->line);

/*
**  Point the previous header to this headers next.
*/

	if (port->mmhs->prev) {
		read_rec (mfl, port->mmhs->prev, (char *)tmmhs);
		tmmhs->next = port->mmhs->next;
		write_rec (mfl, port->mmhs->prev, (char *)tmmhs);
	} else
		mfhs->first = port->mmhs->next;

/*
**  Point the next header to this headers previous.
*/

	if (port->mmhs->next) {
		read_rec (mfl, port->mmhs->next, (char *)tmmhs);
		tmmhs->prev = port->mmhs->prev;
		write_rec (mfl, port->mmhs->next, (char *)tmmhs);
	} else
		mfhs->last = port->mmhs->prev;

/*
**  Point end of free chain to this message header.
*/

	if (mfhs->lfree) {
		read_rec (mfl, mfhs->lfree, (char *)tmmhs);
		tmmhs->next = port->mmhs->rn;
		write_rec (mfl, mfhs->lfree, (char *)tmmhs);
	} else mfhs->ffree = port->mmhs->rn;

/*
**  Link this header into free chain.
**  Assumes mmes->next is zero.
*/

	mfhs->free++;
	/* AY-20 */
	if ((port->mmhs->ext) and (port->mmhs->unu2)) {
		msgbid->next = port->mmhs->ext;
		write_rec(mfl, port->mmhs->unu2, (char *)msgbid);
		mfhs->free++; 
		mfhs->free++;
		mfhs->lfree = port->mmhs->ext;
		port->mmhs->next = port->mmhs->unu2;
	} else if (port->mmhs->unu2) {	/* BID or Domain */
		msgbid->next = 0;
		write_rec(mfl, port->mmhs->unu2, (char *)msgbid);
		mfhs->free++;
		mfhs->lfree = port->mmhs->unu2;
		port->mmhs->next = port->mmhs->unu2;
	} else if (port->mmhs->ext)	/* Distribution Only */
	{
		mfhs->free++;
		mfhs->lfree = port->mmhs->ext;
		port->mmhs->next = port->mmhs->ext;
	} else {					/* No Extention Item */
		mfhs->lfree = port->mmhs->rn;
		port->mmhs->next = 0;
	}

	wt_mmhs();

/*
**  Update the file header.
*/

	mfhs->count--;
	write_rec (mfl, 0, (char *)mfhs);
	s_param setbit s_mchange;
#if SHARE
	DBGMSG((stderr, "kmsg()\n\r"));
	DB_UNLOCK(mfl);
#endif
}

#if !DOMAIN_NO	/* AY-880903 */
get_title()
{
	while(!getdat());
	if (port->mode & discon) return(false);
	remnl(port->line);
        if ( strlen (port->line) >= mhtitl)
                port->line[mhtitl-1] = '\0' ; /* Long Title Stop. May16.91 */
        strncpy(port->mmhs->title, port->line, mhtitl);
	return(true);
}
#else
get_title()
{
	char call[BIDSIZ + 5];

	while(!getdat());
	if (port->mode & discon) return;   /* AY-871215 */
	remnl(port->line);
	strncpy(port->mmhs->title, port->line, mhtitl);

#if !SHARE	/* AY-880604 */
	if (port->dev is p_console)        /* AY-871215 */
		if (!(bidname[0]))
		{
			while(true)
			{
				outstr(bidprompt);
				getdat(); 
				uc(port->line); /* You can type lower case AY-871002 */

				if (strlen(port->line) > BIDSIZ)
				{ 
					outstr(bid_toolong); 
					continue; 
				}

				unbl(tmp->scr, port->line, BIDSIZ);
				if (port->line[0] is '\n')
				{
					outstr(bidprompt);
					unbl(bidname, cport->user->call, ln_call);
					sprintf(tmp->scr, "%s%u\n", bidname, mfhs->next_msg);
					outstr(tmp->scr);
				}
				if (port->line[0] is ' ') break;  /* No BID */

				remnl(tmp->scr);
				unbl(bidname, tmp->scr, BIDSIZ);

				if (!bidserch(bidname)) break;

				outstr(bidprompt); 
				outstr(bidname); 
				outstr("- already in use\n");
				bidname[0] = '\0';
			}
		}
#endif
}
#endif

/*
 *  Parse the fields of a send command.
 */

pfld()
{
register int tosysop; /* AY-880903 */
	
	port->mmhs->type = port->opt2;
        pcall(port->mmhs->to, port->fld[1]);

#if !SWAP_NO								/* AY-880920 */
	swap_to();
#endif

	mayhave_bid = iscall(port->mmhs->to);
        tosysop = matchn(port->mmhs->to, "SYSOP", 5);

        switch(port->mmhs->type) {  /* AY-881101 */
                case 'T':
		case 'B':
		case 'P':
			break;
		default:
			if (mayhave_bid || tosysop)
				port->mmhs->type = 'P';
			else
				port->mmhs->type = 'B';
    }

    if (port->mmhs->type != 'T')
            if (port->mmhs->type == 'P' && tosysop)
            mayhave_bid = false;
            else
                if (port->mmhs->type == 'B')
                mayhave_bid = false;
                else
                mayhave_bid = true;
    else
    mayhave_bid = true;     /* BID is TYPE"B" & "P"SYSOP only! by JH1RSV */

    strncpy(port->mmhs->from, port->user->call, ln_call);
    fill(port->mmhs->bbs, ' ', ln_call);

	if (port->flds > 3) atfrom(port->flds -  2);
	if (port->flds > 5) atfrom(port->flds -  4);

	if (port->flds > 2)
	if (*port->fld[port->flds-1] is '$') {

        if (port->flds > 4) atfrom(port->flds -  3);
		if (port->flds > 6) atfrom(port->flds -  5);

		if (*(port->fld[port->flds-1] + 1))
			strcpy(bidname, port->fld[port->flds-1] + 1);

		if (bidname[0])
	    	if (bidserch(bidname)) {
#if (XENIX|UNIX|BSD)	/* AY-880914 */
			flushport(1);
#endif
			sprintf(tmp->scr, "N - BID\n");
			outstr(tmp->scr);
	    		return (false);
		}
	}

#if !SWAP_NO	/* AY-880920 */
	swap_bbs();
#endif

#if (XENIX|UNIX|BSD)	/* AY-880914 */
	flushport(1);
#endif

	if (rfwdok) {
        sprintf(tmp->scr, "OK %-6u\n", mfhs->next_msg); /* d ->u 5/14 */
		outstr(tmp->scr);
	} else {
		if (port->user->options & (u_bbs | u_expert))
			outstr("Sj:\n");
		else
			prtx(mm[0]);
	}

	return (get_title());
}

/*
 *  S command: send a message.
 */
sndmsg()
{
char call[BIDSIZ+5];  /* AY-871203 */

#if	(XENIX|UNIX|BSD)
char mail_from[ln_call + 5], mail_at[ln_call + 5], mail_to[ln_call + 5];
char xenixcmd[80];
#endif

	fill((char *)msgbid, '\0', sizeof(MSG_BID));
#if !DOMAIN_NO					/* AY-880901 */
	domain[0] = bidname[0] = '\0';
#else
	bidname[0] = '\0';
#endif

	port->mmhs->unu3 = 0;

	if (!pfld())
		return;

	if (!rfwdok) {
		if (port->user->options & (u_bbs | u_expert))
			outstr("Msg:\n");
		else
			outstr(mm[1]);
	}

	if (!in_dofilecmd)
	if (port->mode & gone)
		return;

#if SHARE 	/* AY-880531 */
	sprintf(port->cmd, "/tmp/Mb%u", getpid());
	uload (port->cmd); 
#if !EXPFWD_NO	/* AY-880930 */
	{
		if (port->mode & gone)
		if (filesize == 0)
			return;
#else
	if (!(port->mode & gone)) {
#endif
		sync();		/* AY-880601 */
		DBGMSG((stderr, "sndmsg()\n\r"));
		DB_LOCK(mfl);
		SETMFHS();
		sprintf(tmp->scr, "mv %s %s%u",
			port->cmd,
			msgdir,
			mfhs->next_msg);
		(void)system(tmp->scr);
		sprintf(port->cmd, "%s%u", msgdir, mfhs->next_msg);
	}
#else
	sprintf(port->cmd, "%s%u", msgdir, mfhs->next_msg);
	uload (port->cmd);
#endif

#if !EXPFWD_NO	/* AY-880930 */
	/* Well I wish to save This massage for next Forwading Time */
#else
	if (!in_dofilecmd)
	if (port->mode & gone) {
#if SHARE
		DBGMSG((stderr, "sndmsg()\n\r"));
		DB_UNLOCK(mfl);
#endif
		return;
	}
#endif

#if SHARE
	unbl(call, port->mmhs->from, ln_call);
	if (!iscall(call)) {
		DBGMSG((stderr, "sndmsg()\n\r"));
		DB_UNLOCK(mfl);
		return;
	}
	cremsg();
	sync();
	DBGMSG((stderr, "sndmsg()\n\r"));
	DB_UNLOCK(mfl);
#else
	unbl(call, port->mmhs->from, ln_call);
	if (!iscall(call))
		return;
	cremsg();
#endif

#if	(XENIX|UNIX|BSD)        /* AY-880223 */
	unbl(mail_to, port->mmhs->to, ln_call);
	unbl(mail_at, port->mmhs->bbs, ln_call);

	if ((port->mmhs->unu3 == 0)	/* Title hasn't Kanji or ESC seq */
		and (bidname[0] == '\0')	/* Hasn't BID */
		and (!mail_at[0])		/* Sure on This BBS */
					/* And want to fwd to XENIX */
		and (search_file(mail_to, to_file))) {
		unbl(mail_from, port->mmhs->from, ln_call);
		strlwr(mail_from);	/* AY-880510 */
		sprintf(xenixcmd, "%s %s@mb %s %u %c",
				mailer,
				mail_from,
				mail_to,
				mfhs->next_msg - 1,
				port->mmhs->type);

		if (!system(xenixcmd)) {
			if ((kill_after_m) and
			   (*port->mmhs->bbs == ' ') or
			   matchn(port->mmhs->bbs, cport->user->call, ln_call))
				kmsg();
			else {			/* AY-881001 */
				port->mmhs->stat = m_fwd;
				wt_mmhs();
			}
		} else {
#ifdef DEBUG
			sprintf(tmp->scr,"mb:Can't do on To:[%s]\n\r", xenixcmd);
			debugmsg(tmp->scr);
#endif
		}
	} else	if ( port->mmhs->unu3 == 0 /* Is this mail to XENIX? */
		and search_file(mail_to, to_file)) {
		unbl(mail_from, port->mmhs->from, ln_call);
		strlwr(mail_from);	/* AY-880510 */
		sprintf(xenixcmd, "%s %s@mb %s %u %c",
				mailer,
				mail_from,
				mail_to,
				mfhs->next_msg - 1,
				port->mmhs->type);

		if (system(xenixcmd))
			debugmsg("\7mb:can't forward mail.\n\r");

	} else	if (mail_at[0])	/* Is this mail distribute to XENIX? */
		if ((port->mmhs->unu3 == 0)
			and (search_file(mail_at, at_file))) {
			unbl(mail_from, port->mmhs->from, ln_call);
			strlwr(mail_from);	/* AY-880510 */
			sprintf(xenixcmd, "%s %s@mb %s %u %c",
					mailer,
					mail_from,
					mail_at,
					mfhs->next_msg - 1,
					port->mmhs->type);

			if (system(xenixcmd)) {
#ifdef DEBUG
				sprintf(tmp->scr, "MB:Can't do on At:[%s]\n\r", xenixcmd);
				debugmsg(tmp->scr);
#endif
			}
	}
#endif
}

search_file(pattern, filename)
char *pattern, *filename;
{
register FILE *fpd;
char buf[80];

	if ((fpd = fopen(filename, "r")) == 0) {
		fprintf(stderr, "\7mb: Can't open %s\n\r", filename);
		return(false);
	}

	while (fgets(buf, sizeof(buf), fpd))
            {
		remnl(buf);
	    if (buf[0] == *pattern)
	    if (strlen(buf) == strlen(pattern))
	    if (!strcmp(buf, pattern))
	    {
                    fclose(fpd);
		    return(true);
             }
	}
	fclose(fpd);
        return(false);
}

/*
 *  SM command, send a messge to a distribution list.
 */
#if 0  /* Delete SM Command  Feb.18,90 */
sndmlt()
{
	register FILE *fl;
	    char fn[80];
	    int i;                 /* AY-60 */

	fill((char *)msgbid, '\0', sizeof(MSG_BID));
#if !DOMAIN_NO					/* AY-880901 */
        domain[0] = bidname[0] = msgbid->bid[0] = msgbid->domain[0] = '\0';
#else
	    bidname[0] = msgbid->bid[0] = '\0';   /* AY-20 */
#endif

	if ((fl = fopen(port->fld[1], "r")) is 0) { 
		nofile(port->fld[1]);
		return;
	}

	prtx(mm[0]);
	DBGMSG((stderr, "sndmlt()\n\r"));
	DB_LOCK(mfl);
	(void)get_title();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		fclose(fl);
		return;
	}
	prtx(mm[1]);
	strncpy(port->mmhs->from, port->user->call, ln_call);
	sprintf(fn, "%s%u", msgdir, mfhs->next_msg);
	uload (fn);
	if (port->mode & gone) {
		DBGMSG((stderr, "sndmlt()\n\r"));
		DB_UNLOCK(mfl);
		fclose(fl);
		return;
	}

	while (fgets(port->line, linelen, fl) isnt NULL) {
		pparse();
		sprintf(port->line, "%s%u", msgdir, mfhs->next_msg);
		copy(fn, port->line);
		port->mmhs->type = port->opt2;
		pcall(port->mmhs->to, port->fld[1]);
		fill(port->mmhs->bbs, ' ', ln_call);


		if (port->flds > 3)  /* AY-60 */
		    for (i = 2; port->flds > i+1; i++) atfrom(i);

		cremsg();
	}
	DBGMSG((stderr, "sndmlt()\n\r"));
	DB_UNLOCK(mfl);
	fclose(fl);
}
#endif   /* Feb.18,90   delele SM  */
/*
 *  M command, make a message from a file.
 */

makmsg()
{
	fill((char *)msgbid, '\0', sizeof(MSG_BID));
#if !DOMAIN_NO			/* AY-880901 */
    domain[0] = bidname[0] = msgbid->bid[0] = msgbid->domain[0] = '\0';
#else
	bidname[0] = msgbid->bid[0] = '\0';   /* AY-20 */
#endif

	XEPARSE();
	if ((port->fl = fopen(port->fld[2], "r")) is 0) { 
		nofile(port->fld[2]);
		return;
	}
	fclose(port->fl);
	DBGMSG((stderr, "makmsg()\n\r"));
	DB_LOCK(mfl);
	SETMFHS();
	(void)pfld();
	if (port->mode & gone) {
		DBGMSG((stderr, "makmsg()\n\r"));
		DB_UNLOCK(mfl);
		fclose(port->fl);
		return;
	}
	sprintf(port->line, "%s%u", msgdir, mfhs->next_msg);

	copy(port->fld[2], port->line);

	port->mmhs->unu3 = 0;
	cremsg();
	DBGMSG((stderr, "makmsg()\n\r"));
	DB_UNLOCK(mfl);
}

#if   0  /*  !MAKMLT_NO  Delete MM Command  Feb.18,90 */
/*
 *  MM command, make multiple messages from a file.
 */

makmlt()
{
	register FILE *dfl;
        int i;    /* AY-60 */
	char tnm[80];

        fill((char *)msgbid, '\0', sizeof(MSG_BID));
#if !DOMAIN_NO			/* AY-880901 */
        domain[0] = bidname[0] = msgbid->bid[0] = msgbid->domain[0] = '\0';
#else
        bidname[0] = msgbid->bid[0] = '\0'; /* AY-20 */
#endif
	port->mmhs->unu3 = 0;  /* true; AY-880111 */

	XEPARSE();
        if ((port->fl = fopen(port->fld[2], "r")) is NULL)
	    { 
		nofile(port->fld[2]); return; 	}
	fclose(port->fl);

        if ((dfl = fopen(port->fld[1], "r")) is NULL)
	    { 
		nofile(port->fld[1]); return; 	}

	strcpy(tnm, port->fld[2]);

        prtx(mm[0]);
	DBGMSG((stderr, "makmlt()\n\r"));
	    DB_LOCK(mfl);
		SETMFHS();
	    (void)get_title();
		if (port->mode & gone) {
                        DBGMSG((stderr, "makmlt()\n\r"));
			DB_UNLOCK(mfl);
			fclose(dfl);
			return;
		}

	while (fgets(port->line, linelen, dfl) isnt NULL)
	    {
                    pparse();
		    port->mmhs->type = port->opt2;
		    pcall(port->mmhs->to, port->fld[1]);
		    strncpy(port->mmhs->from, port->user->call, ln_call);
		    fill(port->mmhs->bbs, ' ', ln_call);

                    if (port->flds > 3)  /* AY-60 */
                        for (i = 2; port->flds > i+1; i++) atfrom(i);

		    sprintf(port->line, "%s%u", msgdir, mfhs->next_msg);
		    copy(tnm, port->line);
		    cremsg();
	}
	fclose(dfl);
	DBGMSG((stderr, "makmlt()\n\r"));
	    DB_UNLOCK(mfl);
}
#endif

dodis()
{
register char *ptr;
char *disfile_arp();
register FILE *dfl;
register int needfwd;		/* AY-880404 */
char call[ln_call + 1];                        /* AY-871011 */
char *strchr();	

	port->mmhs->ext = 0;
	if ((*port->mmhs->bbs is ' ') and (!bidname[0]))  /* AY-871026 */
		return;

	if (ptr = disfile_arp())
		unbl(tmp->scr, ptr, ln_filenam);
	else if (*port->mmhs->bbs isnt ' ')
		unbl(tmp->scr, port->mmhs->bbs, ln_call);  
	else
		strcpy(tmp->scr, "GENERAL");

	strcpy(port->line, msgdir);
	strcat(port->line, tmp->scr);
	if (bidname[0])				/* AY-870930 */
		strcat(port->line, ".BUL");
	else
		strcat(port->line, ".DIS");

	if ((dfl = fopen(port->line, "r")) is 0) {
		if (!bidname[0]) return;
		sprintf(port->line, "%sGENERAL.BUL", msgdir);
		if ((dfl = fopen(port->line, "r")) is 0)
			return;
	}

	port->mmhs->ext = getrec();
	mmes->next = 0;
	mmes->unu1 = 0;
	mmes->unu2 = 0;
	mmes->unu3 = 0;
	needfwd    = false;				/* AY-880404 */
	/* AY-871011 */
	for (mmes->count =  0; (mmes->count < mmesn); mmes->count++) {
		if (fgets(tmp->scr, scrmax, dfl) == NULL) break;
		strupr(tmp->scr);
		pcall(mmes->call[mmes->count], tmp->scr);
		unbl(call, mmes->call[mmes->count], ln_call);	/* AY-871011 */
		strlwr(call);					/* AY-871011 */

		if (isntfirst(call) == false) {	/* AY-881122 */
			mmes->flag[mmes->count] = true;
			needfwd = true;		/* AY-880404 */
		} else {		/* He/She already has this. AY */
			mmes->flag[mmes->count] = false;
		}
	}
	fclose(dfl);

	if (!needfwd) port->mmhs->stat setbit m_fwd;		/* AY-880404 */
	write_rec(mfl, port->mmhs->ext, (char *)mmes);
}

/*
 * search pathlist
 * return false if nocall in pathlist[]
 */

isntfirst(call)		/* AY-881122 */
char call[];
{
char *strchr();
register char *ptr;
register int i, j, k;

	for(i = 0; pathlist[i] != '\0'; i++) {
	for(j=i,k=0; call[k] != '\0' && call[k] == pathlist[j]; j++,k++)
		;

		if (call[k] == '\0')
		if ((pathlist[j] == '!') || (pathlist[j] == '\0')) {
			if (i == 0) return (false);
			return (true);
		}
	}
	return (false);
}

/*
 *  DW command: make wp message from user file.
 */

dwuser()
{
register int i;
register short first = true;
/*      DELETE DW Command & WP Send    Feb18,90 */
    return ;                    /* NO OPERATION */
/*                                               */

#if 0
    if (*wpcall is ' ') return;
    port->opt1 = 'S';
    filesize = 0;

	DBGMSG((stderr, "dwuser()\n\r"));
	DB_LOCK(ufl);
    for (i = 1; i <= ufhs->count; i++)
    {
		read_rec(ufl, i, (char *)tuser);
	    if (!(tuser->state & u_sent)) if (tuser->state & u_home)
	    {
			if (first)
		    {
				first = false;
			    port->mmhs->type = 'P';
			    strncpy(port->mmhs->to, "WP    ", ln_call);
			    strncpy(port->mmhs->from, cport->user->call, ln_call);
			    strncpy(port->mmhs->bbs, wpcall, ln_call);
			    strcpy(port->mmhs->title, "WP Update");
			    sprintf(port->line, "%s%u", msgdir, mfhs->next_msg);
			    port->fl = fopen(port->line, "w");
			}

			filesize += 18;
		    fprintf(port->fl, "%6.6s qth %6.6s\n", tuser->call, tuser->home_bbs);
		    tuser->state setbit u_sent;
		    write_rec(ufl, i, (char *)tuser);
		    if (i is port->user->rn) port->user->state setbit u_sent;
		}
	}
	DBGMSG((stderr, "dwuser()\n\r"));
	DB_UNLOCK(ufl);

	if (!first)
    {
		fclose(port->fl);
#if !DOMAIN_NO                                  /* Nov12,89 */
                domain[0]=bidname[0]=msgbid->bid[0]=msgbid->domain[0]='\0';
#else
                bidname[0] = msgbid->bid[0] = '\0' ;    /* AY-871014 */
#endif
                port->mmhs->unu3 = 0 ;          /* Nov12,89 */
                cremsg();
	}
#endif
}

#if !EXTFWD_NO	/* AY-880829 */
checkpath(s)
char *s;
{
register char *ptr;
char *strchr();

	if (*s != '!') return (0);

	if (strchr(s, ' ')) return (0);

	if (ptr = strchr(s + 1, '!'))
		return(checkpath(ptr));

	*s++ = '\0';
	return (strlen(s) <= ln_call);	
}
#endif

/*
 *  Create message utility.
 *  Used by "send message", "make message from file",
 *  "kill msg" (service messages), "copy message".
 */
cremsg()
{
FILE *mmhfdp;
register PORTS *p;
char call[ln_call + 5];

register char *tmpptr, *pathptr;
char tmptitle[80], *strchr(), *strrchr();

	port->mmhs->rn = getrec();

    port->mmhs->next   = 0;
    port->mmhs->number = mfhs->next_msg++;
	if (port->mode & gone) {
		if (filesize <= 256)				/* AY-881003 */
			return;
		port->mmhs->stat = m_chk;
	} else {
		port->mmhs->stat = 0;
	}
    strncpy(port->mmhs->date, l_date, ln_date);
    strncpy(port->mmhs->time, l_time, ln_time);
    port->mmhs->read = 0;
    port->mmhs->size = filesize;
    port->mmhs->prev = mfhs->last;
    port->mmhs->unu3 = 0;					/* AY-880228 */

	have_header = 0;
        sprintf(port->cmd,"%s%u",msgdir,port->mmhs->number) ;   /* Dec 1,89 */
	if (mmhfdp = fopen(port->cmd, "r"))			/* AY-880127 */
        {
              mmh(mmhfdp, false);
              fclose(mmhfdp);
        }

/* Allow Auto BID ? */
	if ((bidname[0] == '\0') && !have_header)
	if (!mayhave_bid
		&& ((port->mmhs->bbs[0] != ' ') && !iscall(port->mmhs->bbs)))
	{
		unbl(call, cport->user->call, ln_call);
		sprintf(tmp->scr, "%u_%s", port->mmhs->number, call);
		unbl(bidname, tmp->scr, BIDSIZ);
	}
#if	KILLKANJI
	if (port->mmhs->unu3 != 0) {					/* AY-880201 */
		sprintf(tmp->scr, "rm %s ; ln ./warning_msg %s", port->cmd, port->cmd);
		    (void)system(tmp->scr);
		    strncpy(port->mmhs->date, l_date, ln_date);
		    strncpy(port->mmhs->time, l_time, ln_time);
                    port->mmhs->type = 'P';     /* Set this mail to private  AY-880204 */
		port->mmhs->stat = m_read;	/* Set this mail already read. AY-880214 */
		strncpy(port->mmhs->to, port->mmhs->from, ln_call);
		    strncpy(port->mmhs->from, cport->user->call, ln_call);
		    if (bidname[0]) { /* We need to modify bid */
			bidput(bidname);	/* 1st We must save orignal bid to bid.dat */
			bidname[0] = '\0'; /* AY-880201 */
		}
	}
#endif
	if (*port->mmhs->title == '!') {
		if ((*port->mmhs->bbs == ' ')
				|| (matchn(port->mmhs->bbs,
					cport->user->call, ln_call))) {
			strncpy(tmptitle, port->mmhs->title, sizeof(tmptitle));
                        if ( (pathptr = strchr(tmptitle, '#')) != NULL ) {
                                        *pathptr = '\0';
                              /*  strcpy(tmp->scr, tmptitle);  */
                              /*  if (checkpath(tmp->scr)) {   */
#if 0    /* EXT-FWD => H-FWD  Delete   Dec1,89  */
                                        if (domain[0] == '\0') {
						if (strchr(tmp->scr, '!') == strrchr(tmp->scr, '!'))
							strcpy(domain, tmp->scr + 1);
						else
							strcpy(domain, tmp->scr);
						tmpptr = domain;
						while(*tmpptr) {
							if (*tmpptr == '!')
								*tmpptr++ = '.';
							else
								tmpptr++;
						}
					}
#endif
                                        if (strchr(tmptitle, '!') == strrchr(tmptitle, '!')) {
						pcall(port->mmhs->bbs, tmptitle + 1);
						strncpy(port->mmhs->title, pathptr + 1, sizeof(tmptitle));
					} else {
						*pathptr = '#';	/* resume title first */
						pathptr = strchr(tmptitle + 1, '!');
						*pathptr = '\0';
						pcall(port->mmhs->bbs, tmptitle + 1);
						*pathptr = '!';
						strncpy(port->mmhs->title, pathptr, sizeof(tmptitle));
					}
                         /*            strupr(port->mmhs->bbs);      */
                                        unbl (call, port->mmhs->bbs, ln_call ) ;
                                        uc (call ) ;
                                        pcall (port->mmhs->bbs, call, ln_call ) ;
                                        domain[0] = '\0' ;      /* reset H-adr*/
                              /* } */
			}
		}
	}

/*
**  Create the distribution list, if required.
*/
	dodis();

/*
**  If @ BBS is this BBS, then drop the @ BBS.
*/

        if (s_param & s_nobbs)
        if (matchn(port->mmhs->bbs, cport->user->call, ln_call)) {
		fill(port->mmhs->bbs, ' ', ln_call);
                domain[0] = '\0' ;                   /* May.5,90 */
        }
/*
**  Store the BID/Domain Address if required.
*/
	port->mmhs->unu2 = 0;
	if (bidname[0] || domain[0]) {
		msgbid->next = 0;
		msgbid->bid[0] = msgbid->domain[0] = '\0';

		if (bidname[0]) {
			if (!(port->mode & gone))	/* AY-881002 */
				bidput(bidname);
			unbl(msgbid->bid, bidname, BIDSIZ); /* AY-881101 */
		}

		if (domain[0]) {
			unbl(msgbid->domain, domain, sizeof(msgbid->domain)-1);
			domain[0] = '\0';
		}

		if (msgbid->bid[0] || msgbid->domain[0]) {
		    	port->mmhs->unu2 = getrec();
		    	write_rec(mfl, port->mmhs->unu2, (char *)msgbid);
		}
	}

	wt_mmhs();

/*
**  if this is not the first message, point last msg to this one.
*/

	if (mfhs->last) {
		read_rec(mfl, mfhs->last, (char *)tmmhs);
		tmmhs->next = port->mmhs->rn;
		write_rec(mfl, mfhs->last, (char *)tmmhs);
	}

/*
**  Clean up the file header.
*/

	if (!mfhs->first) mfhs->first = port->mmhs->rn;
	mfhs->last = port->mmhs->rn;
	mfhs->count++;
	write_rec(mfl, 0, (char *)mfhs);
#if SHARE
	sync();
#endif
	s_param setbit s_mchange;

        sprintf(port->line, "%u %-.6s %s",     /* d -> u change 5/14 */
			port->mmhs->number,
			port->mmhs->to,
			port->mmhs->title);

	if (bidname[0]) {
		strcat(port->line, " ");
		strcat(port->line, msgbid->bid);
	}
	log('M', port->opt1, port->mmhs->type, port->line);
}

/*
 *  Get number of next available record in mail file.
 *  From free chain if one available, else at end of file.
 */


getrec()
{
register word t;

    if (!mfhs->ffree) return mfhs->next++;

    t = mfhs->ffree;
    read_rec(mfl, t, (char *)tmmhs);
    mfhs->ffree = tmmhs->next;
    mfhs->free--;
    if (!(mfhs->ffree)) mfhs->lfree = 0;
    return t;
}

/*
 *  E command, edit a message header.
 */

edmsg()
{
register int i;  /* AY-871008 */
register int gotnew;	/* AY-880910 */

	DBGMSG((stderr, "edmsg()\n\r"));
	DB_LOCK(mfl);
	SETMFHS();

	gotnew = 0;

	if (!findmsg(atoi(port->fld[1]))) {
		port->msg = mnmsg;
		DB_UNLOCK(mfl);		/* AY-881117 */
		return;
	}

	msgbid->bid[0] = bidname[0] = '\0';
	msgbid->next = 0;

#if !DOMAIN_NO
	domain[0] = msgbid->domain[0] = '\0';
#endif

	if (port->mmhs->unu2)
		read_rec(mfl, port->mmhs->unu2, (char *)msgbid);

	showexp = TRUE; prthdr(true); showexp = 0;

	unbl(tmp->scr, port->mmhs->to, ln_call);
	outstr(tmp->scr);
	prtx(mm[5]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds)
		pcall(port->mmhs->to, port->fld[0]);

	unbl(tmp->scr, port->mmhs->from, ln_call);        /* AY-871008 */
	outstr(tmp->scr);
	prtx(mm[6]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds)
		pcall(port->mmhs->from, port->fld[0]);

	unbl(tmp->scr, port->mmhs->bbs, ln_call);         /* AY-871008 */
	outstr(tmp->scr);
	prtx(mm[7]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds)
		pcall(port->mmhs->bbs, port->fld[0]);
	else
		if (port->line[0] is ' ')
			fill (port->mmhs->bbs, ' ', ln_call);

	prtx(mm[8]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds) {
		remnl(port->line);
		strncpy(port->mmhs->title, port->line, mhtitl);
	} else if (port->line[0] is ' ')
		*port->mmhs->title = '\0';

	outstr("Stat=");            /* AY-871027 */
	switch(port->mmhs->stat) {
	case m_fwd:
		outchar('F'); break;
	case m_read:
		outchar('Y'); break;
#if !EXPFWD_NO	/* AY-880930 */
	case m_chk:
		outchar('C'); break;
	case m_known:
		outchar('K'); break;
	case m_unk:
		outchar('U'); break;
#endif
	default:     
		outchar('N'); break;
	}
	prtx(mm[9]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds)
		switch(*port->fld[0]) {
#if !EXPFWD_NO	/* AY-880930 */
		case 'C' :
			port->mmhs->stat = m_chk;   break;
		case 'K' :
			port->mmhs->stat = m_known;  break;
		case 'U' :
			port->mmhs->stat = m_unk;   break;
#endif
		case 'F' : 
			port->mmhs->stat = m_fwd;  break;
		case 'N' : 
			port->mmhs->stat = 0;      break;
		case 'Y' : 
			port->mmhs->stat = m_read; break;
		}

	outstr("Type=");            /* AY-871010 */
	outchar(port->mmhs->type);
	prtx(mm[10]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds)
		port->mmhs->type = *port->fld[0];
	else if (port->line[0] is ' ')
		port->mmhs->type = ' ';

#if !DOMAIN_NO
	if (port->mmhs->unu2) {
		sprintf(tmp->scr, "Address= %s ", msgbid->domain);
		outstr(tmp->scr);
		getcmd();
		if (port->mode & gone) {
			DB_UNLOCK(mfl);
			return;
		}

		if (port->flds) {
			strcpy(domain, port->fld[0]);
			strcpy(msgbid->domain, domain);
		}
		if (port->line[0] == ' ')
			domain[0] = msgbid->domain[0] = '\0';
        }
#endif
        if (port->mmhs->unu2 && *msgbid->bid) { /* AY-881001 */
                sprintf(tmp->scr, "Bid= %s ", msgbid->bid);
                outstr(tmp->scr);
                getcmd();
                if (port->mode & gone) {
                        DB_UNLOCK(mfl);
                        return;
                }

                if (port->flds) {
                        strcpy(bidname, port->fld[0]);
                        if (bidserch(bidname)) {
                                sprintf(tmp->scr, "N - BID\n");
                                outstr(tmp->scr);
                                DB_UNLOCK(mfl);
                                return;
                        }
                        strcpy(msgbid->bid, port->fld[0]);
                }
                if (port->line[0] == ' ')
                        bidname[0] = msgbid->bid[0] = '\0';
        }        /* moved   May5,1990 */
#if !DOMAIN_NO
        if (port->mmhs->unu2 &&
                 !(msgbid->bid[0] || msgbid->domain[0]))
                        port->mmhs->unu2 = 0;
         else if ((port->mmhs->unu2) and (domain[0] || bidname[0]))
                        write_rec(mfl, port->mmhs->unu2, (char *)msgbid);

#else
        if (!port->mmhs->unu2 && bidname[0]) {
                 port->mmhs->unu2 = getrec();
                 write_rec(mfl, port->mmhs->unu2, (char *)msgbid);
         } else if (port->mmhs->unu2 and bidname[0])
                         write_rec(mfl, port->mmhs->unu2, (char *)msgbid);
                else if (port->mmhs->unu2 && !(msgbid->bid[0]))
                        port->mmhs->unu2 = 0;

#endif

	if (port->mmhs->ext) {
		read_rec(mfl, port->mmhs->ext, (char *)mmes);

		for (i = 0; i < mmes->count; i++) {
			fill(tmp->scr, ' ', ln_call);
			unbl(tmp->scr + 1, mmes->call[i], ln_call);
			if(!(mmes->flag[i]))
				*(tmp->scr) = '*';
			outstr(tmp->scr);
			prtx(mm[9]);
			getcmd();
			if(port->mode & gone) {
				DB_UNLOCK(mfl);
				return;
			}
			if(port->flds) {
				switch(*port->fld[0]) {
				case 'F':
				case 'Y':
					mmes->flag[i] = false;
					break;
				case 'N':
					mmes->flag[i] = true;
					outstr("Call=");
					getcmd();
					if (port->mode & gone) {
						DBGMSG((stderr, "edmsg()\n\r"));
						DB_UNLOCK(mfl);
						return;
					}
					port->mmhs->stat = 0;
					if (port->flds)
						pcall(mmes->call[i], port->fld[0]);
					break;
				}
			}
		}
		if (bidname[0])
			bidput(bidname);
		bidname[0] = '\0';				/* AY-880622 */
		write_rec(mfl, port->mmhs->ext, (char *)mmes);
	}                /* End of AY-871008 */

/* TEST TEST TEST */
	if (gotnew)
		write_rec(mfl, 0, (char *)mfhs);

	showexp = 1; prthdr(false); showexp = 0;
	wt_mmhs();
	DB_UNLOCK(mfl);
	s_param setbit s_mchange;
}

/*
 *  ET command, edit an NTS traffic message header.
 */

edtfc()
{
register short ok;

	DBGMSG((stderr, "edtfc()\n\r"));
	DB_LOCK(mfl);
	if (!findmsg(atoi(port->fld[1]))) {
		port->msg = mnmsg;
		DB_UNLOCK(mfl);
		return;
	}

	prthdr(true);

	ok = (port->mmhs->type is 'T');
	if (!ok)
		ok = ((port->mmhs->type is ' ')
			and matchn(port->mmhs->to, "NTS", 3));

	if (!ok) {
		port->msg = mm[18];
		DB_UNLOCK(mfl);
		return;
	}

	prtx(mm[14]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds) pcall(port->mmhs->to, port->fld[0]);

	prtx(mm[15]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds)
		pcall(port->mmhs->bbs, port->fld[0]);
	else if (port->line[0] is ' ')
		fill (port->mmhs->bbs, ' ', ln_call);

	prtx(mm[16]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds) {
		remnl(port->line);
		strncpy(port->mmhs->title, port->line, mhtitl);
	} else if (port->line[0] is ' ')
		*port->mmhs->title = '\0';

	prtx(mm[17]);
	getcmd();
	if (port->mode & gone) {
		DB_UNLOCK(mfl);
		return;
	}
	if (port->flds)
		port->mmhs->type = *port->fld[0];
	else if (port->line[0] is ' ')
		port->mmhs->type = ' ';

	port->mmhs->stat = 0;
	prthdr(false);
	wt_mmhs();
	DB_UNLOCK(mfl);
        sprintf(port->line, "%u %-.6s %s",  /* d -> u change 5/14 */
	port->mmhs->number, port->mmhs->to, port->mmhs->title);
	log('M', 'E', port->opt2, port->line);
	s_param setbit s_mchange;
}

#if !SWAP_NO	/* AY-880920 */
swap_to()
{
register TO_SWAP *to_swaptmp;

	for (to_swaptmp = to_swaproot; to_swaptmp; to_swaptmp = to_swaptmp->next) {
		if (wcm(to_swaptmp->to, port->mmhs->to)) {
			pcall(port->mmhs->to, to_swaptmp->new_to);
			break;
		}
	}
}

swap_bbs()
{
register AT_SWAP *at_swaptmp;

	for (at_swaptmp = at_swaproot; at_swaptmp; at_swaptmp = at_swaptmp->next) {
		if (wcm(at_swaptmp->at, port->mmhs->bbs)) {
                        domain[0] = '\0' ; /* reset Domain address  Feb.18,90*/
                        if (*at_swaptmp->new_at is '@')
                              fill (port->mmhs->bbs,' ',ln_call) ;
                        else
                              pcall(port->mmhs->bbs, at_swaptmp->new_at);
			break;
		}
	}
}

static char *disfile_arp()
{
register DISARP *disarp_tmp;

	for (disarp_tmp = disarproot;
		disarp_tmp;
		disarp_tmp = disarp_tmp->next) {

/*        if (wcm(port->mmhs->to, disarp_tmp->to))
        if (wcm(port->mmhs->bbs, disarp_tmp->at)) {   **change 5/13 */
        if (wcm (disarp_tmp->to, port->mmhs->to ))
        if (wcm (disarp_tmp->at, port->mmhs->bbs )) {
			return(disarp_tmp->disfile);
		}
	}
	return (NULL);
}
#endif
