mod_webpresence/mod_webpresence.lua
author Vadim Misbakh-Soloviov <mva@mva.name>
Mon, 06 Aug 2012 17:08:12 +0700
changeset 782 2d83708ea901
parent 779 36044b77b6c2
child 847 1c9a3454eb43
permissions -rw-r--r--
mod_webpresence: fixed text notation, added html, added status message output
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;
782
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
     4
local b64 = require "util.encodings".base64.encode;
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
     5
local sha1 = require "util.hashes".sha1;
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
     7
local function require_resource(name)
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
     8
	local icon_path = module:get_option_string("presence_icons", "icons");
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
     9
	local f, err  = module:load_resource(icon_path.."/"..name);
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
    10
	if f then
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
    11
		return f:read("*a");
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
	end
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
    13
	module:log("warn", "Failed to open image file %s", icon_path..name);
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
    14
	return "";
4
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
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
    17
local statuses = { online = {}, away = {}, xa = {}, dnd = {}, chat = {}, offline = {} };
782
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    18
--[[for status, _ in pairs(statuses) do
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    19
	statuses[status].image = { status_code = 200, headers = { content_type = "image/png" },
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
		body = require_resource("status_"..status..".png") };
782
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    21
	statuses[status].text = { status_code = 200, headers = { content_type = "text/plain" },
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
    22
		body = status };
782
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    23
end]]
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
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
    25
local function handle_request(event, path)
782
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    26
	local status, message;
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
    27
	local jid, type = path:match("([^/]+)/?(.*)$");
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
	if jid then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
		local user, host = jid_split(jid);
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
		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
    31
			user, host = host, event.request.headers.host;
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
			if host then host = host:gsub(":%d+$", ""); end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
		end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
		if user and host then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
			local user_sessions = hosts[host] and hosts[host].sessions[user];
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
			if user_sessions then
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
    37
				status = user_sessions.top_resources[1];
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
				if status and status.presence then
782
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    39
					message = status.presence:child_with_name("status");
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
					status = status.presence:child_with_name("show");
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
					if not status then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
						status = "online";
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
					else
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
						status = status:get_text();
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
					end
782
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    46
					if message then
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    47
						message = message:get_text();
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    48
					end
4
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
			end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
		end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
	end
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
    53
	status = status or "offline";
782
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    54
	if type == "" then type = "image" end;
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    55
	if type == "image" then 
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    56
		statuses[status].image = { status_code = 200, headers = { content_type = "image/png" }, 
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    57
			body = require_resource("status_"..status..".png") };
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    58
	elseif type == "html" then
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    59
		statuses[status].html = { status_code = 200, headers = { content_type = "text/html" },
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    60
			body = [[<div id="]]..sha1(jid,true)..[[_status" class="xmpp_status">]]..
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    61
					[[<img id="]]..sha1(jid,true)..[[_img" class="xmpp_status_image" ]]..
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    62
						[[src="data:image/png;base64,]]..
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    63
						b64(require_resource("status_"..status..".png"))..[[">]]..
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    64
					[[<span id="]]..sha1(jid,true)..[[_name" ]]..
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    65
						[[class="xmpp_status_name">]]..status..[[</span>]]..
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    66
					(message and [[<span id="]]..sha1(jid,true)..[[_message" ]]..
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    67
						[[class="xmpp_status_message">]]..message..[[</span>]] or "")..
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    68
				[[</div>]] };
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    69
	elseif type == "text" then
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    70
		statuses[status].text = { status_code = 200, headers = { content_type = "text/plain" },
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    71
			body = status };
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    72
	elseif type == "message" then
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    73
		statuses[status].message = { status_code = 200, headers = { content_type = "text/plain" },
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    74
			body = (message and message or "") };
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    75
	end
2d83708ea901 mod_webpresence: fixed text notation, added html, added status message output
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 779
diff changeset
    76
	return statuses[status][type];
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
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
    79
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
    80
	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
    81
	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
    82
		["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
    83
	};
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
    84
});