comparison mod_webpresence/mod_webpresence.lua @ 779:36044b77b6c2

mod_webpresence: Added possibility to get status as text
author Vadim Misbakh-Soloviov <mva@mva.name>
date Sun, 05 Aug 2012 09:07:48 +0700
parents 28b0a8cd950a
children 2d83708ea901
comparison
equal deleted inserted replaced
778:56dd7c6420ee 779:36044b77b6c2
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 4
5 if not require_resource then 5 local function require_resource(name)
6 function require_resource(name) 6 local icon_path = module:get_option_string("presence_icons", "icons");
7 local icon_path = module:get_option_string("presence_icons", "icons"); 7 local f, err = module:load_resource(icon_path.."/"..name);
8 local f, err = module:load_resource(icon_path.."/"..name); 8 if f then
9 if f then 9 return f:read("*a");
10 return f:read("*a");
11 end
12 module:log("warn", "Failed to open image file %s", icon_path..name);
13 return "";
14 end 10 end
11 module:log("warn", "Failed to open image file %s", icon_path..name);
12 return "";
15 end 13 end
16 14
17 local statuses = { "online", "away", "xa", "dnd", "chat", "offline" }; 15 local statuses = { online = {}, away = {}, xa = {}, dnd = {}, chat = {}, offline = {} };
18 16
19 for _, status in ipairs(statuses) do 17 for status, _ in pairs(statuses) do
20 statuses[status] = { status_code = 200, headers = { content_type = "image/png" }, 18 statuses[status].image = { status_code = 200, headers = { content_type = "image/png" },
21 body = require_resource("status_"..status..".png") }; 19 body = require_resource("status_"..status..".png") };
20 statuses[status].text = { status_code = 200, headers = { content_type = "plain/text" },
21 body = status };
22 end 22 end
23 23
24 local function handle_request(event, path) 24 local function handle_request(event, path)
25 local jid = path:match("[^/]+$"); 25 local status;
26 local jid, type = path:match("([^/]+)/?(.*)$");
26 if jid then 27 if jid then
27 local user, host = jid_split(jid); 28 local user, host = jid_split(jid);
28 if host and not user then 29 if host and not user then
29 user, host = host, event.request.headers.host; 30 user, host = host, event.request.headers.host;
30 if host then host = host:gsub(":%d+$", ""); end 31 if host then host = host:gsub(":%d+$", ""); end
31 end 32 end
32 if user and host then 33 if user and host then
33 local user_sessions = hosts[host] and hosts[host].sessions[user]; 34 local user_sessions = hosts[host] and hosts[host].sessions[user];
34 if user_sessions then 35 if user_sessions then
35 local status = user_sessions.top_resources[1]; 36 status = user_sessions.top_resources[1];
36 if status and status.presence then 37 if status and status.presence then
37 status = status.presence:child_with_name("show"); 38 status = status.presence:child_with_name("show");
38 if not status then 39 if not status then
39 status = "online"; 40 status = "online";
40 else 41 else
41 status = status:get_text(); 42 status = status:get_text();
42 end 43 end
43 return statuses[status];
44 end 44 end
45 end 45 end
46 end 46 end
47 end 47 end
48 return statuses.offline; 48
49 status = status or "offline";
50 return (type and type == "text") and statuses[status].text or statuses[status].image;
49 end 51 end
50 52
51 module:provides("http", { 53 module:provides("http", {
52 default_path = "/status"; 54 default_path = "/status";
53 route = { 55 route = {