#!/usr/bin/perl
# -----------------------------------------------------------------------------
# bitlbee-html.pl -- encode to / decode from HTML as used by AOL IM
# Copyright (C) 2005 Fabian Pietsch <fabian-irssi@canvon.dyndns.org>
#
# Irssi interface usage taken from kenny.pl
# http://alfie.ist.org/projects/irssi/scripts/kenny.pl
# Copyright (C) 2002 Gerfried Fuchs <alfie@channel.debian.de>
#
# Based on zzzmorse.pl (Irssi version) 0.1.4
# Copyright (C) 2003, 2004 Fabian Pietsch <fabian-irssi@canvon.dyndns.org>
#
# Distributed under the GNU GPL (General Public Licence)
# bitlbee-html.pl is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# bitlbee-html.pl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# -----------------------------------------------------------------------------
#
# Version 0.1.1 (2005-08-12)
#
# Known bugs & limitations:
# - /msg-ing someone out of the blue will trigger an error as there is no
#   Irssi query object yet -- (currently?) needed to get the target's address
#   for matching against bitlbee-html_masks.
# - A match to those masks is only possible after the target's address is known,
#   even with *!*@* or similar; address will be known as soon as a message is
#   received from that nick or a whois completes.
# - When HTML-encoding own messages, they will be displayed encoded in one's
#   own buffer, too.
#
# ChangeLog:
#  - 0.1.1:
#     * initial release
#

use Irssi qw(signal_add_first signal_add_last signal_continue settings_add_str settings_get_str);
use HTML::Entities;
use strict;
use vars qw($VERSION %IRSSI);

$VERSION = '0.1.1';

%IRSSI = (
	'authors'     => 'Fabian Pietsch',
	'contact'     => 'fabian-irssi@canvon.dyndns.org',
	'name'        => 'bitlbee-html',
	'description' => 'Encode bitlbee IM messages to AOL IM users as HTML.',
	'license'     => 'GPL',
	'url'         => 'http://zzz.arara.de/software/raw/script/irssi/bitlbee-html.pl',
	'changed'     => '2005-08-12'
);

settings_add_str('bitlbee-html', 'bitlbee-html_masks', '*!*@login.oscar.aol.com');

sub sig_msg_in
{
	my ($server, $msg, $nick, $address, $target) = @_;
	my $masks = settings_get_str('bitlbee-html_masks');

	return unless $server->masks_match($masks, $nick, $address);

	$_[1] = decode_entities($msg);
	signal_continue(@_);
}
signal_add_first('message private', \&sig_msg_in);
#signal_add_first('message public',  \&sig_msg_in);

sub sig_msg_out
{
	my ($server, $msg, $target, $orig_target) = @_;
	my $masks = settings_get_str('bitlbee-html_masks');

	my $query = $server->query_find($target);
	unless ($query)
	{
		Irssi::print("bitlbee-html: can't find query object for $target", MSGLEVEL_CLIENTERROR);
		return;
	}

	my $address = $query->{address};
	unless ($address)
	{
		$server->print($target, "bitlbee-html: can't get address of $target", MSGLEVEL_CLIENTERROR);
		return;
	}

	return unless $server->masks_match($masks, $target, $address);

	$_[1] = encode_entities($msg);
	signal_continue(@_);
}
signal_add_last('message own_private', \&sig_msg_out);
#signal_add_last('message own_public',  \&sig_msg_out);

