/usr/share/weechat/perl/script.pl is in weechat-scripts 20131007-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 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 259 260 261 262 263 264 265 266 267 268 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | #
# Copyright (c) 2011/12 by Nils Görs <weechatter@arcor.de>
#
# will load/reload/unload script (language independent)
#
# 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/>.
#
# Development is currently hosted at
# https://github.com/weechatter/weechat-scripts
#
# History:
# 2012-08-14: nils_2 <weechatter@arcor.de>:
# version 1.3: add: recognition of new script-plugin
# 2012-03-24: nils_2 <weechatter@arcor.de>:
# version 1.2: fix: invalid pointer for function infolist_get()
# 2012-02-11: nils_2 <weechatter@arcor.de>:
# version 1.1: fix: rephrase of text output
# 2012-02-06: nils_2 <weechatter@arcor.de>:
# version 1.0: complete rewrite of script.
# : add: multi-script capability
# 2012-01-29: nils_2 <weechatter@arcor.de>:
# version 0.7: add; confirmation for auto(un)load option and error messages added
# : fix: not installed script was loaded using option "unload"
# : fix: script with full pathname wasn't loaded from a different path than homepath
# 2012-01-17: nils_2 <weechatter@arcor.de>:
# version 0.6: fix: a error message appeared when "autoload" was used with already installed script
# 2011-11-05: nils_2 <weechatter@arcor.de>:
# version 0.5: added: support for guile script
# : added: option "force_reload" (default: off)
# 2011-09-08: Banton <fbesser@gmail.com> & nils_2:
# version 0.4: added: auto(un)load (by Banton)
# : added: options autoload_load and autounload_unload
# : added: plugin description
# : WeeChat error message occurs twice when a script was loaded
# : invalid command will be catched.
# 2011-09-08: nils_2 <weechatter@arcor.de>:
# version 0.3: added: command force_reload (idea by FiXato)
# : script extension in function "list" added (idea by FlashCode)
# 2011-09-06: Banton@freenode.#weechat:
# version 0.2: added: completion for "/script load"
# 2011-08-21: nils_2 <weechatter@arcor.de>:
# version 0.1: proof-of-concept
#
# Development is currently hosted at
# https://github.com/weechatter/weechat-scripts
use strict;
use File::Basename;
my $PRGNAME = "script";
my $VERSION = "1.3";
my $AUTHOR = "Nils Görs <weechatter\@arcor.de>";
my $LICENCE = "GPL3";
my $DESCR = "to load/reload/unload script (language independent) and also to create/remove symlink";
# internal values
my $weechat_version = "";
my $home_dir = "";
my %script_suffix = (
"python_script" => ".py",
"perl_script" => ".pl",
"ruby_script" => ".rb",
"tcl_script" => ".tcl",
"lua_script" => ".lua",
"guile_script" => ".scm",
);
my %script_counter= (
"python_script" => 0,
"perl_script" => 0,
"ruby_script" => 0,
"tcl_script" => 0,
"lua_script" => 0,
"guile_script" => 0,
);
# default values
my %options = ("autoload_load" => ["off","load script after a symlink was created (default: off)"],
"autounload_unload" => ["off","unload script after a symlink was removed (default: off)"],
"force_reload" => ["off","load the given script, if script is not installed and command \"reload\" is used (default: off)"],
);
# -----------------------------[ main commands ]-----------------------------------
sub list_scripts
{
my $str = "";
my $color1 = weechat::color(weechat::config_color(weechat::config_get("weechat.color.chat_buffer")));
my $color_reset = weechat::color("reset");
foreach my $script_suffix (keys %script_suffix){
$script_counter{$script_suffix} = 0;
my $infolist = weechat::infolist_get($script_suffix,"","");
while (weechat::infolist_next($infolist))
{
my $name = weechat::infolist_string($infolist, "name");
my $version = weechat::infolist_string($infolist, "version");
my $description = weechat::infolist_string($infolist, "description");
my $output = sprintf("%s %s%s %s %s - %s",$color1,$name,$script_suffix{$script_suffix},$color_reset,$version,$description);
$script_counter{$script_suffix} ++;
weechat::print("",$output);
}
weechat::infolist_free($infolist);
}
my $total = 0;
while (my ($script,$count) = each (%script_counter))
{
$total = $total + $count;
$str .= $color1 . $script . ": " . $color_reset . $count . ", ";
}
weechat::print("","\n" . $str . $color1 . "total: " . $color_reset . $total);
}
sub load_script
{
my ($command,$script,$mute,$all) = ($_[0],$_[1],$_[2],$_[3]);
my $short_script_name = check_if_script_is_installed($script);
if ( $short_script_name eq "" ) # script not installed!
{
my $execute_command = script_loader($command,$script,$mute,$all);
if ( $execute_command eq "" )
{
weechat::print("",weechat::prefix("error")."$PRGNAME: \"$command\" error. script with name \"$script\" not found.") if ($mute ne "-mute");
return;
}
weechat::command("","/wait 1ms $execute_command");
}else
{
weechat::print("",weechat::prefix("error")."$PRGNAME: \"$command\" error. script with name \"$short_script_name\" already installed. use: /$PRGNAME reload $short_script_name") if ($mute ne "-mute");
}
}
sub reload_script
{
my ($command,$script,$mute,$all,$force) = ($_[0],$_[1],$_[2],$_[3],$_[4]);
if ( $script eq "-all" or $all eq "-all")
{
script_re_unload_cb($command,"",$mute,$all);
return;
}
my $short_script_name = check_if_script_is_installed($script);
if ( $short_script_name eq "" ) # script not installed!
{
if ( $options{force_reload}[0] eq "on" or $force eq "-force") # use force_reload ??
{
$command = "load";
my $execute_command = script_loader($command,$script,$mute,$all);
if ( $execute_command ne "" )
{
# weechat::print("","command: $command script: $script exe: $execute_command");
weechat::command("","/wait 1ms $execute_command");
}else
{
$short_script_name = $script if ( $short_script_name eq "" );
weechat::print("",weechat::prefix("error")."$PRGNAME: \"$command\" error. script with name \"$short_script_name\" not found.");
}
}else
{
weechat::print("",weechat::prefix("error")."$PRGNAME: \"$command\" error. script \"$script\" not loaded. You should either use \"/$PRGNAME load $script\" or use the \"-force\" argument.");
}
}else # reload script!
{
my $execute_command = script_re_unload_cb($command,$short_script_name,$mute,$all);
if ( $execute_command ne "" )
{
# weechat::print("","command_reload: $command script: $script exe: $execute_command");
weechat::command("","/wait 1ms $execute_command");
}
}
}
sub unload_script
{
my ($command,$script,$mute,$all,$force) = ($_[0],$_[1],$_[2],$_[3],$_[4]);
if ( $script eq "-all" or $all eq "-all")
{
script_re_unload_cb($command,"",$mute,$all);
return;
}
my $short_script_name = check_if_script_is_installed($script);
if ( $short_script_name eq "" ) # script not installed!
{
weechat::print("",weechat::prefix("error")."$PRGNAME: \"$command\" error. script \"$script\" not installed.") if ($mute ne "-mute");
}else
{
my $execute_command = script_re_unload_cb($command,$short_script_name,$mute,$all);
if ( $execute_command ne "" )
{
# weechat::print("","command_unload: $command script: $script exe: $execute_command");
weechat::command("","/wait 1ms $execute_command");
}
}
}
sub autoload_script
{
my ($command,$script,$mute,$all) = ($_[0],$_[1],$_[2],$_[3]);
my @files = get_all_scripts("autoload");
unless ( grep m/\/$script(.pl|.py|.rb|.tcl|.lua|.scm)$/ig, @files)
{
weechat::print("",weechat::prefix("error") . "$PRGNAME: \"$script\" not found.") if ($mute ne "-mute");
return;
}
foreach my $filename (@files)
{
if ($filename =~ /\/$script(.pl|.py|.rb|.tcl|.lua|.scm)/)
{
if ( $command eq "autoload" )
{
my $suffix = ($filename =~ m{([^.]+)$} )[0]; # get suffix
$suffix = ($filename =~ m{.*Logger$})[0] unless($suffix);
my (undef,$path) = fileparse $filename; # remove path
# if symlink don't exists, create one.
unless (-e $path . "/autoload/" . $script . "." . $suffix)
{
weechat::print("",weechat::prefix("error") . "script: \"$script\" will be auto loaded.") if ($mute ne "-mute");
symlink($filename , $path . "/autoload/" . $script . "." . $suffix)
}
if ( $options{autoload_load}[0] eq "on" )
{
my $execute_command = "";
my $short_script_name = check_if_script_is_installed($script);
if ( $short_script_name eq "" ) # script not installed!
{
$execute_command = script_loader("load",$script,$mute,$all);
}
if ( $execute_command ne "" )
{
weechat::command("","/wait 1ms $execute_command");
}
}
}
}
}
}
sub autounload_script
{
my ($command,$script,$mute,$all) = ($_[0],$_[1],$_[2],$_[3]);
my @files = get_all_scripts("autounload");
unless ( grep m/\/$script(.pl|.py|.rb|.tcl|.lua|.scm)$/ig, @files)
{
weechat::print("",weechat::prefix("error") . "$PRGNAME: symlink for \"$script\" not found.") if ($mute ne "-mute");
return;
}
foreach my $filename (@files)
{
if ($filename =~ /\/$script(.pl|.py|.rb|.tcl|.lua|.scm)/)
{
if ( $command eq "autounload" )
{
my $suffix = ($filename =~ m{([^.]+)$} )[0]; # get suffix
$suffix = ($filename =~ m{.*Logger$})[0] unless($suffix);
my (undef,$path) = fileparse $filename; # remove path
# if symlink exists, delete it.
if (-e $path . $script . "." . $suffix)
{
unlink $path . $script . "." . $suffix;
weechat::print("",weechat::prefix("error") . "script \"$script\" will no longer be auto loaded.") if ($mute ne "-mute");
}
if ( $options{autounload_unload}[0] eq "on" )
{
my $execute_command = "";
my $short_script_name = check_if_script_is_installed($script);
if ( $short_script_name ne "" ) # script installed!
{
unload_script("unload",$script,$mute,$all); # unload script
}
}
}
}
}
}
# -----------------------------[ subroutines ]-----------------------------------
sub get_all_scripts
{
my $command = $_[0];
my $path;
my @files;
while (my ($plugin,$suffix) = each (%script_suffix))
{
($plugin,undef) = split(/_/,$plugin);
$path = $home_dir . "/" . $plugin . "/*" .$suffix if ($command eq "autoload");
$path = $home_dir . "/" . $plugin . "/autoload" . "/*" .$suffix if ($command eq "autounload");
my @files_glob = glob($path);
if (@files_glob ne "")
{
foreach my $filename (@files_glob)
{
push(@files,$filename);
}
}
}
return @files;
}
sub check_if_script_is_installed
{
my ( $script ) = ($_[0]);
my $path = "";
if (index($script,"/") >= 0) # /path/to/script given?
{
($script,$path) = fileparse $script; # remove path
}
$script =~ s/\.[^.]+$// if (index($script,"/") == -1); # delete suffix from scriptname if no "/" in name
# check if script is already installed.
foreach my $plugin (keys %script_suffix)
{
my $infolist = weechat::infolist_get( $plugin, "", $script );
weechat::infolist_next($infolist);
my $script_found = weechat::infolist_string( $infolist, "name" ) eq $script;
weechat::infolist_free($infolist);
if ( $script_found eq "1" )
{
return $script;
}
}
return "";
}
sub script_loader{
my ($command,$script,$mute,$all) = ($_[0],$_[1],$_[2],$_[3]);
my $execute_command = "";
my %script_suffix_bak = %script_suffix;
# full script path given by user
if (index($script,"/") >= 0)
{
while (my ($plugin,$suffix) = each (%script_suffix_bak))
{
my ($plugin,undef) = split(/_/,$plugin);
if ( index($script,$suffix) >= 0 )
{
$execute_command = "/$plugin $command $script" if ($mute eq "");
$execute_command = "/mute $plugin $command $script" if ($mute eq "-mute");
return ($execute_command);
}
}
}else
{
my @files;
$script =~ s/\.[^.]+$//; # delete suffix
while (my ($plugin,$suffix) = each (%script_suffix_bak)){
my ($plugin,undef) = split(/_/,$plugin);
@files = glob($home_dir . "/" . $plugin . "/*" .$suffix);
foreach my $file (@files)
{
if ( index($file,$script . $suffix) ne "-1" )
{
$execute_command = "/$plugin $command $script"."$suffix" if ($mute eq "");
$execute_command = "/mute $plugin $command $script"."$suffix" if ($mute eq "-mute");
return ($execute_command);
}
}
}
}
return $execute_command;
}
sub script_re_unload_cb
{
my ($command,$script,$mute,$all) = ($_[0],$_[1],$_[2],$_[3]);
my $execute_command = "";
foreach my $script_suffix (keys %script_suffix)
{
my $infolist = weechat::infolist_get($script_suffix,"","");
while (weechat::infolist_next($infolist)){
my $name = weechat::infolist_string($infolist, "name");
if ( $all eq "-all"){
my ($plugin,undef) = split(/_/,$script_suffix);
$execute_command = "/$plugin $command $name" if ($mute eq "");
$execute_command = "/mute $plugin $command $name" if ($mute eq "-mute");
# weechat::print("","/wait 1ms $execute_command");
weechat::command("","/wait 1ms $execute_command") if ( $execute_command ne "");
}elsif( lc($name) eq lc($script) ){
my ($plugin,undef) = split(/_/,$script_suffix);
$execute_command = "/$plugin $command $name" if ($mute eq "");
$execute_command = "/mute $plugin $command $name" if ($mute eq "-mute");
last;
}
}
weechat::infolist_free($infolist);
}
return $execute_command;
}
# -----------------------------[ command callback ]-----------------------------------
sub my_command_cb
{
my ($getargs) = lc($_[2]);
my $execute_command = "";
return weechat::WEECHAT_RC_OK if ($getargs eq "");
my @args=split(/ /, $getargs);
my @commands = qw(list autoload autounload load reload unload);
my $command = (grep /^$args[0]$/ig, @commands); # search for a command in first option
# error checks...
if ( $command == 0 )
{
weechat::print("",weechat::prefix("error")."$PRGNAME: Yoda says: \"$args[0]\" is not a valid command. \“Do or do not... there is no try\”...");
return weechat::WEECHAT_RC_OK;
}
if ($args[0] eq "list")
{
list_scripts();
return weechat::WEECHAT_RC_OK;
}
if ( not defined $args[1] )
{
weechat::print("",weechat::prefix("error")."$PRGNAME: \"$args[0]\" error. Obi-Wan says: You did not specified a script my young padawn...");
return weechat::WEECHAT_RC_OK;
}
# get additional options
my $args_m = "";
my $args_a = "";
my $args_f = "";
$args_m = "-mute" if ( grep /^-mute$/i, @args );
$args_a = "-all" if ( grep /^-all$/i, @args );
$args_f = "-force" if ( grep /^-force$/i, @args );
my @script_array = grep !/(-all|-mute|-force)/, @args; # remove additional options from script list
push @script_array, "-all" if ( $args_a eq "-all" );
my $i = 0;
while ($i < $#script_array)
{
$i++;
$args[1] = $script_array[$i];
$i = $#script_array if ( $args_a eq "-all" );
# load, unload, reload, autoload, autounload
if ( $args[0] eq "load" )
{
load_script($args[0],$args[1],$args_m,$args_a);
}elsif ( $args[0] eq "reload" )
{
reload_script($args[0],$args[1],$args_m,$args_a,$args_f);
}elsif ( $args[0] eq "unload" )
{
unload_script($args[0],$args[1],$args_m,$args_a,$args_f);
}elsif ( $args[0] eq "autoload" )
{
autoload_script($args[0],$args[1],$args_m,$args_a,$args_f);
}elsif ( $args[0] eq "autounload" )
{
autounload_script($args[0],$args[1],$args_m,$args_a,$args_f);
}
}
return weechat::WEECHAT_RC_OK;
}
# -----------------------------[ completion callback ]-----------------------------------
sub script_completion_cb
{
my ($data,$completion_item,$buffer,$completion) = ($_[0],$_[1],$_[2],$_[3]);
my @files;
while (my ($plugin,$suffix) = each (%script_suffix)){
my ($plugin,undef) = split(/_/,$plugin);
@files = glob($home_dir . "/" . $plugin . "/*" .$suffix);
foreach my $file (@files) {
my $basename = basename($file, $suffix);
weechat::hook_completion_list_add($completion, $basename, 0, weechat::WEECHAT_LIST_POS_SORT);
}
}
return weechat::WEECHAT_RC_OK
}
# -----------------------------[ config ]-----------------------------------
sub init_config
{
foreach my $option (keys %options){
if (!weechat::config_is_set_plugin($option)){
weechat::config_set_plugin($option, $options{$option}[0]);
}else{
$options{$option}[0] = weechat::config_get_plugin($option);
}
if ( ($weechat_version ne "") && (weechat::info_get("version_number", "") >= 0x00030500) ){
weechat::config_set_desc_plugin($option, $options{$option}[1]);
}
}
}
sub toggle_config_by_set
{
my ($pointer, $name, $value) = @_;
$name = substr($name, length("plugins.var.perl.$PRGNAME."), length($name));
$options{$name}[0] = lc($value);
# insert a refresh here
return weechat::WEECHAT_RC_OK;
}
# -------------------------------[ init ]-------------------------------------
# first function called by a WeeChat-script.
weechat::register($PRGNAME, $AUTHOR, $VERSION,
$LICENCE, $DESCR, "", "");
$weechat_version = weechat::info_get("version_number", "");
$home_dir = weechat::info_get ("weechat_dir", "");
# check for script plugin...
my $scrip_plugin = 0;
my $infolist = weechat::infolist_get("plugin","","script");
weechat::infolist_next($infolist);
$scrip_plugin = 1 if ( "script" eq weechat::infolist_string($infolist,"name"));
weechat::infolist_free($infolist);
if ($scrip_plugin == 1)
{
weechat::print("",weechat::prefix("error") . "script plugin is installed. No need to install script: $PRGNAME.pl. Script automatically removed...");
weechat::command("","/wait 1ms /script unload $PRGNAME".".pl");
}
else
{
weechat::hook_command($PRGNAME, $DESCR,
"load <script> || reload <script> -force || unload <script> || autoload <script> || autounload <script> || list || -all || -mute\n",
" list : list all installed scripts (by plugin)\n".
" load <script> : load <script> (no suffix needed)\n".
" reload <script> : reload <script>\n".
" -force : tries to reload a script first and will load the script if not loaded (mainly for programmers)\n".
" unload <script> : unload <script>\n".
" autoload <script> : creates a symlink to start automatically a script at weechat startup\n".
" autounload <script> : remove symlink from autoload\n".
" -all : unload/reload *all* scripts\n".
" -mute : execute command silently\n".
"\n".
"$PRGNAME will only create/remove a symlink in \"~/.weechat/<language>/autoload\"\n".
"to remove/install scripts permanently use \"weeget.py\" script (http://www.weechat.org/files/scripts/weeget.py)\n".
"\n".
"You can do an action on multiple scripts at once.\n".
"\n".
"Examples:\n".
" reload script buddylist:\n".
" /$PRGNAME reload buddylist\n".
" load several scripts at once:\n".
" /$PRGNAME load buddylist colorize_lines buffers\n".
" force to reload/load script buddylist:\n".
" /$PRGNAME force_reload buddylist\n".
" create symlink for script weeget\n".
" /$PRGNAME autoload weeget\n".
"",
"list %-".
"||load %(all_scripts)|-mute|%*".
"||reload %(python_script)|%(perl_script)|%(ruby_script)|%(tcl_script)|%(lua_script)|%(guile_script)|-all|-force|-mute|%*".
"||unload %(python_script)|%(perl_script)|%(ruby_script)|%(tcl_script)|%(lua_script)|%(guile_script)|-all|-mute|%*".
"||autoload %(all_scripts)|-mute|%*".
"||autounload %(all_scripts)|-mute|%*",
"my_command_cb", "");
weechat::hook_completion("all_scripts", "all scripts in script directory", "script_completion_cb", "");
weechat::hook_config("plugins.var.perl.$PRGNAME.*", "toggle_config_by_set", "");
init_config();
}
|