Changeset

232:0ca669b18ef3

merge
author shinysky<shinysky1986(AT)gmail.com>
date Mon, 02 Aug 2010 22:06:19 +0800
parents 230:0b9e8721b9c2 (current diff) 231:b78009874ce5 (diff)
children 233:4ff8068b4d94
files
diffstat 3 files changed, 149 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua	Thu Jul 29 17:39:28 2010 +0800
+++ b/mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua	Mon Aug 02 22:06:19 2010 +0800
@@ -108,6 +108,7 @@
 	{ name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
 	{ name = "max_items", type = "list-single", label = "Maximum number of users",
 		value = { "25", "50", "75", "100", "150", "200", "all" } };
+	{ name = "details", type = "boolean", label = "Show details" };
 };
 
 local get_online_users_result_layout = dataforms_new{
@@ -365,22 +366,36 @@
 			return { status = "canceled" };
 		end
 
-		local fields = add_user_layout:data(data.form);
-		
+		local fields = get_online_users_layout:data(data.form);
+
 		local max_items = nil
 		if fields.max_items ~= "all" then
 			max_items = tonumber(fields.max_items);
 		end
 		local count = 0;
-		local users = nil;
+		local users = {};
 		for username, user in pairs(hosts[data.to].sessions or {}) do
 			if (max_items ~= nil) and (count >= max_items) then
 				break;
 			end
-			users = ((users and users.."\n") or "")..(username.."@"..data.to);
+			users[#users+1] = username.."@"..data.to;
 			count = count + 1;
+			if fields.details then
+				for resource, session in pairs(user.sessions or {}) do
+					local status, priority = "unavailable", tostring(session.priority or "-");
+					if session.presence then
+						status = session.presence:child_with_name("show");
+						if status then
+							status = status:get_text() or "[invalid!]";
+						else
+							status = "available";
+						end
+					end
+					users[#users+1] = " - "..resource..": "..status.."("..priority..")";
+				end
+			end
 		end
-		return { status = "completed", result = {layout = get_online_users_result_layout, data = {onlineuserjids=users}} };
+		return { status = "completed", result = {layout = get_online_users_result_layout, data = {onlineuserjids=t_concat(users, "\n")}} };
 	else
 		return { status = "executing", form = get_online_users_layout }, "executing";
 	end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_couchdb/couchdb/mod_couchdb.lua	Mon Aug 02 22:06:19 2010 +0800
@@ -0,0 +1,79 @@
+
+local http = require "socket.http";
+local url = require "socket.url";
+
+local couchapi = module:require("couchdb/couchapi");
+local json = module:require("couchdb/json");
+
+local couchdb_url = assert(module:get_option("couchdb_url"), "Option couchdb_url not specified");
+local db = couchapi.db(couchdb_url);
+
+local function couchdb_get(key)
+	local a,b = db:doc(key):get()
+	print(json.encode(a));
+	if b == 404 then return nil; end
+	if b == 200 then b = nil; end
+	return a.payload,b;
+end
+
+local function couchdb_put(key, value)
+	local a,b = db:doc(key):get();
+	return db:doc(key):put({ payload = value, _rev = a and a._rev });
+end
+
+local st = require "util.stanza";
+
+local handlers = {};
+
+handlers.accounts = {
+	get = function(self, user)
+		return couchdb_get(self.host.."/"..user.."/account");
+	end;
+	set = function(self, user, data)
+		return couchdb_put(self.host.."/"..user.."/account", data);
+	end;
+};
+handlers.vcard = {
+	get = function(self, user)
+		return couchdb_get(self.host.."/"..user.."/vcard");
+	end;
+	set = function(self, user, data)
+		return couchdb_put(self.host.."/"..user.."/vcard", data);
+	end;
+};
+handlers.private = {
+	get = function(self, user)
+		return couchdb_get(self.host.."/"..user.."/private");
+	end;
+	set = function(self, user, data)
+		return couchdb_put(self.host.."/"..user.."/private", data);
+	end;
+};
+handlers.roster = {
+	get = function(self, user)
+		return couchdb_get(self.host.."/"..user.."/roster");
+	end;
+	set = function(self, user, data)
+		return couchdb_put(self.host.."/"..user.."/roster", data);
+	end;
+};
+
+-----------------------------
+local driver = {};
+
+function driver:open(datastore, typ)
+	local handler = handlers[datastore];
+	if not handler then return nil; end
+	local host = module.host;
+	--local cache_key = host.." "..datastore;
+	--if self.ds_cache[cache_key] then return self.ds_cache[cache_key]; end
+	local instance = setmetatable({ host = host, datastore = datastore }, { __index = handler });
+	--for key,val in pairs(handler) do
+	--	instance[key] = val;
+	--end
+	--if instance.init then instance:init(); end
+	--self.ds_cache[cache_key] = instance;
+	return instance;
+end
+
+module:add_item("data-driver", driver);
--- a/mod_post_msg/mod_post_msg.lua	Thu Jul 29 17:39:28 2010 +0800
+++ b/mod_post_msg/mod_post_msg.lua	Mon Aug 02 22:06:19 2010 +0800
@@ -13,6 +13,16 @@
 local test_password = require "core.usermanager".test_password;
 local b64_decode = require "util.encodings".base64.decode;
 local urldecode = require "net.http".urldecode;
+local urlparams = --require "net.http".getQueryParams or whatever MattJ names it
+function(s)
+	if not s:match("=") then return urldecode(s); end
+	local r = {}
+	s:gsub("([^=&]*)=([^&]*)", function(k,v)
+		r[ urldecode(k) ] = urldecode(v);
+		return nil
+	end)
+	return r
+end;
 
 local function http_response(code, message, extra_headers)
 	local response = {
@@ -23,7 +33,9 @@
 end
 
 local function handle_request(method, body, request)
-	if request.method ~= "POST" then return http_response(405, "Method Not Allowed"); end
+	if request.method == "BREW" then return http_response(418, "I'm a teapot"); end
+	if request.method ~= "POST" then
+		return http_response(405, "Method Not Allowed", {["Allow"] = "POST"}); end
 
 	-- message to?
 	local path_jid = request.url.path:match("[^/]+$");
@@ -43,7 +55,7 @@
 			{["WWW-Authenticate"]='Basic realm="WallyWorld"'})
 	end
 	local from_jid, password = b64_decode(request.headers.authorization
-			:gmatch("[^ ]*$")() or ""):gmatch("([^:]*):(.*)")();
+			:match("[^ ]*$") or ""):match("([^:]*):(.*)");
 	from_jid = jid_prep(from_jid)
 	if not from_jid or not password then return http_response(400, "Bad Request"); end
 	local from_user, from_host = jid_split(from_jid)
@@ -52,11 +64,44 @@
 	-- auth
 	module:log("debug", "testing authz %s", from_jid)
 	if not test_password(from_user, from_host, password) then
-		return http_response(401, "Unauthorized"); end
+		return http_response(401, "Unauthorized")
+	end
+
+	-- parse body
+	local message = {}
+	local body_type = request.headers["content-type"]
+	if body_type == "text/plain" then
+		message = {["body"] = body}
+	elseif body_type == "application/x-www-form-urlencoded" then
+		message = urlparams(body)
+		if type(message) == "string" then
+			message = {["body"] = message}
+		end
+	else
+		return http_response(415, "Unsupported Media Type")
+	end
 
+	-- guess type if not set
+	if not message["type"] then
+		if message["body"] then 
+			if message["subject"] then
+				message["type"] = "normal"
+			else
+				message["type"] = "chat"
+			end
+		elseif not message["body"] and message["subject"] then
+			message["type"] = "headline"
+		end
+	end
+
+	-- build stanza
+	local stanza = msg({["to"]=to_jid, ["from"]=from_jid, ["type"]=message["type"]})
+	if message["body"] then stanza:tag("body"):text(message["body"]):up(); end
+	if message["subject"] then stanza:tag("subject"):text(message["subject"]):up(); end
+
+	-- and finaly post it
 	module:log("debug", "message for %s", to_jid)
-	core_post_stanza(hosts[module.host], msg({to=to_jid, from=from_jid, type="chat"})
-			:tag("body"):text(body))
+	core_post_stanza(hosts[module.host], stanza)
 	return http_response(202, "Accepted")
 end