comparison mod_webpresence/mod_webpresence.lua @ 782:2d83708ea901

mod_webpresence: fixed text notation, added html, added status message output
author Vadim Misbakh-Soloviov <mva@mva.name>
date Mon, 06 Aug 2012 17:08:12 +0700
parents 36044b77b6c2
children 1c9a3454eb43
comparison
equal deleted inserted replaced
781:ab40d1650688 782:2d83708ea901
1 module:depends("http"); 1 module:depends("http");
2 2
3 local jid_split = require "util.jid".prepped_split; 3 local jid_split = require "util.jid".prepped_split;
4 local b64 = require "util.encodings".base64.encode;
5 local sha1 = require "util.hashes".sha1;
4 6
5 local function require_resource(name) 7 local function require_resource(name)
6 local icon_path = module:get_option_string("presence_icons", "icons"); 8 local icon_path = module:get_option_string("presence_icons", "icons");
7 local f, err = module:load_resource(icon_path.."/"..name); 9 local f, err = module:load_resource(icon_path.."/"..name);
8 if f then 10 if f then
11 module:log("warn", "Failed to open image file %s", icon_path..name); 13 module:log("warn", "Failed to open image file %s", icon_path..name);
12 return ""; 14 return "";
13 end 15 end
14 16
15 local statuses = { online = {}, away = {}, xa = {}, dnd = {}, chat = {}, offline = {} }; 17 local statuses = { online = {}, away = {}, xa = {}, dnd = {}, chat = {}, offline = {} };
16 18 --[[for status, _ in pairs(statuses) do
17 for status, _ in pairs(statuses) do 19 statuses[status].image = { status_code = 200, headers = { content_type = "image/png" },
18 statuses[status].image = { status_code = 200, headers = { content_type = "image/png" },
19 body = require_resource("status_"..status..".png") }; 20 body = require_resource("status_"..status..".png") };
20 statuses[status].text = { status_code = 200, headers = { content_type = "plain/text" }, 21 statuses[status].text = { status_code = 200, headers = { content_type = "text/plain" },
21 body = status }; 22 body = status };
22 end 23 end]]
23 24
24 local function handle_request(event, path) 25 local function handle_request(event, path)
25 local status; 26 local status, message;
26 local jid, type = path:match("([^/]+)/?(.*)$"); 27 local jid, type = path:match("([^/]+)/?(.*)$");
27 if jid then 28 if jid then
28 local user, host = jid_split(jid); 29 local user, host = jid_split(jid);
29 if host and not user then 30 if host and not user then
30 user, host = host, event.request.headers.host; 31 user, host = host, event.request.headers.host;
33 if user and host then 34 if user and host then
34 local user_sessions = hosts[host] and hosts[host].sessions[user]; 35 local user_sessions = hosts[host] and hosts[host].sessions[user];
35 if user_sessions then 36 if user_sessions then
36 status = user_sessions.top_resources[1]; 37 status = user_sessions.top_resources[1];
37 if status and status.presence then 38 if status and status.presence then
39 message = status.presence:child_with_name("status");
38 status = status.presence:child_with_name("show"); 40 status = status.presence:child_with_name("show");
39 if not status then 41 if not status then
40 status = "online"; 42 status = "online";
41 else 43 else
42 status = status:get_text(); 44 status = status:get_text();
43 end 45 end
46 if message then
47 message = message:get_text();
48 end
44 end 49 end
45 end 50 end
46 end 51 end
47 end 52 end
48
49 status = status or "offline"; 53 status = status or "offline";
50 return (type and type == "text") and statuses[status].text or statuses[status].image; 54 if type == "" then type = "image" end;
55 if type == "image" then
56 statuses[status].image = { status_code = 200, headers = { content_type = "image/png" },
57 body = require_resource("status_"..status..".png") };
58 elseif type == "html" then
59 statuses[status].html = { status_code = 200, headers = { content_type = "text/html" },
60 body = [[<div id="]]..sha1(jid,true)..[[_status" class="xmpp_status">]]..
61 [[<img id="]]..sha1(jid,true)..[[_img" class="xmpp_status_image" ]]..
62 [[src="data:image/png;base64,]]..
63 b64(require_resource("status_"..status..".png"))..[[">]]..
64 [[<span id="]]..sha1(jid,true)..[[_name" ]]..
65 [[class="xmpp_status_name">]]..status..[[</span>]]..
66 (message and [[<span id="]]..sha1(jid,true)..[[_message" ]]..
67 [[class="xmpp_status_message">]]..message..[[</span>]] or "")..
68 [[</div>]] };
69 elseif type == "text" then
70 statuses[status].text = { status_code = 200, headers = { content_type = "text/plain" },
71 body = status };
72 elseif type == "message" then
73 statuses[status].message = { status_code = 200, headers = { content_type = "text/plain" },
74 body = (message and message or "") };
75 end
76 return statuses[status][type];
51 end 77 end
52 78
53 module:provides("http", { 79 module:provides("http", {
54 default_path = "/status"; 80 default_path = "/status";
55 route = { 81 route = {