Script: launcher.pl

Launch external commands for signals.
Author: FlashCode — Version: 0.7 — License: GPL3
For WeeChat ≥ 0.3.0.
Tags: exec
Added: 2009-02-03 — Updated: 2017-09-09

Download GitHub Repository

  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
#
# Copyright (c) 2009-2011 by Sebastien Helleu <flashcode@flashtux.org>
# Copyright (c) 2010 James Campos <james.r.campos@gmail.com>
#
# 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/>.
#

#
# Launch external commands for signals.
# (this script requires WeeChat 0.3.0 or newer)
#
# History:
#
# 2017-09-07, Alex Xu (Hello71) <alex_y_xu@yahoo.ca>:
#     version 0.7: properly fix escaping of "signal_data"
# 2017-08-29, Alex Xu (Hello71) <alex_y_xu@yahoo.ca>:
#     version 0.6: fix escaping of "signal_data"
# 2011-02-13, Sebastien Helleu <flashcode@flashtux.org>:
#     version 0.5: use new help format for command arguments
# 2010-05-29, Sebastien Helleu <flashcode@flashtux.org>:
#     version 0.4: escape quotes in $signal_data, fix example with notify-send
# 2010-05-28, Sebastien Helleu <flashcode@flashtux.org>:
#     version 0.3: add "$signal_data" in command (thanks to James Campos)
# 2009-05-02, Sebastien Helleu <flashcode@flashtux.org>:
#     version 0.2: sync with last API changes
# 2009-02-03, Sebastien Helleu <flashcode@flashtux.org>:
#     version 0.1: initial release
#

use strict;

my $version = "0.7";
my $command_suffix = " &";

weechat::register("launcher", "FlashCode <flashcode\@flashtux.org>", $version, "GPL3",
                  "Launch external commands for signals", "", "");
weechat::hook_command("launcher", "Associate external commands to signals",
                      "<signal> <command> || -del <signal>",
                      " signal: name of signal, may begin or end with \"*\" to catch many signals (common signals are: \"weechat_highlight\", \"weechat_pv\")\n"
                      ."command: command to launch when this signal is received (see below)\n"
                      ."   -del: delete commande associated to a signal\n\n"
                      ."Notes about command:\n"
                      ."- you can separate many commands with \";\"\n"
                      ."- string \"\$signal_data\" is replaced by signal data, enclosed with quotes (special chars are escaped)\n\n"
                      ."Examples:\n"
                      ."  play a sound for highlights:\n"
                      ."    /launcher weechat_highlight alsaplay -i text ~/sound_highlight.wav\n"
                      ."  play a sound for private messages:\n"
                      ."    /launcher weechat_pv alsaplay -i text ~/sound_pv.wav\n"
                      ."  delete command for signal \"weechat_highlight\":\n"
                      ."    /launcher -del weechat_highlight\n"
                      ."  show messages in popups (requires Awesome):\n"
                      ."    /launcher weechat_highlight echo \$signal_data | sed 's/\\t/\" \"/' | xargs notify-send\n\n"
                      ."For advanced users: it's possible to change commands with /set command:\n"
                      ."  /set plugins.var.perl.launcher.signal.weechat_highlight \"my command here\"",
                      "", "launcher_cmd", "");
weechat::hook_signal("*", "signal", "");

sub launcher_cmd
{
    my ($data, $buffer, $args) = ($_[0], $_[1], $_[2]);
    
    if ($args =~ /([^ ]+) (.*)/)
    {
        if ($1 eq "-del")
        {
            my $signal = $2;
            my $command = weechat::config_get_plugin("signal.${signal}");
            if ($command ne "")
            {
                weechat::config_unset_plugin("signal.${signal}");
                weechat::print("", "launcher: command deleted for signal ${signal}");
            }
            else
            {
                weechat::print("", weechat::prefix("error")."launcher: command not defined for signal \"${signal}\"");
            }
        }
        else
        {
            my $signal = $1;
            my $command = $2;
            if ($command =~ /^"(.*)"$/)
            {
                $command = $1;
            }
            weechat::config_set_plugin("signal.${signal}", "${command}");
            weechat::print("", "launcher: signal \"${signal}\" --> command: \"${command}\"");
        }
    }
    else
    {
        weechat::command($buffer, "/set plugins.var.perl.launcher.*");
    }
    return weechat::WEECHAT_RC_OK;
}

sub signal
{
    my ($signal, $signal_data) = ($_[1], $_[2]);
    
    my $command = weechat::config_get_plugin("signal.$_[1]");
    if ($command ne "")
    {
        $signal_data =~ s/'/'\\''/g;
        $command =~ s/\$signal_data/'$signal_data'/g;
        system($command.$command_suffix);
    }
    return weechat::WEECHAT_RC_OK;
}