Mercurial > prosody-modules
comparison mod_admin_web/admin_web/mod_admin_web.lua @ 927:a9dfa7232d88
Merge
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 12 Mar 2013 12:10:25 +0000 |
parents | e3ad5f3aa6d4 |
children | d643c385d3f6 |
comparison
equal
deleted
inserted
replaced
926:f88381a39c56 | 927:a9dfa7232d88 |
---|---|
20 local st = require "util.stanza"; | 20 local st = require "util.stanza"; |
21 local uuid_generate = require "util.uuid".generate; | 21 local uuid_generate = require "util.uuid".generate; |
22 local is_admin = require "core.usermanager".is_admin; | 22 local is_admin = require "core.usermanager".is_admin; |
23 local pubsub = require "util.pubsub"; | 23 local pubsub = require "util.pubsub"; |
24 local jid_bare = require "util.jid".bare; | 24 local jid_bare = require "util.jid".bare; |
25 local lfs = require "lfs"; | |
26 local open = io.open; | |
27 local stat = lfs.attributes; | |
28 | 25 |
29 module:set_global(); | 26 module:set_global(); |
30 | 27 |
31 local service = {}; | 28 local service = {}; |
32 | |
33 local http_base = module.path:gsub("/[^/]+$","") .. "/www_files/"; | |
34 | 29 |
35 local xmlns_adminsub = "http://prosody.im/adminsub"; | 30 local xmlns_adminsub = "http://prosody.im/adminsub"; |
36 local xmlns_c2s_session = "http://prosody.im/streams/c2s"; | 31 local xmlns_c2s_session = "http://prosody.im/streams/c2s"; |
37 local xmlns_s2s_session = "http://prosody.im/streams/s2s"; | 32 local xmlns_s2s_session = "http://prosody.im/streams/s2s"; |
38 | |
39 local mime_map = { | |
40 html = "text/html"; | |
41 xml = "text/xml"; | |
42 js = "text/javascript"; | |
43 css = "text/css"; | |
44 }; | |
45 | 33 |
46 local idmap = {}; | 34 local idmap = {}; |
47 | 35 |
48 function add_client(session, host) | 36 function add_client(session, host) |
49 local name = session.full_jid; | 37 local name = session.full_jid; |
102 local notifier = st.stanza("retract", { id = id }); | 90 local notifier = st.stanza("retract", { id = id }); |
103 service[host]:retract(xmlns_s2s_session, host, id, notifier); | 91 service[host]:retract(xmlns_s2s_session, host, id, notifier); |
104 end | 92 end |
105 end | 93 end |
106 | 94 |
107 function serve_file(event, path) | |
108 local full_path = http_base .. path; | |
109 | |
110 if stat(full_path, "mode") == "directory" then | |
111 if stat(full_path.."/index.html", "mode") == "file" then | |
112 return serve_file(event, path.."/index.html"); | |
113 end | |
114 return 403; | |
115 end | |
116 | |
117 local f, err = open(full_path, "rb"); | |
118 if not f then | |
119 return 404; | |
120 end | |
121 | |
122 local data = f:read("*a"); | |
123 f:close(); | |
124 if not data then | |
125 return 403; | |
126 end | |
127 | |
128 local ext = path:match("%.([^.]*)$"); | |
129 event.response.headers.content_type = mime_map[ext]; -- Content-Type should be nil when not known | |
130 return data; | |
131 end | |
132 | |
133 function module.add_host(module) | 95 function module.add_host(module) |
134 -- Dependencies | 96 -- Dependencies |
135 module:depends("bosh"); | 97 module:depends("bosh"); |
136 module:depends("admin_adhoc"); | 98 module:depends("admin_adhoc"); |
137 module:depends("http"); | 99 module:depends("http"); |
100 local serve_file = module:depends("http_files").serve { | |
101 path = module:get_directory() .. "/www_files"; | |
102 }; | |
138 | 103 |
139 -- Setup HTTP server | 104 -- Setup HTTP server |
140 module:provides("http", { | 105 module:provides("http", { |
141 name = "admin"; | 106 name = "admin"; |
142 route = { | 107 route = { |
147 ["GET /*"] = serve_file; | 112 ["GET /*"] = serve_file; |
148 } | 113 } |
149 }); | 114 }); |
150 | 115 |
151 -- Setup adminsub service | 116 -- Setup adminsub service |
152 local function simple_broadcast(node, jids, item) | 117 local function simple_broadcast(kind, node, jids, item) |
153 item = st.clone(item); | 118 if item then |
154 item.attr.xmlns = nil; -- Clear the pubsub namespace | 119 item = st.clone(item); |
120 item.attr.xmlns = nil; -- Clear the pubsub namespace | |
121 end | |
155 local message = st.message({ from = module.host, type = "headline" }) | 122 local message = st.message({ from = module.host, type = "headline" }) |
156 :tag("event", { xmlns = xmlns_adminsub .. "#event" }) | 123 :tag("event", { xmlns = xmlns_adminsub .. "#event" }) |
157 :tag("items", { node = node }) | 124 :tag(kind, { node = node }) |
158 :add_child(item); | 125 :add_child(item); |
159 for jid in pairs(jids) do | 126 for jid in pairs(jids) do |
160 module:log("debug", "Sending notification to %s", jid); | 127 module:log("debug", "Sending notification to %s", jid); |
161 message.attr.to = jid; | 128 message.attr.to = jid; |
162 module:send(message); | 129 module:send(message); |