hotlist2extern.pl 11.5 KB
Newer Older
1 2 3 4
# Copyright (c) 2009-2010 by Nils Görs <weechatter@arcor.de>
#
# waiting for hotlist to change and then execute a user specified command
# or writes the hotlist to screen title.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# I am using this script to display the hotlist with STDIN plasmoid on
# KDE desktop.
# http://www.kde-look.org/content/show.php/STDIN+Plasmoid?content=92309
#
23
# Script inspirated and tested by LaoLang_cool
24
#
25
# 0.8	: escape special characters in hotlist (arza)
26 27 28 29
# 0.7	: using %h for weechat-dir instead of hardcoded path in script (flashcode)
# 0.6	: new option "use_title" to print hotlist in screen title.
# 0.5	: lot of internal changes
# 0.4	: highlight_char can be set as often as you want
30
#	: merged buffer will be displayed once
31
#	: more than one metachar-highlight produced a perl error
32
# 0.3	: usersettings won't be loaded, sorry! :-(
33 34 35 36 37
#	: added a more complex sort routine (from important to unimportant and also numeric)
#	: added options: "delimiter", "priority_remove" and "hotlist_remove_format"
#
#
# use the following settings for hotlist_format:
38
# %h = weechat-dir (~/.weechat)
39 40 41
# %H = filled with highlight_char if a highlight message was written in channel. For example: *
# %N = filled with buffer number: 1 2 3 ....
# %S = filled with short name of channel: #weechat
42
#
43
# export the hotlist_format to your external command.
44 45 46
# %X
#
# Usage:
47 48 49
# template to use for display (for example: "1:freenode *2:#weechat"):
# /set plugins.var.perl.hotlist2extern.hotlist_format  "%H%N:%S"
#
50
# Output (for example: "WeeChat Act: %H%N:%S"):
51
# /set plugins.var.perl.hotlist2extern.external_command_hotlist "echo WeeChat Act: %X >%h/hotlist_output.txt"
52
#
53
# Output if there is no activity (for example: "WeeChat: no activity"):
54 55
# /set plugins.var.perl.hotlist2extern.external_command_hotlist_empty "echo 'WeeChat: no activity ' >%h/hotlist_output.txt"
#
56 57
# charset for a highlight message:
# /set plugins.var.perl.hotlist2extern.highlight_char  "*"
58
#
59 60 61
# template that shall be remove when message priority is low. (for example, the buffer name will be removed and only the buffer
# number will be display instead! (1 *2:#weechat):
# /set plugins.var.perl.hotlist2extern.hotlist_remove_format ":%S"
62 63 64 65
#
# message priority when using hotlist_remove_format (-1 means off)
# /set plugins.var.perl.hotlist2extern.priority_remove 0
#
66 67 68
# display messages with level:
# 0=crappy msg (join/part) and core buffer informations, 1=msg, 2=pv, 3=nick highlight
# /set plugins.var.perl.hotlist2extern.lowest_priority  0
69 70
#
# delimiter to use:
71
# /set plugins.var.perl.hotlist2extern.delimiter ","
72 73 74
#
# hotlist will be printed to screen title:
# /set plugins.var.perl.hotlist2extern.use_title "on"
75 76 77

use strict;
my $hotlist_format		= "%H%N:%S";
78
my $hotlist_remove_format	= ":%S";
79
my $external_command_hotlist	= "echo WeeChat Act: %X >%h/hotlist_output.txt";
80
my $external_command_hotlist_empty	= "echo \'WeeChat: no activity \' >%h/hotlist_output.txt";
81 82
my $highlight_char		= "*";
my $lowest_priority		= 0;
83 84
my $priority_remove		= 0;
my $delimiter			= ",";
85 86
my $use_title			= "on";

87
my $prgname	= "hotlist2extern";
88
my $version	= "0.8";
89
my $description	= "Give hotlist to an external file/program/screen title";
90 91 92
my $current_buffer = "";

my $plugin_name		= "";
93
my $weechat_dir		= "";
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
my $buffer_name		= "";
my $buffer_number	= 0;
my $buffer_pointer	= 0;
my $short_name		= "";
my $res 		= "";
my $res2 		= "";
my $priority		= 0;
my @table		= ();
my $table		= "";

sub hotlist_changed{
my ($data, $buffer, $args) = ($_[0], $_[1], $_[2]);			# save callback from hook_signal
@table		= ();
$table		= "";

  $current_buffer = weechat::current_buffer;				# get current buffer
  my $hotlist = weechat::infolist_get("hotlist","","");			# Pointer to Infolist

   while (weechat::infolist_next($hotlist))
    {
        $priority = weechat::infolist_integer($hotlist, "priority");
	  $res = $hotlist_format;							# save hotlist format
	  $res2 = $external_command_hotlist;						# save external_hotlist format

	    $plugin_name = weechat::infolist_string($hotlist,"plugin_name");
	    $buffer_name = weechat::infolist_string($hotlist,"buffer_name");
120
	    $buffer_number = weechat::infolist_integer($hotlist,"buffer_number");	# get number of buffer
121 122 123 124 125 126 127 128 129 130
	    $buffer_pointer = weechat::infolist_pointer($hotlist, "buffer_pointer");	# Pointer to buffer
	    $short_name = weechat::buffer_get_string($buffer_pointer, "short_name");	# get short_name of buffer

	unless ($priority < $lowest_priority){
	  create_output();
	}
    }
  weechat::infolist_free($hotlist);
	    $table = @table;
	    if ($table eq 0){
131
	      unless ($external_command_hotlist_empty eq ""){				# does we have a command for empty string?
132 133 134
		if ($use_title eq "on"){
		  weechat::window_set_title($external_command_hotlist_empty);
		}else{
135 136 137
		if (grep (/\%h/,$external_command_hotlist_empty)){			# does %h is in string?
		  $external_command_hotlist_empty =~ s/%h/$weechat_dir/;		# add weechat-dir
		}
138 139
		  system($external_command_hotlist_empty);
		}
140 141 142 143 144 145 146 147
	      }
	    }
  return weechat::WEECHAT_RC_OK;
}

sub create_output{
	  $res = $hotlist_format;							# save hotlist format
	  $res2 = $external_command_hotlist;						# save external_hotlist format
148

149 150
	    if ($priority == 3){							# priority is highlight
	      if (grep (/\%H/,$hotlist_format)){					# check with original!!!
151
		$res =~ s/\%H/$highlight_char/g;
152 153
	      }
	    }else{									# priority != 3
154
		$res =~ s/\%H//g;							# remove all %H
155
	    }
156 157
   if ($priority <= $priority_remove){
	      $res =~ s/$hotlist_remove_format//;					# remove hotlist_remove_format
158
	    if (grep (/\%S/,$hotlist_format)){						# does %S is in string? (check with original!!!)
159
		  $res =~ s/%S/$short_name/;						# add short_name
160
	    }
161
 	    if (grep (/\%N/,$hotlist_format)){
162 163
	      $res =~ s/%N/$buffer_number/;						# add buffer_number
	    }
164
   }else{
165
	    if (grep (/\%S/,$hotlist_format)){						# does %S is in string? (check with original!!!)
166 167 168 169 170 171
		  $res =~ s/%S/$short_name/;						# add short_name
	    }
 	    if (grep (/\%N/,$hotlist_format)){
	      $res =~ s/%N/$buffer_number/;						# add buffer_number
	    }
    }
172 173 174 175 176 177
	    if ($res ne $hotlist_format and $res ne ""){				# did $res changed?
		my $res2 = $res;							# save search string.
		$res2=qq(\Q$res2);							# kill metachars, for searching first
		unless (grep /^$res2$/, @table){					# does we have added $res to @table?
		push (@table, $res);							# No, then add it to @table
	      }
178 179 180 181
	    }

	    $res=qq(\Q$res);								# kill metachars first
	    if (grep /^$res$/, @table){							# does we have added $res to @table?
182
	      my $export = join("$delimiter", sort_routine(@table));
183
	      $export = qq(\Q$export);							# escape special characters
184 185
	      if (grep (/\%X/,$external_command_hotlist)){				# check for %X option.
		$res2 =~ s/%X/$export/;
186

187 188 189 190
		if (grep (/\%h/,$external_command_hotlist)){				# does %h is in string?
		  $res2 =~ s/%h/$weechat_dir/;						# add weechat-dir
		}

191 192 193
		if ($use_title eq "on"){
		  weechat::window_set_title($res2);
		}else{
194
	    	system($res2);
195 196
		}

197 198 199 200
	      }
	    }
}

201 202 203
# first sort channels with highlight, then channels with
# action and the rest will be put it at the end of list
sub sort_routine {
204
  my @zeilen = @_;
205 206
  my @sortiert = map { $_->[0] }
  map { [$_,(split (/\*/,$_))[1]] } @zeilen ;
207
  sort { $a->[0] cmp $b->[0] }		#sort{$a<=>$b}(@zeilen);
208 209 210
return @sortiert;
}

211 212 213 214 215 216 217 218
sub _extern{
  my ($data) = ($_[0]);
	system($data) unless($data eq "");
  return weechat::WEECHAT_RC_OK;
}

sub init{
# set value of script (for example starting script the first time)
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  if (!weechat::config_is_set_plugin("external_command_hotlist")){
    weechat::config_set_plugin("external_command_hotlist", $external_command_hotlist);
  }else{
    $external_command_hotlist = weechat::config_get_plugin("external_command_hotlist");
  }
  if (!weechat::config_is_set_plugin("external_command_hotlist_empty")){
    weechat::config_set_plugin("external_command_hotlist_empty", $external_command_hotlist_empty);
  }else{
    $external_command_hotlist_empty = weechat::config_get_plugin("external_command_hotlist_empty");
  }
  if (!weechat::config_is_set_plugin("highlight_char")){
    weechat::config_set_plugin("highlight_char", $highlight_char);
  }else{
    $highlight_char = weechat::config_get_plugin("highlight_char");
  }
  if (!weechat::config_is_set_plugin("lowest_priority")){
    weechat::config_set_plugin("lowest_priority", $lowest_priority);
  }else{
    $lowest_priority = weechat::config_get_plugin("lowest_priority");
  }
  if (!weechat::config_is_set_plugin("hotlist_format")){
    weechat::config_set_plugin("hotlist_format", $hotlist_format);
  }else{
    $hotlist_format = weechat::config_get_plugin("hotlist_format");
  }
  if (!weechat::config_is_set_plugin("hotlist_remove_format")){
    weechat::config_set_plugin("hotlist_remove_format", $hotlist_remove_format);
  }else{
    $hotlist_remove_format = weechat::config_get_plugin("hotlist_remove_format");
  }
  if (!weechat::config_is_set_plugin("priority_remove")){
    weechat::config_set_plugin("priority_remove", $priority_remove);
  }else{
    $priority_remove = weechat::config_get_plugin("priority_remove");
  }
  if (!weechat::config_is_set_plugin("delimiter")){
    weechat::config_set_plugin("delimiter", $delimiter);
  }else{
    $delimiter = weechat::config_get_plugin("delimiter");
  }
259 260 261 262 263
  if (!weechat::config_is_set_plugin("use_title")){
    weechat::config_set_plugin("use_title", $use_title);
  }else{
    $use_title = weechat::config_get_plugin("use_title");
  }
264
  $weechat_dir = weechat::info_get("weechat_dir", "");
265
}
266

267 268
sub toggle_config_by_set{
my ( $pointer, $name, $value ) = @_;
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  if ($name eq "plugins.var.perl.$prgname.external_command_hotlist"){
    $external_command_hotlist = $value;
    return weechat::WEECHAT_RC_OK;
  }
  if ($name eq "plugins.var.perl.$prgname.external_command_hotlist_empty"){
    $external_command_hotlist_empty = $value;
    return weechat::WEECHAT_RC_OK;
  }
  if ($name eq "plugins.var.perl.$prgname.highlight_char"){
    $highlight_char = $value;
    return weechat::WEECHAT_RC_OK;
  }
  if ($name eq "plugins.var.perl.$prgname.lowest_priority"){
    $lowest_priority = $value;
    return weechat::WEECHAT_RC_OK;
  }
  if ($name eq "plugins.var.perl.$prgname.hotlist_format"){
    $hotlist_format = $value;
    return weechat::WEECHAT_RC_OK;
  }
  if ($name eq "plugins.var.perl.$prgname.hotlist_remove_format"){
    $hotlist_remove_format = $value;
    return weechat::WEECHAT_RC_OK;
  }
  if ($name eq "plugins.var.perl.$prgname.priority_remove"){
    $priority_remove = $value;
    return weechat::WEECHAT_RC_OK;
  }
  if ($name eq "plugins.var.perl.$prgname.delimiter"){
    $delimiter = $value;
    return weechat::WEECHAT_RC_OK;
  }
302 303 304 305
  if ($name eq "plugins.var.perl.$prgname.use_title"){
    $use_title = $value;
    return weechat::WEECHAT_RC_OK;
  }
306
}
307

308 309 310
# first function called by a WeeChat-script.
weechat::register($prgname, "Nils Görs <weechatter\@arcor.de>", $version,
                  "GPL3", $description, "", "");
311

312
     init();				# get user settings
313

314 315
      weechat::hook_signal("hotlist_changed", "hotlist_changed", "");
      weechat::hook_config( "plugins.var.perl.$prgname.*", "toggle_config_by_set", "" );