Skip to content

Commit

Permalink
new feature from tfheen
Browse files Browse the repository at this point in the history
  • Loading branch information
torhve committed Sep 8, 2014
1 parent f536ffd commit 43423a2
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions pushover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private messages.

SCRIPT_NAME = "pushover"
SCRIPT_AUTHOR = "Tor Hveem <tor@bash.no>"
SCRIPT_VERSION = "2"
SCRIPT_VERSION = "3"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Send push notifications from weechat"

Expand All @@ -46,6 +46,7 @@ local p_config = {
ignore_buffers = 'Comma separated list of buffers to ignore',
ignore_messages = 'Comma separated list of message parts that will ignore the message',
only_when_away = 'Only send messages when away. String with values either on or off',
idle_timeout = 'Start sending messages after N seconds of inactivity',
}
local p_config_defaults = {
token = '',
Expand All @@ -54,19 +55,15 @@ local p_config_defaults = {
ignore_buffers = '',
ignore_messages = '',
only_when_away = 'on',
idle_timeout = 0,
}

p_hook_process = nil

-- printf function
function printf(buffer, fmt, ...)
w.print(buffer, string.format(fmt, unpack(arg)))
end

function p_process_cb(data, command, rc, stdout, stderr)
if tonumber(rc) >= 0 then
p_hook_process = nil
end
return w.WEECHAT_RC_OK
end

Expand All @@ -79,6 +76,30 @@ function get_nick(s)
return s
end

local outstanding_messages = {}
local last_inactivity = 0
local check_interval = 10

function pushover_send_queued_messages(data, remaining_calls)
local timeout = tonumber(w.config_get_plugin('idle_timeout'))
local inactivity = tonumber(w.info_get("inactivity", ""))
if inactivity < last_inactivity + check_interval then
-- either clock goes backwards or you've done something, clear the queue
outstanding_messages = {}
end
if timeout > 0 then
if timeout < inactivity then
local val = table.remove(outstanding_messages)
while val do
w.hook_process_hashtable(value.url, value.options, 10 * 1000, 'p_process_cb', '')
val = table.remove(outstanding_messages)
end
end
end
last_inactivity = inactivity
return w.WEECHAT_RC_OK
end

function pushover_check(data, buffer, time, tags, display, hilight, prefix, msg)
if w.config_get_plugin('only_when_away') == 'on' then
-- Check if buffer has away message set, if not return
Expand Down Expand Up @@ -115,7 +136,17 @@ function pushover_check(data, buffer, time, tags, display, hilight, prefix, msg)
postfields = 'token='..token..'&user='..user..'&title='..channel..'&message='..message
}
local url = 'https://api.pushover.net/1/messages.json'
p_hook_process = w.hook_process_hashtable('url:'..url, options, 10 * 1000, 'p_process_cb', '')

local timeout = tonumber(w.config_get_plugin('idle_timeout'))
if timeout > 0 then
if timeout > tonumber(w.info_get("inactivity", "")) then
table.insert(outstanding_messages, { ["url"] = 'url:'..url, ["options"] = options })
return w.WEECHAT_RC_OK
end
end
w.print("", "would send")
-- p_hook_process = w.hook_process_hashtable('url:'..url, options, 10 * 1000, 'p_process_cb', '')

end
return w.WEECHAT_RC_OK
end
Expand All @@ -138,6 +169,7 @@ function p_init()
end
-- Hook on every message printed
w.hook_print('', '', '', 1, 'pushover_check', '')
w.hook_timer(1000*check_interval, 0, 0, 'pushover_send_queued_messages', '')
end
end

Expand Down

0 comments on commit 43423a2

Please sign in to comment.