annotate mod_carbons/mod_carbons.lua @ 832:9087431d35f6

mod_carbons: Add comments and rename some variables to make it clearer
author Kim Alvefur <zash@zash.se>
date Tue, 18 Sep 2012 18:58:13 +0200
parents 9927d88a1b2a
children 30d49c26d219
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
521
6d0d2673f95e mod_carbons: Add MIT license statement.
Kim Alvefur <zash@zash.se>
parents: 520
diff changeset
1 -- XEP-0280: Message Carbons implementation for Prosody
6d0d2673f95e mod_carbons: Add MIT license statement.
Kim Alvefur <zash@zash.se>
parents: 520
diff changeset
2 -- Copyright (C) 2011 Kim Alvefur
6d0d2673f95e mod_carbons: Add MIT license statement.
Kim Alvefur <zash@zash.se>
parents: 520
diff changeset
3 --
6d0d2673f95e mod_carbons: Add MIT license statement.
Kim Alvefur <zash@zash.se>
parents: 520
diff changeset
4 -- This file is MIT/X11 licensed.
6d0d2673f95e mod_carbons: Add MIT license statement.
Kim Alvefur <zash@zash.se>
parents: 520
diff changeset
5
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local st = require "util.stanza";
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local jid_bare = require "util.jid".bare;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local jid_split = require "util.jid".split;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local xmlns_carbons = "urn:xmpp:carbons:1";
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local xmlns_forward = "urn:xmpp:forward:0";
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local host_sessions = hosts[module.host].sessions;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
804
9927d88a1b2a mod_carbons: Merge enable and disable handlers
Kim Alvefur <zash@zash.se>
parents: 743
diff changeset
13 local function toggle_carbons(event)
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local origin, stanza = event.origin, event.stanza;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 if stanza.attr.type == "set" then
804
9927d88a1b2a mod_carbons: Merge enable and disable handlers
Kim Alvefur <zash@zash.se>
parents: 743
diff changeset
16 local state = stanza.tags[1].name;
9927d88a1b2a mod_carbons: Merge enable and disable handlers
Kim Alvefur <zash@zash.se>
parents: 743
diff changeset
17 module:log("debug", "%s %sd carbons", origin.full_jid, state);
9927d88a1b2a mod_carbons: Merge enable and disable handlers
Kim Alvefur <zash@zash.se>
parents: 743
diff changeset
18 origin.want_carbons = state == "enable";
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 origin.send(st.reply(stanza));
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 return true
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 end
804
9927d88a1b2a mod_carbons: Merge enable and disable handlers
Kim Alvefur <zash@zash.se>
parents: 743
diff changeset
22 end
9927d88a1b2a mod_carbons: Merge enable and disable handlers
Kim Alvefur <zash@zash.se>
parents: 743
diff changeset
23 module:hook("iq/self/"..xmlns_carbons..":disable", toggle_carbons);
9927d88a1b2a mod_carbons: Merge enable and disable handlers
Kim Alvefur <zash@zash.se>
parents: 743
diff changeset
24 module:hook("iq/self/"..xmlns_carbons..":enable", toggle_carbons);
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
26 local function message_handler(event, c2s)
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local origin, stanza = event.origin, event.stanza;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local orig_type = stanza.attr.type;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local orig_to = stanza.attr.to;
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
30 local orig_from = stanza.attr.from;
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 if not (orig_type == nil
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 or orig_type == "normal"
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 or orig_type == "chat") then
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
35 return -- No carbons for messages of type error or headline
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local bare_jid, user_sessions;
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
39 local no_carbon_to = {};
617
3ca933c9d8ff mod_carbons: Make sure parameters to log() are strings.
Kim Alvefur <zash@zash.se>
parents: 606
diff changeset
40 module:log("debug", "Message from %s to %s", tostring(orig_from), tostring(orig_to));
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
41 if c2s then -- Stanza sent by a local client
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 bare_jid = (origin.username.."@"..origin.host)
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 user_sessions = host_sessions[origin.username];
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
44 else -- Stanza about to be delivered to a local client
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
45 local username, hostname, resource = jid_split(orig_to);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
46 bare_jid = jid_bare(orig_to);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
47 user_sessions = host_sessions[username];
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
48 if resource then
605
5175f6d33470 mod_carbons: Add more debug logging
Kim Alvefur <zash@zash.se>
parents: 585
diff changeset
49 module:log("debug", "Message was to resource %s, it will not get carbon", resource);
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
50 no_carbon_to[resource] = true;
585
ce2798a1bc56 mod_carbons: Don't try to send carbons for entirely offline users.
Kim Alvefur <zash@zash.se>
parents: 545
diff changeset
51 elseif user_sessions then
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
52 local top_resources = user_sessions.top_resources;
585
ce2798a1bc56 mod_carbons: Don't try to send carbons for entirely offline users.
Kim Alvefur <zash@zash.se>
parents: 545
diff changeset
53 if top_resources then
832
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
54 -- These will already receive this message per normal routing rules,
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
55 -- so we skip them to avoid duplicated messages.
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
56 for i=1,#top_resources do
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
57 local resource = top_resources[i].resource;
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
58 module:log("debug", "Not sending carbons to top resource %s", resource);
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
59 no_carbon_to[resource] = true;
585
ce2798a1bc56 mod_carbons: Don't try to send carbons for entirely offline users.
Kim Alvefur <zash@zash.se>
parents: 545
diff changeset
60 end
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
61 end
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
62 end
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
585
ce2798a1bc56 mod_carbons: Don't try to send carbons for entirely offline users.
Kim Alvefur <zash@zash.se>
parents: 545
diff changeset
65 if not user_sessions then
605
5175f6d33470 mod_carbons: Add more debug logging
Kim Alvefur <zash@zash.se>
parents: 585
diff changeset
66 module:log("debug", "Skip carbons for offline user");
585
ce2798a1bc56 mod_carbons: Don't try to send carbons for entirely offline users.
Kim Alvefur <zash@zash.se>
parents: 545
diff changeset
67 return -- No use in sending carbons to an offline user
ce2798a1bc56 mod_carbons: Don't try to send carbons for entirely offline users.
Kim Alvefur <zash@zash.se>
parents: 545
diff changeset
68 end
ce2798a1bc56 mod_carbons: Don't try to send carbons for entirely offline users.
Kim Alvefur <zash@zash.se>
parents: 545
diff changeset
69
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
70 if not c2s and stanza:get_child("private", xmlns_carbons) then
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
71 stanza:maptags(function(tag)
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
72 return tag.attr.xmlns == xmlns_carbons
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
73 and tag.name == "private" and tag or nil;
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
74 end);
605
5175f6d33470 mod_carbons: Add more debug logging
Kim Alvefur <zash@zash.se>
parents: 585
diff changeset
75 module:log("debug", "Message tagged private, ignoring");
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
76 return
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
77 end
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
78
832
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
79 -- Create the carbon copy and wrap it as per the Stanza Forwarding XEP
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
80 local copy = st.clone(stanza);
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
81 copy.attr.xmlns = "jabber:client";
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
82 local carbon = st.message{ from = bare_jid, type = orig_type, }
664
2f11d2473afd mod_carbons: Move creation of the carbon stanza out of the loop.
Kim Alvefur <zash@zash.se>
parents: 618
diff changeset
83 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }):up()
2f11d2473afd mod_carbons: Move creation of the carbon stanza out of the loop.
Kim Alvefur <zash@zash.se>
parents: 618
diff changeset
84 :tag("forwarded", { xmlns = xmlns_forward })
832
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
85 :add_child(copy):reset();
664
2f11d2473afd mod_carbons: Move creation of the carbon stanza out of the loop.
Kim Alvefur <zash@zash.se>
parents: 618
diff changeset
86
832
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
87 -- And finally, send the carbon to the sessions that should have it.
618
267548522810 mod_carbons: Remove useless protection against loop that can't happen
Kim Alvefur <zash@zash.se>
parents: 617
diff changeset
88 user_sessions = user_sessions and user_sessions.sessions;
267548522810 mod_carbons: Remove useless protection against loop that can't happen
Kim Alvefur <zash@zash.se>
parents: 617
diff changeset
89 for resource, session in pairs(user_sessions) do
267548522810 mod_carbons: Remove useless protection against loop that can't happen
Kim Alvefur <zash@zash.se>
parents: 617
diff changeset
90 local full_jid = bare_jid .. "/" .. resource;
664
2f11d2473afd mod_carbons: Move creation of the carbon stanza out of the loop.
Kim Alvefur <zash@zash.se>
parents: 618
diff changeset
91 if session.want_carbons and ((c2s and session ~= origin) or (not c2s and not no_carbon_to[resource])) then
832
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
92 carbon.attr.to = full_jid;
664
2f11d2473afd mod_carbons: Move creation of the carbon stanza out of the loop.
Kim Alvefur <zash@zash.se>
parents: 618
diff changeset
93 module:log("debug", "Sending carbon to %s", full_jid);
832
9087431d35f6 mod_carbons: Add comments and rename some variables to make it clearer
Kim Alvefur <zash@zash.se>
parents: 804
diff changeset
94 session.send(carbon);
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
99 local function c2s_message_handler(event)
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
100 return message_handler(event, true)
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 -- Stanzas sent by local clients
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 module:hook("pre-message/bare", c2s_message_handler, 1);
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 module:hook("pre-message/full", c2s_message_handler, 1);
510
59e80326f2b3 mod_carbons: Fix a typo and unindent a line.
Kim Alvefur <zash@zash.se>
parents: 480
diff changeset
106 -- Stanzas to local clients
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
107 module:hook("message/bare", message_handler, 1);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
108 module:hook("message/full", message_handler, 1);
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 module:add_feature(xmlns_carbons);