use strict;
use warnings;

our $VERSION = '0.1'; # 5b09f41e8df71be
our %IRSSI = (
    authors     => 'Nei',
    contact     => 'Nei @ anti@conference.jabber.teamidiot.de',
    url         => "http://anti.teamidiot.de/",
    name        => 'realnames',
    description => 'An idea to show real names instead of nick names, useful for some specific networks.',
    license     => 'ISC',
   );

# Usage
# =====
# Configure the channel(s) where you want this script to apply using
#
#     /set realnames_channel networktag/#channel1 tag/#channel2
#
# To apply it on a whole network, you can write
#
#     /set realnames_channel networktag/*
#
# It only works for new messages.
# Alpha version, beware.

use Irssi;

my %netchans;

sub use_realnames {
    my ($channel) = @_;
    my $tag = lc $channel->{server}{tag};
    my $chan = lc $channel->{visible_name};
    my $netchan = "$tag/$chan";
    exists $netchans{"$tag/*"} || exists $netchans{$netchan} || exists $netchans{"*/$chan"};
}

sub _get_chan {
    my ($server, $channel) = @_;
    return unless $server;
    my $c = $server->channel_find($channel) || return;
    return unless use_realnames($c);
    +{
	s => $server,
	c => $c,
    };
}

sub _get_nick_chan {
    my ($server, $channel, $nick) = @_;
    my $i = _get_chan($server, $channel) || return;
    $i->{n} = $i->{c}->nick_find($nick) || return;
    $i
}

sub sig_message_public {
    my ($server, $msg, $nick, $address, $channel) = @_;
    my $i = _get_nick_chan($server, $channel, $nick) || return;
    if (length $i->{n}{realname}) {
	Irssi::signal_continue(@_[0..1], $i->{n}{realname}, @_[3..$#_]);
    }
}

sub sig_message_own_public {
    my ($server, $msg, $channel) = @_;
    my $i = _get_chan($server, $channel) || return;
    $i->{n} = $i->{c}{ownnick};
    if (length $i->{n}{realname}) {
	Irssi::signal_emit('message public', $server, $msg, $i->{n}{realname}, $i->{n}{host}, $channel);
	Irssi::signal_stop;
    }
}

sub sig_message_nick {
    my ($server, $newnick, $oldnick, $address) = @_;
}

sub sig_message_part {
    my ($server, $channel, $nick, $address, $reason) = @_;
}

sub sig_message_quit {
    my ($server, $nick, $address, $reason) = @_;
}

sub sig_message_topic {
    my ($server, $channel, $topic, $nick, $address) = @_;
}

sub sig_message_irc_action {
    my ($server, $msg, $nick, $address, $target) = @_;
    my $i = _get_nick_chan($server, $target, $nick) || return;
    if (length $i->{n}{realname}) {
	Irssi::signal_continue(@_[0..1], $i->{n}{realname}, @_[3..$#_]);
    }
}

sub sig_message_irc_mode {
    my ($server, $channel, $nick, $address, $mode) = @_;
}

sub sig_setup_changed {
    my @channels = split ' ', lc Irssi::settings_get_str('realnames_channel');
    %netchans = map { ($_ => 1) } @channels;
}

sub init {
    sig_setup_changed();
}

Irssi::settings_add_str('realnames', 'realnames_channel' => '');

Irssi::signal_add('message public' => 'sig_message_public');
Irssi::signal_add('message nick' => 'sig_message_nick');
Irssi::signal_add('message part' => 'sig_message_part');
Irssi::signal_add('message quit' => 'sig_message_quit');
Irssi::signal_add('message topic' => 'sig_message_topic');
Irssi::signal_add('message irc action' => 'sig_message_irc_action');
Irssi::signal_add('message irc mode' => 'sig_message_irc_mode');
Irssi::signal_add_last('message own_public' => 'sig_message_own_public');
Irssi::signal_add('setup changed' => 'sig_setup_changed');

init();
