beep.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. use strict;
  49. my $SCRIPT_NAME = "beep";
  50. my $VERSION = "1.1";
  51. # default values in setup file (~/.weechat/plugins.conf)
  52. my %options_default = ('beep_pv' => ['on', 'beep on private message'],
  53. 'beep_pv_whitelist' => ['off', 'turn whitelist for private messages on or off'],
  54. 'beep_pv_blacklist' => ['off', 'turn blacklist for private messages on or off'],
  55. 'beep_highlight' => ['on', 'beep on highlight'],
  56. 'beep_highlight_whitelist' => ['off', 'turn whitelist for highlights on or off'],
  57. 'beep_highlight_blacklist' => ['off', 'turn blacklist for highlights on or off'],
  58. 'beep_dcc' => ['on', 'beep on dcc'],
  59. 'beep_trigger_pv' => ['', 'word that will trigger execution of beep_command_pv (it empty, anything will trigger)'],
  60. 'beep_trigger_highlight' => ['', 'word that will trigger execution of beep_command_highlight (if empty, anything will trigger)'],
  61. 'beep_command_pv' => ['$bell', 'command for beep on private message, special value "$bell" is allowed, as well as "$bell;command"'],
  62. 'beep_command_highlight' => ['$bell', 'command for beep on highlight, special value "$bell" is allowed, as well as "$bell;command"'],
  63. 'beep_command_dcc' => ['$bell', 'command for beep on dcc, special value "$bell" is allowed, as well as "$bell;command"'],
  64. 'beep_command_timeout' => ['30000', 'timeout for command run (in milliseconds, 0 = never kill (not recommended))'],
  65. 'whitelist_nicks' => ['', 'comma-separated list of "server.nick": if not empty, only these nicks will trigger execution of commands (example: "freenode.nick1,freenode.nick2")'],
  66. '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")'],
  67. 'whitelist_channels' => ['', 'comma-separated list of "server.#channel": if not empty, only these channels will trigger execution of commands (example: "freenode.#weechat,freenode.#channel2")'],
  68. 'bell_always' => ['', 'use $bell on private messages and/or highlights regardless of trigger and whitelist settings (example: "pv,highlight")'],
  69. );
  70. my %options = ();
  71. weechat::register($SCRIPT_NAME, "FlashCode <flashcode\@flashtux.org>", $VERSION,
  72. "GPL3", "Beep (terminal bell) and/or run command on highlight/private message or new DCC", "", "");
  73. init_config();
  74. weechat::hook_config("plugins.var.perl.$SCRIPT_NAME.*", "toggle_config_by_set", "");
  75. weechat::hook_print("", "", "", 1, "pv_and_highlight", "");
  76. weechat::hook_signal("irc_dcc", "dcc", "");
  77. sub pv_and_highlight
  78. {
  79. my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message) = @_;
  80. # return if message is filtered
  81. return weechat::WEECHAT_RC_OK if ($displayed != 1);
  82. # return if nick in message is own nick
  83. my $nick = "";
  84. $nick = $2 if ($tags =~ m/(^|,)nick_([^,]*)(,|$)/);
  85. return weechat::WEECHAT_RC_OK if (weechat::buffer_get_string($buffer, "localvar_nick") eq $nick);
  86. # highlight
  87. if ($highlight)
  88. {
  89. # Always print visual bel, regardless of whitelist and trigger settings
  90. # beep_command_highlight does not need to contain $bell
  91. if ($options{bell_always} =~ m/(^|,)highlight(,|$)/)
  92. {
  93. print STDERR "\a";
  94. }
  95. # Channels whitelist for highlights
  96. if ($options{beep_highlight} eq "on")
  97. {
  98. if ($options{whitelist_channels} ne "")
  99. {
  100. my $serverandchannel = weechat::buffer_get_string($buffer, "localvar_server"). "." .
  101. weechat::buffer_get_string($buffer, "localvar_channel");
  102. if ($options{beep_trigger_highlight} eq "" or $message =~ m/\b$options{beep_trigger_highlight}\b/)
  103. {
  104. if ($options{whitelist_channels} =~ /(^|,)$serverandchannel(,|$)/)
  105. {
  106. beep_exec_command($options{beep_command_highlight});
  107. }
  108. # What if we are highlighted and we're in a PM? For now, do nothing.
  109. }
  110. }
  111. else
  112. {
  113. # Execute $bell and/or command with trigger and whitelist/blacklist settings
  114. beep_trigger_whitelist_blacklist($buffer, $message, $nick, $options{beep_trigger_highlight},
  115. $options{beep_highlight_whitelist}, $options{beep_highlight_blacklist},
  116. $options{beep_command_highlight});
  117. }
  118. }
  119. }
  120. # private message
  121. elsif (weechat::buffer_get_string($buffer, "localvar_type") eq "private" and $tags =~ m/(^|,)notify_private(,|$)/)
  122. {
  123. # Always print visual bel, regardless of whitelist and trigger settings
  124. # beep_command_pv does not need to contain $bell
  125. if ($options{bell_always} =~ m/(^|,)pv(,|$)/)
  126. {
  127. print STDERR "\a";
  128. }
  129. # Execute $bell and/or command with trigger and whitelist/blacklist settings
  130. if ($options{beep_pv} eq "on")
  131. {
  132. beep_trigger_whitelist_blacklist($buffer, $message, $nick, $options{beep_trigger_pv},
  133. $options{beep_pv_whitelist}, $options{beep_pv_blacklist},
  134. $options{beep_command_pv});
  135. }
  136. }
  137. return weechat::WEECHAT_RC_OK;
  138. }
  139. sub dcc
  140. {
  141. beep_exec_command($options{beep_command_dcc}) if ($options{beep_dcc} eq "on");
  142. return weechat::WEECHAT_RC_OK;
  143. }
  144. sub beep_trigger_whitelist_blacklist
  145. {
  146. my ($buffer, $message, $nick, $trigger, $whitelist, $blacklist, $command) = @_;
  147. if ($trigger eq "" or $message =~ m/\b$trigger\b/)
  148. {
  149. my $serverandnick = weechat::buffer_get_string($buffer, "localvar_server").".".$nick;
  150. if ($whitelist eq "on" and $options{whitelist_nicks} ne "")
  151. {
  152. # What to do if there's a wildcard in the whitelit
  153. if ($options{whitelist_nicks} =~ m/\*/ and
  154. match_in_wild_card($serverandnick, $options{whitelist_nicks}))
  155. {
  156. beep_exec_command($command);
  157. }
  158. # What to do if there's no wildcard in the whitelist
  159. elsif ($options{whitelist_nicks} =~ /(^|,)$serverandnick(,|$)/)
  160. {
  161. beep_exec_command($command);
  162. }
  163. }
  164. elsif ($blacklist eq "on" and $options{blacklist_nicks} ne "")
  165. {
  166. # What to do if there's a wildcard in the blacklist
  167. if ($options{blacklist_nicks} =~ m/\*/ and
  168. !match_in_wild_card($serverandnick, $options{blacklist_nicks}))
  169. {
  170. beep_exec_command($command);
  171. }
  172. # What to do if there's no wildcard in the blacklist
  173. elsif ($options{blacklist_nicks} !~ /(^|,)$serverandnick(,|$)/)
  174. {
  175. beep_exec_command($command);
  176. }
  177. }
  178. # What to do if we are not using whitelist of blacklist feature
  179. elsif ($whitelist eq "off" and $blacklist eq "off")
  180. {
  181. beep_exec_command($command);
  182. }
  183. }
  184. }
  185. sub beep_exec_command
  186. {
  187. my $command = $_[0];
  188. if ($command =~ /^\$bell/)
  189. {
  190. print STDERR "\a";
  191. ($command) = $command =~ /^\$bell;(.+)$/;
  192. }
  193. weechat::hook_process($command, $options{beep_command_timeout}, "my_process", "") if ($command);
  194. }
  195. sub match_in_wild_card
  196. {
  197. my ($serverandnick, $white_or_black) = @_;
  198. my $nick_iter;
  199. my @array_of_nicks = split(",", $white_or_black);
  200. foreach $nick_iter (@array_of_nicks)
  201. {
  202. $nick_iter =~ s/\*/[^,]*/g;
  203. if ($serverandnick =~ /$nick_iter/)
  204. {
  205. return 1;
  206. }
  207. }
  208. return 0;
  209. }
  210. sub my_process
  211. {
  212. return weechat::WEECHAT_RC_OK;
  213. }
  214. sub toggle_config_by_set
  215. {
  216. my ($pointer, $name, $value) = @_;
  217. $name = substr($name, length("plugins.var.perl.".$SCRIPT_NAME."."), length($name));
  218. $options{$name} = $value;
  219. return weechat::WEECHAT_RC_OK;
  220. }
  221. sub init_config
  222. {
  223. my $version = weechat::info_get("version_number", "") || 0;
  224. foreach my $option (keys %options_default)
  225. {
  226. if (!weechat::config_is_set_plugin($option))
  227. {
  228. weechat::config_set_plugin($option, $options_default{$option}[0]);
  229. $options{$option} = $options_default{$option}[0];
  230. }
  231. else
  232. {
  233. $options{$option} = weechat::config_get_plugin($option);
  234. }
  235. if ($version >= 0x00030500)
  236. {
  237. weechat::config_set_desc_plugin($option, $options_default{$option}[1]." (default: \"".$options_default{$option}[0]."\")");
  238. }
  239. }
  240. }