###########################################################################
##
## jj for irssi 
## Like IDEA plugin but changes " " to " jj " :-)
##
## RootBear / OH3NWQ Nov 2010
##
## See the color formats near the end of the script
##
## Based on MORSE for irssi by
## Goblet / OH2MMY  Nov 2001
##
## Does 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.
##
###########################################################################

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

sub enc_jj {
  my ($str) = @_;
  $str =~ s/ / jj /g;
  return $str;
}

sub dec_jj {
  my ($str) = @_; 
  $str =~ s/ jj / /g;
  return $str;
}

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

  if (!$server || !$server->{connected}) {
      Irssi::print("Not connected to server");
      return;
  }
 
  if ($data && $witem->{type} eq "CHANNEL") {
      $witem->command("^MSG ".$witem->{name}.
                      " |*E*|JJ|1.0|".enc_jj($data)."|");
  } else {
      Irssi::print("No active channel in window");
  }
  $window->printformat(MSGLEVEL_MSGS,'jj_own',$data);
}

sub check_jj {
  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 "JJ") {
    $server->printformat($dest,MSGLEVEL_PUBLIC,'jj_pub',
                         $nick,dec_jj($txt));
    Irssi::signal_stop();
  }
}

# Here you can change the color formats

Irssi::theme_register([
  'jj_own', '%yjj %n%w>%g $0-',
  'jj_pub', '%yjj %n%c<$0>%n $1-'
]);   

Irssi::signal_add("event privmsg", "check_jj");
Irssi::command_bind("jj", "cmd_jj_say");
Irssi::print("jj 1.00 loaded :-P");
