#!/usr/local/bin/perl
#
# yabbsmon - monitor lots of yabbs stuff
# Copyright (C) 1993 Alex Wetmore
#
# this is part of the yabbs package by alex wetmore
#

# constants
$yabbsport = 4225;				# port of yabbs server
$whocount = 1000;				# how often to generate a who packet so
								# that we don't get autologged off for 
								# inactivity

# get userid and password
print "userid: "; $userid = <STDIN>; chop $userid;
print "passwd: "; $passwd = <STDIN>; chop $passwd;

# make a socket to localhost, port 4225 (yabbs)
$pat = 'S n C4 x8';
$inet = 2;
$this = pack($pat, $inet, 0,          128,2,111,111);
$that = pack($pat, $inet, $yabbsport, 128,2,111,111);

# connect to our socket
if (!socket(S,2,1,6)) { die $!; }
if (!bind(S,$this)) { die $!; }
if (!connect(S,$that)) { die $!; }

sub sendp {
	syswrite(S, @_[0], length @_[0]);
}

# our data processing loop
$done = 0;
$logcount = 0;
while ($done == 0) {
	$p = <S>;							# read packet from server
	chop $p;							# kill trailing newline
	# these will make \: into :, and : into \018
	$p =~ s/\\:/\017/g;					# turn \: into some control code
	$p =~ s/:/\018/g;					# turn : into another control code
	$p =~ s/\017/:/g;					# turn \: into :
	@args = split(/\018/, $p);			# split packet on delimiters

	# check the packet type and do stuff accordingly
	if (@args[1] eq "YABBSHI") {		# initial message from yabbs server
		&sendp(":login:$userid:$passwd\n");
	} elsif (@args[1] eq "LOGIN") {		# conformation of our login
		if (@args[2] eq "OK") {
			&sendp(":startlog\n");
		} else {
			print "invalid login\n";
			$done = 1;
		}
	} elsif (@args[1] eq "LOG") {		# a log printout
		# generate who requests every once in a while so that we don't
		# get killed on an timeout
		if ($logcount++ == $whocount) {
			&sendp(":who\n");
		}
		@log = split(/\t/, @args[4]);
		$user = @args[3];
		if (@log[0] eq "LOGIN") {
			print "$user logged in\n";
		} elsif (@log[0] eq "LOGOUT") {
			print "$user logged out\n";
		} elsif (@log[0] eq "TALK") {
			if (@log[1] eq "JOIN") {
				print "talk: *** $user has joined @log[2]\n";
			} elsif (@log[1] eq "LEAVE") {
				print "talk: *** $user has left @log[2]\n";
			} elsif (@log[1] eq "PUBLIC") {
				print "talk: <$user:@log[2]> @log[3]\n";
			} elsif (@log[1] eq "ACTION") {
				print "talk: $user:@log[2] @log[3]\n";
			} elsif (@log[1] eq "PRIVATE") {
				print "talk: <$user -> @log[2]> @log[3]\n";
			}
		} elsif (@log[0] eq "PAGE") {
			print "talk: *** $user paged @log[1]\n";
		} elsif (@log[0] eq "GETMSGHDRS") {
			print "$user read headers for messages @log[1]/@log[2] to @log[1]/@log[3]\n";
		} elsif (@log[0] eq "GETMSG") {
			print "$user read message @log[1]/@log[2]\n";
		} elsif (@log[0] eq "ADDMSG") {
			print "$user added message to @log[2] about @log[3] on base @log[1]\n";
		} elsif (@log[0] eq "DELETEMSG") {
			print "$user deleted message @log[1]/@log[2]\n";
		} elsif (@log[0] eq "SETPASSWD") {
			print "$user changed his passwd\n";
		} elsif (@log[0] eq "NEWUSER") {
			print "made account for $user\n";
		} elsif (@log[0] eq "GETTXTFILE") {
			print "$user read asked for @log[1]\n";
		} elsif (@log[0] eq "GNUINFO") {
			print "$user read asked for gnu manifesto\n";
		} elsif (@log[0] eq "GETPLANFILE") {
			print "$user read @log[1]'s plan file\n";
		} elsif (@log[0] eq "PUTPLANFILE") {
			print "$user uploaded a new plan file\n";
		} elsif (@log[0] eq "GETGFILE") {
			print "$user is getting gfile @log[3] from @log[1] @log[2]\n";
		} elsif (@log[0] eq "GFILEINDEX" ) {
			print "$user is getting gfileindex @log[3] from @log[1] @log[2]\n";
		} elsif (@log[0] eq "STARTLOG") {
			print "$user started logging\n";
		} elsif (@log[0] eq "STOPLOG") {
			print "$user stopped logging\n";
		} elsif (@log[0] eq "NOTSYSOP") {
			print "only sysops can log\n";
		} else {
			print "unknown log message @log[3]\n";
		}
	}
}
