comparison mod_webpresence/mod_webpresence.in.lua @ 4:63080b8973ee

mod_webpresence: Initial commit
author Matthew Wild <mwild1@gmail.com>
date Fri, 25 Sep 2009 01:43:14 +0100
parents
children
comparison
equal deleted inserted replaced
3:723fd785815f 4:63080b8973ee
1 local jid_split = require "util.jid".prepped_split;
2
3 if not require_resource then
4 function require_resource(name)
5 local f = io.open((config.get("*", "core", "presence_icons") or "")..name);
6 if f then
7 return f:read("*a");
8 end
9 module:log("warn", "Failed to open image file %s", (config.get("*", "core", "presence_icons") or "")..name);
10 return "";
11 end
12 end
13
14 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" };
15
16 local statuses = { "online", "away", "xa", "dnd", "chat", "offline" };
17
18 for _, status in ipairs(statuses) do
19 statuses[status] = { status = "200 OK", headers = { ["Content-Type"] = "image/png" },
20 body = require_resource("icons/status_"..status..".png") };
21 end
22
23 local function handle_request(method, body, request)
24 local jid = request.url.path:match("[^/]+$");
25 if jid then
26 local user, host = jid_split(jid);
27 if host and not user then
28 user, host = host, request.headers.host;
29 if host then host = host:gsub(":%d+$", ""); end
30 end
31 if user and host then
32 local user_sessions = hosts[host] and hosts[host].sessions[user];
33 if user_sessions then
34 local status = user_sessions.top_resources[1];
35 if status and status.presence then
36 status = status.presence:child_with_name("show");
37 if not status then
38 status = "online";
39 else
40 status = status:get_text();
41 end
42 return statuses[status];
43 end
44 end
45 end
46 end
47 return statuses.offline;
48 end
49
50 local ports = config.get(module.host, "core", "http_ports") or { 5280 };
51 require "net.httpserver".new_from_config(ports, "status", handle_request);