Software /
code /
prosody-modules
Annotate
mod_carbons/mod_carbons.lua @ 520:6fa8436f8ee3
mod_carbons: Remove completed TODO
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 06 Jan 2012 12:09:48 +0100 |
parent | 510:59e80326f2b3 |
child | 521:6d0d2673f95e |
rev | line source |
---|---|
462 | 1 local st = require "util.stanza"; |
2 local jid_bare = require "util.jid".bare; | |
3 local jid_split = require "util.jid".split; | |
4 local xmlns_carbons = "urn:xmpp:carbons:1"; | |
5 local xmlns_forward = "urn:xmpp:forward:0"; | |
6 local host_sessions = hosts[module.host].sessions; | |
7 | |
8 module:hook("iq/self/"..xmlns_carbons..":enable", function(event) | |
9 local origin, stanza = event.origin, event.stanza; | |
10 if stanza.attr.type == "set" then | |
11 module:log("debug", "%s enabled carbons", origin.full_jid); | |
12 origin.want_carbons = true; | |
13 origin.send(st.reply(stanza)); | |
14 return true | |
15 end | |
16 end); | |
17 | |
18 module:hook("iq/self/"..xmlns_carbons..":disable", function(event) | |
19 local origin, stanza = event.origin, event.stanza; | |
20 if stanza.attr.type == "set" then | |
21 module:log("debug", "%s disabled carbons", origin.full_jid); | |
22 origin.want_carbons = nil; | |
23 origin.send(st.reply(stanza)); | |
24 return true | |
25 end | |
26 end); | |
27 | |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
28 local function message_handler(event, c2s) |
462 | 29 local origin, stanza = event.origin, event.stanza; |
30 local orig_type = stanza.attr.type; | |
31 local orig_to = stanza.attr.to; | |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
32 local orig_from = stanza.attr.from; |
462 | 33 |
34 if not (orig_type == nil | |
35 or orig_type == "normal" | |
36 or orig_type == "chat") then | |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
37 return -- No carbons for messages of type error or headline |
462 | 38 end |
39 | |
40 local bare_jid, user_sessions; | |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
41 local no_carbon_to = {}; |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
42 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
|
43 if c2s then -- Stanza sent by a local client |
462 | 44 bare_jid = (origin.username.."@"..origin.host) |
45 user_sessions = host_sessions[origin.username]; | |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
46 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
|
47 local username, hostname, resource = jid_split(orig_to); |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
48 bare_jid = jid_bare(orig_to); |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
49 user_sessions = host_sessions[username]; |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
50 if resource then |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
51 no_carbon_to[resource] = true; |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
52 else |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
53 local top_resources = user_sessions.top_resources; |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
54 for _, session in ipairs(top_resources) do |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
55 no_carbon_to[session.resource] = true; |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
56 end |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
57 end |
462 | 58 end |
59 | |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
60 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
|
61 stanza:maptags(function(tag) |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
62 return tag.attr.xmlns == xmlns_carbons |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
63 and tag.name == "private" and tag or nil; |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
64 end); |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
65 return |
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 |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
68 if not stanza:get_child("forwarded", xmlns_forward) then |
462 | 69 user_sessions = user_sessions and user_sessions.sessions; |
70 for resource, session in pairs(user_sessions) do | |
71 local full_jid = bare_jid .. "/" .. resource; | |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
72 if session.want_carbons then |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
73 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
|
74 local msg = st.clone(stanza); |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
75 msg.attr.xmlns = msg.attr.xmlns or "jabber:client"; |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
76 local fwd = st.message{ |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
77 from = bare_jid, |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
78 to = full_jid, |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
79 type = orig_type, |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
80 } |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
81 :tag("forwarded", { xmlns = xmlns_forward }) |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
82 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }):up() |
510
59e80326f2b3
mod_carbons: Fix a typo and unindent a line.
Kim Alvefur <zash@zash.se>
parents:
480
diff
changeset
|
83 :add_child(msg); |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
84 core_post_stanza(origin, fwd); |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
85 end |
462 | 86 end |
87 end | |
88 end | |
89 end | |
90 | |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
91 local function c2s_message_handler(event) |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
92 return message_handler(event, true) |
462 | 93 end |
94 | |
95 -- Stanzas sent by local clients | |
96 module:hook("pre-message/bare", c2s_message_handler, 1); | |
97 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
|
98 -- Stanzas to local clients |
480
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
99 module:hook("message/bare", message_handler, 1); |
0cef5be669de
mod_carbons: Consolidate message handlers
Kim Alvefur <zash@zash.se>
parents:
463
diff
changeset
|
100 module:hook("message/full", message_handler, 1); |
462 | 101 |
102 module:add_feature(xmlns_carbons); |