###########################################################################
##
## Morse for irssi
## Like IDEA plugin but uses morse code :-)
##
## See the color formats near the end of the script
##
## 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.
##
## modified by oh3nwq 3.12.2006
## changed the M to MORSE on display messages
##
###########################################################################
use Irssi;
use Irssi::Irc;
use Irssi::UI;
use strict;

my @morse;
my ($revmorse) = {};

# a-z
@morse[97..122] = (".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
                "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
                "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
                "-.--", "--..");
# numbers
@morse[48..57] = ("-----", ".----", "..---", "...--", "....-",
                  ".....", "-....", "--...", "---..", "----.");
# other
$morse[ord("ä")] =  ".-.-";
$morse[ord("å")] =  ".--.-";
$morse[ord("ö")] =  "---.";
$morse[ord("ü")] =  "..--";
$morse[ord("é")] =  "..-..";
$morse[ord("ñ")] =  "--.--";
$morse[ord("=")] =  "-...-";
$morse[ord("?")] =  "..--..";
$morse[ord(".")] =  ".-.-.-";
$morse[ord(",")] =  "--..--";
$morse[ord(":")] =  "---...";
$morse[ord("-")] =  "-....-";
$morse[ord("/")] =  "-..-.";
$morse[ord(" ")] =  "  ";

my $i; my $count;
foreach $i (@morse) {
  if ($i ne "") {
    $revmorse->{$i} = chr($count);
  }
  $count++;
}

sub to_morse {
  my ($str) = @_;
  my $ret; my $char;
  $str =~ tr/A-ZÄÖÅÜ/a-zäöåü/;
  foreach $char (split("|",$str)) {
    $ret .= $morse[ord($char)]." ";
  }
  $ret =~ s/ +$//g;
  return $ret;
}

sub from_morse {
  my ($str) = @_;
  my $ret; my $char;
  foreach $char (split("[^.-]",$str)) {
    if ($char ne "" && $revmorse->{$char}) {
      $ret .= $revmorse->{$char};
    } else {
      $ret .= " ";
    }
  }
  $ret =~ s/  +/ /g;
  return $ret;
}

sub cmd_morse_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*|MORSE|1.1|".to_morse($data)."|");
  } else {
      Irssi::print("No active channel in window");
  }
  $window->printformat(MSGLEVEL_MSGS,'morse_own',$data);
}

sub check_morse {
  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 "MORSE") {
    $server->printformat($dest,MSGLEVEL_PUBLIC,'morse_pub',
                         $nick,from_morse($txt));
    Irssi::signal_stop();
  }
}

# Here you can change the color formats

Irssi::theme_register([
  'morse_own', '%yMORSE %n%w>%g $0-',
  'morse_pub', '%yMORSE %n%c<$0>%n $1-'
]);

Irssi::signal_add("event privmsg", "check_morse");
Irssi::command_bind("morse", "cmd_morse_say");
Irssi::print("MORSE 1.1 loaded :-)");

