###########################################################################
##
## CAST5 for irssi 
## Like IDEA plugin but uses CAST5 :-)
##
## RootBear / OH3NWQ Dec 2010
##
## See the color formats near the end of the script
##
## Based on MORSE for irssi by
## Goblet / OH2MMY  Nov 2001
##
## Might not support WTF8, but who the *uck cares.
##
## This script is published under EVVKTVH: http://evvk.com/evvktvh.html
## Insert your bug reports and/or complaints into your ass.
##
## Uses MIME::Base64
##
##
##
## Uses Crypt::CAST5
##
## Copyright (C) 2006 DelTel, Inc.
##
## This library is free software; you can redistribute it and/or modify
## it under the same terms as Perl itself, either Perl version 5.8.5 or,
## at your option, any later version of Perl 5 you may have available.
##
###########################################################################

use Irssi;
use Irssi::Irc;
use Irssi::UI;
use Crypt::CAST5;
use MIME::Base64;

use strict;

## enter here key that is used for CAST5, exactly 16 characters
##                             1234567890123456
my $cipher = new Crypt::CAST5("pälli     ");


my $version = "1.1";
my $blocksize = 8;

my $dec_status;
my $original;


# utf8-conversion between utf8 and latin9 
# 1 to use, 0 not to use
my $utf8_to_l9 = "0";
my $l9_to_utf8 = "1";

if ($utf8_to_l9 and $l9_to_utf8) {  
  die "Unsufficient user IQ error.\n"; 
}


sub encode_CAST5 {
  my ($str) = @_;
  while ((length($str)) %$blocksize != 0){
      $str = $str . " ";
  }

  my $blocks = length($str) / $blocksize;

  my $encoded = "";
  my $substring;
  my $count;

  for ($count=0; $count<$blocks; $count++) {
	$substring = substr($str,($count)*$blocksize,$blocksize);

	$substring = $cipher->encrypt($substring);
	$encoded = $encoded . $substring;
  }
  return $encoded;
}


sub decode_CAST5 {
  my ($str) = @_;

  if ((length ($str)) %$blocksize != 0){
      return "CAST5 ERROR: invalid length ". length ($str)." bytes";
  }

  my $decoded = "";
  my $substring;
  my $count;
  my $blocks = length($str) / $blocksize;

  for ($count=0; $count<$blocks; $count++) {
        $substring = substr($str,($count)*$blocksize,$blocksize);

	$substring = $cipher->decrypt($substring);
	$decoded = $decoded . $substring;
  }
  $dec_status = 1;
  return $decoded;
}


sub decode_wtf8 {
  my ($str) = @_;
    if ($l9_to_utf8) {
  #        $str =~ s/<A4>/\xC2\xA4/g;
          $str =~ s/\xE5/\xC3\xA5/g;
          $str =~ s/\xE4/\xC3\xA4/g;
          $str =~ s/\xF6/\xC3\xB6/g;
          $str =~ s/\xFC/\xC3\xBC/g;
          $str =~ s/\xC5/\xC3\x85/g;
          $str =~ s/\xC4/\xC3\x84/g;
          $str =~ s/\xD6/\xC3\x96/g;
          $str =~ s/\xDC/\xC3\x9C/g;
          $str =~ s/\xB5/\xC2\xB5/g;
          $str =~ s/\xA9/\xC2\xA9/g;
    } 
    if ($utf8_to_l9) {
          $str =~ s/\xC3\xA5/\xE5/g;
          $str =~ s/\xC3\xA4/\xE4/g;
          $str =~ s/\xC3\xB6/\xF6/g;
          $str =~ s/\xC3\x85/\xC5/g;
          $str =~ s/\xC3\x84/\xC4/g;
          $str =~ s/\xC3\x96/\xD6/g;
          $str =~ s/\xE2\x82\xAC/\xA4/g;
          $str =~ s/\xC2\xB5/\xB5/g;
          $str =~ s/\xC2\xA9/\xA9/g;
    } 
  return $str;
}


sub cmd_CAST5_say {
  my ($data,$server,$witem) = @_;
  my $window = Irssi::active_win();
  my $encoded;

  if (!$server || !$server->{connected}) {
      Irssi::print("Not connected to server");
      return;
  }
  if ($data && $witem->{type} eq "CHANNEL") {

      # CAST5
      $encoded = encode_CAST5($data);

      # b64
      $encoded = encode_b64($encoded);

      $witem->command("^MSG ".$witem->{name}.
                     " |*E*|CAST5|$version|$encoded|");
  } else {
      Irssi::print("No active channel in window");
  }
  $window->printformat(MSGLEVEL_MSGS,'CAST5_own',$data);
}


sub encode_b64 {
  my ($inp) = @_;
  my $str = encode_base64($inp);
  return $str;
}


sub decode_b64 {
  my ($inp) = @_;
  if ((length ($inp)) %4 != 0){
      return "CAST5 B64 ERROR: invalid length ". length($inp)." bytes";
  }
  my $str = decode_base64($inp);
  return $str;
}


sub check_CAST5 {
  my ($srv, $data, $nick, $addr) = @_;
  my ($dest, $text) = split(/ :/, $data, 2);
  my $server = Irssi::active_server();
  my ($dummy, $tag, $proto, $ver, $txt) = split(/\|/,$text);

  if ($tag eq "*E*" && $proto eq "CAST5") {
    $original = $text;
    $dec_status = 0;

    # b64
    $txt = decode_b64($txt);

    # CAST5
    $txt = decode_CAST5($txt);

    # wtf8
    $txt = decode_wtf8($txt);

    if ($dec_status) {
	$server->printformat($dest,MSGLEVEL_PUBLIC,'CAST5_pub',$nick,$txt);
    } else {
	$server->printformat($dest,MSGLEVEL_PUBLIC,'CAST5_error',$nick,$original);
    }

    Irssi::signal_stop();
  }
}


Irssi::theme_register([
  'CAST5_own', '%yCAST5 %n%w>%g $0-',
  'CAST5_pub', '%yCAST5 %n%c<$0>%n $1-',
  'CAST5_error', '%rCAST5 ERROR %n%c<$0>%n $1-'
]);


Irssi::signal_add("event privmsg", "check_CAST5");
Irssi::command_bind("cast5", "cmd_CAST5_say");
Irssi::print("CAST5 $version loaded :-P");


