###########################################################################
##
## ROT47 for irssi 
## Like IDEA plugin but uses ROT47 :-)
##
## RootBear / OH3NWQ Jan 2007
##
## 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 license: 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 do_rot47 {
  my ($str) = @_;
  $str =~ tr/!-~/P-~!-O/;
  return $str;
}

sub cmd_rot47_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*|ROT47|1.0|".do_rot47($data)."|");
  } else {
      Irssi::print("No active channel in window");
  }
  $window->printformat(MSGLEVEL_MSGS,'rot47_own',$data);
}

sub check_rot47 {
  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 "ROT47") {
    $server->printformat($dest,MSGLEVEL_PUBLIC,'rot47_pub',
                         $nick,do_rot47($txt));
    Irssi::signal_stop();
  }
}

# Here you can change the color formats

Irssi::theme_register([
  'rot47_own', '%yROT47 %n%w>%g $0-',
  'rot47_pub', '%yROT47 %n%c<$0>%n $1-'
]);   

Irssi::signal_add("event privmsg", "check_rot47");
Irssi::command_bind("rot47", "cmd_rot47_say");
Irssi::print("ROT47 1.00 loaded :-P");

