###########################################################################
##
## ROT13 for irssi 
## Like IDEA plugin but uses ROT13 :-)
##
## RootBear / OH3NWQ Nov 2006
##
## 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_rot13 {
  my ($str) = @_;
  $str =~ tr/A-Za-z/N-ZA-Mn-za-m/;
  return $str;
}

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

sub check_rot13 {
  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 "ROT13") {
    $server->printformat($dest,MSGLEVEL_PUBLIC,'rot13_pub',
                         $nick,do_rot13($txt));
    Irssi::signal_stop();
  }
}

# Here you can change the color formats

Irssi::theme_register([
  'rot13_own', '%yROT13 %n%w>%g $0-',
  'rot13_pub', '%yROT13 %n%c<$0>%n $1-'
]);   

Irssi::signal_add("event privmsg", "check_rot13");
Irssi::command_bind("rot13", "cmd_rot13_say");
Irssi::print("ROT13 1.00 loaded :-P");
