1use strict;
2use Data::Dumper;
3use vars qw($VERSION %IRSSI);
4
5$VERSION = '1.3';
6%IRSSI = (
7	authors		=> 'Tijmen "timing" Ruizendaal',
8	contact		=> 'tijmen.ruizendaal@gmail.com',
9	name		=> 'BitlBee_nick_change',
10	description 	=> 'Shows an IM nickchange in an Irssi way. (in a query and in the bitlbee channel). (For bitlbee 3.0+)',
11	license		=> 'GPLv2',
12	url		=> 'http://the-timing.nl/stuff/irssi-bitlbee',
13	changed		=> '2010-07-28'
14);
15
16my $bitlbee_server; # server object
17my @control_channels; # mostly: &bitlbee, &facebook etc.
18init();
19
20sub init { # if script is loaded after connect
21	my @servers = Irssi::servers();
22	foreach my $server(@servers) {
23		if( $server->isupport('NETWORK') eq 'BitlBee' ){
24			$bitlbee_server = $server;
25			my @channels = $server->channels();
26			foreach my $channel(@channels) {
27				if( $channel->{mode} =~ /C/ ){
28					push @control_channels, $channel->{name} unless (grep $_ eq $channel->{name}, @control_channels);
29				}
30			}
31		}
32	}
33}
34# if connect after script is loaded
35Irssi::signal_add_last('event 005' => sub {
36	my( $server ) = @_;
37	if( $server->isupport('NETWORK') eq 'BitlBee' ){
38		$bitlbee_server = $server;
39	}
40});
41# if new control channel is synced after script is loaded
42Irssi::signal_add_last('channel sync' => sub {
43	my( $channel ) = @_;
44	if( $channel->{mode} =~ /C/ && $channel->{server}->{tag} eq $bitlbee_server->{tag} ){
45		push @control_channels, $channel->{name} unless (grep $_ eq $channel->{name}, @control_channels);
46	}
47});
48
49# BEGIN bitlbee_nick_change.pl
50
51sub event_notice {
52	my ($server, $msg, $nick, $address, $target) = @_;
53	if( $server->{tag} eq $bitlbee_server->{tag} && $msg =~ /.*Changed name to.*/ ){
54		my $friendly_name = $msg;
55		$friendly_name =~ s/.*Changed name to `(.*)'.*/$1/;
56		my $window = $server->window_find_item($nick);
57		if ($window) {
58			$window->printformat(MSGLEVEL_CRAP, 'nick_change', $nick, $address, 'changed name to `'.$friendly_name.'`');
59			Irssi::signal_stop();
60		} else {
61			# TODO find control channel where this user is located and display the notice there
62			#my $window = $server->window_find_item($bitlbee_channel);
63			#$window->printformat(MSGLEVEL_CRAP, 'nick_change', $nick, $address, 'changed name to `'.$friendly_name.'`');
64			#Irssi::signal_stop();
65		}
66	}
67};
68
69Irssi::signal_add_last('message irc notice', 'event_notice');
70Irssi::theme_register(['nick_change', '{channick_hilight $0} [$1] $2']);
71
72# END bitbee_nick_change.pl
73