###########################################################################
##
## Blowfish for irssi 
## Like IDEA plugin but helps you to blow a fish :-)
##
## RootBear / OH3NWQ Dec 2010
##
## 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::OpenSSL::Blowfish
##
###########################################################################

use Irssi;
use Irssi::Irc;
use Irssi::UI;
use Crypt::OpenSSL::Blowfish;
use MIME::Base64;

use strict;

## enter here key that is used for Blowfish, exactly 8 characters
##                                         12345678
my $cipher = new Crypt::OpenSSL::Blowfish("b l o w");

my $version = "1.1";
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_blowfish {
  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_blowfish {
  my ($str) = @_;

  if ((length ($str)) %8 != 0){
      return "BLOWFISH 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_blowfish_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") {

      # blowfish
      $encoded = encode_blowfish($data);

      # b64
      $encoded = encode_b64($encoded);

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


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

    # b64
    $txt = decode_b64($txt);

    # blowfish
    $txt = decode_blowfish($txt);

    # wtf8
    $txt = decode_wtf8($txt);

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

    Irssi::signal_stop();
  }
}


Irssi::theme_register([
  'blowfish_own', '%yblowfish %n%w>%g $0-',
  'blowfish_pub', '%yblowfish %n%c<$0>%n $1-',
  'blowfish_error', '%rBLOWFISH ERROR %n%c<$0>%n $1-'
]);


Irssi::signal_add("event privmsg", "check_blowfish");
Irssi::command_bind("blowfish", "cmd_blowfish_say");
Irssi::print("Blowfish $version loaded :-P");


