Annotate

mod_webpresence/mod_webpresence.lua @ 5591:680fb3344357

mod_invites_register_web: Don't register user when refresh `register_error.html`.
author Trần H. Trung <work@trung.fun>
date Fri, 26 May 2023 02:15:45 +0700
parent 5588:f16720087ef2
child 5594:14480ca9576e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
1 module:depends("adhoc");
643
28b0a8cd950a mod_webpresence: Update to timber, remove squish hack (icons are now loaded relative to the module file)
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
2 module:depends("http");
28b0a8cd950a mod_webpresence: Update to timber, remove squish hack (icons are now loaded relative to the module file)
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
3
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
4 local moduleHost = module.host;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
5 local moduleName = module:get_name();
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
6 local jid = require "util.jid";
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local jid_split = require "util.jid".prepped_split;
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
8 local serialization = require "util.serialization";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
9
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
10 -- ADHOC
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
11 local storage = module:open_store(moduleName, "keyval");
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
12 local utilDataforms = require "util.dataforms";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
13 local utilAdhoc = require "util.adhoc";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
14 local adhoc_new = module:require("adhoc").new;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
15
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
16 local function webpresence_set(user, value)
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
17 local ok, err = storage:set(user, value); -- value is table.
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
18
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
19 if not ok or err then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
20 module:log(error, "Could not write data %s", tostring(user));
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
21 return ok, err;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
22 else
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
23 return ok;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
24 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
25 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
26 local function webpresence_get(user)
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
27 local result = storage:get(user);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
28 if not result then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
29 result = { ["webpresence"] = false };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
30 webpresence_set(user, result);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
31 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
32 return result[moduleName]; -- bool
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
33 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
34
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
35 local form = utilDataforms.new {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
36 title = "Web Presence Policy";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
37 instructions = "Your webpresence shows offline by default";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
38 {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
39 type = "boolean";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
40 name = moduleName;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
41 label = "Show";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
42 value = webpresence_get();
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
43 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
44 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
45 local formResult = utilDataforms.new {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
46 title = "Web Presence Policy";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
47 {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
48 type = "boolean";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
49 name = moduleName;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
50 label = "Show";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
51 value;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
52 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
53 {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
54 type = "text-multi";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
55 name = "url";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
56 label = "Check your presence at";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
57 value = "text-multi\n";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
58 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
59 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
60
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
61 local function webpresence_url(jid_bare)
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
62 local config, path = module:get_option("http_paths");
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
63 if config then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
64 for k, v in pairs(config) do
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
65 if k == moduleName then path = v;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
66 else path = "/status" .. "/"; end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
67 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
68 else path = "/status" .. "/"; end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
69
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
70 local urlConfig = module:get_option_string("http_external_link");
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
71 local urlBase = urlConfig..path..jid_bare;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
72 local style = { "/text", "/message", "/json", "/html" };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
73 local urlResult = urlBase.."\n";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
74 for _, v in ipairs(style) do
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
75 urlResult = urlResult..urlBase..v.."\n";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
76 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
77 return urlResult;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
78 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
79
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
80 local adhoc_handler = utilAdhoc.new_simple_form(form, function(fields, state, data)
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
81 local jid_bare = jid.bare(data.from);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
82 local user, host = jid_split(jid_bare);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
83
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
84 local oldData, _ = storage:get(user);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
85 oldValue = webpresence_get(user);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
86 form.webpresence = oldValue;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
87
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
88 local urlResult = webpresence_url(jid_bare);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
89 local newValue = {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
90 [moduleName] = fields.webpresence;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
91 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
92
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
93 if state then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
94 return {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
95 status = "completed";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
96 info = "No change for: "..tostring(data.from).." …\n"
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
97 .."Old data: "..serialization.serialize(oldData).."\n"
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
98 .."New data: "..serialization.serialize(newValue).."\n";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
99 result = {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
100 layout = formResult;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
101 values = {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
102 webpresence = oldValue;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
103 url = urlResult;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
104 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
105 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
106 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
107 else
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
108 local resultSet, resultErr = webpresence_set(user, newValue)
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
109 if not resultSet or resultErr then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
110 module:log(error, "Could not set value: %s", errOut);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
111 return {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
112 status = "completed";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
113 info = "Could not set value: "..tostring(data.from).." …\n"
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
114 .."Old data: "..serialization.serialize(oldData).."\n"
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
115 .."New data: "..serialization.serialize(newValue).."\n"
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
116 .."Error: "..errOut.."\n";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
117 result = {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
118 layout = formResult;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
119 values = {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
120 webpresence = newValue[moduleName];
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
121 url = urlResult;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
122 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
123 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
124 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
125 else
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
126 return {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
127 status = "completed";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
128 info = "Changing value for: "..tostring(data.from).." …\n"
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
129 .."Old data: "..serialization.serialize(oldData).."\n"
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
130 .."New data: "..serialization.serialize(newValue).."\n";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
131 result = {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
132 layout = formResult;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
133 values = {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
134 webpresence = newValue[moduleName];
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
135 url = urlResult;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
136 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
137 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
138 };
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
139 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
140 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
141 end);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
142
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
143 module:provides("adhoc", adhoc_new("Web Presence Policy", moduleName, adhoc_handler, "any"));
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
144
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
145 -- HTTP
782
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
146 local b64 = require "util.encodings".base64.encode;
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
147 local sha1 = require "util.hashes".sha1;
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
148 local stanza = require "util.stanza".stanza;
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
149 local json = require "util.json".encode_ordered;
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
150 local usermanager = require "core.usermanager";
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
152 local function require_resource(name)
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
153 local icon_path = module:get_option_string("webpresence_icons", "icons");
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
154 local f, err = module:load_resource(icon_path.."/"..name);
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
155 if f then
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
156 return f:read("*a");
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
157 end
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
158 module:log("warn", "Failed to open image file %s", icon_path..name);
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
159 return "";
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
162 local statuses = { online = {}, away = {}, xa = {}, dnd = {}, chat = {}, offline = {} };
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
164 local function user_list(host) return user:list(host); end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
165
643
28b0a8cd950a mod_webpresence: Update to timber, remove squish hack (icons are now loaded relative to the module file)
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
166 local function handle_request(event, path)
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
167 local status, message;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
168 local jid, type = path:match("([^/]+)/?(.*)$");
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
169 if jid then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
170 local user, host = jid_split(jid);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
171 if host and not user then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
172 user, host = host, event.request.headers.host;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
173 if host then host = host:gsub(":%d+$", ""); end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
174 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
175 if host ~= moduleHost then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
176 status = "offline";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
177 else
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
178 if user and host and usermanager.user_exists(user, host) then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
179 local user_sessions = hosts[host] and hosts[host].sessions[user];
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
180 local show = webpresence_get(user)
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
181 if show == false then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
182 status = "offline";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
183 else
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
184 if user_sessions and user_sessions.top_resources then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
185 status = user_sessions.top_resources[1];
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
186 if status and status.presence then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
187 message = status.presence:child_with_name("status");
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
188 status = status.presence:child_with_name("show");
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
189 if not status then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
190 status = "online";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
191 else
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
192 status = status:get_text();
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
193 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
194 if message then
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
195 message = message:get_text();
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
196 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
197 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
198 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
199 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
200 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
201 end
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
202 status = status or "offline";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
203 end
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
204
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
205 statuses[status].image = function()
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
206 return { status_code = 200, headers = { content_type = "image/png" },
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
207 body = require_resource("status_"..status..".png")
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
208 };
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
209 end;
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
210 statuses[status].html = function()
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
211 local jid_hash = sha1(jid, true);
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
212 return { status_code = 200, headers = { content_type = "text/html" },
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
213 body = [[<!DOCTYPE html>]]..
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
214 tostring(
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
215 stanza("html")
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
216 :tag("head")
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
217 :tag("title"):text("XMPP Status Page for "..jid):up():up()
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
218 :tag("body")
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
219 :tag("div", { id = jid_hash.."_status", class = "xmpp_status" })
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
220 :tag("img", { id = jid_hash.."_img", class = "xmpp_status_image xmpp_status_"..status,
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
221 src = "data:image/png;base64,"..b64(require_resource("status_"..status..".png")) }):up()
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
222 :tag("span", { id = jid_hash.."_status_name", class = "xmpp_status_name" })
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
223 :text("\194\160"..status):up()
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
224 :tag("span", { id = jid_hash.."_status_message", class = "xmpp_status_message" })
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
225 :text(message and "\194\160"..message.."" or "")
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
226 )
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
227 };
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
228 end;
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
229 statuses[status].text = function()
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
230 return { status_code = 200, headers = { content_type = "text/plain" },
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
231 body = status
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
232 };
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
233 end;
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
234 statuses[status].message = function()
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
235 return { status_code = 200, headers = { content_type = "text/plain" },
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
236 body = (message and message or "")
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
237 };
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
238 end;
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
239 statuses[status].json = function()
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
240 return { status_code = 200, headers = { content_type = "application/json" },
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
241 body = json({
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
242 jid = jid,
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
243 show = status,
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
244 status = (message and message or "null")
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
245 })
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
246 };
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
247 end;
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
248 statuses[status].xml = function()
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
249 return { status_code = 200, headers = { content_type = "application/xml" },
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
250 body = [[<?xml version="1.0" encoding="utf-8"?>]]..
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
251 tostring(
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
252 stanza("result")
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
253 :tag("jid"):text(jid):up()
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
254 :tag("show"):text(status):up()
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
255 :tag("status"):text(message)
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
256 )
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
257 };
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
258 end
1495
9a1b3f0d0939 mod_webpresence: fixed render-type handling (thanks to biszkopcik and Zash)
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1054
diff changeset
259
9a1b3f0d0939 mod_webpresence: fixed render-type handling (thanks to biszkopcik and Zash)
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1054
diff changeset
260 if ((type == "") or (not statuses[status][type])) then
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
261 type = "image"
1495
9a1b3f0d0939 mod_webpresence: fixed render-type handling (thanks to biszkopcik and Zash)
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1054
diff changeset
262 end;
9a1b3f0d0939 mod_webpresence: fixed render-type handling (thanks to biszkopcik and Zash)
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1054
diff changeset
263
1054
432dc4056114 mod_webpresence: added /xml and /json; Rewritten /html; A bit of refactoring;
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 847
diff changeset
264 return statuses[status][type]();
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266
643
28b0a8cd950a mod_webpresence: Update to timber, remove squish hack (icons are now loaded relative to the module file)
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
267 module:provides("http", {
5588
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
268 default_path = "/status";
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
269 route = {
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
270 ["GET /*"] = handle_request;
f16720087ef2 mod_webpresence: Add opt-in mechanism.
Trần H. Trung <work@trung.fun>
parents: 2348
diff changeset
271 };
643
28b0a8cd950a mod_webpresence: Update to timber, remove squish hack (icons are now loaded relative to the module file)
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
272 });