mod_webpresence/mod_webpresence.lua
author Matthew Wild <mwild1@gmail.com>
Sat, 28 Apr 2012 19:44:42 +0100
changeset 643 28b0a8cd950a
parent 4 mod_webpresence/mod_webpresence.in.lua@63080b8973ee
child 779 36044b77b6c2
permissions -rw-r--r--
mod_webpresence: Update to timber, remove squish hack (icons are now loaded relative to the module file)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
     1
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
     2
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local jid_split = require "util.jid".prepped_split;
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
if not require_resource then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
	function require_resource(name)
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
     7
		local icon_path = module:get_option_string("presence_icons", "icons");
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
     8
		local f, err  = module:load_resource(icon_path.."/"..name);
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
		if f then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
			return f:read("*a");
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
		end
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
    12
		module:log("warn", "Failed to open image file %s", icon_path..name);
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
		return "";
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
	end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
local statuses = { "online", "away", "xa", "dnd", "chat", "offline" };
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
for _, status in ipairs(statuses) do
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
    20
	statuses[status] = { status_code = 200, headers = { content_type = "image/png" }, 
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
    21
		body = require_resource("status_"..status..".png") };
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
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
    24
local function handle_request(event, path)
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
    25
	local jid = path:match("[^/]+$");
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
	if jid then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
		local user, host = jid_split(jid);
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
		if host and not user then
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
    29
			user, host = host, event.request.headers.host;
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
			if host then host = host:gsub(":%d+$", ""); end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
		end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
		if user and host then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
			local user_sessions = hosts[host] and hosts[host].sessions[user];
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
			if user_sessions then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
				local status = user_sessions.top_resources[1];
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
				if status and status.presence then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
					status = status.presence:child_with_name("show");
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
					if not status then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
						status = "online";
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
					else
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
						status = status:get_text();
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
					end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
					return statuses[status];
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
	end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
	return statuses.offline;
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
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
    51
module:provides("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
    52
	default_path = "/status";
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
    53
	route = {
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
    54
		["GET /*"] = handle_request;
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
    55
	};
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
    56
});