#!/usr/bin/perl

# popupwrite
#
# writes messages to NetPopUp windows on Win95 machines
# from the example file in O'Reilly's Perl book
# by Brian Clayton (brian@tlg.net) 10/9/96
#
# format for sending data to NetPopUp (v. 2.15) is:
# nickname/$0
# message/$1
# systemname/$2
# subject/$3
# priority/$4

($message,$station,$nickname,$systemname,$subject) = @ARGV;

if ($message eq '') {
  print "\n";
  print "For sending messages to NetPopUp on Windows 95 machines\n";
  print "\n";
  print "popupwrite 'message' [tostation] [fromuser] [fromstation] ['subject']\n";
  print "\n";
  print " message        is a string\n";
  print " tostation      is machine to write to      (default: localhost)\n";
  print " fromuser       is your name                (default: login)\n";
  print " fromstation    is your computer            (default: FooBar)\n";
  print " subject        is the window title         (default: A MESSAGE!)\n";
  print "\n";
  exit;
}

# DEFAULTS
$station = 'localhost' unless $station;		# set to site running NetPopUp 
$nickname = `whoami` unless $nickname;		# this tells who is sending 
$subject = 'A MESSAGE!' unless $subject;	# title of popup
$systemname = 'FooBar' unless $systemname;	# from some computer
$priority = '0';				# prio=1 says alert or somesuch 

$port = 7777;					# default netpopup port

#
# Telnet code from O'Reilly
#

$AF_INET = 2;
$SOCK_STREAM = 1;

$sockaddr = 'S n a4 x8';

chop($hostname = `hostname`);

($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp')
    unless $port =~ /^\d+$/;;
($name,$aliases,$type,$len,$thisaddr) =
	gethostbyname($hostname);
($name,$aliases,$type,$len,$thataddr) = gethostbyname($station);

$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);

# Make the socket filehandle.

if (! (socket(S, $AF_INET, $SOCK_STREAM, $proto))) { 
    print "socket failed!\n";
    die $!;
}

# Give the socket an address.

if (! (bind(S, $this))) {
    print "bind failed!\n";
    die $!;
}

# Call up the server.

if (! (connect(S,$that))) {
    print "connect failed!\n";
    die $!;
}

# Set socket to be command buffered.

select(S); $| = 1; select(STDOUT);

# Send that message!

print S "$nickname/\$0$message/\$1$systemname/\$2$subject/\$3$priority/\$4\n"; 

# da end
