From 2c3d4a17579063a4b889beef5f22fdd6cc341eea Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Wed, 8 Apr 2015 17:23:18 +0200 Subject: [PATCH] MT#12267 add mod_carbons and sync mod_smacks - while at it upgrade debian description and copyright files Change-Id: I966f23fb7108b3d21cb25d99ff089f927ef2bc4f (cherry picked from commit 844707127ad37b3fc6d80839767f3faf882ea5ac) --- debian/control | 26 ++++---- debian/copyright | 21 +++++- plugins/mod_carbons.lua | 137 ++++++++++++++++++++++++++++++++++++++++ plugins/mod_smacks.lua | 10 +-- 4 files changed, 178 insertions(+), 16 deletions(-) create mode 100644 plugins/mod_carbons.lua diff --git a/debian/control b/debian/control index 123f00d..526a240 100644 --- a/debian/control +++ b/debian/control @@ -25,17 +25,21 @@ Description: ngcp modules for the prosody Jabber/XMPP server . The following modules are available: . - * mod_auth_sql.lua: simple SQL Authentication module - * mod_sipwise_vcard_cusax.lua: return a vcard containing SIP URIs - for phone/video of the requested user - * mod_sipwise_vhosts_sql.lua: load vhosts from DB on startup of - Prosody XMPP server - * mod_sipwise_vjud.lua: implement Jabber Search with implicit number - normalization using the NGCP rewrite rules - * mod_sipwise_groups.lua: inserts PBX groups to the user contact list - * mod_websocket.lua: XMPP over websocket, (c) 2012 Florian Zeitz * mod_sipwise_admin_telnet: admin_telnet with inteface selection - * mod_sipwise_redis_sessions: keep server's session info on redis + * mod_sipwise_groups.lua: inserts PBX groups to the user contact list + * mod_sipwise_lastactivity: XEP-0012 * mod_sipwise_pushd: push notification to mobile users + * mod_sipwise_redis_sessions: keep server's session info on redis + * mod_sipwise_vcard_cusax.lua: return a vcard containing SIP URIs for + phone/video of the requested user + * mod_sipwise_vhosts_sql.lua: load vhosts from DB on startup of Prosody + XMPP server + * mod_sipwise_vjud.lua: implement Jabber Search with implicit number + normalization using the NGCP rewrite rules + . + The following modules are from prosody-modules: + . + * mod_auth_sql.lua: simple SQL Authentication module + * mod_carbons: XEP-0280: Message Carbons * mod_log_auth: logs IP address in a failed authentication attempt - * mod_sipwise_lastactivity: XEP-0012 + * mod_websocket.lua: XMPP over websocket diff --git a/debian/copyright b/debian/copyright index b99af6b..6814a33 100644 --- a/debian/copyright +++ b/debian/copyright @@ -11,10 +11,29 @@ License: MIT/X11 Files: plugins/mod_sipwise_vcard_cusax.lua Copyright: 2008-2010 Matthew Wild - 2008-2010 Waqas Hussain + 2008-2010 Waqas Hussain 2013 Sipwise GmbH License: MIT/X11 +Files: plugins/mod_carbons.lua +Copyright: 2011 Kim Alvefur +License: MIT/X11 + +Files: plugins/mod_sipwise_lastactivity.lua +Copyright: 2008-2010 Matthew Wild + 2008-2010 Waqas Hussain + 2015 Sipwise GmbH +License: MIT/X11 + +Files: plugins/mod_log_auth.lua +Copyright: 2013 Kim Alvefur +License: MIT/X11 + +Files: plugins/mod_smacks.lua +Copyright: 2015 Matthew Wild + 2015 Kim Alvefur +Licence: MIT/X11 + Files: * Copyright: 2013 Sipwise GmbH License: MIT/X11 diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua new file mode 100644 index 0000000..6d70826 --- /dev/null +++ b/plugins/mod_carbons.lua @@ -0,0 +1,137 @@ +-- XEP-0280: Message Carbons implementation for Prosody +-- Copyright (C) 2011 Kim Alvefur +-- +-- This file is MIT/X11 licensed. + +local st = require "util.stanza"; +local jid_bare = require "util.jid".bare; +local xmlns_carbons = "urn:xmpp:carbons:2"; +local xmlns_carbons_old = "urn:xmpp:carbons:1"; +local xmlns_carbons_really_old = "urn:xmpp:carbons:0"; +local xmlns_forward = "urn:xmpp:forward:0"; +local full_sessions, bare_sessions = full_sessions, bare_sessions; + +local function toggle_carbons(event) + local origin, stanza = event.origin, event.stanza; + local state = stanza.tags[1].attr.mode or stanza.tags[1].name; + module:log("debug", "%s %sd carbons", origin.full_jid, state); + origin.want_carbons = state == "enable" and stanza.tags[1].attr.xmlns; + return origin.send(st.reply(stanza)); +end +module:hook("iq-set/self/"..xmlns_carbons..":disable", toggle_carbons); +module:hook("iq-set/self/"..xmlns_carbons..":enable", toggle_carbons); + +-- COMPAT +module:hook("iq-set/self/"..xmlns_carbons_old..":disable", toggle_carbons); +module:hook("iq-set/self/"..xmlns_carbons_old..":enable", toggle_carbons); +module:hook("iq-set/self/"..xmlns_carbons_really_old..":carbons", toggle_carbons); + +local function message_handler(event, c2s) + local origin, stanza = event.origin, event.stanza; + local orig_type = stanza.attr.type; + local orig_from = stanza.attr.from; + local orig_to = stanza.attr.to; + + if not (orig_type == nil + or orig_type == "normal" + or orig_type == "chat") then + return -- No carbons for messages of type error or headline + end + + -- Stanza sent by a local client + local bare_jid = jid_bare(orig_from); + local target_session = origin; + local top_priority = false; + local user_sessions = bare_sessions[bare_jid]; + + -- Stanza about to be delivered to a local client + if not c2s then + bare_jid = jid_bare(orig_to); + target_session = full_sessions[orig_to]; + user_sessions = bare_sessions[bare_jid]; + if not target_session and user_sessions then + -- The top resources will already receive this message per normal routing rules, + -- so we are going to skip them in order to avoid sending duplicated messages. + local top_resources = user_sessions.top_resources; + top_priority = top_resources and top_resources[1].priority + end + end + + if not user_sessions then + module:log("debug", "Skip carbons for offline user"); + return -- No use in sending carbons to an offline user + end + + if stanza:get_child("private", xmlns_carbons) then + if not c2s then + stanza:maptags(function(tag) + if not ( tag.attr.xmlns == xmlns_carbons and tag.name == "private" ) then + return tag; + end + end); + end + module:log("debug", "Message tagged private, ignoring"); + return + elseif stanza:get_child("no-copy", "urn:xmpp:hints") then + module:log("debug", "Message has no-copy hint, ignoring"); + return + elseif stanza:get_child("x", "http://jabber.org/protocol/muc#user") then + module:log("debug", "MUC PM, ignoring"); + return + end + + -- Create the carbon copy and wrap it as per the Stanza Forwarding XEP + local copy = st.clone(stanza); + copy.attr.xmlns = "jabber:client"; + local carbon = st.message{ from = bare_jid, type = orig_type, } + :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }) + :tag("forwarded", { xmlns = xmlns_forward }) + :add_child(copy):reset(); + + -- COMPAT + local carbon_old = st.message{ from = bare_jid, type = orig_type, } + :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons_old }):up() + :tag("forwarded", { xmlns = xmlns_forward }) + :add_child(copy):reset(); + + -- COMPAT + local carbon_really_old = st.clone(stanza) + :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons_really_old }):up() + + user_sessions = user_sessions and user_sessions.sessions; + for _, session in pairs(user_sessions) do + -- Carbons are sent to resources that have enabled it + if session.want_carbons + -- but not the resource that sent the message, or the one that it's directed to + and session ~= target_session + -- and isn't among the top resources that would receive the message per standard routing rules + and (c2s or session.priority ~= top_priority) + -- don't send v0 carbons (or copies) for c2s + and (not c2s or session.want_carbons ~= xmlns_carbons_really_old) then + carbon.attr.to = session.full_jid; + module:log("debug", "Sending carbon to %s", session.full_jid); + local carbon = session.want_carbons == xmlns_carbons_old and carbon_old -- COMPAT + or session.want_carbons == xmlns_carbons_really_old and carbon_really_old -- COMPAT + or carbon; + session.send(carbon); + end + end +end + +local function c2s_message_handler(event) + return message_handler(event, true) +end + +-- Stanzas sent by local clients +module:hook("pre-message/host", c2s_message_handler, 1); +module:hook("pre-message/bare", c2s_message_handler, 1); +module:hook("pre-message/full", c2s_message_handler, 1); +-- Stanzas to local clients +module:hook("message/bare", message_handler, 1); +module:hook("message/full", message_handler, 1); + +module:add_feature(xmlns_carbons); +module:add_feature(xmlns_carbons_old); +if module:get_option_boolean("carbons_v0") then + module:add_feature(xmlns_carbons_really_old); +end diff --git a/plugins/mod_smacks.lua b/plugins/mod_smacks.lua index 18e2ded..3c5daa7 100644 --- a/plugins/mod_smacks.lua +++ b/plugins/mod_smacks.lua @@ -58,13 +58,13 @@ module:hook("s2s-stream-features", end); local function outgoing_stanza_filter(stanza, session) - local is_stanza = stanza.attr and not stanza.attr.xmlns; + local is_stanza = stanza.attr and not stanza.attr.xmlns and not stanza.name:find":"; if is_stanza and not stanza._cached then -- Stanza in default stream namespace local queue = session.outgoing_stanza_queue; local cached_stanza = st.clone(stanza); cached_stanza._cached = true; - if cached_stanza and cached_stanza:get_child("delay", xmlns_delay) == nil then + if cached_stanza and cached_stanza.name ~= "iq" and cached_stanza:get_child("delay", xmlns_delay) == nil then cached_stanza = cached_stanza:tag("delay", { xmlns = xmlns_delay, from = session.host, stamp = datetime.datetime()}); end @@ -266,7 +266,7 @@ module:hook("pre-resource-unbind", function (event) -- (for example, the client may have bound a new resource and -- started a new smacks session, or not be using smacks) local curr_session = full_sessions[session.full_jid]; - if false and session.destroyed then + if session.destroyed then session.log("debug", "The session has already been destroyed"); elseif curr_session and curr_session.resumption_token == resumption_token -- Check the hibernate time still matches what we think it is, @@ -315,7 +315,9 @@ function handle_resume(session, stanza, xmlns_sm) original_session.ip = session.ip; original_session.conn = session.conn; original_session.send = session.send; - original_session.send.filter = original_session.filter; + original_session.filter = session.filter; + original_session.send.filter = session.filter; + original_session.data.filter = session.filter; original_session.stream = session.stream; original_session.secure = session.secure; original_session.hibernating = nil;