html/act_fifo.pl


   1 #
   2 # Prints a window activity line to a fifo
   3 #
   4 # Based on activity_file and nicklist
   5 #
   6 #
   7 # The script uses 3 variables:
   8 #    act_fifo_width:    How many characters wide the act list can be
   9 #    act_fifo_path:     Path to the fifo
  10 #    act_fifo_hilight:  How to hilight the "ACT" in the beginning:
  11 #             0: No "ACT"
  12 #             1: "ACT" with no color
  13 #             2: "ACT" with color
  14 #             3: " ACT " with color and added spaces
  15 #             
  16 #  Changes:
  17 #     1.1:  Now also updates fifo when config or window numbers are changed.
  18 # 
  19 
  20 use Irssi qw(
  21 	settings_get_int settings_get_str
  22 	settings_add_int settings_add_str
  23 	signal_add_last
  24 	);
  25 use Fcntl;
  26 use IO::Handle;
  27 use vars qw($VERSION %IRSSI);
  28 
  29 $VERSION = "1.1";
  30 %IRSSI =
  31 (
  32 	authors     => 'Daniel Kalør (Xnybre)',
  33 	contact		=> 'irssi@kalor.dk',
  34 	name        => 'act_fifo',
  35 	description => 'Print window activity to a fifo',
  36 	license     => 'GPLv2',
  37 	changed     => '2008-08-27'
  38 );
  39 
  40 
  41 my $last_values = {};
  42 
  43 my @colors = ("","\033[0;36;40m","\033[1;37;40m","\033[1;35;40m");
  44 
  45 sub fifo_stop
  46 {
  47 	close FIFO;
  48 	Irssi::print("Fifo closed.");
  49 }
  50 
  51 sub item_status_changed
  52 {
  53 	my ($item, $oldstatus) = @_;
  54 	
  55 
  56 	return if ! ref $item->{server};
  57 
  58 	my $tag = $item->{server}{tag};
  59 	my $name = $item->{name};
  60 
  61 	return if ! $tag || ! $name;
  62 
  63 	store_status() if ! $last_values->{$tag}{$name} ||
  64 	$last_values->{$tag}{$name}{level} != $item->{data_level};
  65 }
  66 
  67 
  68 sub store_status
  69 {
  70 	my $new_values = {};
  71 	my @items = ();
  72 
  73 	for my $window ( sort { $a->{refnum} <=> $b->{refnum} } Irssi::windows() )
  74 	{
  75 
  76 		for my $item ( $window->items() )
  77 		{
  78 
  79 			next if ! ref $item->{server};
  80 
  81 			my $tag = $item->{server}{tag};
  82 			my $name = $item->{name};
  83 
  84 			next if ! $tag || ! $name || $item->{data_level} == 0;
  85 
  86 			$new_values->{$tag}{$name} =
  87 			{
  88 				level => $item->{data_level},
  89 				window => $window->{refnum},
  90 			};
  91 
  92 			push @items, $new_values->{$tag}{$name};
  93 		}
  94 	}
  95 
  96 
  97 	print FIFO "\033[2J\033[1;1H" or fifo_stop();
  98 
  99 	my $maxw = settings_get_int('act_fifo_width');
 100 	my $w = 0;
 101 
 102 	if(scalar(@items) > 0)
 103 	{
 104 		my $hi = settings_get_int('act_fifo_hilight');
 105 		if($hi == 1)
 106 		{
 107 			print FIFO "ACT:" or fifo_stop();
 108 			$w += 4;
 109 
 110 		}
 111 		elsif($hi == 2)
 112 		{
 113 			print FIFO "\033[1;37;41mACT:\033[0m" or fifo_stop();
 114 			$w += 4;
 115 		}
 116 		elsif($hi > 2)
 117 		{
 118 			print FIFO "\033[1;37;41m ACT: \033[0m" or fifo_stop();
 119 			$w += 6;
 120 		}
 121 	}
 122 
 123 	for ( @items )
 124 	{
 125 		my $win = $_->{window};
 126 		my $winw = int(log($win)/log(10))+1;
 127 		if($winw + 2 + $w > $maxw)
 128 		{
 129 			print FIFO "\033[1;31;40mM\033[0m";
 130 			last;
 131 		}
 132 		print FIFO "$colors[$_->{level}] $win\033[0m" or fifo_stop();
 133 		$w += $winw + 1;
 134 	}
 135 
 136 
 137 	$last_values = $new_values;
 138 
 139 }
 140 
 141 settings_add_int('act_fifo','act_fifo_width',25);
 142 settings_add_int('act_fifo','act_fifo_hilight',2);
 143 settings_add_str('act_fifo', 'act_fifo_path', Irssi::get_irssi_dir . '/act_fifo');
 144 
 145 ## open/create FIFO
 146 my $path = settings_get_str('act_fifo_path');
 147 unless (-p $path)
 148 { # not a pipe
 149 	if (-e _)
 150 	{ # but a something else
 151 		die "$0: $path exists and is not a pipe, please remove it\n";
 152 	}
 153 	else
 154 	{
 155 		require POSIX;
 156 		POSIX::mkfifo($path, 0666) or die "can\'t mkfifo $path: $!";
 157 		Irssi::print("Fifo created. Start reading it (\"cat $path\") and try again.");
 158 		return;
 159 	}
 160 }
 161 if (!sysopen(FIFO, $path, O_WRONLY | O_NONBLOCK))
 162 { # or die "can't write $path: $!";
 163 	print("Couldn\'t write to the fifo ($!). Please start reading the fifo (\"cat $path\") and try again.");
 164 	return;
 165 }
 166 FIFO->autoflush(1);
 167 print FIFO "\033[2J\033[1;1H"; # erase screen & jump to 0,0
 168 
 169 # store initial status
 170 store_status();
 171 
 172 signal_add_last('setup changed', \&store_status);
 173 signal_add_last('window item activity', \&item_status_changed);
 174 signal_add_last('window refnum changed', \&store_status);
 175