annotate mod_http_presence/mod_http_presence.lua @ 6285:27e061d455b9

mod_http_presence: add new module to display presence over the http server
author Nicholas George <wirlaburla@worlio.com>
date Sun, 06 Jul 2025 15:57:55 -0500
parents
children 9dcdb56f75dd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6285
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
1 local mod_pep = module:depends("pep");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
2 module:depends("http");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
3
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
4 local storagemanager = require "core.storagemanager";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
5 local usermanager = require "core.usermanager";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
6 local stanza = require "util.stanza".stanza;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
7 local deserialize = require "util.stanza".deserialize;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
8 local base64_decode = require "util.encodings".base64.decode;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
9 local base64_encode = require "util.encodings".base64.encode;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
10 local http = require "net.http";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
11 local jid = require "util.jid";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
12
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
13 function get_user_presence(bare_jid)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
14 local host = jid.host(bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
15 local sessions = prosody.hosts[host] and prosody.hosts[host].sessions[jid.node(bare_jid)];
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
16 if not sessions then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
17 return { status = "offline", message = nil };
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
18 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
19
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
20 local highest_priority_session = nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
21 local highest_priority = -math.huge;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
22
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
23 for resource, session in pairs(sessions.sessions) do
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
24 if session.presence then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
25 local priority = session.priority or 0;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
26 if priority > highest_priority then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
27 highest_priority = priority;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
28 highest_priority_session = session;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
29 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
30 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
31 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
32
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
33 if not highest_priority_session then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
34 return { status = "offline", message = nil };
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
35 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
36
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
37 local presence = highest_priority_session.presence;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
38 return {
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
39 status = presence and (presence:get_child("show") and presence:get_child("show"):get_text() or "online") or "offline",
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
40 message = presence and presence:get_child("status") and presence:get_child("status"):get_text() or nil
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
41 };
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
42 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
43
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
44 function get_user_avatar(bare_jid)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
45 local pep_service = mod_pep.get_pep_service(jid.node(bare_jid));
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
46 if not pep_service then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
47 module:log("error", "PEP storage not available");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
48 return nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
49 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
50
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
51 local meta_ok, hash, meta = pep_service:get_last_item("urn:xmpp:avatar:metadata", module.host);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
52 if not meta_ok or not hash then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
53 module:log("debug", "Failed to get avatar metadata for %s: %s", bare_jid, "Not OK");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
54 return nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
55 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
56
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
57 local data_ok, data_hash, data = pep_service:get_last_item("urn:xmpp:avatar:data", module.host, hash);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
58 local data_err = nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
59 if not data_ok then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
60 data_err = "Not OK";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
61 elseif data_hash ~= hash then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
62 data_err = "Hash does not match";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
63 elseif type(data) ~= "table" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
64 data_err = "Data of type table";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
65 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
66 if data_err then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
67 module:log("debug", "Failed to get avatar data for %s, hash %s: %s", bare_jid, hash, data_err);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
68 return nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
69 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
70 local info = meta.tags[1]:get_child("info");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
71 if not info then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
72 module:log("debug", "Missing avatar info for %s, hash %s", bare_jid, hash);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
73 return nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
74 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
75 return info and info.attr.type or "application/octet-stream", data[1]:get_text();
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
76 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
77
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
78 function get_user_nickname(bare_jid)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
79 local pep_service = mod_pep.get_pep_service(jid.node(bare_jid));
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
80 if not pep_service then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
81 module:log("error", "PEP storage not available");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
82 return nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
83 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
84
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
85 local ok, nick, nick_item = pep_service:get_last_item("urn:xmpp:vcard4", module.host);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
86 if not ok then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
87 module:log("debug", "Failed to get nick for %s: %s", bare_jid, "Not OK");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
88 return nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
89 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
90
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
91 local nickname;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
92 if nick_item and nick_item.tags and nick_item.tags[1] and nick_item.tags[1].tags and nick_item.tags[1].tags[2] then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
93
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
94 local nick_element = nick_item.tags[1].tags[2]; -- <nickname> element
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
95 if nick_element.name == "nickname" and nick_element.tags[1] and nick_element.tags[2][1] then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
96 nickname = nick_element.tags[2][1]; -- Text content: "Wongo"
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
97 module:log("debug", "Nickname found for JID %s: %s", bare_jid, nickname);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
98 else
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
99 module:log("debug", "No <nickname> element in vCard4 for JID %s", bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
100 return nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
101 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
102 else
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
103 module:log("debug", "Invalid vCard4 item structure for JID %s", bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
104 return nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
105 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
106
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
107 return nickname or jid.node(bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
108 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
109
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
110 function get_muc_avatar(bare_jid)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
111 local node = jid.node(bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
112 local vcard_store = storagemanager.open(module.host, "vcard_muc")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
113 if not vcard_store then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
114 module:log("error", "MUC vCard store not available for host: %s", module.host);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
115 return nil, nil, "MUC vCard store not available";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
116 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
117
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
118 local vcard_data, err = vcard_store:get(node);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
119 if not vcard_data then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
120 module:log("debug", "No vCard data for MUC %s: %s", bare_jid, err or "No data");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
121 return nil, nil, err or "No vCard data";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
122 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
123
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
124 local vcard = deserialize(vcard_data);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
125 if not vcard then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
126 module:log("debug", "Failed to parse vCard for MUC %s", bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
127 return nil, nil, "Failed to parse vCard";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
128 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
129
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
130 local photo = vcard:get_child("PHOTO");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
131 if not photo then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
132 module:log("debug", "No <PHOTO> element in vCard for MUC %s", bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
133 return nil, nil, "No photo element";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
134 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
135
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
136 local content_type = photo:get_child_text("TYPE") or "application/octet-stream";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
137 local avatar_data = photo:get_child_text("BINVAL");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
138 if not avatar_data then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
139 module:log("debug", "No <BINVAL> in <PHOTO> for MUC %s", bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
140 return nil, nil, "No avatar data";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
141 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
142
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
143 module:log("debug", "MUC avatar found for JID %s: type=%s, data=%s",
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
144 bare_jid, content_type, avatar_data:sub(1, 20) .. "...");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
145 return content_type, avatar_data, nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
146 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
147
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
148 function get_muc_info(bare_jid)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
149 local node = jid.node(bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
150 local muc_store = storagemanager.open(module.host, "config");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
151 if not muc_store then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
152 module:log("error", "MUC config store not available for host: %s", module.host);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
153 return nil, nil, "MUC config store not available";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
154 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
155
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
156 local config_data, err = muc_store:get(node);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
157 if not config_data then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
158 module:log("debug", "No config data for JID %s: %s", bare_jid, err or "No data");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
159 return nil, nil, err or "No config data";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
160 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
161
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
162 local muc_name = config_data._data and config_data._data.name;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
163 local muc_description = config_data._data and config_data._data.description;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
164 if not muc_name and not muc_description then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
165 module:log("debug", "No name or description in config for JID %s", bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
166 return nil, nil, "No name or description";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
167 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
168
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
169 module:log("debug", "MUC info for JID %s: name=%s, desc=%s", bare_jid, muc_name, muc_description);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
170 return muc_name, muc_description, nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
171 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
172
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
173 function get_muc_users(bare_jid)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
174 local component = hosts[module.host];
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
175 if not component then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
176 module:log("error", "No component found for host: %s", module.host);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
177 return nil, "No MUC component found";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
178 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
179 local muc = component.modules.muc;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
180 if not muc then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
181 module:log("error", "MUC module not loaded for host: %s", module.host);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
182 return nil, "MUC module not loaded";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
183 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
184 local room = muc.get_room_from_jid(bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
185 if not room then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
186 module:log("error", "Room %s does not exist", bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
187 return nil, "Room does not exist";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
188 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
189 local count = 0;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
190 for _ in room:each_occupant() do
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
191 count = count + 1;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
192 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
193
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
194 module:log("debug", "Room %s has %d occupants", bare_jid, count);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
195 return count, nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
196 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
197
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
198 function serve_user(response, format, user_jid)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
199 local presence = get_user_presence(user_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
200 local nickname = get_user_nickname(user_jid) or user_jid;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
201
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
202 local status = presence.status or "offline";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
203 local message = presence.message or "";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
204
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
205 if not format or format == "" or format == "full" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
206 response.headers["Content-Type"] = "text/html";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
207 return response:send(
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
208 [[<!DOCTYPE html>]]..
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
209 tostring(
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
210 stanza("html")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
211 :tag("head")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
212 :tag("title"):text(nickname):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
213 :tag("link", { rel = "stylesheet", href = "data:text/css;base64,"..base64_encode(request_resource("style.css")) })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
214 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
215 :tag("body")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
216 :tag("table", { width = "100%" })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
217 :tag("colgroup")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
218 :tag("col", { width = "64px" }):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
219 :tag("col"):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
220 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
221 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
222 :tag("td", { rowspan = "3", valign = "top" })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
223 :tag("img", { id = "avatar", src = "./avatar", width = "64" })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
224 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
225 :tag("td")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
226 :tag("img", { id = "status-icon", src = "./status-icon", title = status, alt = "("..status..")" }):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
227 :tag("b", { id = "nickname"}):text(" "..nickname):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
228 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
229 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
230 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
231 :tag("td", { id = "msg-cell" }):text(message):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
232 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
233 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
234 :tag("td", { id = "jid-cell" })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
235 :tag("i")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
236 :tag("a", { href = "xmpp:"..user_jid.."?add" }):text(user_jid):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
237 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
238 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
239 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
240 )
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
241 );
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
242 elseif format == "nickname" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
243 response.headers["Content-Type"] = "text/plain";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
244 return response:send(nickname);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
245 elseif format == "status" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
246 response.headers["Content-Type"] = "text/plain";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
247 return response:send(status);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
248 elseif format == "message" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
249 response.headers["Content-Type"] = "text/plain";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
250 return response:send(message);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
251 elseif format == "status-icon" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
252 response.headers["Content-Type"] = "image/png";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
253 local status_resource = request_resource(status..".png");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
254 if not status_resource then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
255 return response:send(request_resource("offline.png"));
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
256 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
257 return response:send(status_resource);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
258 elseif format == "avatar" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
259 local avatar_mime, avatar_data = get_user_avatar(user_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
260 if not avatar_mime or not avatar_data then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
261 response.headers["Content-Type"] = "image/png";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
262 return response:send(request_resource("avatar.png"));
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
263 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
264 response.headers["Content-Type"] = avatar_mime;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
265 return response:send(base64_decode(avatar_data));
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
266 else
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
267 response.headers["Content-Type"] = "text/plain";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
268 return response:send(status..": "..message);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
269 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
270 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
271
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
272 function serve_muc(response, format, muc_jid)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
273 local muc_name, muc_desc, err = get_muc_info(muc_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
274 local muc_users, _ = get_muc_users(muc_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
275
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
276 if not format or format == "" or format == "full" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
277 response.headers["Content-Type"] = "text/html";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
278 return response:send(
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
279 [[<!DOCTYPE html>]]..
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
280 tostring(
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
281 stanza("html")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
282 :tag("head")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
283 :tag("title"):text(muc_name or muc_jid):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
284 :tag("link", { rel = "stylesheet", href = "data:text/css;base64,"..base64_encode(request_resource("style.css")) })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
285 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
286 :tag("body")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
287 :tag("table", { width = "100%" })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
288 :tag("colgroup")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
289 :tag("col", { width = "64px" }):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
290 :tag("col"):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
291 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
292 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
293 :tag("td", { rowspan = "3", valign = "top" })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
294 :tag("img", { id = "avatar", src = "./avatar", width = "64" })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
295 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
296 :tag("td")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
297 :tag("img", { id = "status-icon", src = "./status-icon", title = "muc", alt = "(muc)" }):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
298 :tag("b", { id = "nickname" }):text(" "..(muc_name or muc_jid)):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
299 :tag("a", { id = "muc-users" }):text(" ("..muc_users.." users)"):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
300 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
301 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
302 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
303 :tag("td", { id = "msg-cell" }):text(muc_desc):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
304 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
305 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
306 :tag("td", { id = "jid-cell" })
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
307 :tag("i")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
308 :tag("a", { href = "xmpp:"..muc_jid.."?join" }):text(muc_jid):up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
309 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
310 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
311 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
312 )
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
313 );
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
314 elseif format == "users" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
315 response.headers["Content-Type"] = "text/plain";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
316 return response:send(muc_users.." users");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
317 elseif format == "name" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
318 response.headers["Content-Type"] = "text/plain";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
319 return response:send(muc_name);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
320 elseif format == "status" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
321 response.headers["Content-Type"] = "text/plain";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
322 return response:send("muc");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
323 elseif format == "description" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
324 response.headers["Content-Type"] = "text/plain";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
325 return response:send(muc_desc);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
326 elseif format == "status-icon" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
327 response.headers["Content-Type"] = "image/png";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
328 return response:send(request_resource("muc.png"));
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
329 elseif format == "avatar" then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
330 local avatar_mime, avatar_data = get_muc_avatar(muc_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
331 if not avatar_mime or not avatar_data then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
332 response.headers["Content-Type"] = "image/png";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
333 return response:send(request_resource("avatar.png"));
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
334 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
335 response.headers["Content-Type"] = avatar_mime;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
336 return response:send(base64_decode(avatar_data));
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
337 else
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
338 response.headers["Content-Type"] = "text/plain";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
339 return response:send((muc_name or muc_jid)..": "..(muc_desc or ""));
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
340 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
341 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
342
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
343 function request_resource(name)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
344 local resource_path = module:get_option_string("presence_resource_path", "resources");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
345 local i, err = module:load_resource(resource_path.."/"..name);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
346 if not i then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
347 module:log("warn", "Failed to open resource file %s: %s", resource_path.."/"..name, err);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
348 return "";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
349 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
350 return i:read("*a");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
351 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
352
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
353 function handle_request(event, path)
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
354 local request = event.request;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
355 local response = event.response;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
356 local name, format = path:match("^([%w-_\\.]+)/(.*)$");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
357 module:log("debug", "loading format '%s' for jid %s", format or "standard", name);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
358
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
359 if not name then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
360 response.status_code = 404;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
361 return response:send("Missing JID");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
362 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
363
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
364 local bare_jid = jid.join(name, module.host, nil);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
365 local component = hosts[module.host];
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
366 if component.type == "component" and component.modules.muc then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
367 local muc = component.modules.muc;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
368 if not muc.get_room_from_jid(bare_jid) then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
369 response.status_code = 404;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
370 return response:send("MUC does not exist");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
371 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
372 return serve_muc(response, format or "full", bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
373 else
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
374 if not usermanager.user_exists(name, module.host) then
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
375 response.status_code = 404;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
376 return response:send("User does not exist");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
377 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
378 return serve_user(response, format or "full", bare_jid);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
379 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
380 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
381
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
382 module:provides("http", {
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
383 default_path = module:get_option_string("presence_http_path", "/presence");
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
384 route = {
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
385 ["GET /*"] = handle_request;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
386 };
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
387 });