#!PERL
# tt: Dutch Teletekst browser
# Copyright (C) 2000 J.P.M. de Vreught
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Contact information:
# Hans de Vreught <J.P.M.deVreught@cs.tudelft.nl>
# De Brink 70
# 2553 HA  The Hague
# The Netherlands

$version = "1.0.1";

require 5;
use Socket;

if ( $#ARGV != 0) {
	print STDERR "Usage: tt page[-subpage]\n";
	print STDERR "       Dutch Teletekst browser\n";
	print STDERR "       Version $version\n";
	exit 1;
}

$host = `hostname`;
chomp $host;
$host_address = sockaddr_in 0, inet_aton $host;

# STD 2 says that port 80 should be called www-http and not www nor http! But
# just in case www-http is not supported, we check these aliases when needed.

$port = getservbyname 'www-http', 'tcp';
$port = getservbyname 'www', 'tcp' if $port eq '';
$port = getservbyname 'http', 'tcp' if $port eq '';
$proto = getprotobyname 'tcp';

$remote = 'teletekst.nos.nl';
$remote_address = sockaddr_in $port, inet_aton $remote;
$page = "/cgi-bin/tt/nos/page/t/$ARGV[0]";

socket SOCKET, PF_INET, SOCK_STREAM, $proto || die "socket: $!";
bind SOCKET, $host_address || die "bind: $!";
connect SOCKET, $remote_address || die "connect: $!";

if ($child = fork) {
	&child(SOCKET, STDOUT);
} else {
	&parent(STDIN, SOCKET);
}

exit(0);

sub child {
	my ($from, $to) = @_;
	while (<$from>) {
		if (/<title>Found/io) {
			if ($ARGV[0] =~ /-/) {
				print STDERR "No such subpage\n";
			} else {
				print STDERR "No subpage specified\n";
			}
			exit 2;
		} elsif (/<title>Foutmelding/io) {
			print STDERR "Not present\n";
			exit 3;
		}
		next unless /<pre>/i;
		s/<br>/\n/gio;
		s/<[^>]*>//go;
		print $to $_;
	}
}

sub parent {
	my ($from, $to) = @_;
	select $to;
	$| = 1;
	print $to "GET $page\n";
}

__END__

=head1 NAME

B<tt> - Dutch Teletekst browser.

=head1 SYNOPSIS

B<tt> I<page>[I<-subpage>]

=head1 DESCRIPTION

With B<tt> you can access Dutch text-based teletekst (sub)pages. The pages
are retieved from I<http://www.omroep.nl/cgi-bin/tt/nos/page/t/>. If you
want page 102 you use the command B<tt>I< 102>, if you want subpage 3 of
page 705 use the command B<tt>I< 705-3>.

=head1 AUTHOR

Copyright (C) 2000 J.P.M. de Vreught

This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions. See the
GNU General Public License for more details.
