v07i085: A mail watcher for SUNwindows

sources-request at mirror.UUCP sources-request at mirror.UUCP
Thu Dec 11 04:17:50 AEST 1986


Submitted by: seismo!ut-sally!ut-ngp!clyde (Head UNIX Hacquer)
Mod.sources: Volume 7, Issue 85
Archive-name: sunmailwatch

# This is a little hack that I put together to watch for incoming mail.
# I use it instead of biff(1) for this.  Originally this was a shell script,
# but I got tired of the slowness and other problems (such as signal
# handling), so I converted it to C.
#
# So when new mail arrives at my SUN, an icon on my desktop changes to let
# me know.  Slightly expensive (requires running a shelltool), but worth
# it and simple.
#
#	This is a shell archive.
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	README
#	Makefile
#	emptymbox
#	fullmbox
#	mailwatch.1
#	mailwatch.c
cat << \SHAR_EOF > README
Mailwatch distribution notes

README		- this file
Makefile	- q.e.d
emptymbox	- 'empty mailbox' icon (place in /usr/include/images)
fullmbox	- 'full mailbox' icon (place in /usr/include/images)
mailwatch.1	- manual page
mailwatch.c	- C source

Bugs to clyde at ngp.utexas.edu, ihnp4!ut-ngp!clyde
SHAR_EOF
cat << \SHAR_EOF > Makefile
mailwatch:	mailwatch.c
	cc -o mailwatch -O mailwatch.c
SHAR_EOF
cat << \SHAR_EOF > emptymbox
/* Format_version=1, Width=64, Height=64, Depth=1, Valid_bits_per_item=16
 */
	0x8888,0x8888,0x8888,0x8888,0x8888,0x8888,0x8888,0x8888,
	0x2222,0x2222,0x2222,0x2222,0x2222,0x2222,0x22FF,0x2222,
	0x8888,0x8888,0x8B00,0xC888,0x8888,0x8888,0x9C00,0x2888,
	0x2222,0x2222,0x6000,0x1222,0x2222,0x2223,0x8000,0x0A22,
	0x8888,0x888C,0x0000,0x0C88,0x8888,0x88F0,0x0000,0x0488,
	0x2222,0x2300,0x0000,0x0222,0x2222,0x27F0,0x0000,0x0222,
	0x8888,0x980C,0x0000,0x0288,0x8888,0xE003,0x0000,0x0288,
	0x2222,0x8000,0x8000,0x0222,0x2223,0x0000,0x4000,0x0222,
	0x888A,0x0000,0x2000,0x0288,0x888A,0x0000,0x2000,0x0288,
	0x2224,0x0000,0x1000,0x0222,0x2224,0x0000,0x1000,0x0222,
	0x8888,0x0000,0x0800,0x0288,0x8888,0x0000,0x0800,0x2288,
	0x2228,0x0000,0x0800,0xE222,0x2228,0x0000,0x0801,0xE222,
	0x8888,0x0000,0x0803,0xE288,0x8888,0x0000,0x080E,0xE288,
	0x2229,0x1013,0x081C,0xE222,0x2229,0xB301,0x0830,0xE222,
	0x8889,0x5091,0x0820,0xCC88,0x8889,0x5391,0x0800,0x8888,
	0x2229,0x1491,0x0800,0x3222,0x2229,0x1391,0x0800,0x6222,
	0x8888,0x0000,0x0800,0x8888,0x8888,0x0000,0x0801,0x8888,
	0x2228,0x0000,0x0802,0x2222,0x2228,0x0000,0x0806,0x2222,
	0x8888,0x0000,0x0808,0x8888,0x8888,0x0000,0x0818,0x8888,
	0x2228,0x0000,0x0822,0x2222,0x2228,0x0000,0x08E2,0x2222,
	0x8888,0x0000,0x0988,0x8888,0x8888,0x0000,0x0A88,0x8888,
	0x2228,0x0000,0x0E22,0x2222,0x222F,0xFFFF,0xFE22,0x2222,
	0x8888,0x8888,0xBE88,0x8888,0x8888,0x8888,0xBE88,0x8888,
	0x2222,0x2222,0x3E22,0x2222,0x2222,0x2222,0x3E22,0x2222,
	0x8888,0x8888,0xBE88,0x8888,0x8888,0x8888,0xBE88,0x8888,
	0x2222,0x2222,0x3E22,0x2222,0x2222,0x2222,0x3E22,0x2222,
	0x8888,0x8888,0xBE88,0x8888,0x8888,0x8888,0xBE88,0x8888,
	0x2222,0x2222,0x3E22,0x2222,0x2222,0x2222,0x3E22,0x2222,
	0x8888,0x8888,0xBE88,0x8888,0x8888,0x8888,0xBE88,0x8888,
	0x2222,0x2222,0x3E22,0x2222,0x2222,0x2222,0x3E22,0x2222,
	0x8888,0x8888,0xBE88,0x8888,0x8888,0x8888,0xBE88,0x8888,
	0x2222,0x2222,0x3E22,0x2222,0x2222,0x2222,0x3E22,0x2222
SHAR_EOF
cat << \SHAR_EOF > fullmbox
/* Format_version=1, Width=64, Height=64, Depth=1, Valid_bits_per_item=16
 */
	0x8888,0x8888,0x8888,0x8888,0x8888,0x8888,0x8888,0x8888,
	0x2222,0x2222,0x2222,0x2222,0x2222,0x2222,0x22FF,0x2222,
	0x8888,0x8888,0x8B00,0xC888,0x8888,0x8888,0x9C00,0x2888,
	0x2222,0x2222,0x6000,0x1222,0x2222,0x2223,0x8000,0x0A22,
	0x8888,0x888C,0x0000,0x0C88,0x8888,0x88F0,0x0000,0x0488,
	0x2222,0x2300,0x0001,0x0222,0x2222,0x27F0,0x0007,0x0222,
	0x8888,0x9AAC,0x001F,0x0288,0x8888,0xF557,0x003F,0x0288,
	0x2222,0xAAAA,0x803F,0x0222,0x2223,0x5555,0x403F,0x0222,
	0x888A,0xAAAA,0xA03E,0x0288,0x888B,0x5555,0x6038,0x0288,
	0x2226,0xAAAA,0xB030,0x0222,0x2225,0x5555,0x5030,0x0222,
	0x888A,0xAAAA,0xA830,0x0288,0x888D,0x5555,0x5830,0x0288,
	0x222A,0xAAAA,0xA830,0x0222,0x222D,0x5555,0x5830,0x0222,
	0x888A,0xAAAA,0xA830,0x0288,0x888D,0x5555,0x5830,0x0288,
	0x222A,0xAAAA,0xA830,0x0222,0x222D,0x5555,0x7830,0x0222,
	0x888A,0xAAAA,0xE820,0x0C88,0x888D,0x5555,0xD800,0x0888,
	0x222A,0xAAAB,0x0800,0x3222,0x222D,0x5555,0x0800,0x6222,
	0x888A,0xAAB9,0x0800,0x8888,0x888D,0x5571,0x0801,0x8888,
	0x222A,0xAAE1,0x0802,0x2222,0x222D,0x5543,0x0806,0x2222,
	0x888A,0xAE8C,0x0808,0x8888,0x888D,0x5D30,0x1818,0x8888,
	0x222A,0xBAC0,0x2822,0x2222,0x222D,0x7700,0x58E2,0x2222,
	0x888A,0xAB55,0xA988,0x8888,0x888F,0x5555,0x5A88,0x8888,
	0x222E,0xAAAA,0xAE22,0x2222,0x222F,0xFFFF,0xFE22,0x2222,
	0x8890,0x0000,0x1E88,0x8888,0x88A0,0x0000,0x3E88,0x8888,
	0x2240,0x0000,0x7E22,0x2222,0x2280,0x0000,0xBE22,0x2222,
	0x8900,0x0001,0xBE88,0x8888,0x8A00,0x0002,0xBE88,0x8888,
	0x2400,0x0006,0x3E22,0x2222,0x2800,0x000A,0x3E22,0x2222,
	0x9000,0x0018,0xBE88,0x8888,0xA000,0x0028,0xBE88,0x8888,
	0x4000,0x0062,0x3E22,0x2222,0x4000,0x00A2,0x3E22,0x2222,
	0xC000,0x0188,0xBE88,0x8888,0xA000,0x0688,0xBE88,0x8888,
	0x3000,0x0A22,0x3E22,0x2222,0x2E01,0xF222,0x3E22,0x2222,
	0x89FE,0x8888,0xBE88,0x8888,0x8888,0x8888,0xBE88,0x8888,
	0x2222,0x2222,0x3E22,0x2222,0x2222,0x2222,0x3E22,0x2222
SHAR_EOF
cat << \SHAR_EOF > mailwatch.1
.TH MAILWATCH 1
.SH NAME
mailwatch \- watch for mail
.SH SYNOPSIS
.B mailwatch
[
.B \-a
] [
.B \-c
] [
.B \-e
] [
.B \-o
] [
.B \-i
.I empty-icon
] [
.B \-I
.I full-icon
] [
.B \-l
.I empty-label
] [
.B \-L
.I full-label
] [
.B \-m
.I mail-reader
] [
.B \-t
.I check-time
]
.SH DESCRIPTION
.I Mailwatch
monitors the users' mailbox (as given in the
.B MAIL
environment setting) and alerts the user when there is new mail.
.I Mailwatch
uses the SUN
.IR shelltool (1)
terminal emulation capabilities to post the new mail alert.
It will not function properly unless running in a SUN tty emulation window.
.PP
While waiting for mail, the window will be closed and displaying the
.I empty-mailbox
icon.  When new mail arrives, the
.I full-mailbox
icon is displayed.  The
.I mailwatch
window may optionally pop to the front of the screen, open and start up
a mail reader.
After the mail reader has exited and the mail
disposed of, the window is closed and the
.I empty-mailbox
icon is displayed.
.SH OPTIONS
.TP 15
.B  \-a
Ask before starting up the mail reader program.
.TP
.B \-c
Clear the terminal emulator window before starting up the mail reader program.
.TP
.B \-e
Bring window to front when mail arrives.
.TP
.B \-o
Automatically open window when mail arrives.
.TP
\fB\-i\fP\fI empty-icon\fP
Icon to display when mailbox empty.
(Default is \fI/usr/include/images/emptymbox\fP).
.TP
\fB\-I\fP\fI full-icon\fP
Icon to display when mail arrives.
(Default is \fI/usr/include/images/fullmbox\fP).
.TP
\fB\-l \fP\fIempty-label\fP
String to use as label for empty mailbox icon. (Default is for no label).
.TP
\fB\-L \fP\fIfull-label\fP
String to use as label for full mailbox icon. (Default is for no label).
.TP
\fB\-m \fP\fImail-reader\fP
Mail reading program to use. (Default is
.IR Mail(1)).
.TP
\fB\-t \fP\fIinterval\fP
Check for mail every
.I interval
seconds.  (Default is 60 seconds).
.PP
.I Mailwatch
can also be forced to check the mailbox by sending it an interrupt signal
(\fBSIGINT\fP).  Sending a quit signal (\fBSIGQUIT\fP) causes 
.I mailwatch
to exit.
.PP
.I Mailwatch
uses the SUN tty emulator window capabilities which are controlled by
command strings, so it need not run on the SUN workstation itself.
.SH BUGS
.PP
The window is closed only if the mailbox file is empty.
.sp
All pending input is flushed when new mail is detected, and this sometimes
causes 
.I mailwatch
output to be garbled.
.sp
This program could well run as an actual tool, sparing the expense of running a
shelltool for mail monitoring.
.sp
SUN UNIX 3.0 has a
.I mailtool
which performs many of these functions, but we don\'t run 3.0 yet.
.SH AUTHOR
Clyde Hoover, University of Texas Compuation Center, Austin Texas.
.SH FILES
The name of the mail file watched is taken from the
.B MAIL
environment entry.
.br
.I /usr/include/images/emptymbox
and
.I /usr/include/images/fullmbox
are the default icons displayed.
SHAR_EOF
cat << \SHAR_EOF > mailwatch.c
/*
 *	mailwatch - New mail watcher which manipulates SUN tty windows
 *		Changes icons when new mail arrives and optionally
 *		opens SUN window and starts up mail reading program
 *
 *	Usage: mailwatch -a (Ask before starting up mail reader)
 *			 -c (Clear screen before reading mail)
 *			 -e (Bring window to top when mail arrives)
 *			 -o (Automatically open window for new mail)
 *			 -i (Icon to display when mailbox empty)
 *			 -I (Icon to display when mailbox full)
 *			 -l (Label for empty mailbox icon)
 *			 -L (Label for full mailbox icon)
 *			 -m (Mail reading program)
 *			 -t (New mail check time)
 *
 *	Compliation:	cc -o mailwatch -O mailwatch.c
 *	Ownership:	any/any
 *	Mode:		0755
 *	Binary:		anywhere
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <signal.h>
#include <setjmp.h>

int	CheckTime = 120;	/* Time between mail checks */
char	*MailBox;		/* Mailbox file */

char	*EmptyMboxIcon = "/usr/include/images/emptymbox", /* CONFIG */
		/* Icon for empty mailbox */
	*FullMboxIcon = "/usr/include/images/fullmbox";	  /* CONFIG */
		/* Icon for full mailbox */

char	*EmptyMboxLabel = "",	/* Label for empty mbox icon */
	*FullMboxLabel = "";	/* Label for full mbox icon */

char	*MailReader = "Mail";	/* Mail reading program */

int	AutoOpen = 0,		/* Automatically open window for mail */
	Clear = 0,		/* Clear window before starting MailReader */
	Ask = 0,		/* Ask before starting MailReader */
	Expose = 0;		/* Expose sun window for mail */

int	OnSun;			/* Running under SUN windows */

char	*clr_window = "\014",	/* Clear SUN window */
	*c_window = "\033[2t",	/* Close SUN window */
	*o_window = "\033[1t",	/* Open SUN window */
	*e_window = "\033[5t",	/* Expose SUN window */
	*i_label = "\033]L%s\033\\",	/* Set icon label */
	*s_label = "\033]l%s\033\\",	/* Set top stripe label */
	*s_icon = "\033]I%s\033\\";	/* Set icon */

int	checked = 0;		/* Mail check commanded (via SIGINT) */
jmp_buf	here;			/* Where to return to for signals */

	/* Usage message */
char	*Usage = "Usage: mailwatch [-a] [-c] [-e] [-o]\n\
	[-i emptymboxicon] [-I fullmboxicon]\n\
	[-l emptymboxlabel] [-L fullmboxlabel]\n\
	[-m mailreader] [-t check-time]\n";

/*
 *	Simple things
 */
#define	seticon(I)	printf(s_icon, I)
#define	seticonlabel(S)	printf(i_label, S)
#define	setstripe(S)	printf(s_label, S)
#define	putstr(S)	fputs(S, stdout)

#define	CLOSED	0	/* Window is closed */
#define	OPEN	1	/* Window is open */

main(argc, argv)
int	argc;
char	**argv;
{
	int	state = OPEN;	/* Current window status */
	int	catcher();	/* Signal catcher */

	setbuf(stdout, (char *)NULL);
	init(argc, argv);
	(void) signal(SIGINT, catcher);
	(void) signal(SIGQUIT, catcher);

	setjmp(here);
	for (;;) {
		if (filesize(MailBox) > 0) {
			state = OPEN;
			seticon(FullMboxIcon);
			seticonlabel(FullMboxLabel);
			setstripe("      You have mail.");
			if (Expose)
				putstr(e_window);
			if (AutoOpen)
				putstr(o_window);
			if (Ask) {
				char	buf[80];	/* Input buffer */

				putstr("\nPress RETURN to read mail");
				fflush(stdout);
				tossinput();
				(void) fgets(buf, 80, stdin);
			}
			if (Clear)
				putstr(clr_window);
			(void) system(MailReader);
		}
		else {
			if (state == OPEN) {
				putstr(c_window);
				seticon(EmptyMboxIcon);
				seticonlabel(EmptyMboxLabel);
				setstripe("      Waiting for mail.");
				state = CLOSED;
			}
			if (checked) {
				putstr("No mail.\n");
				checked = 0;
			}
			tsleep(CheckTime);
		}
	}
}

/*
 *	filesize - get size of file
 *
 *	Returns: size of file or -1 if file not found
 */
filesize(file)
char	*file;		/* File to size */
{
	struct stat s;	/* File status */

	if (stat(file, &s) < 0)
		return(-1);
	return(s.st_size);
}

/*
 *	catcher - signal catcher
 */
catcher(sig)
int	sig;	/* Signal which got us here */
{
	if (sig == SIGQUIT) {
		putstr("\nQuit\n");
		exit(1);
	}
	checked = 1;		/* Set user check flag */
	longjmp(here, 1);
}

/*
 *	tossinput - flush pending terminal input
 */
tossinput()
{
	tsleep(2);
	ioctl(0, TIOCFLUSH, 0);
}


/*
 *	tsleep -- sleep for <time> seconds or until terminal
 *		is ready for input.
 */
tsleep(time)
int	time;		/* Seconds to sleep */
{
	struct timeval	kw;	/* Select timeout */
	int	imask;	/* Input fd mask */ /* XXX SELECT TYPE */

	kw.tv_sec = time;
	kw.tv_usec = 0;
	imask = (1 << fileno(stdin));
	return(select(fileno(stdin)+1, &imask, (int *)0, (int *)0, &kw));
}

/*
 *	init - process arguments and initialize options
 *
 * Arguments are:
 * a = auto open
 * c = clear before mailreader exec
 * e = expose window when mail arrives
 * i fn = empty icon
 * I fn = full icon
 * l str = empty label
 * L str = full label
 * t time = sleep time
 * m prog = mail-reader
 */
init(argc, argv)
int	argc;		/* Arg count from main() */
char	**argv;		/* Arg vector from main() */
{
	extern int	optind;		/* From getopt() */
	extern char	*optarg;	/* From getopt() */
	char	*p;		/* Temp */
	int	argchar;	/* Temp */
	char	*optstring = "acei:I:l:L:t:m:o?";	/* Option list */

	extern char	*getenv();

	OnSun = (int )getenv("WINDOW_ME");
	/*
	 * If running under suntools, ignore stop signals.
	 * Actually this is desirable ONLY if running directly under
	 * shelltool, but there is no easy way to figure that out.
	 */
	if (OnSun)
		(void) signal(SIGTSTP, SIG_IGN);

	/*
	 * If TERM != sun, all bets are off!
	 */
	p = getenv("TERM");
	if (strncmp(p, "sun", 3) && strcmp(p, "Mu")) {
		printf("Warning: TERM not set to 'sun'.\n");
		printf("\tMailwatch will only work in SUN tty emulator window\n");
	}

	if ((MailBox = getenv("MAIL")) == (char *)0) {
		fprintf(stderr, "No mailbox\n");
		exit(1);
	}

	while ((argchar = getopt(argc, argv, optstring)) != EOF) {
		switch(argchar) {
		case 'a':	Ask = 1; break;
		case 'c':	Clear = 1; break;
		case 'e':	Expose = 1; break;
		case 'i':	EmptyMboxIcon = optarg; break;
		case 'I':	FullMboxIcon = optarg; break;
		case 'l':	EmptyMboxLabel = optarg; break;
		case 'L':	FullMboxLabel = optarg; break;
		case 'm':	MailReader = optarg; break;
		case 'o':	AutoOpen = 1; break;
		case 't':	CheckTime = atoi(optarg); break;
		case '?':	printf(Usage); exit(1);
		}
	}
	if (filesize(EmptyMboxIcon) < 0) {
		fprintf(stderr, "Can't find empty mailbox icon %s\n",
			EmptyMboxIcon);
		exit(1);
	}
	if (filesize(FullMboxIcon) < 0) {
		fprintf(stderr, "Can't find full mailbox icon %s\n",
			FullMboxIcon);
		exit(1);
	}
/*
 *	printf("Mailwatch starting. 
 *		Will check mail every %d seconds\n
 *		SIGINT to check mail\n
 *		SIGQUIT to exit\n", CheckTime);
 */
}
SHAR_EOF
#	End of shell archive
exit 0



More information about the Mod.sources mailing list