#!/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.2 (2005-08-13)
#
# Setup:
# - Just load the script in Irssi: /script load [PATH/]bitlbee-html.pl
# - If necessary, adjust the masks for which bitlbee-html should HTML-encode
#   messages:
#     /set bitlbee-html_masks MASK1 MASK2 MASK3
#   Format of MASKn is the usual NICK!USER@HOST, but see limitations below.
# - If you want incoming messages to be HTML-decoded as well, do:
#     /set bitlbee-html_in on
#   This is disabled by default as bitlbee has "set html = strip" (I'm told).
#
# Usage:
# - Just query with your AOL IM contacts using bitlbee as usual. Characters
#   with special meaning in HTML will automatically be encoded as HTML entities
#   (i.e., "<" becomes "&lt;" etc.).
# - If you ever want to sent HTML as-is,
#   prefix your message with "[HTML] " (case insensitive, but beware the space)
#
# 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
#  - 0.1.2:
#     * documentation: setup notes, source comments
#     * added setting bitlbee-html_in (boolean) for HTML entity decoding of
#       incoming messages; not done by default anymore
#     * don't HTML entity encode own messages starting with "[HTML] ";
#       only strip this prefix off them
#

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

$VERSION = '0.1.2';

%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-13'
);

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

sub sig_msg_in
{
	# only handle incoming messages if explicitly configured (see above)
	return unless settings_get_bool('bitlbee-html_in');

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

	# only handle messages to user-set targets
	return unless $server->masks_match($masks, $nick, $address);

	# decode HTML entities (i.e., "&lt;" becomes "<" etc.)
	$_[1] = decode_entities($msg);

	# continue signal handling with filtered message
	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');

	# try to find a query corresponding to the message's target
	my $query = $server->query_find($target);
	unless ($query)
	{
		Irssi::print("bitlbee-html: can't find query object for $target", MSGLEVEL_CLIENTERROR);
		return;
	}

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

	# only handle messages to user-set targets
	return unless $server->masks_match($masks, $target, $address);

	# don't filter own messages beginning with "[HTML] "
	if ($msg =~ s/^\[HTML\] //i)
	{
		$_[1] = $msg;
	}
	else
	{
		# escape chars with special meaning in HTML als HTML entities
		# (i.e., "<" becomes "&lt;" etc.)
		$_[1] = encode_entities($msg);
	}

	# continue signal handling with filtered message
	signal_continue(@_);
}
signal_add_first('message own_private', \&sig_msg_out);
#signal_add_first('message own_public',  \&sig_msg_out);

