summaryrefslogtreecommitdiffstats
path: root/scripts/query-connection-notifier.pl
blob: 450512f863a438ca81705e29a1425e80c7d8dbbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use Irssi;
use Irssi::UI;
use Irssi::TextUI;
use vars qw($VERSION %IRSSI);

$VERSION = '0.1';
%IRSSI = (
	author      => 'meh',
	contact     => 'meh@schizofreni.co',
	name        => 'Query connection notification',
	description => 'Notify in the query window when the nick connects',
	license     => 'WTFPL',
);

Irssi::theme_register([
	'connect', '{channick_hilight $0} {chanhost_hilight $1} has connected'
]);

my %quit;

Irssi::signal_add 'message join' => sub {
	my ($server, $channel, $nick, $address) = @_;

	if ($quit{"$server->{tag}:$nick"}) {
		delete $quit{"$server->{tag}:$nick"};

		foreach $query (Irssi::queries()) {
			if ($query->{server_tag} eq $server->{tag} && $query->{name} eq $nick) {
				$query->printformat(MSGLEVEL_JOINS, 'connect', $nick, $address);

				break;
			}
		}
	}
};

Irssi::signal_add 'message quit' => sub {
	my ($server, $nick, $address, $reason) = @_;

	$quit{"$server->{tag}:$nick"} = 1;
};

Irssi::signal_add 'query created' => sub {
	my ($query, $automatic) = @_;

	foreach $channel ($query->{server}->channels) {
		foreach $nick ($channel->nicks) {
			if ($nick eq $query->{name}) {
				return;
			}
		}
	}

	$quit{"$query->{server}->{tag}:$query->{name}"} = 1;
};

Irssi::signal_add 'query destroyed' => sub {
	my ($query) = @_;

	delete $quit{"$query->{server}->{tag}:$query->{name}"};
};

Irssi::signal_add 'message private' => sub {
	my ($server, $msg, $nick, $address) = @_;

	delete $quit{"$server->{tag}:$nick"};
};