Merge 0.10->trunk
authorKim Alvefur <zash@zash.se>
Sun, 28 Sep 2014 01:56:22 +0200
changeset 6445 0f4025abbe8f
parent 6435 388786af0dd2 (current diff)
parent 6444 bcb1ea9047d3 (diff)
child 6446 f4403c270ea2
Merge 0.10->trunk
net/server_select.lua
plugins/mod_pubsub/mod_pubsub.lua
util/pubsub.lua
--- a/net/server_select.lua	Sat Sep 27 19:00:09 2014 +0200
+++ b/net/server_select.lua	Sun Sep 28 01:56:22 2014 +0200
@@ -569,6 +569,9 @@
 						_ = status and status( handler, "ssl-handshake-complete" )
 						if self.autostart_ssl and listeners.onconnect then
 							listeners.onconnect(self);
+							if bufferqueuelen ~= 0 then
+								_sendlistlen = addsocket(_sendlist, client, _sendlistlen)
+							end
 						end
 						_readlistlen = addsocket(_readlist, client, _readlistlen)
 						return true
--- a/plugins/mod_pubsub/mod_pubsub.lua	Sat Sep 27 19:00:09 2014 +0200
+++ b/plugins/mod_pubsub/mod_pubsub.lua	Sun Sep 28 01:56:22 2014 +0200
@@ -64,6 +64,8 @@
 	get_items = { "retrieve-items" };
 	add_subscription = { "subscribe" };
 	get_subscriptions = { "retrieve-subscriptions" };
+	set_configure = { "config-node" };
+	get_default = { "retrieve-default" };
 };
 
 local function add_disco_features_from_service(service)
@@ -195,6 +197,7 @@
 				retract = true;
 				delete = true;
 				get_nodes = true;
+				configure = true;
 
 				subscribe = true;
 				unsubscribe = true;
@@ -215,6 +218,19 @@
 			};
 		};
 
+		node_config_form = require"util.dataforms".new {
+			{
+				type = "hidden";
+				name = "FORM_TYPE";
+				value = "http://jabber.org/protocol/pubsub#node_config";
+			};
+			{
+				type = "text-single";
+				name = "pubsub#max_items";
+				label = "Max # of items to persist";
+			};
+		};
+
 		autocreate_on_publish = autocreate_on_publish;
 		autocreate_on_subscribe = autocreate_on_subscribe;
 
--- a/plugins/mod_pubsub/pubsub.lib.lua	Sat Sep 27 19:00:09 2014 +0200
+++ b/plugins/mod_pubsub/pubsub.lib.lua	Sun Sep 28 01:56:22 2014 +0200
@@ -3,6 +3,7 @@
 
 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
+local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
 
 local _M = {};
 
@@ -17,6 +18,7 @@
 	["item-not-found"] = { "cancel", "item-not-found" };
 	["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" };
 	["forbidden"] = { "cancel", "forbidden" };
+	["not-allowed"] = { "cancel", "not-allowed" };
 };
 local function pubsub_error_reply(stanza, error)
 	local e = pubsub_errors[error];
@@ -222,4 +224,59 @@
 	return origin.send(reply);
 end
 
+function handlers.get_configure(origin, stanza, config, service)
+	local node = config.attr.node;
+	if not node then
+		return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
+	end
+
+	if not service:may(node, actor, "configure") then
+		return origin.send(pubsub_error_reply(stanza, "forbidden"));
+	end
+
+	local node_obj = service.nodes[node];
+	if not node_obj then
+		return origin.send(pubsub_error_reply(stanza, "item-not-found"));
+	end
+
+	local form = self.config.node_config_form;
+	if not form then
+		return origin.send(pubsub_error_reply(stanza, "not-allowed"));
+	end
+
+	local reply = st.reply(stanza)
+		:tag("pubsub", { xmlns = xmlns_pubsub_owner })
+			:tag("configure", { node = node })
+				:add_child(form:form(node_obj.config));
+	return origin.send(reply);
+end
+
+function handlers.set_configure(origin, stanza, config, service)
+	local node = config.attr.node;
+	if not node then
+		return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
+	end
+	local form, node_obj = service:get_node_config_form(node, stanza.attr.from);
+	if not form then
+		return origin.send(pubsub_error_reply(stanza, node_obj));
+	end
+	local new_config, err = form:data(config.tags[1]);
+	if not new_config then
+		return origin.send(st.error_reply(stanza, "modify", "bad-request", err));
+	end
+	local ok, err = service:set_node_config(node, stanza.attr.from, new_config);
+	if not ok then
+		return origin.send(pubsub_error_reply(stanza, err));
+	end
+	return origin.send(st.reply(stanza));
+end
+
+function handlers.get_default(origin, stanza, default, service)
+	local reply = st.reply(stanza)
+		:tag("pubsub", { xmlns = xmlns_pubsub_owner })
+			:tag("configure", { node = node })
+				:add_child(form:form(service.node_default_config));
+	return origin.send(reply);
+end
+
 return _M;
--- a/util/pubsub.lua	Sat Sep 27 19:00:09 2014 +0200
+++ b/util/pubsub.lua	Sun Sep 28 01:56:22 2014 +0200
@@ -6,16 +6,20 @@
 local service = {};
 local service_mt = { __index = service };
 
-local default_config = {
+local default_config = { __index = {
 	broadcaster = function () end;
 	get_affiliation = function () end;
 	capabilities = {};
-};
+} };
+local default_node_config = { __index = {
+	["pubsub#max_items"] = "20";
+} };
 
 function new(config)
 	config = config or {};
 	return setmetatable({
-		config = setmetatable(config, { __index = default_config });
+		config = setmetatable(config, default_config);
+		node_defaults = setmetatable(config.node_defaults or {}, default_node_config);
 		affiliations = {};
 		subscriptions = {};
 		nodes = {};
@@ -204,7 +208,7 @@
 	return true, node_obj.subscribers[jid];
 end
 
-function service:create(node, actor)
+function service:create(node, actor, options)
 	-- Access checking
 	if not self:may(node, actor, "create") then
 		return false, "forbidden";
@@ -218,7 +222,7 @@
 	self.nodes[node] = {
 		name = node;
 		subscribers = {};
-		config = {};
+		config = setmetatable(options or {}, {__index=self.node_defaults});
 		affiliations = {};
 	};
 	setmetatable(self.nodes[node], { __index = { data = self.data[node] } }); -- COMPAT
@@ -259,6 +263,14 @@
 	end
 end
 
+local function trim_items(data, max)
+	max = tonumber(max);
+	if not max or #data <= max then return end
+	repeat
+		data[t_remove(data, 1)] = nil;
+	until #data <= max
+end
+
 function service:publish(node, actor, id, item)
 	-- Access checking
 	if not self:may(node, actor, "publish") then
@@ -278,8 +290,9 @@
 	end
 	local node_data = self.data[node];
 	remove_item_by_id(node_data, id);
-	node_data[#self.data[node] + 1] = id;
+	node_data[#node_data + 1] = id;
 	node_data[id] = item;
+	trim_items(node_data, node_obj.config["pubsub#max_items"]);
 	self.events.fire_event("item-published", { node = node, actor = actor, id = id, item = item });
 	self.config.broadcaster("items", node, node_obj.subscribers, item);
 	return true;
@@ -411,4 +424,22 @@
 	return true;
 end
 
+function service:set_node_config(node, actor, new_config)
+	if not self:may(node, actor, "configure") then
+		return false, "forbidden";
+	end
+
+	local node_obj = self.nodes[node];
+	if not node_obj then
+		return false, "item-not-found";
+	end
+
+	for k,v in pairs(new_config) do
+		node_obj.config[k] = v;
+	end
+	trim_items(self.data[node], node_obj.config["pubsub#max_items"]);
+
+	return true;
+end
+
 return _M;