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