[comp.windows.news] slightly spruced up version of jeremy's 'newsbiff' program

mh@awds26.eaton.com (Mike Hoegeman) (09/19/89)

The subject says it all

I did not do a whole lot other than fix up the calling syntax a tad
and make it a little more sendmail friendly. see the header on newsbiff.c
for more info.

-mike

----------------------CUT HERE----------------------------------
echo x - MESSAGE
sed 's/^X//' >MESSAGE <<'*-*-END-of-MESSAGE-*-*'
XDate: Fri, 19 Aug 88 12:46:37 EDT
XTo: NeWS-makers@brillig.umd.edu
XSubject: NeWS mail notifier
XFrom: eagle!icdoc!Ist!jh@ucbvax.Berkeley.EDU  (Jeremy Huxtable)
X
XHere is (yet) another NeWS mail notifier which shows you when mail
Xarrives and allows you to read the article.  It was inspired by the
Xrecent posting to this group by Josh Siegel.
X
XWhen mail arrives you get a little envelope appearing on your screen
Xwhich is the icon for a window displaying the offending article.
X
XDocumentation consists of the comment at the top of the program.
X
XSuggestions for enhancements will be gratefully received.  Some possible
Xones are:
X
X     o Use a TextCanvas type window for reading the mail item.
X     o For short messages, pop up a postcard rather than an envelope.
X     o For really long messages, pop up a parcel icon.
X     o For really short messages, pop up a telegram and change all
X       the message into upper case with periods replaced by STOP
X       etc.
X
*-*-END-of-MESSAGE-*-*
echo x - Makefile
sed 's/^X//' >Makefile <<'*-*-END-of-Makefile-*-*'
XCFLAGS=-g
XINCLUDE=-I/usr/NeWS/include
XLIBS=/usr/NeWS/lib/libcps.a
X
Xnewsbiff: newsbiff.c newsbiff.h
X	$(CC) $(CFLAGS) $(INCLUDE) newsbiff.c -o newsbiff $(LIBS)
X	cp newsbiff /usr/NeWS/bin
X
Xnewsbiff.h: newsbiff.cps
X	cps newsbiff.cps
X
Xclean:
X	rm -f newsbiff.h newsbiff *.o core *~ *.BAK
*-*-END-of-Makefile-*-*
echo x - demo
sed 's/^X//' >demo <<'*-*-END-of-demo-*-*'
X#!/bin/csh -f
X# Run this on the same machine you're running the NeWS server on.
Xset hostname = awds26
Xset username = `whoami`
Xecho "UserProfile /UserName ($username) put" | psh
X./newsbiff $username < MESSAGE
*-*-END-of-demo-*-*
echo x - newsbiff.c
sed 's/^X//' >newsbiff.c <<'*-*-END-of-newsbiff.c-*-*'
X/*
X** Created By:
X**
X**      Jeremy Huxtable (jh@ist.co.uk)
X**
X**      Mon Aug 15 12:33:25 BST 1988
X**
X** mh@wlbr.imsd.contel.com: spruced up a bit. 
X**
X*/
X
X/*
X**LIBS: -lcps
X*/
X
X/*
X
Xnewsbiff.c:
X
X    Usage: newsbiff name@host@port name@host@port.....
X
X    Inspired by the program posted by Josh Siegel (siegel@hc.dspo.gov),
X    this program will notify you via your NeWS server when new mail
X    arrives.  The mail item should be given as standard input.  The
X    program opens a connection to the NeWS server (if any) on
X    <host.port>, checks that the user is the same as <username>, and
X    pops up an icon shaped like an envelope. You can click on this icon
X    to get a window displaying the whole mail item.
X
X    To use this you must change two files:
X
X    1) in your "user.ps" you need a line of the form:  
X    
X	UserProfile /UserName (<name>) put 
X    
X    and you probably need to disable network security:  
X    
X	systemdict /NetSecurityWanted false put
X
X    2) in your ".forward" file in your home directory you need a line
X    of the form:  
X    
X	<user>,|"newsbiff user@host@port"
X
X    (you can leave off @host@port part, in which case it will use
X    "@localhost@2000" as a default)
X
X    mine's like this:
X    
X mh,|"/nbin/newsbiff mh@awds26@2000"
X
X    NOTE: if you work on many different machines that all have the same
X    physical home directory (via NFS) put something like this in your
X    .forward file.
X
X mh@awds26,mh-biff@awds26
X
X    and then put mh-biff in /etc/aliases like so...
X
X mh-biff: "|/nbin/newsbiff mh@awds26@2000","|/nbin/newsbiff mh@awds14@2000",...
X
X    this way you'll get biff-ed on each machine that you are running a
X    NeWS server on. If someone else is running a NeWS server it will
X    not biff them as long as they do not have the same UserName in
X    their UserProfile PostScript dictionary as you.
X
X    Of course, this is only a "biff" type program. It merely tells you
X    that mail has arrived and lets you read it, nothing else.
X
X
X*/
X
X#ifdef SYSVREF
X#ifdef INTERLANTCP
X#include <interlan/il_types.h>
X#include <interlan/netdb.h>
X#include <interlan/in.h>
X#else
X#include <sys/types.h>
X#endif
X#else
X#include <sys/types.h>
X#include <netdb.h>
X#include <netinet/in.h>
X#endif
X
X#include <psio.h>
X#include "newsbiff.h"
X
X#define MAXSTR  512
X
Xmain(argc, argv)
X    int             argc;
X    char          **argv;
X{
X    for(argc--, argv++; argc >= 1; argc--, argv++)
X	biffit(*argv);
X    exit(0);
X}
X
Xbiffit(spec)
X    char *spec;
X{
X    extern char    *strchr();
X    extern long     atol();
X    struct hostent *hp;
X    char            from[MAXSTR];
X    char            line[MAXSTR];
X    char            server[MAXSTR];
X    char            host[MAXSTR];
X    char            username[MAXSTR];
X    char           *p;
X    int             port = 2000;
X    int             count = 0;
X
X    strcpy(username, spec);
X    if ((p = strchr(username, '@')))
X    {
X	*p++ = '\0';
X	strcpy(host, p);
X    }
X    else
X	strcpy(host, "localhost");
X    if ((p = strchr(host, '@')))
X    {
X	*p++ = '\0';
X	port = atol(p);
X    }
X    else
X	port = 2000;
X    if (!(hp = gethostbyname(host)))
X    {
X	return (1);		/* Host unknown */
X    }
X
X
X    /*
X     * * There ought to be a better way of doing this rather than *
X     * putting NEWSSERVER into the environment.
X     */
X    sprintf(server,
X      "NEWSSERVER=%lu.2000;%s\n", ntohl(*(u_long *) hp->h_addr), host);
X    putenv(server);
X
X    if (!ps_open_PostScript())
X	return (0);		/* Can't contact NeWS server */
X
X    ps_username(line);
X    if (strcmp(line, username) != 0)
X    {
X	ps_close_PostScript();
X	return (0);		/* User name doesn't match */
X    }
X
X
X    strcpy(from, "Anon");
X    ps_begin_list();
X    while (fgets(line, 255, stdin))
X    {
X	line[strlen(line) - 1] = 0;
X	if (count++ > 100)
X	{
X	    ps_string("More follows......");
X	    break;
X	}
X	ps_string(line);
X	sscanf(line, "From: %s", from);
X    }
X    ps_end_list();
X
X    /*- shorten the label on put on the envelope.  just use the
X    user@host portion of the mailing address, can the domain stuff -*/
X
X    if ((p = strchr(from, '@')) && (p = strchr(p, '.')))
X	*p = '\0';
X    ps_mailwindow(from);
X    ps_close_PostScript();
X    return (0);
X}
*-*-END-of-newsbiff.c-*-*
echo x - newsbiff.cps
sed 's/^X//' >newsbiff.cps <<'*-*-END-of-newsbiff.cps-*-*'
X%
X% NeWS interface for the "newsmail" program.
X%
X
X#define NAME_TAG        1
X
X% Get the user name of the owner of the NeWS server
Xcdef ps_username(string s) => NAME_TAG(s)
X    UserProfile /UserName known
X    { UserProfile /UserName get }
X    { (?) } ifelse
X    NAME_TAG tagprint typedprint
X
X% Three procedures for sending down an array of strings
Xcdef ps_begin_list()
X    [
X
Xcdef ps_end_list()
X    ]
X
Xcdef ps_string(string s)
X    s
X
X% Actually create the icon.
Xcdef ps_mailwindow(string sender)
X
X/MailWindow DefaultWindow [
X    /Text       % Text of the mail item
X    /Sender     % Name of the sender
X]
Xclassbegin
X    /TextFont /Times-Roman findfont 14 scalefont def
X    /SmallFont /Times-Roman findfont 10 scalefont def
X    /Margin 10 def
X
X    /new { % text sender => instance
X	/new super send begin
X	    /Sender exch def
X	    /Text exch def
X	    /Iconic? true def
X	    /IconX 50 def
X	    /IconY 800 def
X	    /IconWidth 64  2 mul def
X	    /IconHeight 40  2 mul def
X
X	    IconX IconY         % position of window
X	    % Now calculate the width and height of the window
X	    TextFont setfont
X	    0 Text { stringwidth pop max } forall
X	    Margin 2 mul add BorderLeft add BorderRight add % Width
X
X	    TextFont fontheight Text length mul Margin 2 mul add
X	    BorderTop add BorderBottom add % Height
X
X	    3 2 roll 1 index sub IconHeight add 3 1 roll % Adjust Y position
X	    reshape             % Shape the window
X	    IconX IconY move    % Move the icon to its proper place
X	    currentdict
X	end
X    } def
X
X    % Write the mail item into the window.
X    /PaintClient {
X	1 fillcanvas 0 setgray
X	TextFont setfont
X	Margin ClientHeight Margin sub TextFont fontheight sub moveto
X	Text {
X	    show
X	    Margin currentpoint exch pop moveto
X	    0 TextFont fontheight neg rmoveto
X	} forall
X    } def
X
X    % Make the Icon look like an envelope.
X    /PaintIcon {
X	gsave
X	IconCanvas setcanvas
X	1 fillcanvas 0 strokecanvas
X	matrix currentmatrix
X	IconWidth IconHeight scale
X	0 0 moveto 0 1 lineto .5 .3 lineto 1 1 lineto
X	1 0 lineto closepath clip                 % Set flap clipping path
X	0 0 moveto .5 .7 lineto 1 0 lineto stroke % Draw two lines clipped by flap
X	initclip                                  % Reset clip path
X	0 1 moveto .5 .3 lineto 1 1 lineto stroke % Draw flap
X	.5 .7 moveto
X	setmatrix
X	SmallFont setfont Sender cshow
X	grestore
X    } def
X
Xclassend def
X
X% Create the window.  We create a new process group so that the window
X% does not get zapped when the C program closes its socket.
X{
X    newprocessgroup
X    sender framebuffer /new MailWindow send
X    /map exch send
X} fork clear
*-*-END-of-newsbiff.cps-*-*
exit