launcher.pl 4.83 KB
Newer Older
1
#
2
# Copyright (c) 2009-2011 by Sebastien Helleu <flashcode@flashtux.org>
3
# Copyright (c) 2010 James Campos <james.r.campos@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# 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.
21
# (this script requires WeeChat 0.3.0 or newer)
22 23 24
#
# History:
#
25 26
# 2017-08-29, Alex Xu (Hello71) <alex_y_xu@yahoo.ca>:
#     version 0.6: fix escaping of "signal_data"
27 28 29
# 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>:
30
#     version 0.4: escape quotes in $signal_data, fix example with notify-send
31
# 2010-05-28, Sebastien Helleu <flashcode@flashtux.org>:
32
#     version 0.3: add "$signal_data" in command (thanks to James Campos)
33
# 2009-05-02, Sebastien Helleu <flashcode@flashtux.org>:
34
#     version 0.2: sync with last API changes
35
# 2009-02-03, Sebastien Helleu <flashcode@flashtux.org>:
36 37 38 39 40
#     version 0.1: initial release
#

use strict;

41
my $version = "0.6";
42
my $command_suffix = " &";
43 44 45 46

weechat::register("launcher", "FlashCode <flashcode\@flashtux.org>", $version, "GPL3",
                  "Launch external commands for signals", "", "");
weechat::hook_command("launcher", "Associate external commands to signals",
47
                      "<signal> <command> || -del <signal>",
48
                      " signal: name of signal, may begin or end with \"*\" to catch many signals (common signals are: \"weechat_highlight\", \"weechat_pv\")\n"
49
                      ."command: command to launch when this signal is received (see below)\n"
50
                      ."   -del: delete commande associated to a signal\n\n"
51 52
                      ."Notes about command:\n"
                      ."- you can separate many commands with \";\"\n"
53
                      ."- string \"\$signal_data\" is replaced by signal data, enclosed with quotes (special chars are escaped)\n\n"
54 55 56 57 58 59
                      ."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"
60 61
                      ."    /launcher -del weechat_highlight\n"
                      ."  show messages in popups (requires Awesome):\n"
62
                      ."    /launcher weechat_highlight echo \$signal_data | sed 's/\\t/\" \"/' | xargs notify-send\n\n"
63 64
                      ."For advanced users: it's possible to change commands with /set command:\n"
                      ."  /set plugins.var.perl.launcher.signal.weechat_highlight \"my command here\"",
65 66
                      "", "launcher_cmd", "");
weechat::hook_signal("*", "signal", "");
67 68 69

sub launcher_cmd
{
70 71 72
    my ($data, $buffer, $args) = ($_[0], $_[1], $_[2]);
    
    if ($args =~ /([^ ]+) (.*)/)
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
    {
        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
    {
102
        weechat::command($buffer, "/set plugins.var.perl.launcher.*");
103 104 105 106 107 108
    }
    return weechat::WEECHAT_RC_OK;
}

sub signal
{
109 110
    my ($signal, $signal_data) = ($_[1], $_[2]);
    
111
    my $command = weechat::config_get_plugin("signal.$_[1]");
112 113
    if ($command ne "")
    {
114 115
        $signal_data =~ s/'/'\''/g;
        $command =~ s/\$signal_data/'$signal_data'/g;
116 117
        system($command.$command_suffix);
    }
118 119
    return weechat::WEECHAT_RC_OK;
}