sound.pl 3.44 KB
Newer Older
ptitlouis's avatar
ptitlouis committed
1
#
2
# Copyright (c) 2006-2009 by FlashCode <flashcode@flashtux.org>
ptitlouis's avatar
ptitlouis committed
3 4 5
#
# 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
6
# the Free Software Foundation; either version 3 of the License, or
ptitlouis's avatar
ptitlouis committed
7 8 9 10 11 12 13 14
# (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
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ptitlouis's avatar
ptitlouis committed
16 17 18
#

#
19
# Play a sound for IRC "CTCP SOUND" message.
20
# (this script requires WeeChat 0.3.0 or newer)
ptitlouis's avatar
ptitlouis committed
21 22 23
#
# History:
#
24 25
# 2009-05-02, FlashCode <flashcode@flashtux.org>:
#     version 0.7: sync with last API changes
26
# 2009-02-03, FlashCode <flashcode@flashtux.org>:
27
#     version 0.6: conversion to WeeChat 0.3.0+
28
#                  move commands for highlight and pv to new script launcher.pl
29 30 31
# 2007-09-17, FlashCode <flashcode@flashtux.org>:
#     version 0.5: fixed security problem with message parsing and execution
#                  of system command
32 33
# 2007-08-10, FlashCode <flashcode@flashtux.org>:
#     version 0.4: upgraded licence to GPL 3
ptitlouis's avatar
ptitlouis committed
34 35 36 37 38 39 40 41
# 2006-05-30, FlashCode <flashcode@flashtux.org>:
#     added plugin options for commands
# 2004-10-01, FlashCode <flashcode@flashtux.org>:
#     initial release
#

use strict;

42
my $version = "0.7";
ptitlouis's avatar
ptitlouis committed
43 44
my $command_suffix = " >/dev/null 2>&1 &";

45 46 47
# default values in setup file (~/.weechat/plugins.conf)
my $default_cmd_ctcp        = "alsaplay -i text \$filename";
my $default_sound_extension = ".wav";
ptitlouis's avatar
ptitlouis committed
48

49 50 51 52
weechat::register("sound", "FlashCode <flashcode\@flashtux.org>", $version, "GPL3",
                  "Sound for IRC \"CTCP SOUND\" message", "", "");
weechat::config_set_plugin("cmd_ctcp", $default_cmd_ctcp) if (weechat::config_get_plugin("cmd_ctcp") eq "");
weechat::config_set_plugin("sound_extension", $default_sound_extension) if (weechat::config_get_plugin("sound_extension") eq "");
ptitlouis's avatar
ptitlouis committed
53

54 55
weechat::hook_signal("*,irc_in_privmsg", "sound", "");
weechat::hook_command("sound", "Play sound on IRC channel", "filename", "filename: sound filename", "", "sound_cmd", "");
ptitlouis's avatar
ptitlouis committed
56 57 58

sub sound
{
59 60
    my ($data, $server, $signal) = ($_[0], $_[1], $_[2]);
    if ($signal =~ /(.*) PRIVMSG (.*)/)
ptitlouis's avatar
ptitlouis committed
61 62 63 64
    {
        my ($host, $msg) = ($1, $2);
	if ($host ne "localhost")
	{
65
            # sound received
66
            if ($msg =~ /\001SOUND ([a-zA-Z0-9-_\.]*)\001/)
ptitlouis's avatar
ptitlouis committed
67 68
            {
                my $filename = $1;
69 70 71 72 73 74
                my $command = weechat::config_get_plugin("cmd_ctcp");
                if ($command ne "")
                {
                    $command =~ s/(\$\w+)/$1/gee;
                    system($command.$command_suffix);
                }
ptitlouis's avatar
ptitlouis committed
75 76 77
            }
	}
    }
78
    return weechat::WEECHAT_RC_OK;
ptitlouis's avatar
ptitlouis committed
79 80 81 82 83 84
}

sub sound_cmd
{
    if ($#_ == 1)
    {
85 86 87 88 89 90 91 92 93 94
        my $extension = weechat::config_get_plugin("sound_extension");
        my $filename = $_[1];
        $filename .= $extension if ($extension ne "");
        my $command = weechat::config_get_plugin("cmd_ctcp");
        if ($command ne "")
        {
            $command =~ s/(\$\w+)/$1/gee;
            system($command.$command_suffix);
        }
        weechat::command($_[0], "/quote PRIVMSG ".weechat::buffer_get_string($_[0], "localvar_channel")." :\001SOUND $filename\001") if (@_);
ptitlouis's avatar
ptitlouis committed
95
    }
96
    return weechat::WEECHAT_RC_OK;
ptitlouis's avatar
ptitlouis committed
97
}