summaryrefslogtreecommitdiffstats
path: root/scripts/act_fifo.pl
blob: c0c55d01e6c3c9e1dc5d4c0baacfe52e71ab9e53 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#
# Prints a window activity line to a fifo
#
# Based on activity_file and nicklist
#
#
# The script uses 3 variables:
#    act_fifo_width:    How many characters wide the act list can be
#    act_fifo_path:     Path to the fifo
#    act_fifo_hilight:  How to hilight the "ACT" in the beginning:
#             0: No "ACT"
#             1: "ACT" with no color
#             2: "ACT" with color
#             3: " ACT " with color and added spaces
#             
#  Changes:
#     1.1:  Now also updates fifo when config or window numbers are changed.
# 

use strict;
use Irssi qw(
	settings_get_int settings_get_str
	settings_add_int settings_add_str
	signal_add_last
	);
use Fcntl;
use IO::Handle;
use vars qw($VERSION %IRSSI);

$VERSION = "1.1";
%IRSSI =
(
	authors     => 'Daniel Kal�r (Xnybre)',
	contact		=> 'irssi@kalor.dk',
	name        => 'act_fifo',
	description => 'Print window activity to a fifo',
	license     => 'GPLv2',
	changed     => '2008-08-27'
);


my $last_values = {};

my @colors = ("","\033[0;36;40m","\033[1;37;40m","\033[1;35;40m");

sub fifo_stop
{
	close FIFO;
	Irssi::print("Fifo closed.");
}

sub item_status_changed
{
	my ($item, $oldstatus) = @_;
	

	return if ! ref $item->{server};

	my $tag = $item->{server}{tag};
	my $name = $item->{name};

	return if ! $tag || ! $name;

	store_status() if ! $last_values->{$tag}{$name} ||
	$last_values->{$tag}{$name}{level} != $item->{data_level};
}


sub store_status
{
	my $new_values = {};
	my @items = ();

	for my $window ( sort { $a->{refnum} <=> $b->{refnum} } Irssi::windows() )
	{

		for my $item ( $window->items() )
		{

			next if ! ref $item->{server};

			my $tag = $item->{server}{tag};
			my $name = $item->{name};

			next if ! $tag || ! $name || $item->{data_level} == 0;

			$new_values->{$tag}{$name} =
			{
				level => $item->{data_level},
				window => $window->{refnum},
			};

			push @items, $new_values->{$tag}{$name};
		}
	}


	print FIFO "\033[2J\033[1;1H" or fifo_stop();

	my $maxw = settings_get_int('act_fifo_width');
	my $w = 0;

	if(scalar(@items) > 0)
	{
		my $hi = settings_get_int('act_fifo_hilight');
		if($hi == 1)
		{
			print FIFO "ACT:" or fifo_stop();
			$w += 4;

		}
		elsif($hi == 2)
		{
			print FIFO "\033[1;37;41mACT:\033[0m" or fifo_stop();
			$w += 4;
		}
		elsif($hi > 2)
		{
			print FIFO "\033[1;37;41m ACT: \033[0m" or fifo_stop();
			$w += 6;
		}
	}

	for ( @items )
	{
		my $win = $_->{window};
		my $winw = int(log($win)/log(10))+1;
		if($winw + 2 + $w > $maxw)
		{
			print FIFO "\033[1;31;40mM\033[0m";
			last;
		}
		print FIFO "$colors[$_->{level}] $win\033[0m" or fifo_stop();
		$w += $winw + 1;
	}


	$last_values = $new_values;

}

settings_add_int('act_fifo','act_fifo_width',25);
settings_add_int('act_fifo','act_fifo_hilight',2);
settings_add_str('act_fifo', 'act_fifo_path', Irssi::get_irssi_dir . '/act_fifo');

## open/create FIFO
my $path = settings_get_str('act_fifo_path');
unless (-p $path)
{ # not a pipe
	if (-e _)
	{ # but a something else
		die "$0: $path exists and is not a pipe, please remove it\n";
	}
	else
	{
		require POSIX;
		POSIX::mkfifo($path, oct(666)) or die "can\'t mkfifo $path: $!";
		Irssi::print("Fifo created. Start reading it (\"cat $path\") and try again.");
		return;
	}
}
if (!sysopen(FIFO, $path, O_WRONLY | O_NONBLOCK))
{ # or die "can't write $path: $!";
	print("Couldn\'t write to the fifo ($!). Please start reading the fifo (\"cat $path\") and try again.");
	return;
}
FIFO->autoflush(1);
print FIFO "\033[2J\033[1;1H"; # erase screen & jump to 0,0

# store initial status
store_status();

signal_add_last('setup changed', \&store_status);
signal_add_last('window item activity', \&item_status_changed);
signal_add_last('window refnum changed', \&store_status);