# Copyright (c) 2009, Chris Jones
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# - Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use strict;
use Irssi;
use vars qw/$VERSION %IRSSI/;

$VERSION = '0.1';
%IRSSI = (
    author      => 'Chris Jones',
    contact     => 'cjones@gruntle.org',
    name        => 'binary',
    description => 'converts binary ASCII in real time',
    license     => 'BSD',
    commands    => 'bin debin',
);

sub binary_to_text($) {
    my $binary = shift;
    my $text = '';
    for (my $i = 0; $i < length($binary); $i += 8) {
        my $j = 7;
        my $val = 0;
        foreach my $ch (split(//, substr($binary, $i, 8))) {
            $val += ((2 ** $j) * $ch);
            $j--;
        }
        $text .= chr($val);
    }
    return($text);
}


sub text_to_binary($) {
    my $text = shift;
    my $binary = '';
    foreach my $ch (split(//, $text)) {
        my $n = ord($ch);
        my $bin = '';
        while ($n > 0) {
            $bin = ($n % 2) . $bin;
            $n = $n >> 1;
        }
        $binary .= ('0' x (8 - length($bin))) . $bin;
    }
    return $binary;
}

sub handle_own_public($$$) {
    my ($server, $message, $target) = @_;
    my $window = $server->window_item_find($target);
    return unless ($window && $message =~ /^\s*(?:[01]{8}\s*)+$/);
    $message =~ s/\s+//g;
    $window->print('BINARY: ' . binary_to_text($message));
}


sub handle_public($$$$$) {
    my ($server, $message, $nick, $user, $target) = @_;
    handle_own_public($server, $message, $target);
}


sub handle_binary_command($$$) {
    my ($message, $server, $window) = @_;
    my $bin = text_to_binary($message);
    if ($window && ($window->{type} eq 'CHANNEL' || $window->{type} eq 'QUERY')) {
        $window->command(sprintf('MSG %s %s', $window->{name}, $bin));
    } else {
        print CLIENTCRAP 'BINARY: ' . $bin;
    }
}


sub handle_debinary_command($$$) {
    my ($message, $server, $window) = @_;
    return unless $message =~ /^\s*(?:[01]{8}\s*)+$/;
    $message =~ s/\s+//g;
    print CLIENTCRAP 'BINARY: ' . binary_to_text($message);
}

Irssi::signal_add('message public', \&handle_public);
Irssi::signal_add('message own_public', \&handle_own_public);
Irssi::command_bind('bin', \&handle_binary_command);
Irssi::command_bind('debin', \&handle_debinary_command);


