#!/usr/bin/perl
#
# lit203conf - Show the config for Litmus 2.03
#
# Author: PinkFreud / Nightstar IRC Network
# Date  : 2003-12-14
#
# Based on information found on http://loonie.kewl.org/
#
$version = 1.0.1;
#
# Revision history:
#	1.0.0		Initial release
#	1.0.1		Supports modified AND byte (is this in the wild?)

sub usage {
  die "Usage: $0 <Litmus 2.03 exe>\n";
}
$#ARGV == 0 or usage;


sub readconf {
  my ($fh, $offset, $length) = @_;

  seek ($fh, $offset, 0);
  my $current_offset = tell ($fh);
  die
  "Uh oh, I tried to seek to $offset, but found myself at $current_offset instead.\n"
    unless $offset == $current_offset;
  
  my $bytesread = read ($fh, $data, $length);
  die
  "Whoops, tried to read $length bytes, but got $bytesread instead - truncated file?\n"
    unless $bytesread == $length;

  return $data;
}

sub deobfuscate {
  my (@data) = @_;
  my ($data);
  my (@realdata);

  for $data (@data) {
    my ($tmp, $chr);
    for $chr (split (//, $data)) {
      $tmp .= chr (ord ($chr) + 88 & 255);
    }
    push (@realdata, $tmp);
  }

  return (@realdata);
}


my $litmus = $ARGV[0];
(-r $litmus) or die "$litmus not found, or not readable.  Exiting...\n";
(-s $litmus) == 36384 or
  warn "$litmus isn't 36384 bytes - possibly corrupt or not Litmus 2.03\n";

open (LITMUS, $litmus) or die "Failed to open $litmus: $!\n";
binmode (LITMUS);

my $nick = unpack (Z19, readconf (*LITMUS, 0x94f3, 19));
my $key = unpack (Z16, readconf (*LITMUS, 0x9506, 16));
my $spass = unpack (Z20, readconf (*LITMUS, 0x951c, 20));
my $litmusver = unpack (A20, readconf (*LITMUS, 0x9530, 20));
my $exe = unpack (Z25, readconf (*LITMUS, 0x9544, 25));
my $bpass = unpack (Z20, readconf (*LITMUS, 0x96ba, 20));
my $server = unpack (Z39, readconf (*LITMUS, 0x970c, 39));
my $channel = unpack (Z38, readconf (*LITMUS, 0x9733, 38));

close (LITMUS);

warn
"We don't seem to be looking at Litmus 2.03 - things may not work as expected.\n" 
  unless $litmusver =~ 'Litmus 2.03';

($nick, $bpass, $channel, $key, $server, $spass, $exe) =
  deobfuscate ($nick, $bpass, $channel, $key, $server, $spass, $exe);

print <<_EOF_;
Version        : $litmusver
Nick prefix    : $nick
Bot password   : $bpass
Channel        : $channel
Channel key    : $key
Server         : $server
Server password: $spass
Bot filename   : $exe
_EOF_
