-
Notifications
You must be signed in to change notification settings - Fork 1
/
pushover.rb
297 lines (257 loc) · 7.9 KB
/
pushover.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# pushover.rb
#
# Copyright (c) 2014 Les Aker <[email protected]>
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Changelog:
# 0.0.3 - Fix encoding error
# 0.0.2 - Fix message coalescing
# 0.0.1 - Initial functionality
require 'enc/encdb.so'
require 'net/https'
PUSHOVER_DEFAULTS = {
userkey: nil,
appkey: nil,
devicename: nil,
interval: '10', # seconds
pm_priority: '1',
hilight_priority: '1',
onlywhendetached: '1',
enabled: '1'
}.freeze
PUSHOVER_URL = URI.parse('https://api.pushover.net/1/messages.json')
##
# API rate limit calculator
class RateCalc
def initialize(resp, interval)
seconds_left = resp['X-Limit-App-Reset'].to_i - Time.now.to_i
@potential = seconds_left / interval.to_i
@remaining = resp['X-Limit-App-Remaining'].to_i
end
def value
@remaining - @potential
end
end
##
# Handler for Pushover API
class PushoverClient
attr_reader :client
def initialize(params)
create_template params
create_client
end
def send(params)
data = @template.dup
data.merge! params
req = Net::HTTP::Post.new PUSHOVER_URL.path
req.set_form_data data
@client.request req
end
def close
@client.finish
end
private
def create_template(params)
@template = {
token: params[:appkey],
user: params[:userkey]
}
@template[:device] = params[:device] if params[:device]
end
def create_client
@client = Net::HTTP.new PUSHOVER_URL.host, PUSHOVER_URL.port
@client.use_ssl = true
@client.verify_mode = OpenSSL::SSL::VERIFY_PEER
@client.start
end
end
##
# Message object
class PushoverMessage
attr_reader :title, :text, :is_pm
def initialize(buffer, nick, text)
@title, @is_pm = PushoverMessage.parse_buffer buffer
@text = "#{"<#{nick}> " unless @is_pm}#{text}"
trim!
end
private
def trim!
@text = "#{text.slice(0, 497 - @title.length)}..." if @text.length > 500
end
class << self
def parse_buffer(buffer)
name = Weechat.buffer_get_string(buffer, 'full_name').split('.').last
type = Weechat.buffer_get_string(buffer, 'localvar_type')
[name, (type == 'private')]
end
end
end
##
# Coalesced message, used to conserve API requests
class PushoverMessageBundle < PushoverMessage
def initialize(messages)
@is_pm = messages.any?(&:is_pm)
if messages.map(&:title).uniq.size == 1
parse_single_author messages
else
parse_multi_author messages
end
trim!
end
private
def parse_single_author(messages)
@title = messages.first.title
@text = messages.map(&:text).join(' || ')
end
def parse_multi_author(messages)
@title = 'Messages from multiple sources'
counts = Hash[messages.group_by(&:title).map { |k, v| [k, v.size] }]
@text = counts.reduce('') { |a, (k, v)| a << "#{k} (#{v}), " }[0..-3]
end
end
##
# Handles message queue and configuration
class PushoverConfig # rubocop:disable Metrics/ClassLength
def initialize
@queue = []
@health_check = 200
@rate_calc = 0
load_options
load_hooks
end
def command_hook(_, _, args)
case args
when 'enable' then enable!
when 'disable' then disable!
when /^set (?<option>\w+) (?<value>[\w]+)/
set Regexp.last_match['option'], Regexp.last_match['value']
else
Weechat.print('', "Syntax: #{completion_text}")
end
Weechat::WEECHAT_RC_OK
end
def message_hook(*args) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
buffer, is_highlight, nick, text = args.values_at(1, 5, 6, 7)
is_pm = (Weechat.buffer_get_string(buffer, 'localvar_type') == 'private')
return Weechat::WEECHAT_RC_OK if is_highlight.to_i.zero? && !is_pm
return Weechat::WEECHAT_RC_OK if Weechat.config_string_to_boolean(@options[:enabled]).to_i.zero?
unless Weechat.config_string_to_boolean(@options[:onlywhendetached]).to_i.zero?
attached = Weechat.config_string(
Weechat.config_get('plugins.var.ruby.tmux_track.attached')
).to_i.nonzero?
return Weechat::WEECHAT_RC_OK if attached
end
@queue << PushoverMessage.new(buffer, nick, text)
Weechat::WEECHAT_RC_OK
end
def timer_hook(*_)
return Weechat::WEECHAT_RC_OK if @queue.empty?
if @health_check < 0
@health_check += 1
return Weechat::WEECHAT_RC_OK
end
coalesce_messages if @rate_calc < 0
send_messages
Weechat::WEECHAT_RC_OK
end
private
def coalesce_messages
@queue = [PushoverMessageBundle.new(@queue)]
end
def send_messages # rubocop:disable Metrics/MethodLength
client = PushoverClient.new @options
@queue = @queue.drop_while do |message|
resp = client.send(
title: message.title,
message: message.text,
priority: @options[message.is_pm ? :pm_priority : :hilight_priority]
)
parse_response resp
end
rescue Errno::ETIMEDOUT
server_failure!
end
def parse_response(resp)
case resp.code
when /200/
@rate_calc = RateCalc.new(resp, @options[:interval]).value
when /429/
disable! 'Pushover message limit exceeded'
when /4\d\d/
disable! "Pushover error: #{resp.body}"
else
server_failure!
end
end
def disable!(reason)
Weechat.print '', reason
Weechat.print '', 'Disabling Pushover notifications'
@options[:enabled] = '0'
@queue.clear
false
end
def server_failure!
Weechat.print '', 'Pushover server error detected, delaying notifications'
@health_check -= 5
false
end
def set(option, value)
if @options.key? option.to_sym
@options[option.to_sym] = value
Weechat.config_set_plugin option, value
Weechat.print '', "Pushover: set #{option} to #{value}"
load_hooks if [:interval].include? option.to_sym
else
Weechat.print '', "Available options: #{@options.keys.join ', '}"
end
end
def load_options
@options = PUSHOVER_DEFAULTS.dup
@options.each_key do |key|
value = Weechat.config_get_plugin key.to_s
@options[key] = value if value && value.length.nonzero?
Weechat.config_set_plugin key.to_s, @options[key].to_s
end
end
def load_hooks # rubocop:disable Metrics/MethodLength
tags = 'notify_message,notify_private,notify_highlight'
@hooks.each_value { |x| Weechat.unhook x } if @hooks
@hooks = {
command: Weechat.hook_command(
'pushover', 'Control Pushover options',
'set OPTION VALUE', '', completion_text,
'command_hook', ''
),
message: Weechat.hook_print('', tags, '', 1, 'message_hook', ''),
timer: Weechat.hook_timer(@options[:interval].to_i * 1000, 0, 0, 'timer_hook', '')
}
end
def completion_text
"set #{@options.keys.join '|'}"
end
end
def weechat_init
Weechat.register(
'pushover', 'Les Aker <[email protected]>',
'0.0.3', 'MIT',
'Send hilight notifications via Pushover',
'', ''
)
$Pushover = PushoverConfig.new
Weechat::WEECHAT_RC_OK
end
require 'forwardable'
extend Forwardable
def_delegators :$Pushover, :message_hook, :command_hook, :timer_hook