annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
5 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
6 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
7 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
8 if f then
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
9 return f:read("*a");
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 end
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
11 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
12 return "";
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
15 local statuses = { online = {}, away = {}, xa = {}, dnd = {}, chat = {}, offline = {} };
4
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 for status, _ in pairs(statuses) do
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
18 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
19 body = require_resource("status_"..status..".png") };
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
20 statuses[status].text = { status_code = 200, headers = { content_type = "plain/text" },
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
21 body = status };
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)
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
25 local status;
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
26 local jid, type = path:match("([^/]+)/?(.*)$");
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if jid then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local user, host = jid_split(jid);
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 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
30 user, host = host, event.request.headers.host;
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if host then host = host:gsub(":%d+$", ""); end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if user and host then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local user_sessions = hosts[host] and hosts[host].sessions[user];
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 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
36 status = user_sessions.top_resources[1];
4
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if status and status.presence then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 status = status.presence:child_with_name("show");
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if not status then
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 status = "online";
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 else
63080b8973ee mod_webpresence: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 status = status:get_text();
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 end
779
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
48
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
49 status = status or "offline";
36044b77b6c2 mod_webpresence: Added possibility to get status as text
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 643
diff changeset
50 return (type and type == "text") and statuses[status].text or statuses[status].image;
4
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
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
53 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
54 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
55 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
56 ["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
57 };
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
58 });