###########################################################################
##
## IDEA for irssi 
## Like MORSE plugin but uses IDEA :-)
##
## Including decode of WTF-8 "characters" åäöÅÄÖ to ISO Latin 9 (or vice versa)
##
## RootBear / OH3NWQ June 2010
##
##
## See the color formats near the end of the script
##
## Based on MORSE for irssi by
## Goblet / OH2MMY  Dec 2001
##
## This script is published under EVVKTVH license: http://evvk.com/evvktvh.html
## Insert your bug reports and/or complaints into your ass.
##
## FBI / CIA / Yo Momma is watching you!
##
###########################################################################

use Irssi;
use Irssi::Irc;
use Irssi::UI;
use strict;

my $version = "5.9";

# 1 to use the theme colors, 0 not to use
my $use_theme = "1";

# 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_idea {
  my ($str,$own,$ch) = @_;
  $str =~ s/\x60/'/g;
  $str =~ s/\\/\\\\/g;
  $str =~ s/\"/\\\"/g;
  $str = `~/bin/laden \"encrypt $ch $own :$str\"`;
  return $str;
}

sub decode_idea {
  my ($str) = @_;
  $str =`~/bin/laden \"decrypt :$str\"`;
  $str = substr($str, 0, -1);
  $str =~ s/^[^\:]*\:(.*)$/\1/s;

    if ($l9_to_utf8) {
  #        $str =~ s/¤/\xC2\xA4/g;
	  $str =~ s/å/\xC3\xA5/g;
	  $str =~ s/ä/\xC3\xA4/g;
	  $str =~ s/ö/\xC3\xB6/g;
	  $str =~ s/ü/\xC3\xBC/g;
	  $str =~ s/Å/\xC3\x85/g;
	  $str =~ s/Ä/\xC3\x84/g;
	  $str =~ s/Ö/\xC3\x96/g;
	  $str =~ s/Ü/\xC3\x9C/g;
	  $str =~ s/µ/\xC2\xB5/g;
	  $str =~ s/©/\xC2\xA9/g;
    } 
    if ($utf8_to_l9) {
	  $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_idea_say {
  my ($data,$server,$witem) = @_;
  my $window = Irssi::active_win();
  my $encoded;
  my $ch;
  my $own = Irssi::settings_get_str('nick');

  if (!$server || !$server->{connected}) {
      Irssi::print("Not connected to IRC server.");
      return;
  }

  if ($data && $witem->{type} eq "CHANNEL") {
      $ch = $witem->{name};

      $encoded = substr(encode_idea($data,$own,$ch), 0, -1);
        if ($encoded =~ /error/) {
                $window->printformat(MSGLEVEL_MSGS,'idea_own',"IDEA error: $encoded");
        } else {
                $encoded = substr $encoded, 1;
                $witem->command("^MSG ".$witem->{name}.
                     " $encoded");
        }
  } else {
      Irssi::print("No active channel in window.");
  }
  $window->printformat(MSGLEVEL_MSGS,'idea_own',$data);
}

sub check_idea {
  my ($srv, $data, $nick, $addr) = @_;
  my ($dest, $text) = split(/ :/, $data, 2);
  my $server = Irssi::active_server();
  my ($dummy, $tag, $proto, $ver, $key, $txt) = split(/\|/,$text);
  if ($tag eq "*E*" && $proto eq "IDEA") {
    my $hit = decode_idea($text);
        if (($hit eq "Decryption failed") or ($hit eq "Invalid message format")  or ($hit eq "Unknown key") ) {
            $server->printformat($dest,MSGLEVEL_PUBLIC,'idea_no',$nick,$text);
        }
    $server->printformat($dest,MSGLEVEL_PUBLIC,'idea_pub',$nick,$hit);
    Irssi::signal_stop();
  }
}

if ($use_theme) {
  # Here you can change the color formats
  Irssi::theme_register([
    'idea_no',  '%n%w <$0> $1',
    'idea_own', '%yIDEA %n%w>%B $0-',
    'idea_pub', '%yIDEA %n%w<%W$0%w>%B $1-'
  ]);
} else {
  Irssi::theme_register([
    'idea_no',  '<$0> $1',
    'idea_own', 'IDEA > $0-',
    'idea_pub', 'IDEA <$0> $1-'
  ]);
}

Irssi::signal_add("event privmsg", "check_idea");
Irssi::command_bind("idea", "cmd_idea_say");
Irssi::print("IDEA $version WTF-8 loaded :-P");
