annotate mod_carbons/mod_carbons.lua @ 491:5b3db688213d

mod_ircd: Fixed nick change logic (thanks mva), so that the self nick-change "flag" is removed properly, improved the logic to use verse's room_mt:change_nick (thanks Zash) yet to be pushed into main, added squished verse with the meta method included.
author Marco Cirillo <maranda@lightwitch.org>
date Fri, 02 Dec 2011 20:53:09 +0000
parents 0cef5be669de
children 59e80326f2b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require "util.stanza";
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local jid_bare = require "util.jid".bare;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local jid_split = require "util.jid".split;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local xmlns_carbons = "urn:xmpp:carbons:1";
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local xmlns_forward = "urn:xmpp:forward:0";
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local host_sessions = hosts[module.host].sessions;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 -- TODO merge message handlers into one somehow
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 module:hook("iq/self/"..xmlns_carbons..":enable", function(event)
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local origin, stanza = event.origin, event.stanza;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 if stanza.attr.type == "set" then
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 module:log("debug", "%s enabled carbons", origin.full_jid);
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 origin.want_carbons = true;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 origin.send(st.reply(stanza));
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 return true
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end);
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 module:hook("iq/self/"..xmlns_carbons..":disable", function(event)
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local origin, stanza = event.origin, event.stanza;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 if stanza.attr.type == "set" then
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 module:log("debug", "%s disabled carbons", origin.full_jid);
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 origin.want_carbons = nil;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 origin.send(st.reply(stanza));
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 return true
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end);
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
30 local function message_handler(event, c2s)
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local origin, stanza = event.origin, event.stanza;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local orig_type = stanza.attr.type;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 local orig_to = stanza.attr.to;
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
34 local orig_from = stanza.attr.from;
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 if not (orig_type == nil
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 or orig_type == "normal"
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 or orig_type == "chat") then
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
39 return -- No carbons for messages of type error or headline
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local bare_jid, user_sessions;
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
43 local no_carbon_to = {};
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
44 module:log("debug", "origin (%s) type: %s", tostring(origin), origin.type)
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
45 if c2s then -- Stanza sent by a local client
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 bare_jid = (origin.username.."@"..origin.host)
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 user_sessions = host_sessions[origin.username];
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
48 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
49 local username, hostname, resource = jid_split(orig_to);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
50 bare_jid = jid_bare(orig_to);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
51 user_sessions = host_sessions[username];
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
52 if resource then
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
53 no_carbon_to[resource] = true;
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
54 else
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
55 local top_resources = user_sessions.top_resources;
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
56 for _, session in ipairs(top_resources) do
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
57 no_carbon_to[session.resource] = true;
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
58 end
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
59 end
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
62 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
63 stanza:maptags(function(tag)
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
64 return tag.attr.xmlns == xmlns_carbons
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
65 and tag.name == "private" and tag or nil;
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
66 end);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
67 return
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
68 end
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
69
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
70 if not stanza:get_child("forwarded", xmlns_forward) then
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 user_sessions = user_sessions and user_sessions.sessions;
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 for resource, session in pairs(user_sessions) do
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 local full_jid = bare_jid .. "/" .. resource;
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
74 if session.want_carbons then
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
75 if (c2s and session ~= origin) or (not c2s and not no_carbon_to[resource]) then
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
76 local msg = st.clone(stanza);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
77 msg.attr.xmlns = msg.attr.xmlns or "jabber:client";
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
78 local fwd = st.message{
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
79 from = bare_jid,
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
80 to = full_jid,
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
81 type = orig_type,
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
82 }
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
83 :tag("forwarded", { xmlns = xmlns_forward })
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
84 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }):up()
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
85 :add_child(msg);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
86 core_post_stanza(origin, fwd);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
87 end
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
93 local function c2s_message_handler(event)
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
94 return message_handler(event, true)
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
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 -- Stanzas sent by local clients
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 module:hook("pre-message/bare", c2s_message_handler, 1);
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 module:hook("pre-message/full", c2s_message_handler, 1);
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 -- Stanszas to local clients
480
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
101 module:hook("message/bare", message_handler, 1);
0cef5be669de mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents: 463
diff changeset
102 module:hook("message/full", message_handler, 1);
462
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103
f28a3f260fc2 mod_carbons: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 module:add_feature(xmlns_carbons);