Software /
code /
verse
Annotate
plugins/uptime.lua @ 465:6707e3a47f71
Add 'shutdown' event for a self-initiated disconnect
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 17 Mar 2023 09:23:15 +0000 |
parent | 351:4455b07f77ed |
rev | line source |
---|---|
250 | 1 local verse = require "verse"; |
2 | |
194
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
3 local xmlns_last = "jabber:iq:last"; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
4 |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
5 local function set_uptime(self, uptime_info) |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
6 self.starttime = uptime_info.starttime; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
7 end |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
8 |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
9 function verse.plugins.uptime(stream) |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
10 stream.uptime = { set = set_uptime }; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
11 stream:hook("iq/"..xmlns_last, function (stanza) |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
12 if stanza.attr.type ~= "get" then return; end |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
13 local reply = verse.reply(stanza) |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
14 :tag("query", { seconds = tostring(os.difftime(os.time(), stream.uptime.starttime)), xmlns = xmlns_last }); |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
15 stream:send(reply); |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
16 return true; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
17 end); |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
18 |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
19 function stream:query_uptime(target_jid, callback) |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
20 callback = callback or function (uptime) return stream:event("uptime/response", uptime); end |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
21 stream:send_iq(verse.iq({ type = "get", to = target_jid }) |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
22 :tag("query", { xmlns = xmlns_last }), |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
23 function (reply) |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
24 local query = reply:get_child("query", xmlns_last); |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
25 if reply.attr.type == "result" then |
351
4455b07f77ed
verse.plugins.uptime: Explicitly cast uptime to a number
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
26 local seconds = tonumber(query.attr.seconds); |
194
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
27 callback({ |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
28 seconds = seconds or nil; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
29 }); |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
30 else |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
31 local type, condition, text = reply:get_error(); |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
32 callback({ |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
33 error = true; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
34 condition = condition; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
35 text = text; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
36 type = type; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
37 }); |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
38 end |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
39 end); |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
40 end |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
41 return true; |
651c696e0b21
Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
42 end |