Software /
code /
prosody-modules
Comparison
mod_auto_moved/mod_auto_moved.lua @ 4679:f95a1e197a07
mod_auto_moved: New module implementing XEP-0283 r0.2.0
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 12 Sep 2021 18:49:56 +0100 |
comparison
equal
deleted
inserted
replaced
4678:0bcbff950f14 | 4679:f95a1e197a07 |
---|---|
1 local id = require "util.id"; | |
2 local jid = require "util.jid"; | |
3 local promise = require "util.promise"; | |
4 local rm = require "core.rostermanager"; | |
5 local st = require "util.stanza"; | |
6 | |
7 local errors = require "util.error".init(module.name, { | |
8 ["statement-not-found"] = { type = "cancel", condition = "item-not-found" }; | |
9 ["statement-mismatch"] = { type = "cancel", condition = "conlict" }; | |
10 }); | |
11 | |
12 module:hook("presence/bare", function (event) | |
13 local origin, stanza = event.origin, event.stanza; | |
14 if stanza.attr.type ~= "subscribe" then | |
15 return; -- We're only interested in subscription requests | |
16 end | |
17 local moved = stanza:get_child("moved", "urn:xmpp:moved:1"); | |
18 if not moved then | |
19 return; -- We're only interested in stanzas with a moved notification | |
20 end | |
21 | |
22 local verification = stanza:get_child("moved-verification", "https://prosody.im/protocol/moved"); | |
23 if verification then | |
24 return; -- We already attempted to verify this stanza | |
25 end | |
26 | |
27 module:log("debug", "Received moved notification from %s", stanza.attr.from); | |
28 | |
29 local old_jid = moved:get_child_text("old-jid"); | |
30 if not old_jid then | |
31 return; -- Failed to read old JID | |
32 end | |
33 | |
34 local to_user = jid.node(stanza.attr.to); | |
35 local new_jid_unverified = jid.bare(stanza.attr.from); | |
36 | |
37 if not rm.is_contact_subscribed(to_user, module.host, old_jid) then | |
38 return; -- Old JID was not an existing contact, ignore | |
39 end | |
40 | |
41 if rm.is_contact_pending_in(to_user, module.host, new_jid_unverified) | |
42 or rm.is_contact_subscribed(to_user, module.host, new_jid_unverified) then | |
43 return; -- New JID already subscribed or pending, ignore | |
44 end | |
45 | |
46 local moved_statement_query = st.iq({ to = old_jid, type = "get", id = id.short() }) | |
47 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) | |
48 :tag("items", { node = "urn:xmpp:moved:1" }) | |
49 :tag("item", { id = "current" }):up() | |
50 :up() | |
51 :up(); | |
52 -- TODO: Catch and handle <gone/> errors per note in XEP-0283. | |
53 module:send_iq(moved_statement_query):next(function (reply) | |
54 module:log("debug", "Statement reply: %s", reply.stanza); | |
55 local moved_statement = reply.stanza:find("{http://jabber.org/protocol/pubsub}pubsub/items/{http://jabber.org/protocol/pubsub}item/{urn:xmpp:moved:1}moved"); | |
56 if not moved_statement then | |
57 return promise.reject(errors.new("statement-not-found")); -- No statement found | |
58 end | |
59 | |
60 local new_jid = jid.prep(moved_statement:get_child_text("new-jid")); | |
61 if new_jid ~= new_jid_unverified then | |
62 return promise.reject(errors.new("statement-mismatch")); -- Verification failed; JIDs do not match | |
63 end | |
64 | |
65 -- Verified! | |
66 module:log("info", "Verified moved notification <%s> -> <%s>", old_jid, new_jid); | |
67 | |
68 -- Add incoming subscription and respond | |
69 rm.set_contact_pending_in(to_user, module.host, new_jid); | |
70 rm.subscribed(to_user, module.host, new_jid); | |
71 module:send(st.presence({ to = new_jid, from = to_user.."@"..module.host, type = "subscribed" })); | |
72 rm.roster_push(to_user, module.host, new_jid); | |
73 | |
74 -- Request outgoing subscription if old JID had one | |
75 if rm.is_user_subscribed(to_user, module.host, old_jid) then | |
76 module:log("debug", "Requesting subscription to new JID"); | |
77 rm.set_contact_pending_out(to_user, module.host, new_jid); | |
78 module:send(st.presence({ to = new_jid, from = to_user.."@"..module.host, type = "subscribe" })); | |
79 end | |
80 end):catch(function (err) | |
81 module:log("debug", "Failed to verify moved statement for <%s> -> <%s>: %s", old_jid, new_jid_unverified, require "util.serialization".serialize(err, "debug")); | |
82 stanza:reset() | |
83 :tag("moved-verification", { xmlns = "https://prosody.im/protocol/moved", status = "failed" }) | |
84 :up(); | |
85 module:send(stanza, origin); | |
86 end); | |
87 | |
88 -- Halt processing of the stanza, for now | |
89 return true; | |
90 end, 1); |