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> 
   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. 
  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. 
  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/>. 
  20 # Beep (terminal bell) and/or run command on highlight/private message or new DCC. 
  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" 
  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 
  50 my $SCRIPT_NAME = "beep"; 
  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")'], 
  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", "", ""); 
  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", ""); 
  84     my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message) = @_; 
  86     # return if message is filtered 
  87     return weechat
::WEECHAT_RC_OK 
if ($displayed != 1); 
  89     # return if nick in message is own nick 
  91     $nick = $2 if ($tags =~ m/(^|,)nick_([^,]*)(,|$)/); 
  92     return weechat
::WEECHAT_RC_OK 
if (weechat
::buffer_get_string
($buffer, "localvar_nick") eq $nick); 
  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(,|$)/) 
 103         # Channels whitelist for highlights 
 104         if ($options{beep_highlight
} eq "on") 
 106             if ($options{whitelist_channels
} ne "") 
 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/) 
 112                     if ($options{whitelist_channels
} =~ /(^|,)$serverandchannel(,|$)/) 
 114                         beep_exec_command
($options{beep_command_highlight
}); 
 116                     # What if we are highlighted and we're in a PM? For now, do nothing. 
 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
}); 
 129     elsif (weechat
::buffer_get_string
($buffer, "localvar_type") eq "private" and $tags =~ m/(^|,)notify_private(,|$)/) 
 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(,|$)/) 
 137         # Execute $bell and/or command with trigger and whitelist/blacklist settings 
 138         if ($options{beep_pv
} eq "on") 
 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
}); 
 145     return weechat
::WEECHAT_RC_OK
; 
 150     beep_exec_command
($options{beep_command_dcc
}) if ($options{beep_dcc
} eq "on"); 
 151     return weechat
::WEECHAT_RC_OK
; 
 154 sub beep_trigger_whitelist_blacklist
 
 156     my ($buffer, $message, $nick, $trigger, $whitelist, $blacklist, $command) = @_; 
 158     if ($trigger eq "" or $message =~ m/\b$trigger\b/) 
 160         my $serverandnick = weechat
::buffer_get_string
($buffer, "localvar_server").".".$nick; 
 161         if ($whitelist eq "on" and $options{whitelist_nicks
} ne "") 
 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
})) 
 167                 beep_exec_command
($command); 
 169             # What to do if there's no wildcard in the whitelist 
 170             elsif ($options{whitelist_nicks
} =~ /(^|,)$serverandnick(,|$)/) 
 172                 beep_exec_command
($command); 
 175         elsif ($blacklist eq "on" and $options{blacklist_nicks
} ne "") 
 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
})) 
 181                 beep_exec_command
($command); 
 183             # What to do if there's no wildcard in the blacklist 
 184             elsif ($options{blacklist_nicks
} !~ /(^|,)$serverandnick(,|$)/) 
 186                 beep_exec_command
($command); 
 189         # What to do if we are not using whitelist of blacklist feature 
 190         elsif ($whitelist eq "off" and $blacklist eq "off") 
 192             beep_exec_command
($command); 
 197 sub beep_exec_command
 
 200     if ($command =~ /^\$bell/) 
 203         ($command) = $command =~ /^\$bell;(.+)$/; 
 205     weechat
::hook_process
($command, $options{beep_command_timeout
}, "my_process", "") if ($command); 
 208 sub match_in_wild_card
 
 210     my ($serverandnick, $white_or_black) = @_; 
 212     my @array_of_nicks = split(",", $white_or_black); 
 214     foreach $nick_iter (@array_of_nicks) 
 216         $nick_iter =~ s/\*/[^,]*/g; 
 217         if ($serverandnick =~ /$nick_iter/) 
 227     return weechat
::WEECHAT_RC_OK
; 
 230 sub toggle_config_by_set
 
 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
; 
 240     my $version = weechat
::info_get
("version_number", "") || 0; 
 241     foreach my $option (keys %options_default) 
 243         if (!weechat
::config_is_set_plugin
($option)) 
 245             weechat
::config_set_plugin
($option, $options_default{$option}[0]); 
 246             $options{$option} = $options_default{$option}[0]; 
 250             $options{$option} = weechat
::config_get_plugin
($option); 
 252         if ($version >= 0x00030500) 
 254             weechat
::config_set_desc_plugin
($option, $options_default{$option}[1]." (default: \"".$options_default{$option}[0]."\")");