#!/usr/bin/perl
#
# irccontact30conf - Show the config for IRCContact 3.0
#
# Author: PinkFreud / Nightstar IRC Network
# Date  : 2005-08-10
#
$version = 1.0.1;
#
# Revision history:
#	0.9.9		Initial version
#	1.0.0		Initial release
#	1.0.1		I had the username and realnames mixed up.  whoops.
#
# Thanks go to zchrist, who helped with the bit math in the deobfuscation sub.

sub usage {
  die "Usage: $0 <IRCContact 3.0 exe> [offset]\n";
}
$#ARGV > -1 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);
    my (@d);
    for $chr (split (//, $data)) {
      my $o = ord $chr;
      $tmp .= chr (($o & 0x0f) << 4 | $o >> 4);
    }
    push (@realdata, $tmp);
  }

  return (@realdata);
}


my $ircconf = $ARGV[0];
my $offset = $#ARGV > 0 ? eval $ARGV[1] : 0x18e00;
printf "Using offset 0x%x\n", $offset;

(-r $ircconf) or die "$ircconf not found, or not readable.  Exiting...\n";
(-s $ircconf) == 106381 or
  warn "$ircconf isn't 106381 bytes - possibly corrupt or not IrcContact 3.0\n";

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

my $data1 = readconf (*IRCCONF, $offset, 0x220);

close (IRCCONF);

my ($head, $nick, $nick_pw, $username, $channels, $userpass, $masterpass,
    $server, $realname, $exe, $port, $regflag, $rejoinflag, $ircconfver);

($head, $nick, $nick_pw, $username, $channels, $userpass, $masterpass, $server,
 $realname, $exe, $port, $regflag, $rejoinflag, $ircconfver) =
  unpack (Z72Z20Z36Z72Z120Z40Z40Z40Z40Z20Z8Z4Z4Z28, $data1);

warn
"We don't seem to be looking at IrcContact 3.0 - things may not work as expected.\n" 
  unless $ircconfver eq (deobfuscate ('IrcContact 3.0 by Impactus'))[0];

($nick, $nick_pw, $realname, $channels, $userpass, $masterpass, $server,
 $username, $exe, $port, $regflag, $rejoinflag, $ircconfver) =
deobfuscate ($nick, $nick_pw, $username, $channels, $userpass, $masterpass,
             $server, $realname, $exe, $port, $regflag, $rejoinflag,
             $ircconfver);


print <<_EOF_;
Version        : $ircconfver
Nick prefix    : $nick
Username       : $username
Realname       : $realname
Register nick  : $regflag
Nick password  : $nick_pw
User password  : $userpass
Master password: $masterpass
Channels       : $channels
Autorejoin     : $rejoinflag
Server         : $server
Port           : $port
Bot filename   : $exe
_EOF_
