annotate mod_muc_eventsource/mod_muc_eventsource.lua @ 4352:f6da234b21b8

mod_http_admin_api: Add groups property to users
author Matthew Wild <mwild1@gmail.com>
date Sun, 17 Jan 2021 17:51:23 +0000
parents 39485b9bbdd6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2883
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module:depends("http");
2897
39485b9bbdd6 mod_muc_eventsource: validate node with nodeprep
Senya <senya@kinetiksoft.com>
parents: 2883
diff changeset
2 local nodeprep = require "util.encodings".stringprep.nodeprep;
2883
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local jid_split = require "util.jid".split;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local json = require "util.json";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local streams = {};
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 function client_closed(response)
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local node = response._eventsource_node;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 module:log("debug", "Destroying client for %q", node);
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 streams[node][response] = nil;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 if next(streams[node]) == nil then
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 streams[node] = nil;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 function serve_stream(event, node)
2897
39485b9bbdd6 mod_muc_eventsource: validate node with nodeprep
Senya <senya@kinetiksoft.com>
parents: 2883
diff changeset
19 local response = event.response;
39485b9bbdd6 mod_muc_eventsource: validate node with nodeprep
Senya <senya@kinetiksoft.com>
parents: 2883
diff changeset
20
39485b9bbdd6 mod_muc_eventsource: validate node with nodeprep
Senya <senya@kinetiksoft.com>
parents: 2883
diff changeset
21 node = nodeprep(node);
39485b9bbdd6 mod_muc_eventsource: validate node with nodeprep
Senya <senya@kinetiksoft.com>
parents: 2883
diff changeset
22 if node == nil then
39485b9bbdd6 mod_muc_eventsource: validate node with nodeprep
Senya <senya@kinetiksoft.com>
parents: 2883
diff changeset
23 return 400;
39485b9bbdd6 mod_muc_eventsource: validate node with nodeprep
Senya <senya@kinetiksoft.com>
parents: 2883
diff changeset
24 end
39485b9bbdd6 mod_muc_eventsource: validate node with nodeprep
Senya <senya@kinetiksoft.com>
parents: 2883
diff changeset
25
2883
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 module:log("debug", "Client subscribed to: %s", node);
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 response.on_destroy = client_closed;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 response._eventsource_node = node;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 response.conn:write(table.concat({
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 "HTTP/1.1 200 OK";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 "Content-Type: text/event-stream";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 "Access-Control-Allow-Origin: *";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 "Access-Control-Allow-Methods: GET";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 "Access-Control-Max-Age: 7200";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 "";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 "";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 }, "\r\n"));
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local clientlist = streams[node];
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if not clientlist then
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 clientlist = {};
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 streams[node] = clientlist;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 clientlist[response] = response.conn;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return true;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 function handle_message(event)
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local room, stanza = event.room, event.stanza;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local node = (jid_split(event.room.jid));
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local clientlist = streams[node];
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 if not clientlist then module:log("debug", "No clients for %q", node); return; end
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 -- Extract body from message
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local body = event.stanza:get_child_text("body");
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if not body then
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local nick = select(3, jid_split(stanza.attr.from));
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 -- Encode body and broadcast to eventsource subscribers
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local json_data = json.encode({
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 nick = nick;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 body = body;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 });
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local data = "data: "..json_data:gsub("\n", "\ndata: \n").."\n\n";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 for response, conn in pairs(clientlist) do
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 conn:write(data);
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 module:provides("http", {
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 name = "eventsource";
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 route = {
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 ["GET /*"] = serve_stream;
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 };
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 });
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
7c16afc70d11 mod_muc_eventsource: New module forked from mod_pubsub_eventsource, exposes room message stream over SSE
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 module:hook("muc-broadcast-message", handle_message);