Changeset

457:73d4eb93657b

Update to use util.id for random ids instead of counters (thanks Zash)
author Matthew Wild <mwild1@gmail.com>
date Thu, 16 Mar 2023 11:41:52 +0000
parents 456:6a65142052c8
children 458:6c3797c0bb44
files client.lua libs/adhoc.lib.lua plugins/archive.lua plugins/bind.lua plugins/jingle.lua plugins/jingle_ibb.lua plugins/jingle_s5b.lua plugins/legacy.lua plugins/ping.lua plugins/proxy65.lua squishy
diffstat 11 files changed, 33 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/client.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/client.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -4,11 +4,21 @@
 local jid_split = require "util.jid".split;
 local adns = require "net.adns";
 local st = require "util.stanza";
+local new_id = require "util.id".short;
+
+math.randomseed((require"socket".gettime() * 1000000) % 0x80000000);
 
 -- Shortcuts to save having to load util.stanza
 verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply =
 	st.message, st.presence, st.iq, st.stanza, st.reply, st.error_reply;
 
+function verse.iq(attr)
+	if not attr.id then
+		attr.id = new_id();
+	end
+	return st.iq(attr);
+end
+
 local new_xmpp_stream = require "util.xmppstream".new;
 
 local xmlns_stream = "http://etherx.jabber.org/streams";
@@ -208,13 +218,8 @@
 end
 
 function stream:send_iq(iq, callback)
-	local id = self:new_id();
+	local id = iq.attr.id or uuid.generate();
 	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
--- a/libs/adhoc.lib.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/libs/adhoc.lib.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -4,7 +4,7 @@
 -- COPYING file in the source package for more information.
 --
 
-local st, uuid = require "util.stanza", require "util.uuid";
+local st, new_id = require "util.stanza", require "util.id".short;
 
 local xmlns_cmd = "http://jabber.org/protocol/commands";
 
@@ -25,7 +25,7 @@
 end
 
 function _M.handle_cmd(command, origin, stanza)
-	local sessionid = stanza.tags[1].attr.sessionid or uuid.generate();
+	local sessionid = stanza.tags[1].attr.sessionid or new_id();
 	local dataIn = {};
 	dataIn.to = stanza.attr.to;
 	dataIn.from = stanza.attr.from;
--- a/plugins/archive.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/plugins/archive.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -7,7 +7,7 @@
 local xmlns_mam = "urn:xmpp:mam:2"
 local xmlns_forward = "urn:xmpp:forward:0";
 local xmlns_delay = "urn:xmpp:delay";
-local uuid = require "util.uuid".generate;
+local new_id = require "util.id".short;
 local parse_datetime = require "util.datetime".parse;
 local datetime = require "util.datetime".datetime;
 local dataform = require"util.dataforms".new;
@@ -23,8 +23,8 @@
 
 function verse.plugins.archive(stream)
 	function stream:query_archive(where, query_params, callback)
-		local queryid = uuid();
-		local query_st = st.iq{ type="set", to = where }
+		local queryid = new_id();
+		local query_st = st.iq{ id = queryid, type="set", to = where }
 			:tag("query", { xmlns = xmlns_mam, queryid = queryid });
 
 
--- a/plugins/bind.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/plugins/bind.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -7,7 +7,7 @@
 	local function handle_features(features)
 		if stream.bound then return; end
 		stream:debug("Binding resource...");
-		stream:send_iq(verse.iq({ type = "set" }):tag("bind", {xmlns=xmlns_bind}):tag("resource"):text(stream.resource),
+		stream:send_iq(verse.iq({ id="bind",type = "set" }):tag("bind", {xmlns=xmlns_bind}):tag("resource"):text(stream.resource),
 			function (reply)
 				if reply.attr.type == "result" then
 					local result_jid = reply
--- a/plugins/jingle.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/plugins/jingle.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -1,6 +1,6 @@
 local verse = require "verse";
 local timer = require "util.timer";
-local uuid_generate = require "util.uuid".generate;
+local new_id = require "util.id".short;
 
 local xmlns_jingle = "urn:xmpp:jingle:1";
 local xmlns_jingle_errors = "urn:xmpp:jingle:errors:1";
@@ -20,7 +20,7 @@
 		return verse.eventable(setmetatable(base or {
 			role = "initiator";
 			peer = to;
-			sid = uuid_generate();
+			sid = new_id();
 			stream = stream;
 		}, jingle_mt));
 	end
--- a/plugins/jingle_ibb.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/plugins/jingle_ibb.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -1,6 +1,6 @@
 local verse = require "verse";
 local base64 = require "util.encodings".base64;
-local uuid_generate = require "util.uuid".generate;
+local new_id = require "util.id".short;
 
 local xmlns_jingle_ibb = "urn:xmpp:jingle:transports:ibb:1";
 local xmlns_ibb = "http://jabber.org/protocol/ibb";
@@ -159,7 +159,7 @@
 	end
 	function ibb:generate_initiate()
 		print("ibb:generate_initiate() as ".. self.role);
-		local sid = uuid_generate();
+		local sid = new_id();
 		self.sid = sid;
 		self.stanza = 'iq';
 		self.block = 2048;
--- a/plugins/jingle_s5b.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/plugins/jingle_s5b.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -3,7 +3,7 @@
 local xmlns_s5b = "urn:xmpp:jingle:transports:s5b:1";
 local xmlns_bytestreams = "http://jabber.org/protocol/bytestreams";
 local sha1 = require "util.hashes".sha1;
-local uuid_generate = require "util.uuid".generate;
+local new_id = require "util.id".short;
 
 local function negotiate_socks5(conn, hash)
 	local function suppress_connected()
@@ -92,7 +92,7 @@
 	local s5b = {};
 
 	function s5b:generate_initiate()
-		self.s5b_sid = uuid_generate();
+		self.s5b_sid = new_id();
 		local transport = verse.stanza("transport", { xmlns = xmlns_s5b,
 			mode = "tcp", sid = self.s5b_sid });
 		local p = 0;
--- a/plugins/legacy.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/plugins/legacy.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -1,5 +1,5 @@
 local verse = require "verse";
-local uuid = require "util.uuid".generate;
+local new_id = require "util.id".short;
 
 local xmlns_auth = "jabber:iq:auth";
 
@@ -15,7 +15,7 @@
 		local auth_data = {
 			username = stream.username;
 			password = stream.password;
-			resource = stream.resource or uuid();
+			resource = stream.resource or new_id();
 			digest = false, sequence = false, token = false;
 		};
 		local request = verse.iq({ to = stream.host, type = "set" })
--- a/plugins/ping.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/plugins/ping.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -1,12 +1,15 @@
 local verse = require "verse";
 local gettime = require"socket".gettime;
+local new_id = require "util.id".short;
 
 local xmlns_ping = "urn:xmpp:ping";
 
 function verse.plugins.ping(stream)
 	function stream:ping(jid, callback)
 		local t = gettime();
-		stream:send_iq(verse.iq{ to = jid, type = "get" }:tag("ping", { xmlns = xmlns_ping }),
+		local id = new_id();
+		local ping = verse.iq{ id = id, to = jid, type = "get" }:tag("ping", { xmlns = xmlns_ping });
+		stream:send_iq(ping,
 			function (reply)
 				if reply.attr.type == "error" then
 					local type, condition, text = reply:get_error();
@@ -17,6 +20,7 @@
 				end
 				callback(gettime()-t, jid);
 			end);
+		return id;
 	end
 	stream:hook("iq/"..xmlns_ping, function(stanza)
 		return stream:send(verse.reply(stanza));
--- a/plugins/proxy65.lua	Fri Mar 03 12:10:54 2023 +0000
+++ b/plugins/proxy65.lua	Thu Mar 16 11:41:52 2023 +0000
@@ -1,5 +1,5 @@
 local verse =	require "verse";
-local uuid = require "util.uuid";
+local new_id = require "util.id".short;
 local sha1 = require "util.hashes".sha1;
 
 local proxy65_mt = {};
@@ -92,7 +92,7 @@
 function proxy65_mt:new(target_jid, proxies)
 	local conn = verse.new(nil, {
 		target_jid = target_jid;
-		bytestream_sid = uuid.generate();
+		bytestream_sid = new_id();
 	});
 
 	local request = verse.iq{type="set", to = target_jid}
--- a/squishy	Fri Mar 03 12:10:54 2023 +0000
+++ b/squishy	Thu Mar 16 11:41:52 2023 +0000
@@ -20,6 +20,7 @@
 Module "util.uuid"		"util/uuid.lua"
 Module "net.dns"               "net/dns.lua"
 Module "net.adns"              "net/adns.lua"
+Module "util.id" "util/id.lua"
 Module "net.server"		"net/server_select.lua"
 Module "util.xmppstream"	"util/xmppstream.lua"
 Module "util.jid"		"util/jid.lua"