annotate mod_http_presence/mod_http_presence.lua @ 6319:04c3273cb81f

mod_auth_cyrus: Add empty 'profile' table to SASL handler objects This is for compatibility with Prosody's built-in util.sasl objects. A SASL profile table usually includes methods supported by the backend, which can be used by SASL mechanism handlers to perform operations (such as testing the password). It also optionally contains a 'cb' field with channel binding method handlers. The Cyrus backend doesn't support channel binding, and doesn't have the same concept of auth backend methods (it handles all that internally, and Prosody has no insight or control over it). Thus, we create an empty profile which informs Prosody that the SASL handler does not support any of the auth or channel binding methods. Some features will not work, but they didn't work anyway. This just makes it explicit. This fixes a traceback in mod_sasl2_fast, which expected SASL handlers to always contain a 'profile' field.
author Matthew Wild <mwild1@gmail.com>
date Thu, 04 Sep 2025 10:14:46 +0100
parents 9dcdb56f75dd
children
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
6287
9dcdb56f75dd mod_http_presence: news flash: order of vcard is not always the same (fixes 500 on nickname)
Nicholas George <wirlaburla@worlio.com>
parents: 6285
diff changeset
91 if nick_item and nick_item.tags and nick_item.tags[1] and nick_item.tags[1].tags then
9dcdb56f75dd mod_http_presence: news flash: order of vcard is not always the same (fixes 500 on nickname)
Nicholas George <wirlaburla@worlio.com>
parents: 6285
diff changeset
92 for _, tag in ipairs(nick_item.tags[1].tags) do
9dcdb56f75dd mod_http_presence: news flash: order of vcard is not always the same (fixes 500 on nickname)
Nicholas George <wirlaburla@worlio.com>
parents: 6285
diff changeset
93 if tag.name == "nickname" and tag.tags and tag.tags[1] and tag.tags[1][1] then
9dcdb56f75dd mod_http_presence: news flash: order of vcard is not always the same (fixes 500 on nickname)
Nicholas George <wirlaburla@worlio.com>
parents: 6285
diff changeset
94 nickname = tag.tags[1][1];
9dcdb56f75dd mod_http_presence: news flash: order of vcard is not always the same (fixes 500 on nickname)
Nicholas George <wirlaburla@worlio.com>
parents: 6285
diff changeset
95 module:log("debug", "Nickname found for JID %s: %s", bare_jid, nickname);
9dcdb56f75dd mod_http_presence: news flash: order of vcard is not always the same (fixes 500 on nickname)
Nicholas George <wirlaburla@worlio.com>
parents: 6285
diff changeset
96 return nickname;
9dcdb56f75dd mod_http_presence: news flash: order of vcard is not always the same (fixes 500 on nickname)
Nicholas George <wirlaburla@worlio.com>
parents: 6285
diff changeset
97 end
6285
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
98 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
99 else
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
100 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
101 return nil;
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
102 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
103
6287
9dcdb56f75dd mod_http_presence: news flash: order of vcard is not always the same (fixes 500 on nickname)
Nicholas George <wirlaburla@worlio.com>
parents: 6285
diff changeset
104 module:log("debug", "No <nickname> element in vCard4 for JID %s", bare_jid);
9dcdb56f75dd mod_http_presence: news flash: order of vcard is not always the same (fixes 500 on nickname)
Nicholas George <wirlaburla@worlio.com>
parents: 6285
diff changeset
105 return jid.node(bare_jid);
6285
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
106 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
107
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
108 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
109 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
110 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
111 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
112 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
113 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
114 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
115
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
116 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
117 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
118 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
119 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
120 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
121
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
122 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
123 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
124 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
125 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
126 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
127
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
128 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
129 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
130 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
131 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
132 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
133
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
134 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
135 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
136 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
137 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
138 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
139 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
140
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
141 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
142 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
143 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
144 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
145
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
146 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
147 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
148 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
149 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
150 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
151 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
152 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
153
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
154 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
155 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
156 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
157 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
158 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
159
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
160 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
161 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
162 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
163 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
164 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
165 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
166
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
167 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
168 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
169 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
170
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
171 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
172 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
173 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
174 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
175 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
176 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
177 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
178 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
179 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
180 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
181 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
182 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
183 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
184 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
185 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
186 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
187 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
188 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
189 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
190 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
191
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
192 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
193 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
194 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
195
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
196 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
197 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
198 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
199
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
200 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
201 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
202
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
203 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
204 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
205 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
206 [[<!DOCTYPE html>]]..
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
207 tostring(
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
208 stanza("html")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
209 :tag("head")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
210 :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
211 :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
212 :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("body")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
214 :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
215 :tag("colgroup")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
216 :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
217 :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
218 :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("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
220 :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
221 :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
222 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
223 :tag("td")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
224 :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
225 :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
226 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
227 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
228 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
229 :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
230 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
231 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
232 :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
233 :tag("i")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
234 :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
235 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
236 :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 )
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
239 );
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
240 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
241 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
242 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
243 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
244 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
245 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254 end
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(status_resource);
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
256 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
257 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
258 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
259 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
260 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
261 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
262 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
263 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
264 else
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
265 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
266 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
267 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
268 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
269
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
270 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
271 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
272 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
273
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
274 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
275 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
276 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
277 [[<!DOCTYPE html>]]..
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
278 tostring(
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
279 stanza("html")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
280 :tag("head")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
281 :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
282 :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
283 :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("body")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
285 :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
286 :tag("colgroup")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
287 :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
288 :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
289 :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("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
291 :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
292 :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
293 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
294 :tag("td")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
295 :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
296 :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
297 :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
298 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
299 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
300 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
301 :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
302 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
303 :tag("tr")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
304 :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
305 :tag("i")
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
306 :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
307 :up()
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
308 :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 )
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
311 );
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
312 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
313 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
314 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
315 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
316 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
317 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
318 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
319 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
320 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
321 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
322 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
323 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
324 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
325 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
326 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
327 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
328 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
329 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
330 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
331 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
332 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
333 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
334 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
335 else
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
336 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
337 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
338 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
339 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
340
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
341 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
342 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
343 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
344 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
345 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
346 return "";
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
347 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
348 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
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
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
351 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
352 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
353 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
354 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
355 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
356
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
357 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
358 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
359 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
360 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
361
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
362 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
363 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
364 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
365 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
366 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
367 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
368 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
369 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
370 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
371 else
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
372 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
373 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
374 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
375 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
376 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
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 end
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
379
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
380 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
381 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
382 route = {
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
383 ["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
384 };
27e061d455b9 mod_http_presence: add new module to display presence over the http server
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
385 });