###########################################################################
##
## Camellia for irssi 
## Like IDEA plugin but uses Camellia :-)
##
## RootBear / OH3NWQ Nov 2013
##
## 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 fuck cares.
##
## This script is published under EVVKTVH: http://evvk.com/evvktvh.html
## Insert your bug reports and/or complaints into your rectum.
##
## Uses MIME::Base64
##
##
##
## Uses Crypt::Camellia_PP
##
## 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::CBC;
use Crypt::Camellia_PP;
use MIME::Base64;

use strict;

## enter here key that is used for Camellia, at 128 bits
#					     =	 16 bytes
##         1234567890123456
my $key = "mursu1        ";

my $cipher = Crypt::Camellia_PP->new($key); 

my $version = "0.3";
my $blocksize = 16;

my $dec_status;
my $original;


sub encode_Camellia {
  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_Camellia {
  my ($str) = @_;

  if ((length ($str)) %$blocksize != 0){
      return "Camellia 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) = @_;
        $str =~ s/\xC3\xA5/å/g;
        $str =~ s/\xC3\xA4/ä/g;
        $str =~ s/\xC3\xB6/ö/g;
        $str =~ s/\xC3\x85/Å/g;
        $str =~ s/\xC3\x84/Ä/g;
        $str =~ s/\xC3\x96/Ö/g;
        $str =~ s/\xE2\x82\xAC/¤/g;
        $str =~ s/\xC2\xB5/µ/g;    
        $str =~ s/\xC2\xA9/©/g;    
  return $str;
}


sub cmd_Camellia_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") {

      # Camellia
      $encoded = encode_Camellia($data);

      # b64
      $encoded = encode_b64($encoded);

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


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

    # b64
    $txt = decode_b64($txt);

    # Camellia
    $txt = decode_Camellia($txt);

    # wtf8
    $txt = decode_wtf8($txt);

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

    Irssi::signal_stop();
  }
}


Irssi::theme_register([
  'Camellia_own', '%yCamellia %n%w>%g $0-',
  'Camellia_pub', '%yCamellia %n%c<$0>%n $1-',
  'Camellia_error', '%rCamellia ERROR %n%c<$0>%n $1-'
]);


Irssi::signal_add("event privmsg", "check_Camellia");
Irssi::command_bind("camellia", "cmd_Camellia_say");
Irssi::print("Camellia v. $version loaded :-P");


