package SendZ;

use strict;

require Err;

return 1;

BEGIN {@main::Ids = (@main::Ids, '$Id: SendZ.pm,v 1.8 2004/11/29 01:46:16 jweiss Exp $ ');}

BEGIN {
    $SendZ::broadcast_class = 'sipb';
    $SendZ::broadcast_inst = 'office';
    $SendZ::broadcast_sig = 'Automated Cleaning Notice';

    $SendZ::debug_class = 'oyster';
    $SendZ::debug_inst = 'zbot';
    $SendZ::debug_sig = 'Automated Status Information';

    @SendZ::announcement = ('It\'s office cleaning time!  Please stand up and help clean the office', 
			    'Clean the microwave, rinse and recycle cans, throw out the trash,',  
			    'recycle scrap paper, wipe down tables and dust monitors and bookshelves.');
}

# Announce an office cleaning over zephyr, notifying each logged-in user 
#   [as specified in the arguments] by personal zephyr and broadcasting
#   the cleaning announcement as well.
sub announce {
    foreach my $user (@_) {
	foreach my $message (@SendZ::announcement) {
	    zpersonal($user, $message);
	}
    }

    foreach my $message (@SendZ::announcement) {
	zbroadcast($message);
    }
}

# Send a zephyr, with message specified as the argument, to the
#   debugging information zephyr class.
sub zdebug {
    my $message = shift or die 'No message specified';

    zwrite($message, "-d -n -s \'$SendZ::debug_sig\' -c $SendZ::debug_class -i $SendZ::debug_inst");
}

# Send a zephyr with message specified as the argument to the 
#   broadcast zephyr class.
sub zbroadcast {
    my $message = shift or die 'No message specified';

    zwrite($message, "-d -n -s \'$SendZ::broadcast_sig\' -c $SendZ::broadcast_class -i $SendZ::broadcast_inst");
}

# Send a personal zephyr.  The recipient is specified by the first 
#   argument and the message by the second.
sub zpersonal {
    my $target = shift or die 'No zephyr recipient specified';
    my $message = shift or die 'No message specified';

    zwrite($message, "-d -n -s \'$SendZ::broadcast_sig\' $target");	   
}

# Send a zephyr using zwrite, with the message specified by the first
#   argument and the command-line arguments to zwrite specified by 
#   the second.  These command-line arguments must include the 
#   recipient(s) of the zephyr.
sub zwrite {
    my $message = shift or die 'No message specified for zwrite';
    my $cmdargs = shift or die 'No arguments specified for zwrite';

    open(ZWR, "|/usr/athena/bin/zwrite $cmdargs 2>&1 >/dev/null") 
	or (Err::mail_error('Cannot run zwrite', $message, $cmdargs, $!) and return);
    
    print ZWR $message;
    
    close(ZWR) 
	or ($! and Err::mail_error('Error closing zwrite pipe', $message, $cmdargs, $!))
	    or Err::mail_error('zwrite exited with error', $message, $cmdargs, $?);
}
