]> git.r.bdr.sh - rbdr/dotfiles/blob - weechat/perl/beep.pl
Update karabiner
[rbdr/dotfiles] / weechat / perl / beep.pl
1 #
2 # Copyright (C) 2006-2012 Sebastien Helleu <flashcode@flashtux.org>
3 # Copyright (C) 2011 Nils Görs <weechatter@arcor.de>
4 # Copyright (C) 2011 ArZa <arza@arza.us>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 #
20 # Beep (terminal bell) and/or run command on highlight/private message or new DCC.
21 #
22 # History:
23 # 2012-06-05, ldvx<ldvx@freenode>:
24 # version 1.1: Added wildcard support for whitelist_nicks.
25 # 2012-05-09, ldvx <ldvx@freenode>:
26 # version 1.0: Added beep_pv_blacklist, beep_highlight_blacklist, blacklist_nicks,
27 # and wildcard support for blacklist_nicks.
28 # 2012-05-02, ldvx <ldvx@freenode>:
29 # version 0.9: fix regex for nick in tags, add options "whitelist_channels"
30 # and "bell_always"
31 # 2012-04-19, ldvx <ldvx@freenode>:
32 # version 0.8: add whitelist, trigger, use hook_process for commands,
33 # rename option "beep_command" to "beep_command_pv", add help for options
34 # 2011-04-16, ArZa <arza@arza.us>:
35 # version 0.7: fix default beep command
36 # 2011-03-11, nils_2 <weechatter@arcor.de>:
37 # version 0.6: add additional command options for dcc and highlight
38 # 2011-03-09, nils_2 <weechatter@arcor.de>:
39 # version 0.5: add option for beep command and dcc
40 # 2009-05-02, Sebastien Helleu <flashcode@flashtux.org>:
41 # version 0.4: sync with last API changes
42 # 2008-11-05, Sebastien Helleu <flashcode@flashtux.org>:
43 # version 0.3: conversion to WeeChat 0.3.0+
44 # 2007-08-10, Sebastien Helleu <flashcode@flashtux.org>:
45 # version 0.2: upgraded licence to GPL 3
46 # 2006-09-02, Sebastien Helleu <flashcode@flashtux.org>:
47 # version 0.1: initial release
48
49 use strict;
50 my $SCRIPT_NAME = "beep";
51 my $VERSION = "1.1";
52
53 # default values in setup file (~/.weechat/plugins.conf)
54 my %options_default = ('beep_pv' => ['on', 'beep on private message'],
55 'beep_pv_whitelist' => ['off', 'turn whitelist for private messages on or off'],
56 'beep_pv_blacklist' => ['off', 'turn blacklist for private messages on or off'],
57 'beep_highlight' => ['on', 'beep on highlight'],
58 'beep_highlight_whitelist' => ['off', 'turn whitelist for highlights on or off'],
59 'beep_highlight_blacklist' => ['off', 'turn blacklist for highlights on or off'],
60 'beep_dcc' => ['on', 'beep on dcc'],
61 'beep_trigger_pv' => ['', 'word that will trigger execution of beep_command_pv (it empty, anything will trigger)'],
62 'beep_trigger_highlight' => ['', 'word that will trigger execution of beep_command_highlight (if empty, anything will trigger)'],
63 'beep_command_pv' => ['$bell', 'command for beep on private message, special value "$bell" is allowed, as well as "$bell;command"'],
64 'beep_command_highlight' => ['$bell', 'command for beep on highlight, special value "$bell" is allowed, as well as "$bell;command"'],
65 'beep_command_dcc' => ['$bell', 'command for beep on dcc, special value "$bell" is allowed, as well as "$bell;command"'],
66 'beep_command_timeout' => ['30000', 'timeout for command run (in milliseconds, 0 = never kill (not recommended))'],
67 'whitelist_nicks' => ['', 'comma-separated list of "server.nick": if not empty, only these nicks will trigger execution of commands (example: "freenode.nick1,freenode.nick2")'],
68 'blacklist_nicks' => ['', 'comma-separated list of "server.nick": if not empty, these nicks will not be able to trigger execution of commands. Cannot be used in conjuction with whitelist (example: "freenode.nick1,freenode.nick2")'],
69 'whitelist_channels' => ['', 'comma-separated list of "server.#channel": if not empty, only these channels will trigger execution of commands (example: "freenode.#weechat,freenode.#channel2")'],
70 'bell_always' => ['', 'use $bell on private messages and/or highlights regardless of trigger and whitelist settings (example: "pv,highlight")'],
71 );
72 my %options = ();
73
74 weechat::register($SCRIPT_NAME, "FlashCode <flashcode\@flashtux.org>", $VERSION,
75 "GPL3", "Beep (terminal bell) and/or run command on highlight/private message or new DCC", "", "");
76 init_config();
77
78 weechat::hook_config("plugins.var.perl.$SCRIPT_NAME.*", "toggle_config_by_set", "");
79 weechat::hook_print("", "", "", 1, "pv_and_highlight", "");
80 weechat::hook_signal("irc_dcc", "dcc", "");
81
82 sub pv_and_highlight
83 {
84 my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message) = @_;
85
86 # return if message is filtered
87 return weechat::WEECHAT_RC_OK if ($displayed != 1);
88
89 # return if nick in message is own nick
90 my $nick = "";
91 $nick = $2 if ($tags =~ m/(^|,)nick_([^,]*)(,|$)/);
92 return weechat::WEECHAT_RC_OK if (weechat::buffer_get_string($buffer, "localvar_nick") eq $nick);
93
94 # highlight
95 if ($highlight)
96 {
97 # Always print visual bel, regardless of whitelist and trigger settings
98 # beep_command_highlight does not need to contain $bell
99 if ($options{bell_always} =~ m/(^|,)highlight(,|$)/)
100 {
101 print STDERR "\a";
102 }
103 # Channels whitelist for highlights
104 if ($options{beep_highlight} eq "on")
105 {
106 if ($options{whitelist_channels} ne "")
107 {
108 my $serverandchannel = weechat::buffer_get_string($buffer, "localvar_server"). "." .
109 weechat::buffer_get_string($buffer, "localvar_channel");
110 if ($options{beep_trigger_highlight} eq "" or $message =~ m/\b$options{beep_trigger_highlight}\b/)
111 {
112 if ($options{whitelist_channels} =~ /(^|,)$serverandchannel(,|$)/)
113 {
114 beep_exec_command($options{beep_command_highlight});
115 }
116 # What if we are highlighted and we're in a PM? For now, do nothing.
117 }
118 }
119 else
120 {
121 # Execute $bell and/or command with trigger and whitelist/blacklist settings
122 beep_trigger_whitelist_blacklist($buffer, $message, $nick, $options{beep_trigger_highlight},
123 $options{beep_highlight_whitelist}, $options{beep_highlight_blacklist},
124 $options{beep_command_highlight});
125 }
126 }
127 }
128 # private message
129 elsif (weechat::buffer_get_string($buffer, "localvar_type") eq "private" and $tags =~ m/(^|,)notify_private(,|$)/)
130 {
131 # Always print visual bel, regardless of whitelist and trigger settings
132 # beep_command_pv does not need to contain $bell
133 if ($options{bell_always} =~ m/(^|,)pv(,|$)/)
134 {
135 print STDERR "\a";
136 }
137 # Execute $bell and/or command with trigger and whitelist/blacklist settings
138 if ($options{beep_pv} eq "on")
139 {
140 beep_trigger_whitelist_blacklist($buffer, $message, $nick, $options{beep_trigger_pv},
141 $options{beep_pv_whitelist}, $options{beep_pv_blacklist},
142 $options{beep_command_pv});
143 }
144 }
145 return weechat::WEECHAT_RC_OK;
146 }
147
148 sub dcc
149 {
150 beep_exec_command($options{beep_command_dcc}) if ($options{beep_dcc} eq "on");
151 return weechat::WEECHAT_RC_OK;
152 }
153
154 sub beep_trigger_whitelist_blacklist
155 {
156 my ($buffer, $message, $nick, $trigger, $whitelist, $blacklist, $command) = @_;
157
158 if ($trigger eq "" or $message =~ m/\b$trigger\b/)
159 {
160 my $serverandnick = weechat::buffer_get_string($buffer, "localvar_server").".".$nick;
161 if ($whitelist eq "on" and $options{whitelist_nicks} ne "")
162 {
163 # What to do if there's a wildcard in the whitelit
164 if ($options{whitelist_nicks} =~ m/\*/ and
165 match_in_wild_card($serverandnick, $options{whitelist_nicks}))
166 {
167 beep_exec_command($command);
168 }
169 # What to do if there's no wildcard in the whitelist
170 elsif ($options{whitelist_nicks} =~ /(^|,)$serverandnick(,|$)/)
171 {
172 beep_exec_command($command);
173 }
174 }
175 elsif ($blacklist eq "on" and $options{blacklist_nicks} ne "")
176 {
177 # What to do if there's a wildcard in the blacklist
178 if ($options{blacklist_nicks} =~ m/\*/ and
179 !match_in_wild_card($serverandnick, $options{blacklist_nicks}))
180 {
181 beep_exec_command($command);
182 }
183 # What to do if there's no wildcard in the blacklist
184 elsif ($options{blacklist_nicks} !~ /(^|,)$serverandnick(,|$)/)
185 {
186 beep_exec_command($command);
187 }
188 }
189 # What to do if we are not using whitelist of blacklist feature
190 elsif ($whitelist eq "off" and $blacklist eq "off")
191 {
192 beep_exec_command($command);
193 }
194 }
195 }
196
197 sub beep_exec_command
198 {
199 my $command = $_[0];
200 if ($command =~ /^\$bell/)
201 {
202 print STDERR "\a";
203 ($command) = $command =~ /^\$bell;(.+)$/;
204 }
205 weechat::hook_process($command, $options{beep_command_timeout}, "my_process", "") if ($command);
206 }
207
208 sub match_in_wild_card
209 {
210 my ($serverandnick, $white_or_black) = @_;
211 my $nick_iter;
212 my @array_of_nicks = split(",", $white_or_black);
213
214 foreach $nick_iter (@array_of_nicks)
215 {
216 $nick_iter =~ s/\*/[^,]*/g;
217 if ($serverandnick =~ /$nick_iter/)
218 {
219 return 1;
220 }
221 }
222 return 0;
223 }
224
225 sub my_process
226 {
227 return weechat::WEECHAT_RC_OK;
228 }
229
230 sub toggle_config_by_set
231 {
232 my ($pointer, $name, $value) = @_;
233 $name = substr($name, length("plugins.var.perl.".$SCRIPT_NAME."."), length($name));
234 $options{$name} = $value;
235 return weechat::WEECHAT_RC_OK;
236 }
237
238 sub init_config
239 {
240 my $version = weechat::info_get("version_number", "") || 0;
241 foreach my $option (keys %options_default)
242 {
243 if (!weechat::config_is_set_plugin($option))
244 {
245 weechat::config_set_plugin($option, $options_default{$option}[0]);
246 $options{$option} = $options_default{$option}[0];
247 }
248 else
249 {
250 $options{$option} = weechat::config_get_plugin($option);
251 }
252 if ($version >= 0x00030500)
253 {
254 weechat::config_set_desc_plugin($option, $options_default{$option}[1]." (default: \"".$options_default{$option}[0]."\")");
255 }
256 }
257 }