Mercurial > dotfiles
comparison weechat/perl/pushover.pl @ 278:9cc79143db21
Update pushover script.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 27 Jan 2015 15:26:44 -0800 |
parents | 726728f13152 |
children |
comparison
equal
deleted
inserted
replaced
277:e1c6d6953875 | 278:9cc79143db21 |
---|---|
1 # | 1 # |
2 # Copyright (C) 2013-2014 stfn <stfnmd@gmail.com> | 2 # Copyright (C) 2013-2015 stfn <stfnmd@gmail.com> |
3 # https://github.com/stfnm/weechat-scripts | 3 # https://github.com/stfnm/weechat-scripts |
4 # | 4 # |
5 # This program is free software: you can redistribute it and/or modify | 5 # This program is free software: you can redistribute it and/or modify |
6 # it under the terms of the GNU General Public License as published by | 6 # it under the terms of the GNU General Public License as published by |
7 # the Free Software Foundation, either version 3 of the License, or | 7 # the Free Software Foundation, either version 3 of the License, or |
20 use warnings; | 20 use warnings; |
21 | 21 |
22 my %SCRIPT = ( | 22 my %SCRIPT = ( |
23 name => 'pushover', | 23 name => 'pushover', |
24 author => 'stfn <stfnmd@gmail.com>', | 24 author => 'stfn <stfnmd@gmail.com>', |
25 version => '1.2', | 25 version => '1.3', |
26 license => 'GPL3', | 26 license => 'GPL3', |
27 desc => 'Send push notifications to your mobile devices using Pushover, NMA or Pushbullet', | 27 desc => 'Send push notifications to your mobile devices using Pushover, NMA or Pushbullet', |
28 opt => 'plugins.var.perl', | 28 opt => 'plugins.var.perl', |
29 ); | 29 ); |
30 my %OPTIONS_DEFAULT = ( | 30 my %OPTIONS_DEFAULT = ( |
42 'redact_priv_msg' => ['off', 'When receiving private message notifications, hide the actual message text'], | 42 'redact_priv_msg' => ['off', 'When receiving private message notifications, hide the actual message text'], |
43 'only_if_away' => ['off', 'Notify only if away status is active'], | 43 'only_if_away' => ['off', 'Notify only if away status is active'], |
44 'only_if_inactive' => ['off', 'Notify only if buffer is not the active (current) buffer'], | 44 'only_if_inactive' => ['off', 'Notify only if buffer is not the active (current) buffer'], |
45 'blacklist' => ['', 'Comma separated list of buffers (full name) to blacklist for notifications (wildcard "*" is allowed, name beginning with "!" is excluded)'], | 45 'blacklist' => ['', 'Comma separated list of buffers (full name) to blacklist for notifications (wildcard "*" is allowed, name beginning with "!" is excluded)'], |
46 'verbose' => ['1', 'Verbosity level (0 = silently ignore any errors, 1 = display brief error, 2 = display full server response)'], | 46 'verbose' => ['1', 'Verbosity level (0 = silently ignore any errors, 1 = display brief error, 2 = display full server response)'], |
47 'rate_limit' => ['0', 'Rate limit in seconds (0 = unlimited), will send a maximum of 1 notification per time limit'], | |
48 'short_name' => ['off', 'Use short buffer name in notification'], | |
47 ); | 49 ); |
48 my %OPTIONS = (); | 50 my %OPTIONS = (); |
49 my $TIMEOUT = 30 * 1000; | 51 my $TIMEOUT = 30 * 1000; |
50 | 52 my $WEECHAT_VERSION; |
53 | |
54 # Enable for debugging | |
51 my $DEBUG = 0; | 55 my $DEBUG = 0; |
56 | |
57 # Rate limit flag | |
58 my $RATE_LIMIT_OK = 1; | |
52 | 59 |
53 # Register script and initialize config | 60 # Register script and initialize config |
54 weechat::register($SCRIPT{"name"}, $SCRIPT{"author"}, $SCRIPT{"version"}, $SCRIPT{"license"}, $SCRIPT{"desc"}, "", ""); | 61 weechat::register($SCRIPT{"name"}, $SCRIPT{"author"}, $SCRIPT{"version"}, $SCRIPT{"license"}, $SCRIPT{"desc"}, "", ""); |
55 init_config(); | 62 init_config(); |
56 | 63 |
65 # Handle config stuff | 72 # Handle config stuff |
66 # | 73 # |
67 sub init_config | 74 sub init_config |
68 { | 75 { |
69 weechat::hook_config("$SCRIPT{'opt'}.$SCRIPT{'name'}.*", "config_cb", ""); | 76 weechat::hook_config("$SCRIPT{'opt'}.$SCRIPT{'name'}.*", "config_cb", ""); |
70 my $version = weechat::info_get("version_number", "") || 0; | 77 $WEECHAT_VERSION = weechat::info_get("version_number", "") || 0; |
71 foreach my $option (keys %OPTIONS_DEFAULT) { | 78 foreach my $option (keys %OPTIONS_DEFAULT) { |
72 if (!weechat::config_is_set_plugin($option)) { | 79 if (!weechat::config_is_set_plugin($option)) { |
73 weechat::config_set_plugin($option, $OPTIONS_DEFAULT{$option}[0]); | 80 weechat::config_set_plugin($option, $OPTIONS_DEFAULT{$option}[0]); |
74 $OPTIONS{$option} = $OPTIONS_DEFAULT{$option}[0]; | 81 $OPTIONS{$option} = $OPTIONS_DEFAULT{$option}[0]; |
75 } else { | 82 } else { |
76 $OPTIONS{$option} = weechat::config_get_plugin($option); | 83 $OPTIONS{$option} = weechat::config_get_plugin($option); |
77 } | 84 } |
78 if ($version >= 0x00030500) { | 85 if ($WEECHAT_VERSION >= 0x00030500) { |
79 weechat::config_set_desc_plugin($option, $OPTIONS_DEFAULT{$option}[1]." (default: \"".$OPTIONS_DEFAULT{$option}[0]."\")"); | 86 weechat::config_set_desc_plugin($option, $OPTIONS_DEFAULT{$option}[1]." (default: \"".$OPTIONS_DEFAULT{$option}[0]."\")"); |
80 } | 87 } |
81 } | 88 } |
82 } | 89 } |
90 | |
83 sub config_cb | 91 sub config_cb |
84 { | 92 { |
85 my ($pointer, $name, $value) = @_; | 93 my ($pointer, $name, $value) = @_; |
86 $name = substr($name, length("$SCRIPT{opt}.$SCRIPT{name}."), length($name)); | 94 $name = substr($name, length("$SCRIPT{opt}.$SCRIPT{name}."), length($name)); |
87 $OPTIONS{$name} = $value; | 95 $OPTIONS{$name} = $value; |
109 $toencode =~ s/([^a-zA-Z0-9_.~-])/uc sprintf("%%%02x",ord($1))/eg; | 117 $toencode =~ s/([^a-zA-Z0-9_.~-])/uc sprintf("%%%02x",ord($1))/eg; |
110 return $toencode; | 118 return $toencode; |
111 } | 119 } |
112 | 120 |
113 # | 121 # |
122 # Evaluate expression (used for /secure support) | |
123 # | |
124 sub eval_expr($) | |
125 { | |
126 my $value = $_[0]; | |
127 if ($WEECHAT_VERSION >= 0x00040200) { | |
128 my $eval_expression = weechat::string_eval_expression($value, {}, {}, {}); | |
129 return $eval_expression if ($eval_expression ne ""); | |
130 } | |
131 return $value; | |
132 } | |
133 | |
134 # | |
135 # Flip rate_limit flag back to OK | |
136 # | |
137 sub rate_limit_cb | |
138 { | |
139 $RATE_LIMIT_OK = 1; | |
140 if ($DEBUG) { | |
141 weechat::print("", "[$SCRIPT{name}] Rate Limit Deactivated"); | |
142 } | |
143 } | |
144 | |
145 # | |
114 # Catch printed messages | 146 # Catch printed messages |
115 # | 147 # |
116 sub print_cb | 148 sub print_cb |
117 { | 149 { |
118 my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message) = @_; | 150 my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message) = @_; |
119 | 151 |
120 my $buffer_type = weechat::buffer_get_string($buffer, "localvar_type"); | 152 my $buffer_type = weechat::buffer_get_string($buffer, "localvar_type"); |
121 my $buffer_full_name = weechat::buffer_get_string($buffer, "full_name"); | 153 my $buffer_full_name = ""; |
154 # check for long or short name | |
155 if ($OPTIONS{short_name} eq 'on') { | |
156 $buffer_full_name = weechat::buffer_get_string($buffer, "short_name"); | |
157 } else { | |
158 $buffer_full_name = weechat::buffer_get_string($buffer, "full_name"); | |
159 } | |
122 my $away_msg = weechat::buffer_get_string($buffer, "localvar_away"); | 160 my $away_msg = weechat::buffer_get_string($buffer, "localvar_away"); |
123 my $away = ($away_msg && length($away_msg) > 0) ? 1 : 0; | 161 my $away = ($away_msg && length($away_msg) > 0) ? 1 : 0; |
124 | 162 |
125 if ($OPTIONS{enabled} ne "on" || | 163 if ($OPTIONS{enabled} ne "on" || |
126 $displayed == 0 || | 164 $displayed == 0 || |
127 ($OPTIONS{only_if_away} eq "on" && $away == 0) || | 165 ($OPTIONS{only_if_away} eq "on" && $away == 0) || |
128 ($OPTIONS{only_if_inactive} eq "on" && $buffer eq weechat::current_buffer()) || | 166 ($OPTIONS{only_if_inactive} eq "on" && $buffer eq weechat::current_buffer()) || |
129 weechat::buffer_match_list($buffer, $OPTIONS{blacklist})) { | 167 weechat::buffer_match_list($buffer, $OPTIONS{blacklist})) { |
168 return weechat::WEECHAT_RC_OK; | |
169 } | |
170 | |
171 if ($RATE_LIMIT_OK == 0) { | |
172 if ($DEBUG) { | |
173 weechat::print("", "[$SCRIPT{name}] No Notification - Rate Limited."); | |
174 } | |
130 return weechat::WEECHAT_RC_OK; | 175 return weechat::WEECHAT_RC_OK; |
131 } | 176 } |
132 | 177 |
133 my $msg = "[$buffer_full_name] <$prefix> "; | 178 my $msg = "[$buffer_full_name] <$prefix> "; |
134 | 179 |
199 # | 244 # |
200 sub notify($) | 245 sub notify($) |
201 { | 246 { |
202 my $message = $_[0]; | 247 my $message = $_[0]; |
203 | 248 |
249 # Start timer | |
250 if ($OPTIONS{rate_limit}) { | |
251 my $timer = $OPTIONS{rate_limit} * 1000; | |
252 | |
253 if ($DEBUG) { | |
254 weechat::print("", "[$SCRIPT{name}] Rate Limit Activated. Timer: $timer"); | |
255 } | |
256 | |
257 $RATE_LIMIT_OK = 0; | |
258 weechat::hook_timer($timer, 0, 1, "rate_limit_cb", ""); | |
259 } | |
260 | |
204 # Notify services | 261 # Notify services |
205 if (grep_list("pushover", $OPTIONS{service})) { | 262 if (grep_list("pushover", $OPTIONS{service})) { |
206 notify_pushover($OPTIONS{token}, $OPTIONS{user}, $message, "weechat", $OPTIONS{priority}, $OPTIONS{sound}); | 263 notify_pushover(eval_expr($OPTIONS{token}), eval_expr($OPTIONS{user}), $message, "weechat", $OPTIONS{priority}, $OPTIONS{sound}); |
207 } | 264 } |
208 if (grep_list("nma", $OPTIONS{service})) { | 265 if (grep_list("nma", $OPTIONS{service})) { |
209 notify_nma($OPTIONS{nma_apikey}, "weechat", "$SCRIPT{name}.pl", $message, $OPTIONS{priority}); | 266 notify_nma(eval_expr($OPTIONS{nma_apikey}), "weechat", "$SCRIPT{name}.pl", $message, $OPTIONS{priority}); |
210 } | 267 } |
211 if (grep_list("pushbullet", $OPTIONS{service})) { | 268 if (grep_list("pushbullet", $OPTIONS{service})) { |
212 notify_pushbullet($OPTIONS{pb_apikey}, $OPTIONS{pb_device_iden}, "weechat", $message); | 269 notify_pushbullet(eval_expr($OPTIONS{pb_apikey}), eval_expr($OPTIONS{pb_device_iden}), "weechat", $message); |
213 } | 270 } |
214 } | 271 } |
215 | 272 |
216 # | 273 # |
217 # https://pushover.net/api | 274 # https://pushover.net/api |