Software / code / prosody-modules
Annotate
mod_couchdb/couchdb/json.lib.lua @ 6301:fa45ae704b79
mod_cloud_notify: Update Readme
diff --git a/mod_cloud_notify/README.md b/mod_cloud_notify/README.md
--- a/mod_cloud_notify/README.md
+++ b/mod_cloud_notify/README.md
@@ -1,109 +1,106 @@
----
-labels:
-- 'Stage-Beta'
-summary: 'XEP-0357: Cloud push notifications'
----
+# Introduction
-Introduction
-============
+This module enables support for sending "push notifications" to clients
+that need it, typically those running on certain mobile devices.
-This module enables support for sending "push notifications" to clients that
-need it, typically those running on certain mobile devices.
+As well as this module, your client must support push notifications (the
+apps that need it generally do, of course) and the app developer's push
+gateway must be reachable from your Prosody server (this happens over a
+normal XMPP server-to-server 's2s' connection).
-As well as this module, your client must support push notifications (the apps
-that need it generally do, of course) and the app developer's push gateway
-must be reachable from your Prosody server (this happens over a normal XMPP
-server-to-server 's2s' connection).
-
-Details
-=======
+# Details
Some platforms, notably Apple's iOS and many versions of Android, impose
-limits that prevent applications from running or accessing the network in the
-background. This makes it difficult or impossible for an XMPP application to
-remain reliably connected to a server to receive messages.
-
-In order for messaging and other apps to receive notifications, the OS vendors
-run proprietary servers that their OS maintains a permanent connection to in
-the background. Then they provide APIs to application developers that allow
-sending notifications to specific devices via those servers.
+limits that prevent applications from running or accessing the network
+in the background. This makes it difficult or impossible for an XMPP
+application to remain reliably connected to a server to receive
+messages.
-When you connect to your server with an app that requires push notifications,
-it will use this module to set up a "push registration". When you receive
-a message but your device is not connected to the server, this module will
-generate a notification and send it to the push gateway operated by your
-application's developers). Their gateway will then connect to your device's
-OS vendor and ask them to forward the notification to your device. When your
-device receives the notification, it will display it or wake up the app so it
-can connect to XMPP and receive any pending messages.
+In order for messaging and other apps to receive notifications, the OS
+vendors run proprietary servers that their OS maintains a permanent
+connection to in the background. Then they provide APIs to application
+developers that allow sending notifications to specific devices via
+those servers.
-This protocol is described for developers in [XEP-0357: Push Notifications].
+When you connect to your server with an app that requires push
+notifications, it will use this module to set up a "push registration".
+When you receive a message but your device is not connected to the
+server, this module will generate a notification and send it to the push
+gateway operated by your application's developers). Their gateway will
+then connect to your device's OS vendor and ask them to forward the
+notification to your device. When your device receives the notification,
+it will display it or wake up the app so it can connect to XMPP and
+receive any pending messages.
-For this module to work reliably, you must have [mod_smacks], [mod_mam] and
-[mod_carbons] also enabled on your server.
+This protocol is described for developers in \[XEP-0357: Push
+Notifications\].
+
+For this module to work reliably, you must have \[mod_smacks\],
+\[mod_mam\] and \[mod_carbons\] also enabled on your server.
-Some clients, notably Siskin and Snikket iOS need some additional extensions
-that are not currently defined in a standard XEP. To support these clients,
-see [mod_cloud_notify_extensions].
+Some clients, notably Siskin and Snikket iOS need some additional
+extensions that are not currently defined in a standard XEP. To support
+these clients, see \[mod_cloud_notify_extensions\].
-Configuration
-=============
+# Configuration
- Option Default Description
- ------------------------------------ ----------------- -------------------------------------------------------------------------------------------------------------------
- `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty
- `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled
- `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached)
- `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours)
- `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended.
- `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended.
+ Option Default Description
+ -------------------------------------- ---------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty
+ `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled
+ `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached)
+ `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours)
+ `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended.
+ `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended.
-(\*) There are privacy implications for enabling these options.
+(\*) There are privacy implications for enabling these options.[^1]
-Internal design notes
-=====================
+# Internal design notes
-App servers are notified about offline messages, messages stored by [mod_mam]
-or messages waiting in the smacks queue.
-The business rules outlined [here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) are all honored[^2].
+App servers are notified about offline messages, messages stored by
+\[mod_mam\] or messages waiting in the smacks queue. The business rules
+outlined
+[here](//mail.jabber.org/pipermail/standards/2016-February/030925.html)
+are all honored[^2].
-To cooperate with [mod_smacks] this module consumes some events:
-`smacks-ack-delayed`, `smacks-hibernation-start` and `smacks-hibernation-end`.
-These events allow this module to send out notifications for messages received
-while the session is hibernated by [mod_smacks] or even when smacks
-acknowledgements for messages are delayed by a certain amount of seconds
-configurable with the [mod_smacks] setting `smacks_max_ack_delay`.
+To cooperate with \[mod_smacks\] this module consumes some events:
+`smacks-ack-delayed`, `smacks-hibernation-start` and
+`smacks-hibernation-end`. These events allow this module to send out
+notifications for messages received while the session is hibernated by
+\[mod_smacks\] or even when smacks acknowledgements for messages are
+delayed by a certain amount of seconds configurable with the
+\[mod_smacks\] setting `smacks_max_ack_delay`.
-The `smacks_max_ack_delay` setting allows to send out notifications to clients
-which aren't already in smacks hibernation state (because the read timeout or
-connection close didn't already happen) but also aren't responding to acknowledgement
-request in a timely manner. This setting thus allows conversations to be smoother
-under such circumstances.
+The `smacks_max_ack_delay` setting allows to send out notifications to
+clients which aren't already in smacks hibernation state (because the
+read timeout or connection close didn't already happen) but also aren't
+responding to acknowledgement request in a timely manner. This setting
+thus allows conversations to be smoother under such circumstances.
-The new event `cloud-notify-ping` can be used by any module to send out a cloud
-notification to either all registered endpoints for the given user or only the endpoints
-given in the event data.
+The new event `cloud-notify-ping` can be used by any module to send out
+a cloud notification to either all registered endpoints for the given
+user or only the endpoints given in the event data.
-The config setting `push_notification_important_body` can be used to specify an alternative
-body text to send to the remote pubsub node if the stanza is encrypted or has a body.
-This way the real contents of the message aren't revealed to the push appserver but it
-can still see that the push is important.
-This is used by Chatsecure on iOS to send out high priority pushes in those cases for example.
+The config setting `push_notification_important_body` can be used to
+specify an alternative body text to send to the remote pubsub node if
+the stanza is encrypted or has a body. This way the real contents of the
+message aren't revealed to the push appserver but it can still see that
+the push is important. This is used by Chatsecure on iOS to send out
+high priority pushes in those cases for example.
-Compatibility
-=============
-
-**Note:** This module should be used with Lua 5.2 and higher. Using it with
-Lua 5.1 may cause push notifications to not be sent to some clients.
+# Compatibility
------- -----------------------------------------------------------------------------
- trunk Works
- 0.12 Works
- 0.11 Works
- 0.10 Works
- 0.9 Support dropped, use last supported version [675726ab06d3](//hg.prosody.im/prosody-modules/raw-file/675726ab06d3/mod_cloud_notify/mod_cloud_notify.lua)
------- -----------------------------------------------------------------------------
+**Note:** This module should be used with Lua 5.2 and higher. Using it
+with Lua 5.1 may cause push notifications to not be sent to some
+clients.
+ ------- -----------------------------------------------------------------
+ trunk Works as of 25-06-13
+ 13 Works
+ 0.12 Works
+ ------- -----------------------------------------------------------------
-[^1]: The service which is expected to forward notifications to something like Google Cloud Messaging or Apple Notification Service
-[^2]: [business_rules.markdown](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.markdown)
+[^1]: The service which is expected to forward notifications to
+ something like Google Cloud Messaging or Apple Notification Service
+
+[^2]: [business_rules.md](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.md)
| author | Menel <menel@snikket.de> |
|---|---|
| date | Fri, 13 Jun 2025 10:36:52 +0200 |
| parent | 204:316d7c8e1fb0 |
| rev | line source |
|---|---|
| 204 | 1 |
| 2 local type = type; | |
| 3 local t_insert, t_concat, t_remove = table.insert, table.concat, table.remove; | |
| 4 local s_char = string.char; | |
| 5 local tostring, tonumber = tostring, tonumber; | |
| 6 local pairs, ipairs = pairs, ipairs; | |
| 7 local next = next; | |
| 8 local error = error; | |
| 9 local newproxy, getmetatable = newproxy, getmetatable; | |
| 10 local print = print; | |
| 11 | |
| 12 --module("json") | |
| 13 local _M = {}; | |
| 14 | |
| 15 local null = newproxy and newproxy(true) or {}; | |
| 16 if getmetatable and getmetatable(null) then | |
| 17 getmetatable(null).__tostring = function() return "null"; end; | |
| 18 end | |
| 19 _M.null = null; | |
| 20 | |
| 21 local escapes = { | |
| 22 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", | |
| 23 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"}; | |
| 24 local unescapes = { | |
| 25 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/", | |
| 26 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"}; | |
| 27 for i=0,31 do | |
| 28 local ch = s_char(i); | |
| 29 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end | |
| 30 end | |
| 31 | |
| 32 local valid_types = { | |
| 33 number = true, | |
| 34 string = true, | |
| 35 table = true, | |
| 36 boolean = true | |
| 37 }; | |
| 38 local special_keys = { | |
| 39 __array = true; | |
| 40 __hash = true; | |
| 41 }; | |
| 42 | |
| 43 local simplesave, tablesave, arraysave, stringsave; | |
| 44 | |
| 45 function stringsave(o, buffer) | |
| 46 -- FIXME do proper utf-8 and binary data detection | |
| 47 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\""); | |
| 48 end | |
| 49 | |
| 50 function arraysave(o, buffer) | |
| 51 t_insert(buffer, "["); | |
| 52 if next(o) then | |
| 53 for i,v in ipairs(o) do | |
| 54 simplesave(v, buffer); | |
| 55 t_insert(buffer, ","); | |
| 56 end | |
| 57 t_remove(buffer); | |
| 58 end | |
| 59 t_insert(buffer, "]"); | |
| 60 end | |
| 61 | |
| 62 function tablesave(o, buffer) | |
| 63 local __array = {}; | |
| 64 local __hash = {}; | |
| 65 local hash = {}; | |
| 66 for i,v in ipairs(o) do | |
| 67 __array[i] = v; | |
| 68 end | |
| 69 for k,v in pairs(o) do | |
| 70 local ktype, vtype = type(k), type(v); | |
| 71 if valid_types[vtype] or v == null then | |
| 72 if ktype == "string" and not special_keys[k] then | |
| 73 hash[k] = v; | |
| 74 elseif (valid_types[ktype] or k == null) and __array[k] == nil then | |
| 75 __hash[k] = v; | |
| 76 end | |
| 77 end | |
| 78 end | |
| 79 if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then | |
| 80 t_insert(buffer, "{"); | |
| 81 local mark = #buffer; | |
| 82 for k,v in pairs(hash) do | |
| 83 stringsave(k, buffer); | |
| 84 t_insert(buffer, ":"); | |
| 85 simplesave(v, buffer); | |
| 86 t_insert(buffer, ","); | |
| 87 end | |
| 88 if next(__hash) ~= nil then | |
| 89 t_insert(buffer, "\"__hash\":["); | |
| 90 for k,v in pairs(__hash) do | |
| 91 simplesave(k, buffer); | |
| 92 t_insert(buffer, ","); | |
| 93 simplesave(v, buffer); | |
| 94 t_insert(buffer, ","); | |
| 95 end | |
| 96 t_remove(buffer); | |
| 97 t_insert(buffer, "]"); | |
| 98 t_insert(buffer, ","); | |
| 99 end | |
| 100 if next(__array) then | |
| 101 t_insert(buffer, "\"__array\":"); | |
| 102 arraysave(__array, buffer); | |
| 103 t_insert(buffer, ","); | |
| 104 end | |
| 105 if mark ~= #buffer then t_remove(buffer); end | |
| 106 t_insert(buffer, "}"); | |
| 107 else | |
| 108 arraysave(__array, buffer); | |
| 109 end | |
| 110 end | |
| 111 | |
| 112 function simplesave(o, buffer) | |
| 113 local t = type(o); | |
| 114 if t == "number" then | |
| 115 t_insert(buffer, tostring(o)); | |
| 116 elseif t == "string" then | |
| 117 stringsave(o, buffer); | |
| 118 elseif t == "table" then | |
| 119 tablesave(o, buffer); | |
| 120 elseif t == "boolean" then | |
| 121 t_insert(buffer, (o and "true" or "false")); | |
| 122 else | |
| 123 t_insert(buffer, "null"); | |
| 124 end | |
| 125 end | |
| 126 | |
| 127 function _M.encode(obj) | |
| 128 local t = {}; | |
| 129 simplesave(obj, t); | |
| 130 return t_concat(t); | |
| 131 end | |
| 132 | |
| 133 ----------------------------------- | |
| 134 | |
| 135 | |
| 136 function _M.decode(json) | |
| 137 local pos = 1; | |
| 138 local current = {}; | |
| 139 local stack = {}; | |
| 140 local ch, peek; | |
| 141 local function next() | |
| 142 ch = json:sub(pos, pos); | |
| 143 pos = pos+1; | |
| 144 peek = json:sub(pos, pos); | |
| 145 return ch; | |
| 146 end | |
| 147 | |
| 148 local function skipwhitespace() | |
| 149 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do | |
| 150 next(); | |
| 151 end | |
| 152 end | |
| 153 local function skiplinecomment() | |
| 154 repeat next(); until not(ch) or ch == "\r" or ch == "\n"; | |
| 155 skipwhitespace(); | |
| 156 end | |
| 157 local function skipstarcomment() | |
| 158 next(); next(); -- skip '/', '*' | |
| 159 while peek and ch ~= "*" and peek ~= "/" do next(); end | |
| 160 if not peek then error("eof in star comment") end | |
| 161 next(); next(); -- skip '*', '/' | |
| 162 skipwhitespace(); | |
| 163 end | |
| 164 local function skipstuff() | |
| 165 while true do | |
| 166 skipwhitespace(); | |
| 167 if ch == "/" and peek == "*" then | |
| 168 skipstarcomment(); | |
| 169 elseif ch == "/" and peek == "*" then | |
| 170 skiplinecomment(); | |
| 171 else | |
| 172 return; | |
| 173 end | |
| 174 end | |
| 175 end | |
| 176 | |
| 177 local readvalue; | |
| 178 local function readarray() | |
| 179 local t = {}; | |
| 180 next(); -- skip '[' | |
| 181 skipstuff(); | |
| 182 if ch == "]" then next(); return t; end | |
| 183 t_insert(t, readvalue()); | |
| 184 while true do | |
| 185 skipstuff(); | |
| 186 if ch == "]" then next(); return t; end | |
| 187 if not ch then error("eof while reading array"); | |
| 188 elseif ch == "," then next(); | |
| 189 elseif ch then error("unexpected character in array, comma expected"); end | |
| 190 if not ch then error("eof while reading array"); end | |
| 191 t_insert(t, readvalue()); | |
| 192 end | |
| 193 end | |
| 194 | |
| 195 local function checkandskip(c) | |
| 196 local x = ch or "eof"; | |
| 197 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end | |
| 198 next(); | |
| 199 end | |
| 200 local function readliteral(lit, val) | |
| 201 for c in lit:gmatch(".") do | |
| 202 checkandskip(c); | |
| 203 end | |
| 204 return val; | |
| 205 end | |
| 206 local function readstring() | |
| 207 local s = ""; | |
| 208 checkandskip("\""); | |
| 209 while ch do | |
| 210 while ch and ch ~= "\\" and ch ~= "\"" do | |
| 211 s = s..ch; next(); | |
| 212 end | |
| 213 if ch == "\\" then | |
| 214 next(); | |
| 215 if unescapes[ch] then | |
| 216 s = s..unescapes[ch]; | |
| 217 next(); | |
| 218 elseif ch == "u" then | |
| 219 local seq = ""; | |
| 220 for i=1,4 do | |
| 221 next(); | |
| 222 if not ch then error("unexpected eof in string"); end | |
| 223 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end | |
| 224 seq = seq..ch; | |
| 225 end | |
| 226 s = s..s.char(tonumber(seq, 16)); -- FIXME do proper utf-8 | |
| 227 next(); | |
| 228 else error("invalid escape sequence in string"); end | |
| 229 end | |
| 230 if ch == "\"" then | |
| 231 next(); | |
| 232 return s; | |
| 233 end | |
| 234 end | |
| 235 error("eof while reading string"); | |
| 236 end | |
| 237 local function readnumber() | |
| 238 local s = ""; | |
| 239 if ch == "-" then | |
| 240 s = s..ch; next(); | |
| 241 if not ch:match("[0-9]") then error("number format error"); end | |
| 242 end | |
| 243 if ch == "0" then | |
| 244 s = s..ch; next(); | |
| 245 if ch:match("[0-9]") then error("number format error"); end | |
| 246 else | |
| 247 while ch and ch:match("[0-9]") do | |
| 248 s = s..ch; next(); | |
| 249 end | |
| 250 end | |
| 251 if ch == "." then | |
| 252 s = s..ch; next(); | |
| 253 if not ch:match("[0-9]") then error("number format error"); end | |
| 254 while ch and ch:match("[0-9]") do | |
| 255 s = s..ch; next(); | |
| 256 end | |
| 257 if ch == "e" or ch == "E" then | |
| 258 s = s..ch; next(); | |
| 259 if ch == "+" or ch == "-" then | |
| 260 s = s..ch; next(); | |
| 261 if not ch:match("[0-9]") then error("number format error"); end | |
| 262 while ch and ch:match("[0-9]") do | |
| 263 s = s..ch; next(); | |
| 264 end | |
| 265 end | |
| 266 end | |
| 267 end | |
| 268 return tonumber(s); | |
| 269 end | |
| 270 local function readmember(t) | |
| 271 local k = readstring(); | |
| 272 checkandskip(":"); | |
| 273 t[k] = readvalue(); | |
| 274 end | |
| 275 local function fixobject(obj) | |
| 276 local __array = obj.__array; | |
| 277 if __array then | |
| 278 obj.__array = nil; | |
| 279 for i,v in ipairs(__array) do | |
| 280 t_insert(obj, v); | |
| 281 end | |
| 282 end | |
| 283 local __hash = obj.__hash; | |
| 284 if __hash then | |
| 285 obj.__hash = nil; | |
| 286 local k; | |
| 287 for i,v in ipairs(__hash) do | |
| 288 if k ~= nil then | |
| 289 obj[k] = v; k = nil; | |
| 290 else | |
| 291 k = v; | |
| 292 end | |
| 293 end | |
| 294 end | |
| 295 return obj; | |
| 296 end | |
| 297 local function readobject() | |
| 298 local t = {}; | |
| 299 next(); -- skip '{' | |
| 300 skipstuff(); | |
| 301 if ch == "}" then next(); return t; end | |
| 302 if not ch then error("eof while reading object"); end | |
| 303 readmember(t); | |
| 304 while true do | |
| 305 skipstuff(); | |
| 306 if ch == "}" then next(); return fixobject(t); end | |
| 307 if not ch then error("eof while reading object"); | |
| 308 elseif ch == "," then next(); | |
| 309 elseif ch then error("unexpected character in object, comma expected"); end | |
| 310 if not ch then error("eof while reading object"); end | |
| 311 readmember(t); | |
| 312 end | |
| 313 end | |
| 314 | |
| 315 function readvalue() | |
| 316 skipstuff(); | |
| 317 while ch do | |
| 318 if ch == "{" then | |
| 319 return readobject(); | |
| 320 elseif ch == "[" then | |
| 321 return readarray(); | |
| 322 elseif ch == "\"" then | |
| 323 return readstring(); | |
| 324 elseif ch:match("[%-0-9%.]") then | |
| 325 return readnumber(); | |
| 326 elseif ch == "n" then | |
| 327 return readliteral("null", null); | |
| 328 elseif ch == "t" then | |
| 329 return readliteral("true", true); | |
| 330 elseif ch == "f" then | |
| 331 return readliteral("false", false); | |
| 332 end | |
| 333 end | |
| 334 error("eof while reading value"); | |
| 335 end | |
| 336 next(); | |
| 337 return readvalue(); | |
| 338 end | |
| 339 | |
| 340 function _M.test(object) | |
| 341 local encoded = encode(object); | |
| 342 local decoded = decode(encoded); | |
| 343 local recoded = encode(decoded); | |
| 344 if encoded ~= recoded then | |
| 345 print("FAILED"); | |
| 346 print("encoded:", encoded); | |
| 347 print("recoded:", recoded); | |
| 348 else | |
| 349 print(encoded); | |
| 350 end | |
| 351 return encoded ~= recoded; | |
| 352 end | |
| 353 | |
| 354 return _M; |