Annotate

mod_webpresence/mod_webpresence.in.lua @ 635:30be50d2537f

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