###########################################################################
##
## IDEA2 for irssi 
## Like IDEA plugin but uses IDEA :-)
##
## RootBear / OH3NWQ Dec 2010
##
## This IDEA plugin is not compatible with other IDEA plugins
## commonly used in IRC
##
## See the color formats near the end of the script
##
## Based on MORSE for irssi by
## Goblet / OH2MMY  Nov 2001
##
## Seems to support WTF8.
##
## 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::IDEA
##
###########################################################################

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

use strict;

## enter here key that is used for IDEA, exactly 16 characters
##                      1234567890123456
my $cipher = new IDEA ("ei näin             ");

my $version = "1.2";
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_IDEA2 {
  my ($str) = @_;
  while ((length($str)) %8 != 0){
      $str = $str . " ";
  }

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

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

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

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


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

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

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

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

	$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_IDEA2_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") {

      # IDEA2
      $encoded = encode_IDEA2($data);

      # b64
      $encoded = encode_b64($encoded);

      $witem->command("^MSG ".$witem->{name}.
                     " |*E*|IDEA2|$version|$encoded|");
  } else {
      Irssi::print("No active channel in window");
  }
  $window->printformat(MSGLEVEL_MSGS,'IDEA2_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 "IDEA2 B64 ERROR: invalid length ". length($inp)." bytes";
  }
  my $str = decode_base64($inp);
  return $str;
}


sub check_IDEA2 {
  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 "IDEA2") {
    $original = $text;
    $dec_status = 0;

    # b64
    $txt = decode_b64($txt);

    # IDEA2
    $txt = decode_IDEA2($txt);

    # wtf8
    $txt = decode_wtf8($txt);

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

    Irssi::signal_stop();
  }
}


Irssi::theme_register([
  'IDEA2_own', '%yIDEA2 %n%w>%g $0-',
  'IDEA2_pub', '%yIDEA2 %n%c<$0>%n $1-',
  'IDEA2_error', '%rIDEA2 ERROR %n%c<$0>%n $1-'
]);


Irssi::signal_add("event privmsg", "check_IDEA2");
Irssi::command_bind("idea2", "cmd_IDEA2_say");
Irssi::print("IDEA2 $version loaded :-P");


