###########################################################################
##
## MD5 for irssi 
## Like IDEA plugin but hashes the message with MD5 :-)
##
## RootBear / OH3NWQ Mar 2011
##
## You can brute force the message to see what was sent.
##
## See the color formats near the end of the script
##
## Based on MORSE for irssi by
## Goblet / OH2MMY  Nov 2001
##
## This script is published under EVVKTVH: http://evvk.com/evvktvh.html
## Insert your bug reports and/or complaints into your rectum.
##
###########################################################################

use Irssi;
use Irssi::Irc;
use Irssi::UI;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use strict;

my $version = "1.0";

sub encode_hex ($)
{
    my $str = shift;
    my $ret = unpack 'H*', $str;
    return $ret;
}

sub encode_MD5 {
  my ($str) = @_;
  $str = encode_hex(md5($str));
  return $str;
}

sub cmd_MD5_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") {
      $encoded = encode_MD5($data);

      $witem->command("^MSG ".$witem->{name}.
                     " |*E*|MD5|$version|$encoded|");
  } else {
      Irssi::print("No active channel in window");
  }
  $window->printformat(MSGLEVEL_MSGS,'MD5_own',$data);
}

# Here you can change the color formats
Irssi::theme_register([
  'MD5_own', '%yMD5 %n%w>%g $0-',
  'MD5_pub', '%yMD5 %n%c<$0>%n $1-'
]);   

Irssi::command_bind("md5", "cmd_MD5_say");
Irssi::print("MD5 $version loaded :-P");

