[comp.sources.misc] uupc.uu.8.2

Stuart.Lynne@van-bc.UUCP (Stuart Lynne) (08/17/87)

>From uucp Wed Aug 12 02:45 PDT 1987
>From slynne  Wed Aug 12 02:45:18 1987 remote from slmac
Received: by van-bc.uucp (smail2.3)
	id AA17641; 12 Aug 87 02:45:18 PDT (Wed)
Received: by slmac.vnet.van-bc.uucp (pcmail) Wed Aug 12 02:40:31 1987
Date: Wed Aug 12 02:40:31 1987
From: Stuart Lynne - test a mac <slynne@slmac.vnet.van-bc.uucp>
Message-ID: <153@slmac.vnet.van-bc.uucp>
To: sl@van-bc
Subject: shar/uupc.uu.8.2

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	host.h
#	lib.c
#	lmail.c
#	mail.c
#	mailhost.c
#	mlib.c
#	ndir.h
#	pcmail.c
#	rmail.c
#	ulib.c
#	uuhost.c
#	systems
# This archive created: Wed Aug 12 02:02:15 1987
# By:	Stuart Lynne - test a mac ()
export PATH; PATH=/bin:$PATH
if test -f 'host.h'
then
       echo shar: will not over-write existing file "'host.h'"
else
cat << \SHAR_EOF > 'host.h'
/*		host.h

*/

#include "local/host.h"

SHAR_EOF
chmod +x 'host.h'
fi # end of overwriting check
if test -f 'lib.c'
then
       echo shar: will not over-write existing file "'lib.c'"
else
cat << \SHAR_EOF > 'lib.c'
/* 		lib.c

*/

#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include "host.h"

#ifndef NULL
#define NULL 0L
#endif

char *index();
char *rindex();

MKDIR( path )
char * path;
{
	char * cp = path;

	if ( *cp == '\0' )
		return( 0 );

	/* see if we need to make any intermediate directories */
	while ( ( cp = index( cp, '/' ) ) != (char *) NULL ) {
		*cp = '\0';
		mkdir( path );
		*cp = '/';
		cp++;
	}

	/* make last dir */
	return( mkdir( path ) );

}

CHDIR( path )
char * path;
{
	char * cp = path;

	if ( *cp == '\0' )
		return( 0 );

	MKDIR( path );

	/* change to last directory */
	return( chdir( path ) );

}

FILE * FOPEN( name, mode, ftype )
char * name;
char * mode;
char ftype;
{

	char * last;
	FILE * results;

	/* are we opening for write or append */

	FILEMODE( ftype );
	results = fopen( name, mode );

	if ( results != (FILE *) NULL || *mode == 'r' )
		return( results );

	/* are we opening in sub-directory */
	last = rindex( name, '/' );

	/* lets just verify that all sub-dir's exist */
	if ( last != (char *) NULL ) {
		*last = '\0';
		MKDIR( name );
		*last = '/';
	}

	/* now try open again */
	return( fopen( name, mode ));

}

int CREAT( name, mode, ftyp )
char * name;
int mode;
char ftyp;
{

	char * last;
	int results;

	/* are we opening for write or append */
	FILEMODE( ftyp );
	results = creat( name, mode );

	if ( results != -1 )
		return( results );

	/* are we opening in sub-directory */
	last = rindex( name, '/' );

	/* lets just verify that all sub-dir's exist */
	if ( last != (char *) NULL ) {
		*last = '\0';
		MKDIR( name );
		*last = '/';
	}

	/* now try open again */
	return( creat( name, mode ) );

}

extern FILE *logfile;
extern int debuglevel;
extern int remote;

#define MASTER 1


int getargs( line, flds )
char *line;
char **flds;
{
	int i = 0;
	char *s;

	while ( (*line != '\0') && (*line != '\n') )
	{
	   if ( isspace(*line) )
	   {
	      line++;
	      continue;
	   }
	   *flds++ = line;
	   i++;
	   while( (isspace(*line) == 0) && (*line != '\0') ) line++;
	   if (isspace(*line)) *line++ = '\0';
	}
	return(i);
}



SHAR_EOF
chmod +x 'lib.c'
fi # end of overwriting check
if test -f 'lmail.c'
then
       echo shar: will not over-write existing file "'lmail.c'"
else
cat << \SHAR_EOF > 'lmail.c'
/*			lmail.c

*/

#define NOMAIN
/* #define RMAIL /* if defined we get rmail() else we get lmail() */

#include	"pcmail.c"


SHAR_EOF
chmod +x 'lmail.c'
fi # end of overwriting check
if test -f 'mail.c'
then
       echo shar: will not over-write existing file "'mail.c'"
else
cat << \SHAR_EOF > 'mail.c'
/*
 *	mail.c
 */

#define	exit	return

#include <stdio.h>
#include "host.h"

FILE *FOPEN();
FILE *freopen();

int	debuglevel = 1;

#define LSIZE   	256

char	line[LSIZE];
char	resp[LSIZE];

char	mfilename[132];

struct ldesc {
   int   	delete;		/* status of this message */
   long		adr;		/* address of From line */
   long		date;		/* address of Date: line */
   long		subject;	/* address of Subject: line */
   long		from;		/* address of From: line */
   long		size;		/* number of lines */
};

struct	ldesc letters[300];

int   	letternum   = 0;
char   	thefilename[50];

char   	tmailbox[64];
char 	tmailbag[64];


FILE   	*fmailbox;
FILE 	*rmailbox;
FILE	*fmailbag;

int	error = 0;
int	modified;

int	printonly;

int	PageCount = 0;

mailmain(argc, argv)
char **argv;
{

	/* HOSTINIT;
	loadenv(); */

	mkfilename( tmailbox, tempdir, "mailbox");
	mkfilename( tmailbag, tempdir, "mailbag");

	unlink( tmailbox );

	if ( mailbox == (char *)NULL )
		mailbox = "???";

	if ( argc == 1 ||
		( argv[1][0] == '-' ) && ( argv[1][1] != 's' ))
		showmail (argc, argv );
	else
		lmail (argc, argv );

	finis();

}

finis()
{
	if ( fmailbag != (FILE *)NULL) fclose( fmailbag );
	unlink( tmailbox );
	unlink( tmailbag );
	exit( error );
}

/*	sendmail
	send message n to "line"
*/
sendmail( n, line )
{
	char 	*argv[50];
	int 	argc;
	char	buf[BUFSIZ];
	long 	bytes;

	/* open a temporary file */
	fmailbag = FOPEN( tmailbag, "w", 'b' );

	/* copy nth message to mailbag file */
	fseek( fmailbox, letters[ n ].adr, 0 );
	bytes = letters[ n+1 ].adr - letters[ n ].adr;

	while( bytes > 0 && fgets( buf, BUFSIZ, fmailbox) ) {
		fputs( buf, fmailbag  );
		bytes -= strlen( buf );
	}

	fclose( fmailbag );

	/* use mailbag as stdin to mail delivery */
	fclose( stdin );
	FILEMODE( 'b' );
	if ( freopen( tmailbag, "r", stdin ) != (FILE *)NULL ) {
		argc = getargs( line, argv );

		lmail( argc, argv );

		fclose( stdin );
	}
	
#ifdef MSDOS
	FILEMODE( 't' );
	freopen( CONSOLE, "r", stdin );
#else
	freopen( device, "r", stdin );
#endif

	/* get rid of mailbag */
	unlink( tmailbag );
}


invert( i )
{
	return( letternum - i - 1 );
}

showmail(argc, argv)
char **argv;
{
	int flg, i, j, k, print;
	char *p, *cp, *getnext();
	long nextadr;
	struct ldesc *letter;

	/* get mailbox file name */
	mkfilename( mfilename, maildir, mailbox );

	/* parse arg's */
	for (; argc > 1; argv++, argc--) {
	   if ( argv[1][0] == '-' ) {
	      if ( argv[1][1] == 'f' ) {
	         if ( argc >= 3) {
	            if ( argv[2][0] == '=' )
	               mkfilename( mfilename, maildir, &argv[2][1] );
	            else
	               strcpy( mfilename, argv[2] );
	         argv++;
	         argc--;
	         }

	      } else if ( argv[1][1 ]== 'p' ) printonly++;
	   else {
	      fprintf(stderr, "mail: unknown option %c\n", argv[1][1]);
	      finis();
	   }
	   } else break;
	}



	/* open real mailbox file */
	if (( rmailbox = FOPEN( mfilename, "r", 'b' ))  == (FILE *)NULL) {
		fprintf( stdout, "No mail in %s.\n", mfilename );
		return;
	}

	/* open up tmp mailbox file */
  	if (( fmailbox = FOPEN( tmailbox, "w", 'b' )) == (FILE *)NULL ) {
		fprintf( stderr, "mail: cannot open %s for writing\n", tmailbox );
		finis();
	}


	/* copy real mailbox file to tmp mailbox file */
	letternum = 0 ;
	nextadr = 0;
	while ( fgets(line, LSIZE, rmailbox ) != (char *)NULL) {
		fputs( line, fmailbox );

		if ( strncmp( line, "From ", 5 ) == SAME ) {
			letter = &letters[letternum++];
			letter->from =
			letter->subject =
			letter->date = -1L;
			letter->adr = nextadr;
			letter->delete = FALSE;
			letter->size = 0L;
		}else if ( strncmp( line, "Date: ", 6 ) == SAME )
			letter->date = nextadr;
		else if ( strncmp( line, "From: ", 6 ) == SAME )
			letter->from = nextadr;
		else if ( strncmp( line, "Subject: ", 9 ) == SAME )
			letter->subject = nextadr;

		letter->size += 1L;
		nextadr += strlen( line );
	}

	letters[letternum].adr = nextadr;   /* last plus 1 */

	/* close mailbox files */
	fclose( rmailbox );
	fclose( fmailbox );

	fmailbox = FOPEN( tmailbox, "r", 'b' );

	modified = 0;

	if ( printonly ) {
		j = letternum;
		while ( j > 0 ) {
			pager( j );
			j--;
		}

		return;
     }


	printsub( -1 );

	i = letternum - 1;
	while ( TRUE ) {

		if ( i < 0 )
			i = 0;
		if ( i >= letternum)
			i = letternum - 1;
		j = invert( i );

		fprintf( stdout, "%d ? ", invert( i ) );

		if ( fgets( resp, LSIZE, stdin) == (char *)NULL )
			break;

		switch ( resp[0] ) {
		default:
			fprintf(stderr, "usage\n");
		case '?':
			print = 0;
			fprintf( stderr, "q\tquit\n" );
			fprintf( stderr, "x\texit\tmailbox restored\n" );
			fprintf( stderr, "p\tprint message\n" );
			fprintf( stderr, "s[file]\tsave message to a file (default =mbox)\n" );
			fprintf( stderr, "w[file]\ttsave message to a file (default =mbox) without header\n" );
			fprintf( stderr, "-\tprint last message\n" );
			fprintf( stderr, "d\tdelete current message\n" );
			fprintf( stderr, "+\tnext message (no delete)\n" );
			fprintf( stderr, "f user\tforward current message to user\n" );
			fprintf( stderr, "m user\tmail a message to user\n" );
			fprintf( stderr, "g num\t set current message\n" );
			break;
		case '+':
		case 'n':
			i-- ;
			break;
		case 'g':
			i = invert( atoi( resp+2 ) );
			break;
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			i = invert( atoi( resp ) );
			break;

		case 'x':
			modified = 0;
		case 'q':
			goto donep;
		case '\n':
		case 'p':
			pager( j );
			break;
		case '^':
		case '-':
			i++;
			break;
		case 'y':
		case 'w':
		case 's':
			flg = 0;
			if (resp[1] != '\n' && resp[1] != ' ') {
				printf("illegal\n");
				flg++;
				print = 0;
				continue;
			}
			if (strlen( resp ) == 1)
				strcat( resp, " =mbox" );
			for (p = resp+1; (p = getnext( &cp, p )) != (char *)NULL; ) {
			   if ( *cp == '=' )
	                      mkfilename( thefilename, maildir, ++cp );
			   else
			      strcpy( thefilename, cp );
			   fprintf( stderr, "%s to %s\n",
			   		(resp[0] == 's') ? "Appending":"Writing", thefilename );
			   if (( fmailbag=FOPEN( thefilename, "a", 't' )) == (FILE *)NULL ) {
			      fprintf( stdout, "mail: cannot append to %s\n",
			               thefilename );
			   flg++;
			   continue;
			   }
			   copymsg( j, fmailbag, resp[0]=='w' );
			   fclose( fmailbag );
			}
			if (flg)
				print = 0;
			else {
				letters[j].delete = TRUE;
				modified++;
				i--;
			}
			break;
		case 'm':
			fprintf( stderr, "mail command not implemented yet!\n" );
			break;
		case 'f':
			flg = 0;
			if (resp[1] == '\n' || resp[1] == '\0') {
				i++;
				continue;
			}
			if (resp[1] != ' ') {
				printf( "invalid command\n" );
				flg++;
				print = 0;
				continue;
			}

			sendmail( j, p );

			if (flg)
				print = 0;
			else {
				letters[j].delete = TRUE;
				modified++;
				i++;
			}
			break;

		case 'd':
			letters[j].delete = TRUE;
			modified++;
			i--;
			if (resp[1] == 'q')
				goto donep;
			break;

		case 'h':
			printsub( -1 );
			break;

		case 'H':
			printsub( i );
			break;

		}

	}
	donep:
	if ( modified )
		copyback();
}

readaline( adr, line )
long adr;
char *line;
{
	char 	buf[132];
	char	*cp;

	*line = '\0';
	if ( adr != -1L )
		if ( fseek( fmailbox, adr, 0 ) == 0 )
			if ( fgets( buf, 132, fmailbox ) != (char *)NULL )
				strcpy( line, buf );

	if ( (cp = index( line, '\n' ) ) != (char *) NULL)
		*cp = '\0';
}


/*
returnaddress()
{
	readaline( lp->from, line );
	if (
		( i = index( line, '<' )) != 0 &&
		( k = index( line, '>')) != 0 &&
		( k > i )
		)
	{
		k -= i;
		strncpy( from, line[i], k );
		from[k] = '\0';
	}
	else {


	}
}
*/

printsub( K )
{
	struct ldesc	*ld;
	int		j;
	char	from[132], subject[132], date[132], line[132];
	register char	*sp, *dp, *lp, *tp;
	int k, mink, maxk;

	pagereset();

	if ( K == -1 ) {
		maxk = letternum -1;
		mink = 0;
	}
	else
		maxk = mink = K;

	for ( k = maxk ; k >= mink ; k-- ) {

		j = invert( k );
		ld = & letters[ j /*k*/];

 		(void)strcpy(from, "unknown");	/* default to "unknown" */
		readaline( ld->from, line );
		if ( strlen( line ) > 0 ) {
			/* Assume From: mailbox@site.domain */
			/* get first usable stuff after From: */
			sp = index( line, ':' ) + 1;

			/* do we have From: mailbox@site.domain ( Proper Name ) */
			if ( ( ( tp = index( line, '(' )) != (char *) NULL &&
					( lp = index( line, ')')) != (char *) NULL  ) )
				sp = ++tp;
			/* or we may have From: Proper Name < mailbox@site.domain > */
			else
				lp = index( line, '<');

			if ( *lp == '<' || *lp == ')' )
				--lp;

			/* if we didn't get either of the last two, just grab line */
			if ( lp < sp || lp == (char *) NULL )
				lp = line + strlen( line ) - 1;

			/*debugMsg("\PCheck isspace *sp");*/
			while ( isspace( *sp ) ) sp++;

			/*debugMsg("\PCheck isspace *lp");*/
			while ( isspace( *lp ) ) lp--;

			dp = from;
			while ( sp <= lp ) *dp++ = *sp++;
			*dp = '\0';
		}

		/* Date: Wed May 13 23:59:53 1987 */
 		*date = '\0';	/* default date to null */
		readaline( ld->date, date );
		if ( strlen( date ) > 0 ) {
			sscanf( date, "%*s %*s %s %s", line, subject );
			sprintf( date, "%s %s", line, subject );
		}

		strcpy( subject, "--- no subject ---" );
		readaline( ld->subject, line );
		if ( strlen( line ) > 0 ) {
			sp = line;
			while ( !isspace( *sp ) ) sp++;
			while ( isspace( *sp ) ) sp++;
			strcpy( subject, sp );
			}

		from[20] = '\0';
		date[6] = '\0';
		subject[35] = '\0';

		sprintf( line, "%3d  %6s  %20s %35s (%6d)\n",
						j, date, from, subject, ld->size );
		if ( pageline( line ) == TRUE )
			break;
	}
}


copyback()
{
	register i, n, c;
	int new = 0;

	if (( fmailbag = FOPEN( mfilename, "w", 'b' )) == (FILE *)NULL ) {
		fprintf (stderr, "mail: can't rewrite %s\n", mfilename );
		finis();
	}
	n = 0;
	for ( i = 0; i < letternum; i++ )
		if ( letters[i].delete == FALSE ) {
			copymsg( i, fmailbag, FALSE );
			n++;
		}
	fclose (fmailbag );
}


/* copy a message
   if noheaders true, don't copy header lines
*/
copymsg( n, f, noheaders )
FILE *f;
{
	long 	bytes;
	char	buf[BUFSIZ];

	fseek( fmailbox, letters[n].adr, 0 );
	bytes = letters[n+1].adr - letters[n].adr;

	while ( bytes > 0 && fgets( buf, BUFSIZ, fmailbox ) != (char *)NULL ) {
		bytes -= strlen( buf );

		/* write line */
		if ( !noheaders  )
			fputs( buf, f );

		/* reset header */
		if ( noheaders  && strcmp( buf, "\n") == SAME )
			noheaders  = FALSE;

	}
}

pager(n)
{
	long 	bytes;
	char	buf[BUFSIZ];

	fseek( fmailbox, letters[n].adr, 0 );
	bytes = letters[n+1].adr - letters[n].adr;

	pagereset();
	while ( bytes > 0 && fgets( buf, BUFSIZ, fmailbox ) != (char *)NULL ) {
		bytes -= strlen( buf );
		if ( pageline( buf ) == TRUE )
			break;
	}

	pageline( "\n" );
}

pagereset()
{
	PageCount = 0;
}

pageline( s )
char * s;
{
	char c;

	fputs( s, stdout );
	if ( ++PageCount > 24 ) {
		PageCount = 0;
		c = get_one();
		switch( c ) {
		case 'q':
		case 'Q':
		case '\003':
		case 'x':
		case 'X':
			return( TRUE );
		}
	}
	return( FALSE );
}

char *getnext( s, p )
register char **s, *p;
{
	*s = (char *)NULL;
	while ( *p == ' ' || *p == '\t' )
		p++;
	if ( *p == '\n' || *p == '\0' )
		return( (char *)NULL );
	*s = p;
	while ( *p != ' ' && *p != '\t' && *p != '\n' && *p != '\0' )
		*p++;
	if ( *p != '\0' )
		*p++ = '\0';
	return(p);
}




SHAR_EOF
chmod +x 'mail.c'
fi # end of overwriting check
if test -f 'mailhost.c'
then
       echo shar: will not over-write existing file "'mailhost.c'"
else
cat << \SHAR_EOF > 'mailhost.c'
/*		mailhost.c

*/
#define	MAIN	mailmain
#include	"local/host.c"

SHAR_EOF
chmod +x 'mailhost.c'
fi # end of overwriting check
if test -f 'mlib.c'
then
       echo shar: will not over-write existing file "'mlib.c'"
else
cat << \SHAR_EOF > 'mlib.c'
/* 		mlib.c

*/

#include	"local/mlib.c"

SHAR_EOF
chmod +x 'mlib.c'
fi # end of overwriting check
if test -f 'ndir.h'
then
       echo shar: will not over-write existing file "'ndir.h'"
else
cat << \SHAR_EOF > 'ndir.h'
/* @(#)ndir.h	1.4	4/16/85 */
#include "local/ndir.h"


SHAR_EOF
fi # end of overwriting check
if test -f 'pcmail.c'
then
       echo shar: will not over-write existing file "'pcmail.c'"
else
cat << \SHAR_EOF > 'pcmail.c'
/* 		pcmail.c



		copyright (C) 1987 Stuart Lynne

		Copying and use of this program are controlled by the terms of the
		Free Software Foundations GNU Emacs General Public License.


		version		0.1		March 31/1987


pcmail

		pcmail address1 address2 ... < the.message

description

	An 822 compatible (hopefully) mail delivery system for pc's.

	Designed for delivering mail on a pc based system. It will put
	local mail (ie, not @ or ! in address) into files in the default
	mail directory.

	If remote it will put into an outgoing mailbag in the default mail
	directory. Performs a simple bundling of mail messages into one
	file with arguments prepended as To: arg header lines. And adds a
	Message-Lines: header which gives the number of lines in the
	content part of the message (after the first blank line).

		pcmail john jack jill@xyz.uucp < afile

		To: john
		To: jack
		To: jill@xyz.uucp
		X-Message-Lines: ?????
		Content-Length: ?????

		...
		...
		...

	Content-Length: is used without X- prepended to be compatible with AT&T
	Mail bundles. This is not 822 compatible per se, but is allowed.

	It also adds the from From and Date lines. Subject: lines may be inserted
	by placing them at the beginning of the message.

	A Unix version should lock the /usr/mail/mailbag file.

	Another program called rpcmail will unbundle the files created by
	pcmail and deliver each message to the local rmail. So conceptually

		(pcmail ..... < ...; pcmail .... < ...) | sz -> rz | rpcmail

	would deliver remote messages intact.

environment variables

	The following evironment variables are used:

		MAILBOX		current user's mailbox, 	"stuart"
		NAME		current user's name, 		"Stuart Lynne"
		DOMAIN		domain of this machine, 	"slynne.mac.van-bc.can"
		MAILDIR		where is mail kept,			"mpw:mail"

compiling

	Compiled by itself it will operate as a standalone program. If the
	compiler option:

		-DNOMAIN

	is used, it will compile as a routine:

		pcmail (argc, argv)
		char **argv;
		int argc;

	and can be used internally in other programs.


Customization

	PCMAIL		mailbag for remote mail
	FAKEUUX		emulate uux, make appropriate files in SPOOLDIR

	RMAIL		rmail

	DEBUG1		first level Debug trace


*/

/*
#ifdef NOMAIN
#include "dcp.h"
#else
*/
#include <stdio.h>
#include "host.h"
/*
#endif
*/

FILE *FOPEN();

#define FORWARD		"Forward to"


#define	SBUFSIZ		124

FILE   *mailfile;
FILE   *tempfile;

char	buf[BUFSIZ];
char	miscbuff[100];
long int lines = 0;
long int bytes = 0;
long int sequence = 0;

long tloc;
char chartime[26];	/* current time in characters */
char *thetime;

char	tfilename[100];
char	mfilename[100];
char	mailsent[100];


int local = TRUE;

char 	remotes[BUFSIZ];


char uucp[] = "uucp";

char *fgets();
int fputs();

#ifdef NOMAIN

#ifdef RMAIL
#define main	rmail
#else
#define	main	lmail
#endif

#define exit	return

extern int debuglevel;

#else
int debuglevel;
#endif

#ifndef RMAIL
char	Subject[132] = "";
#endif

char *mcurdir;
char s_mcurdir[128];	/* current directory path (save malloc call) */
char * getcwd();
int chdir();



main(argc, argv)
char *argv[];
{
	long int position;
	register int header = 1;
	register int amount;

	register int argcount;
	register char **argvec;
	int remote;
	int s1, s2;




#ifndef NOMAIN
	/* get environment var's */
	HOSTINIT;
	loadenv();

 	if (argc <= 1) {
		fprintf( stderr, "pcmail usage: pcmail addresses < message\n" );
		exit(1);
	}
	debuglevel = 1;

#endif
#ifdef RMAIL
	local = FALSE;
#else
	local = TRUE;
#endif

	if ( debuglevel > 5 ) {
		fprintf( stderr, "pcmail: argc %d ", argc );
		argcount = argc;
		argvec = argv;
		while (argcount--) {
			fprintf( stderr, " \"%s\"", *argvec++ );
			}
		fprintf( stderr, "\n" );

		tloc = time( (long *)NULL );
		thetime = ctime(&tloc);
		fprintf( stderr, "thetime: %s\n",thetime );
	}

#ifdef MSDOS
 	mcurdir = getcwd( s_mcurdir, sizeof(s_mcurdir) );
#else
	mcurdir = getcwd( s_mcurdir, 0 );
#endif
	chdir( spooldir );

	/* get sequence number */
	mkfilename( tfilename, confdir, SFILENAME );

 	if ( debuglevel > 4 )
 	   fprintf( stderr, "pcmail: opening %s\n", tfilename ); /* */

	tempfile = FOPEN( tfilename, "r", 't' );
	if (tempfile != (FILE *)NULL) {
		fscanf( tempfile, "%ld", &sequence );
		fclose( tempfile );
	}
 	else {
 		fprintf( stderr, "pcmail: can't find %s file, creating\n",
 					tfilename );
 		sequence = 1;	/* start at 1 */
 	};

	/* update sequence number */
	if ( debuglevel > 5 )
		fprintf( stderr, "pcmail: new sequence # %ld\n", sequence );

	tempfile = FOPEN( tfilename, "w", 't' );
	if (tempfile != (FILE *)NULL) {
		fprintf( tempfile, "%ld\n", sequence+1 );
		fclose( tempfile );
	}

	/* open a temporary file */
	/* sprintf( tfilename, TFILENAME, sequence ); */
 	sprintf( miscbuff, TFILENAME, sequence );
 	mkfilename( tfilename, tempdir, miscbuff );

 	if ( debuglevel > 5 )
 		fprintf( stderr, "pcmail: opening %s\n", tfilename );

	tempfile = FOPEN( tfilename, "w", 'b' );
 	if (tempfile == (FILE *)NULL)  {
		fprintf( stderr, "pcmail: can't open %s\n", tfilename );
		exit(1);
	}

	/* copy stdin to tempfile, counting content lines and bytes */
	header = 1;
	while (fgets( buf, 512, stdin ) != (char *)NULL) {
		if (header != 0) {
			if (strlen( buf ) == 1) {
				header = 0;
				fprintf( tempfile, "\n" );
				continue;
			}
			else if (strchr( buf, ':' ) == NULL) {
				header = 0;
				fprintf( tempfile, "\n" );
			}
		}
		if (header == 0) {
			lines++;
			bytes += strlen( buf );
		}
		fputs( buf, tempfile );
	}
#ifndef RMAIL
	/* copy stdin to tempfile, counting content lines and bytes */
	/* get signature */
	mkfilename( mfilename, home, SIGFILE );

	if (debuglevel > 4)
 	   fprintf( stderr, "pcmail: opening sigfile %s\n", mfilename );
	mailfile = FOPEN( mfilename, "r", 't' );
	if (mailfile != (FILE *)NULL) {
		fputs( "\n--\n", tempfile );
		while (fgets( buf, 512, mailfile ) != (char *)NULL) {
			lines++;
			bytes += strlen( buf );
			fputs( buf, tempfile );
		}
		fclose( mailfile );
 	}
#endif

 	fclose( tempfile );

	if ( debuglevel > 4 ) {
		fprintf( stderr, "pcmail: stdin copied to tmp %ld %ld\n",
	                 bytes, lines );
 		fprintf( stderr, "pcmail: args %d\n", argc );
	}

	/* loop on args, copying to appropriate postbox,
	   do remote only once
	   remote checking is done empirically, could be better
	*/
	remotes[0] = '\0';


#ifndef RMAIL
	if ( strcmp( argv[1], "-s" ) == SAME ) {
		argv++;argv++;
		argc--;argc--;
		if ( argc == 0 )
			return( -1 );
		strcpy( Subject, *argv );
		}
#endif
	argcount = argc;
	argvec = argv;

	while (--argcount > 0) {
		argvec++;
		if ( debuglevel > 5 )
			fprintf( stderr, "pcmail: arg# %d\ %s\n",
							argcount, *argvec );

		if (
			(strchr( *argvec, '!' ) != SAME) ||
			(strchr( *argvec, '@' ) != SAME) ||
			(strchr( *argvec, '%' ) != SAME)
		   ) {
			if ( debuglevel > 5 )
				fprintf( stderr, "pcmail: send to remote\n" );

			s1 = strlen( remotes );
			s2 = strlen( *argvec );

			/* can we cram one more address on line */
			if ( s1 > 0 && (s1 + s2 + 1) > 128 ) {
				/* dump it then, to bad */
				sendone( argc, argv, remotes, TRUE );
				remotes[0] = '\0';
			}

			/* add *arvgvec to list of remotes */
			strcat( remotes, " " );
			strcat( remotes, *argvec );

		}
		else {
			if ( debuglevel > 5 )
				fprintf( stderr, "pcmail: calling sendone %s\n",
							*argvec );

			sendone( argc, argv, *argvec, FALSE );
		}

	}
	/* dump remotes if necessary */
	if ( strlen( remotes ) > 0 )
		sendone( argc, argv, remotes, TRUE );

#ifndef RMAIL

	mkfilename( mailsent, home, COPYFILE );
	if ( debuglevel > 4 )
	   fprintf( stderr, "pcmail: copfile = %s\n", mailsent );
	sendone( argc, argv, mailsent, FALSE );

#endif

	unlink( tfilename );
	chdir( mcurdir );
	exit(0);
}


char fpat1[] = "%c.%.7s%04ld";
char fpat2[] = "S %s %s %s - %s 0666 %s";


/* sendone copies file plus headers to appropriate postbox
   NB. we do headers here to allow flexibility later, for example
   in being able to do bcc, per host service processing etc.
*/
sendone( argc, argv, address, remote )
char **argv;
int argc;
char *address;
{
	register char 	*cp;
	char	icfilename[32];		/* local C. copy file */
	char	ixfilename[32];		/* local X. xqt file */
	char	idfilename[32];		/* local D. data file */
	char	rxfilename[32];		/* remote X. xqt file */
	char	rdfilename[32];		/* remote D. data file */
 	char	tmfilename[32];		/* temporary storage */
#if MSDOS
 	char	cixfilename[32];		/* canonical ixfilename */
 	char	cidfilename[32];		/* canonical idfilename */
#endif
 	if ( remote ) {
 		/* sprintf all required file names */
		sprintf( tmfilename, fpat1, 'C', mailserv, sequence );
		importpath( icfilename, tmfilename );
#ifdef MSDOS
 		sprintf( cidfilename, fpat1, 'D', mailserv, sequence );
		importpath( idfilename, cidfilename );
		sprintf( cixfilename, fpat1, 'D', nodename, sequence );
 		importpath( ixfilename, cixfilename );
#else
		sprintf( tmfilename, fpat1, 'D', mailserv, sequence );
		importpath( idfilename, tmfilename );
		sprintf( tmfilename, fpat1, 'D', nodename, sequence );
		importpath( ixfilename, tmfilename );
#endif
		sprintf( rdfilename, fpat1, 'D', nodename, sequence );
		sprintf( rxfilename, fpat1, 'X', nodename, sequence );
 	}
 	else {
		/* postbox file name */
 		if ( index( address, SEPCHAR ) == (char *)NULL )
			mkfilename( idfilename, maildir, address );
		else
			strcpy( idfilename, address );
	}

	if ( debuglevel > 5 )
 		fprintf( stderr, "pcmail: sendone: %s\n", idfilename );

 	if ( remote == FALSE ) {
 	   if ( debuglevel > 5 )
 	      fprintf( stderr, "pcmail: sendone: check for remote\n" );
		/* check for forwarding */
		if ( (mailfile = FOPEN( idfilename, "r", 'b' )) != (FILE *)NULL ) {
			cp = fgets( buf, BUFSIZ, mailfile );
			fclose( mailfile );
			if (cp != (char *)NULL)
				if (strncmp( buf, FORWARD, 10 ) == 0) {
					strcpy( buf, buf+11 );
					return( sendone( argc, argv, buf, FALSE ) );
				}
		}
	}

	/* open mailfile */
	if ( (mailfile = FOPEN( idfilename, "a", 'b' )) == (FILE *)NULL ) {
		fprintf( stdout, "pcmail: cannot append to %s\n", idfilename );
		return( 0 );
	}

	if ( debuglevel > 5 )
		fprintf( stderr, "pcmail: append to mailfile\n" );

	tloc = time( (long *)NULL );
	thetime = ctime(&tloc);
 	(void)strcpy(chartime, thetime);	/* make our own copy */
 	thetime = chartime;	/* and work with our own copy */
	thetime[strlen(thetime)-1] = '\0';

#ifdef RMAIL
	fprintf( mailfile, "From uucp %s", thetime );
	fputc( '\012', mailfile );
	fprintf( mailfile, "Received: by %s (pcmail) %s", domain, thetime );
	fputc( '\012', mailfile );
#else /* RMAIL */
	fprintf( mailfile, "From %s %s", mailbox, thetime );
	if ( remote )
		fprintf( mailfile, " remote from %s", nodename );
	fputc( '\012', mailfile );
	fprintf( mailfile, "Received: by %s (pcmail) %s", domain, thetime );
	fputc( '\012', mailfile );
	fprintf( mailfile, "Date: %s", thetime );
	fputc( '\012', mailfile );
	/* add Date:, From: and Message-ID: headers */
	fprintf( mailfile, "From: %s <%s@%s>", name, mailbox, domain );
	fputc( '\012', mailfile );
	fprintf( mailfile, "Message-ID: <%ld@%s>", sequence, domain );
	fputc( '\012', mailfile );
	/* add To: headers */
	while (--argc > 0) {
		fprintf( mailfile, "To: %s", *++argv );
		fputc( '\012', mailfile );
	}
	if ( strlen( Subject ) > 0 ) {
		fprintf( mailfile, "Subject: %s", Subject );
		fputc( '\012', mailfile );
	}
#ifdef PCMAIL
	/* add Message-Lines: and Content-Length: headers */
	fprintf( mailfile, "X-Message-Lines: %ld", lines );
	fputc( '\012', mailfile );
	fprintf( mailfile, "Content-Length: %ld", bytes );
	fputc( '\012', mailfile );
#endif /* PCMAIL */
#endif /* RMAIL */

	/* copy tempfile to postbox file */
	if (debuglevel > 4)
	   fprintf( stderr, "pcmail: copy tempfile %s to %s\n",
	                    tfilename, idfilename );
	tempfile = FOPEN( tfilename, "r", 'b' );
	if ( tempfile == (FILE *)NULL) {
		fprintf( stdout, "pcmail: can't re-open %s\n", tfilename );
		return( 0 );
	}
	while (fgets( buf, 512, tempfile ) != (char *)NULL) {
		if ( strncmp( buf, "From ", 5 ) == 0 )
			fputc( '>', mailfile );
		cp = &buf[ strlen(buf)-1 ];
		if ( *cp == '\n' )
			*cp = '\0';
		fputs( buf, mailfile );
		fputc( '\012', mailfile );
 	}

	/* close files */
	fclose( mailfile );
	fclose( tempfile );

	/* all done unless going to remote via uucp */
	/* must create the job control files */
	if ( remote == TRUE ) {

		/* create remote X xqt file */
		mailfile = FOPEN( ixfilename, "w", 'b' );
		if 	(mailfile == (FILE *)NULL) {
			fprintf( stdout, "pcmail: cannot append to %s\n", ixfilename );
			return( 0 );
		}
		fprintf( mailfile, "U %s %s", uucp, nodename );
		fputc( '\012', mailfile );
		fprintf( mailfile, "F %s", rdfilename );
		fputc( '\012', mailfile );
		fprintf( mailfile, "I %s", rdfilename );
		fputc( '\012', mailfile );
		fprintf( mailfile, "C rmail %s", address );
		fputc( '\012', mailfile );
		fclose( mailfile );

		/* create local C copy file */
		mailfile = FOPEN( icfilename, "w", 't' );
		if 	(mailfile == (FILE *)NULL) {
			fprintf( stdout, "pcmail: cannot append to %s\n", icfilename );
			return( 0 );
		}

#if MSDOS
 		fprintf( mailfile, fpat2, cidfilename, rdfilename,
 					uucp, cidfilename, uucp );
 		fputc( '\012', mailfile );
 		fprintf( mailfile, fpat2, cixfilename, rxfilename,
 					uucp, cixfilename, uucp );
 		fputc( '\012', mailfile );
#else
		fprintf( mailfile, fpat2, idfilename, rdfilename,
					uucp, idfilename, uucp );
		fputc( '\012', mailfile );
		fprintf( mailfile, fpat2, ixfilename, rxfilename,
					uucp, ixfilename, uucp );
		fputc( '\012', mailfile );
#endif
		fclose( mailfile );

	} /* if ( remote == TRUE ) */

	return( 1 );
}

#ifndef AMIGA
#ifdef RMAIL
rnews(argc, argv)
int argc;
char *argv[];
{
	struct tm	*thetm;
	char	filename[132];
	char	format[128];
	FILE 	*f;
	char	buf[BUFSIZ];

	static int count = 0;

	tloc = time( (long *)NULL );
	thetime = ctime(&tloc);
	tloc = time( (long *)NULL );

	thetm = localtime( &tloc );

	/* mkfilename( format, spooldir, NEWSDIR );	*/
	sprintf( filename, NEWSDIR,
		thetm->tm_year % 100, thetm->tm_mon,
		thetm->tm_mday, thetm->tm_hour,
		thetm->tm_min,  thetm->tm_sec,  count
		);

	count++;

	if ( debuglevel > 5 )
		fprintf( stderr, "rnews: %s\n", filename );

	if ( (f = FOPEN( filename, "w", 't' )) == (FILE *)NULL ) {
		fprintf( stderr, "rnews: can't open %s %d\n", filename, errno );
		return( -1 );
	}

	while ( fgets( buf, BUFSIZ, stdin ) != (char *)NULL )
		fputs( buf, f );

	fclose( f );
}

#endif /* RMAIL */
#endif /* AMIGA */


SHAR_EOF
chmod +x 'pcmail.c'
fi # end of overwriting check
if test -f 'rmail.c'
then
       echo shar: will not over-write existing file "'rmail.c'"
else
cat << \SHAR_EOF > 'rmail.c'
/*			rmail.c

*/

#define NOMAIN
#define RMAIL /* if defined we get rmail() else we get lmail() */

#include	"pcmail.c"


SHAR_EOF
chmod +x 'rmail.c'
fi # end of overwriting check
if test -f 'ulib.c'
then
       echo shar: will not over-write existing file "'ulib.c'"
else
cat << \SHAR_EOF > 'ulib.c'
/* 		ulib.c

*/

#include	"local/ulib.c"

SHAR_EOF
chmod +x 'ulib.c'
fi # end of overwriting check
if test -f 'uuhost.c'
then
       echo shar: will not over-write existing file "'uuhost.c'"
else
cat << \SHAR_EOF > 'uuhost.c'
/*		uuhost.c

*/
#define	MAIN	dcpmain
#define CWDSPOOL
#include	"local/host.c"

SHAR_EOF
chmod +x 'uuhost.c'
fi # end of overwriting check
if test -f 'systems'
then
       echo shar: will not over-write existing file "'systems'"
else
cat << \SHAR_EOF > 'systems'
van-bc Any a HAYES TD939-4756 g ogin:--ogin uuslmac sword:-\c-sword: uuslmac
vdan-bc NONE a DIR 2400 g "" ATZ OK-\d+++\dATZ-OK ATS7=12 OK ATTD939-4782 CONNECT \d\c ogin:--ogin uuslmac sword:-\c-sword: uuslmac

SHAR_EOF
chmod +x 'systems'
fi # end of overwriting check
#	End of shell archive
exit 0

--
{ubc-vision,uunet}!van-bc!sl				Stuart.Lynne@van-bc.uucp