Diff

component.lua @ 485:c9a144591649

component: Avoid adding to the global stream metatable This allows component and client connections to be made side-by-side. Previous to this change, loading this connection module would break the ability to make client connections, due to overriding stream methods such as :reopen() and :reset(). A next step would be to share the methods that the two connection modules have in common.
author Matthew Wild <mwild1@gmail.com>
date Thu, 23 Mar 2023 18:56:32 +0000
parent 411:db462d4feb44
child 490:6b2f31da9610
line wrap: on
line diff
--- a/component.lua	Thu Mar 23 18:54:13 2023 +0000
+++ b/component.lua	Thu Mar 23 18:56:32 2023 +0000
@@ -1,8 +1,7 @@
 local verse = require "verse";
-local stream = verse.stream_mt;
+local stream_mt = verse.stream_mt;
 
 local jid_split = require "util.jid".split;
-local lxp = require "lxp";
 local st = require "util.stanza";
 local sha1 = require "util.hashes".sha1;
 
@@ -42,20 +41,48 @@
 	return stream:event("stanza", stanza);
 end
 
-function stream:reset()
-	if self.stream then
-		self.stream:reset();
-	else
-		self.stream = new_xmpp_stream(self, stream_callbacks);
-	end
-	self.notopen = true;
-	return true;
-end
-
-function stream:connect_component(jid, pass)
+function stream_mt:connect_component(jid, pass)
 	self.jid, self.password = jid, pass;
 	self.username, self.host, self.resource = jid_split(jid);
 
+	-- Component stream methods
+	function self:reset()
+		if self.stream then
+			self.stream:reset();
+		else
+			self.stream = new_xmpp_stream(self, stream_callbacks);
+		end
+		self.notopen = true;
+		return true;
+	end
+
+	function self:reopen()
+		self:reset();
+		self:send(st.stanza("stream:stream", { to = self.jid, ["xmlns:stream"]='http://etherx.jabber.org/streams',
+			xmlns = xmlns_component, version = "1.0" }):top_tag());
+	end
+
+	function self:close(reason)
+		if not self.notopen then
+			self:send("</stream:stream>");
+		end
+		local on_disconnect = self.conn.disconnect();
+		self.conn:close();
+		on_disconnect(conn, reason);
+	end
+
+	function self:send_iq(iq, callback)
+		local id = self:new_id();
+		self.tracked_iqs[id] = callback;
+		iq.attr.id = id;
+		self:send(iq);
+	end
+
+	function self:new_id()
+		self.curr_id = self.curr_id + 1;
+		return tostring(self.curr_id);
+	end
+
 	function self.data(conn, data)
 		local ok, err = self.stream:feed(data);
 		if ok then return; end
@@ -121,29 +148,3 @@
 	self:reopen();
 end
 
-function stream:reopen()
-	self:reset();
-	self:send(st.stanza("stream:stream", { to = self.jid, ["xmlns:stream"]='http://etherx.jabber.org/streams',
-		xmlns = xmlns_component, version = "1.0" }):top_tag());
-end
-
-function stream:close(reason)
-	if not self.notopen then
-		self:send("</stream:stream>");
-	end
-	local on_disconnect = self.conn.disconnect();
-	self.conn:close();
-	on_disconnect(conn, reason);
-end
-
-function stream:send_iq(iq, callback)
-	local id = self:new_id();
-	self.tracked_iqs[id] = callback;
-	iq.attr.id = id;
-	self:send(iq);
-end
-
-function stream:new_id()
-	self.curr_id = self.curr_id + 1;
-	return tostring(self.curr_id);
-end