#!/usr/bin/perl # Hey! - A better write, and a quick mailer.. # Oct 1996 Murf. # This version (C) March 2004 # Ver : 4.0 # hey, 1996-2000,2003,2004 Damian Murphy # The Perl Version of the csh version of the Perl version! ;) # Lots of this code was written by Anon @maths.tcd.ie # I'm in the process of hacking it together with my own csh-hey # Feel free to play around with it. #Declare some sub-functions #Check if a username exists on the system sub exists { local (@r) = getpwnam ($_[0]); $#r != -1; } #Check if a user 'owns' a tty sub owns { local ($who, $which) = ($_[0], $_[1]); local ($uid, $_); $uid = ((getpwnam ($who))[2]); $uid == (stat ("/dev/$which"))[4]; } #Startup... print STDERR "This is Hey, v4.0\n"; #Check our command-line if ( $#ARGV == -1 ) { print STDERR "Usage: hey (user) [ (user2) (user3) ... ]\n"; exit 1; } #Check if the hey files are there... #test for valid user... #Stops the program unless we get at least one valid target @users = (); @ttys = (); arg: while ( $_ = shift ) { if ( /^(.*)\.(.*)$/ ) { $_ = $1; $histty = $2; if ( -c $histty ) { s:^/dev/::; } elsif ( -c "/dev/tty$histty" ) { $histty = "tty$histty"; } elsif ( ! -c "/dev/$histty" ) { print STDERR "no such terminal: $histty\n"; next arg; } if ( &exists ($_) ) { if ( &owns ($_, $histty) ) { push (@users, $_); push (@ttys, $histty); } else { print STDERR "$_ does not own $histty\n"; } } else { print STDERR "user $_ does not exist\n"; } } else { if ( &exists ($_) ) { push (@users, $_); push (@ttys, ''); } else { print STDERR "user $_ does not exist\n"; } } } exit 1 unless @users; if ( $#users == 0 ) { print STDERR "Recipient : "; print STDERR "@users\n"; } else{ print STDERR "Recipients : "; print STDERR "@users\n"; } # Read in message undef $/; $* = 1; @message = split ("\n", ); # Do something with tabs foreach (@message) { next unless /\t/; local (@b) = split ('\t', $_); $_ = shift @b; foreach $x (@b) { $_ .= ' ' x (8 - (length ($_) % 8)); } } # Format message - put box around it and add signature $maxlength = 0; foreach (@message) { $maxlength = length ($_) unless length ($_) < $maxlength; } grep (($_ = '| ' . $_ . (' ' x ($maxlength - length)) . ' |'), @message); unshift (@message, '-' x ($maxlength + 4)); push (@message, '-' x ($maxlength + 4)); # Centre on 80 columns if ( $maxlength < 76 ) { $pad = ' ' x ((76 - $maxlength) / 2); foreach (@message) { $_ = $pad . $_ . $pad; } } #Now, send it! foreach (@users) { print STDERR "$_: "; $histty = shift @ttys; unless (open (WRITE, "|/usr/bin/write $_ $histty") ) { print STDERR "Can't run write: $!\n"; next; } print WRITE "\nHey there, $_ ...\n"; foreach $line (@message) { print WRITE $line, "\n"; } close WRITE; print STDERR "Hey...\n" unless $?; } #Bye!