#!/usr/bin/perl
# pqotd v0.1
# Copyright Alan Ford <alan@whirlnet.co.uk> 07/05/99
# Distributed with no warranty under the GNU Public License
#
# pqotd (Perl Quote of the Day) will connect to the
# specified host on the specified tcp port, receive the data
# given, and display it.
#
# The default port is 17, for Quote of the Day (qotd), but
# other protocols it could be used for include systat (11)
# and daytime (13).

require 5.002;
use IO::Socket;
use Socket;
use English;
sub help ;
sub version ;

my $version = "0.1";

if (not $ARGV[0]) {
   help;
}

if ($ARGV[0] =~ /help$/) {
   help;
}

if ($ARGV[0] eq "-?") {
   help;
}

if ($ARGV[0] eq "-h") {
   help;
}

if ($ARGV[0] eq "--version") {
   version;
}

my $verbose = 0;

if ($ARGV[2] eq "--verbose") {
   $verbose = 1;
}

if ($ARGV[2] eq "-v") {
   $verbose = 1;
}

my $host = $ARGV[0];
my $port = $ARGV[1];
$port = "17" unless $port;

if ($verbose == 1) {
    print "[$host:$port]\n";
}

my $remote = IO::Socket::INET->new(
    	       Proto    => "tcp",
    	       PeerAddr => $host,
    	       PeerPort => $port,
      	      ); 

unless ($remote) { 
    print "Cannot connect to $host\n";
    next;
}

$remote->autoflush(1);

while ($_ = <$remote>) {
    $_ =~ s/\r$//;  # trim annoying \r line-endings on some output
    print;
}
close($remote)	or die "Can't close socket: $!\n";

sub help {
   print <<EOF

pqotd - Perl Quote Of The Day (qotd) Client version $version

pqotd connects to the specified TCP port (defaults to qotd (17)) on the
specified host and displays the data that it is given.

Usage: pqotd [--help | --version] host [port [-v]]

-v         tells pqotd to be verbose in its output
           (limitation: must be third option)

--help     displays this help screen

--version  displays the version

pqotd can also be used for other protocols that just send data, such as
daytime (13) and systat (11).

Report bugs to Alan Ford <alan\@whirlnet.co.uk>

EOF
;
   exit;
}

sub version {
   print "pqotd version $version\n";
   exit;
}
