[net.sources] Mail forwarding utility

dan@aplvax.UUCP (Daniel M. Sunday) (10/22/85)

This is a mail forwarding utility for systems, like V7, which do not
have mailers with a forwarding feature.  The BSD mailer has a built-in,
undocumented, forwarding service.  The forwarding utility here observes
the BSD convention for specifying forwarding.  The program, then, is
primarily useful for people on networks with some non-BSD machines.
I wrote it because I found myself in such an environment.  It has been
tested and runs under V7 and 4.1.

Some mods to the source may be needed for your particular configuration.
Check that MAILDIR is right.  The man page describes usage.  If anyone
upgrades this utility, I would appreciate receiving the upgrades.
As usual, this code can be used for anything you want, except outright
selling it for a profit.

					dan (sunday)
					...umcp-cs!aplvax!dan


: Run this shell script with "sh" not "csh"
PATH=:/bin:/usr/bin:/usr/ucb
export PATH
all=FALSE
if [ $1x = -ax ]; then
	all=TRUE
fi
/bin/echo 'Extracting forward.8'
sed 's/^X//' <<'//go.sysin dd *' >forward.8
X.TH FORWARDMAIL 8 3/28/84
X.UC 4
X.SH NAME
forwardmail - mail forwarding service
X.SH SYNOPSIS
X/usr/lib/forwardmail
X.SH DESCRIPTION
X.s3
This utility is intended for users who have accounts on multiple
UNIX machines, and want mail they receive on all of them to be
forwarded to one site.
X.PP
To use this service, if it has been installed and enabled on one's computer,
a user only needs to put the name of the account to forward his
mail to in a file ".forward" in his home directory (e.g. aplvax!dan).
X.PP
Forwardmail is enabled by having it periodically
executed from 'crontab' (say: once an hour or day).  
When run, it checks to see whether users with pending mail 
in /usr/spool/mail have requested it to be forwarded to another account.  
If a .forward file exists in a user's home directory 
the mail is forwarded with the mail on the local machine being removed.
X.SH FILES
X/usr/lib/crontab
X.br
X/usr/spool/mail/*
X.br
$HOME/.forward
X.br
X/usr/lib/uucp/L.sys
X.SH AUTHOR
Daniel M. Sunday, JHU/APL (...umcp-cs!aplvax!dan)
X.SH BUGS
There is no way to check whether a user has set up two machines to be
forwarding mail to each other.
X.PP
The user should be able to set his own forwarding frequency in
his .forward file.
//go.sysin dd *
made=TRUE
if [ $made = TRUE ]; then
	/bin/chmod 644 forward.8
	/bin/echo -n '	'; /bin/ls -ld forward.8
fi
/bin/echo 'Extracting forward.c'
sed 's/^X//' <<'//go.sysin dd *' >forward.c
X/* %M%	%R%	%G%	%T%	*/
X/* mail forwarding service */
X/* Programmed 3/29/84 by: dan sunday, JHU/APL (aplvax) */
X/* modified 6/12/84 - matt diaz (aplvax) */
X/*
	- scan /usr/spool/mail for pending mail
	- for each user found:
		1) find his home from /etc/passwd
		2) chk his home for a .forward file
	- for each .forward file:
		1) forward his mail to the machine named in .forward
		2) rm his mail file after forwarded
*/


#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>

#define MAILDIR	"/usr/spool/mail"
#define MSIZE 256
#define FORWARD ".forward"	/* name of forward file */

char	*postoffice = MAILDIR;	/* mail directory */
int	pofd;			/* postoffice file descriptor */
struct direct	maildir;
struct stat	mailstat;	/* user's mail status */
char	user [MSIZE];		/* user name in postoffice */
char	usermail [MSIZE];	/* user mail file name */
char	home [MSIZE];		/* user's home from /etc/passwd */
char	sendto [MSIZE];		/* system to send mail to */
char	tempmail [MSIZE];	/* temp file for forwarding */
char	fwdcmd [MSIZE];		/* the actual mail forwarding command */

main()
{
	struct passwd	*pp;
	FILE	*fwf;		/* forward file */

	if ((pofd=open(postoffice,0)) < 0) {
		fprintf(stderr,"can't open %s\n", postoffice);
		exit(1);
	}

	sprintf( tempmail, "%s/%s", MAILDIR, "fmXXXXXX");
	mktemp(tempmail);
	while (read(pofd, &maildir, sizeof(maildir))) {
		if (maildir.d_ino == 0)
			continue;

		if (maildir.d_name[0] == '.')
			continue;

		/* get user name */
		strncpy(user, maildir.d_name, 14);

		/* make sure there is some mail */
		sprintf(usermail, "%s/%s", postoffice, user);
		stat(usermail, &mailstat);
		if (mailstat.st_size == 0)
			continue;		/* no mail */

		/* get user's home from /etc/passwd */
		if ((pp = (struct passwd *)getpwnam(user)) == NULL)
			continue;

		/* check for a forwarding address */
		sprintf(home, "%s/%s", pp->pw_dir, FORWARD);
		if ((fwf = fopen(home, "r")) == NULL)
			continue;

		fgets(sendto, MSIZE, fwf);
		sendto [strlen(sendto) - 1] = '\0';
		fclose(fwf);

		/* set up the mail forwarding command */
		sprintf(fwdcmd,"/bin/mv %s %s", usermail, tempmail);
#ifdef DEBUG
		printf("CMD: %s\n", fwdcmd);
#endif	
		/* move off to avoid mail interference */
		system( fwdcmd );

		sprintf(fwdcmd,"/bin/mail %s <%s",
			sendto, tempmail);
#ifdef DEBUG
		printf("CMD: %s\n", fwdcmd);
#endif
		/* do it */
		system( fwdcmd );
		sleep(7);	/* mksure it's done */

		/* and then */
		unlink(tempmail);
	}
	exit(0);
}


//go.sysin dd *
made=TRUE
if [ $made = TRUE ]; then
	/bin/chmod 644 forward.c
	/bin/echo -n '	'; /bin/ls -ld forward.c
fi