Annotate

plugins/mod_muc.lua @ 992:3153eff6dae2

prosody: Add prosody_shutdown() function to initiate a server shutdown, add code to gracefully close connections before stopping
author Matthew Wild <mwild1@gmail.com>
date Sun, 12 Apr 2009 02:59:09 +0100
parent 965:8bc06338aac3
child 1042:a3d77353c18a
child 1059:aed462ef09d8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
896
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 879
diff changeset
1 -- Prosody IM v0.4
835
2a8bfb7dee77 MUC: Added copyright notice
Waqas Hussain <waqas20@gmail.com>
parents: 834
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
2a8bfb7dee77 MUC: Added copyright notice
Waqas Hussain <waqas20@gmail.com>
parents: 834
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
2a8bfb7dee77 MUC: Added copyright notice
Waqas Hussain <waqas20@gmail.com>
parents: 834
diff changeset
4 --
2a8bfb7dee77 MUC: Added copyright notice
Waqas Hussain <waqas20@gmail.com>
parents: 834
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
2a8bfb7dee77 MUC: Added copyright notice
Waqas Hussain <waqas20@gmail.com>
parents: 834
diff changeset
6 -- COPYING file in the source package for more information.
2a8bfb7dee77 MUC: Added copyright notice
Waqas Hussain <waqas20@gmail.com>
parents: 834
diff changeset
7 --
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
8
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
9
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
10 local register_component = require "core.componentmanager".register_component;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
11 local deregister_component = require "core.componentmanager".deregister_component;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
12 local jid_split = require "util.jid".split;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
13 local jid_bare = require "util.jid".bare;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
14 local st = require "util.stanza";
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
15 local log = require "util.logger".init("mod_muc");
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
16 local multitable_new = require "util.multitable".new;
782
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
17 local t_insert, t_remove = table.insert, table.remove;
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
18
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
19 if module:get_host_type() ~= "component" then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
20 error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
21 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
22
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
23 local muc_domain = module:get_host();
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
24 local muc_name = "MUCMUCMUC!!!";
782
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
25 local history_length = 20;
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
26
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
27 -- room_name -> room
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
28 -- occupant_room_nick -> data
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
29 -- affiliation = ...
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
30 -- role
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
31 -- jid = occupant's real jid
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
32 local rooms = multitable_new();
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
33
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
34 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
35
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
36 -- room_name -> info
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
37 -- name - the room's friendly name
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
38 -- subject - the room's subject
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
39 -- non-anonymous = true|nil
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
40 -- persistent = true|nil
782
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
41 -- history = {preserialized stanzas}
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
42 local rooms_info = multitable_new();
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
43
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
44 local persist_list = datamanager.load(nil, muc_domain, 'room_list') or {};
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
45 for room in pairs(persist_list) do
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
46 rooms_info:set(room, datamanager.store(room, muc_domain, 'rooms') or nil);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
47 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
48
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
49 local component;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
50
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
51 function filter_xmlns_from_array(array, filters)
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
52 local count = 0;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
53 for i=#array,1,-1 do
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
54 local attr = array[i].attr;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
55 if filters[attr and attr.xmlns] then
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
56 t_remove(array, i);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
57 count = count + 1;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
58 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
59 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
60 return count;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
61 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
62 function filter_xmlns_from_stanza(stanza, filters)
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
63 if filters then
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
64 if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
65 return stanza, filter_xmlns_from_array(stanza, filters);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
66 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
67 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
68 return stanza, 0;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
69 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
70 local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
71 function get_filtered_presence(stanza)
830
c0554caf90e6 MUC: Use util.stanza.clone instead of pre/deserialize for cloning stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 828
diff changeset
72 return filter_xmlns_from_stanza(st.clone(stanza), presence_filters);
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
73 end
879
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
74 local kickable_error_conditions = {
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
75 ["gone"] = true;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
76 ["internal-server-error"] = true;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
77 ["item-not-found"] = true;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
78 ["jid-malformed"] = true;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
79 ["recipient-unavailable"] = true;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
80 ["redirect"] = true;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
81 ["remote-server-not-found"] = true;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
82 ["remote-server-timeout"] = true;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
83 ["service-unavailable"] = true;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
84 };
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
85 function get_kickable_error(stanza)
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
86 for _, tag in ipairs(stanza.tags) do
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
87 if tag.name == "error" and tag.attr.xmlns == "jabber:client" then
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
88 for _, cond in ipairs(tag.tags) do
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
89 if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
90 return kickable_error_conditions[cond.name] and cond.name;
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
91 end
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
92 end
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
93 return true; -- malformed error message
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
94 end
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
95 end
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
96 return true; -- malformed error message
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
97 end
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
98 function getUsingPath(stanza, path, getText)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
99 local tag = stanza;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
100 for _, name in ipairs(path) do
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
101 if type(tag) ~= 'table' then return; end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
102 tag = tag:child_with_name(name);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
103 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
104 if tag and getText then tag = table.concat(tag); end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
105 return tag;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
106 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
107 function getTag(stanza, path) return getUsingPath(stanza, path); end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
108 function getText(stanza, path) return getUsingPath(stanza, path, true); end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
109
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
110 function get_disco_info(stanza)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
111 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
112 :tag("identity", {category='conference', type='text', name=muc_name}):up()
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
113 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
114 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
115 function get_disco_items(stanza)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
116 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
117 for room in pairs(rooms_info:get()) do
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
118 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up();
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
119 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
120 return reply; -- TODO cache disco reply
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
121 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
122 function get_room_disco_info(stanza)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
123 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
124 :tag("identity", {category='conference', type='text', name=rooms_info:get(stanza.attr.to, "name")}):up()
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
125 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
126 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
127 function get_room_disco_items(stanza)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
128 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
129 end -- TODO allow non-private rooms
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
130
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
131 function save_room(room)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
132 local persistent = rooms_info:get(room, 'persistent');
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
133 if persistent then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
134 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
135 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
136 if persistent ~= persist_list[room] then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
137 if not persistent then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
138 datamanager.store(room, muc_domain, 'rooms', nil);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
139 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
140 persist_list[room] = persistent;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
141 datamanager.store(nil, muc_domain, 'room_list', persist_list);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
142 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
143 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
144
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
145 function set_subject(current_nick, room, subject)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
146 -- TODO check nick's authority
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
147 if subject == "" then subject = nil; end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
148 rooms_info:set(room, 'subject', subject);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
149 save_room();
818
4dda65cd1405 MUC: Various fixes
Waqas Hussain <waqas20@gmail.com>
parents: 817
diff changeset
150 local msg = st.message({type='groupchat', from=current_nick})
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
151 :tag('subject'):text(subject):up();
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
152 broadcast_message_stanza(room, msg, false);
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
153 return true;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
154 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
155
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
156 function broadcast_message_stanza(room, stanza, historic)
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
157 local r = rooms:get(room);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
158 if r then
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
159 for occupant, o_data in pairs(r) do
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
160 for jid in pairs(o_data.sessions) do
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
161 stanza.attr.to = jid;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
162 core_route_stanza(component, stanza);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
163 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
164 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
165 if historic then -- add to history
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
166 local history = rooms_info:get(room, 'history');
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
167 if not history then history = {}; rooms_info:set(room, 'history', history); end
830
c0554caf90e6 MUC: Use util.stanza.clone instead of pre/deserialize for cloning stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 828
diff changeset
168 -- stanza = st.clone(stanza);
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
169 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
170 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
830
c0554caf90e6 MUC: Use util.stanza.clone instead of pre/deserialize for cloning stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 828
diff changeset
171 t_insert(history, st.clone(st.preserialize(stanza)));
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
172 while #history > history_length do t_remove(history, 1) end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
173 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
174 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
175 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
176 function broadcast_presence_stanza(room, stanza, code, nick)
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
177 stanza = get_filtered_presence(stanza);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
178 local data = rooms:get(room, stanza.attr.from);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
179 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
180 :tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up();
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
181 if code then
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
182 stanza:tag("status", {code=code}):up();
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
183 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
184 local me;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
185 local r = rooms:get(room);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
186 if r then
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
187 for occupant, o_data in pairs(r) do
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
188 if occupant ~= stanza.attr.from then
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
189 for jid in pairs(o_data.sessions) do
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
190 stanza.attr.to = jid;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
191 core_route_stanza(component, stanza);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
192 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
193 else
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
194 me = o_data;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
195 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
196 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
197 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
198 if me then
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
199 stanza:tag("status", {code='110'});
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
200 for jid in pairs(me.sessions) do
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
201 stanza.attr.to = jid;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
202 core_route_stanza(component, stanza);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
203 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
204 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
205 end
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
206
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
207 function handle_to_occupant(origin, stanza) -- PM, vCards, etc
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
208 local from, to = stanza.attr.from, stanza.attr.to;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
209 local room = jid_bare(to);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
210 local current_nick = jid_nick:get(from, room);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
211 local type = stanza.attr.type;
822
a82eadc415ff MUC: Logging - logger doesn't like nils
Waqas Hussain <waqas20@gmail.com>
parents: 820
diff changeset
212 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag());
828
97ea39b7bf90 MUC: Syntax error in last commit - this is lua :)
Waqas Hussain <waqas20@gmail.com>
parents: 827
diff changeset
213 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
214 if stanza.name == "presence" then
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
215 local pr = get_filtered_presence(stanza);
824
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
216 pr.attr.from = current_nick;
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
217 if type == "error" then -- error, kick em out!
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
218 if current_nick then
820
c20545c0dc4d MUC: Added logging
Waqas Hussain <waqas20@gmail.com>
parents: 818
diff changeset
219 log("debug", "kicking %s from %s", current_nick, room);
834
596a6b425eb9 MUC: Replaced some duplicate code
Waqas Hussain <waqas20@gmail.com>
parents: 833
diff changeset
220 handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('This participant is kicked from the room because he sent an error presence')); -- send unavailable
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
221 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
222 elseif type == "unavailable" then -- unavailable
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
223 if current_nick then
820
c20545c0dc4d MUC: Added logging
Waqas Hussain <waqas20@gmail.com>
parents: 818
diff changeset
224 log("debug", "%s leaving %s", current_nick, room);
818
4dda65cd1405 MUC: Various fixes
Waqas Hussain <waqas20@gmail.com>
parents: 817
diff changeset
225 local data = rooms:get(room, current_nick);
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
226 data.role = 'none';
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
227 broadcast_presence_stanza(room, pr);
818
4dda65cd1405 MUC: Various fixes
Waqas Hussain <waqas20@gmail.com>
parents: 817
diff changeset
228 rooms:remove(room, current_nick);
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
229 jid_nick:remove(from, room);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
230 end
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
231 elseif not type then -- available
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
232 if current_nick then
831
e2d2095705dc MUC: Workaround for a Gajim bug (it includes <x xmlns='http://jabber.org/protocol/muc'/> in nick change presences)
Waqas Hussain <waqas20@gmail.com>
parents: 830
diff changeset
233 if #pr == #stanza or current_nick ~= to then
824
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
234 if current_nick == to then -- simple presence
820
c20545c0dc4d MUC: Added logging
Waqas Hussain <waqas20@gmail.com>
parents: 818
diff changeset
235 log("debug", "%s broadcasted presence", current_nick);
824
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
236 rooms:get(room, current_nick).sessions[from] = pr;
818
4dda65cd1405 MUC: Various fixes
Waqas Hussain <waqas20@gmail.com>
parents: 817
diff changeset
237 broadcast_presence_stanza(room, pr);
824
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
238 else -- change nick
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
239 if rooms:get(room, to) then
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
240 log("debug", "%s couldn't change nick", current_nick);
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
241 origin.send(st.error_reply(stanza, "cancel", "conflict"));
757
f77843f31c7d mod_muc: Add 'nick' to unavailable presence of nick changes. Thanks to Asterix for spotting :)
Matthew Wild <mwild1@gmail.com>
parents: 756
diff changeset
242 else
824
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
243 local data = rooms:get(room, current_nick);
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
244 local to_nick = select(3, jid_split(to));
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
245 if to_nick then
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
246 log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to);
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
247 local p = st.presence({type='unavailable', from=current_nick});
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
248 broadcast_presence_stanza(room, p, '303', to_nick);
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
249 rooms:remove(room, current_nick);
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
250 rooms:set(room, to, data);
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
251 jid_nick:set(from, room, to);
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
252 pr.attr.from = to;
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
253 rooms:get(room, to).sessions[from] = pr;
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
254 broadcast_presence_stanza(room, pr);
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
255 else
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
256 --TODO malformed-jid
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
257 end
757
f77843f31c7d mod_muc: Add 'nick' to unavailable presence of nick changes. Thanks to Asterix for spotting :)
Matthew Wild <mwild1@gmail.com>
parents: 756
diff changeset
258 end
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
259 end
824
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
260 else -- possible rejoin
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
261 log("debug", "%s had connection replaced", current_nick);
965
8bc06338aac3 mod_muc: Fix malformed presence stanzas (thanks elmex)
Matthew Wild <mwild1@gmail.com>
parents: 937
diff changeset
262 handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('Replaced by new connection'):up()); -- send unavailable
824
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
263 handle_to_occupant(origin, stanza); -- resend available
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
264 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
265 else -- enter room
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
266 local new_nick = to;
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
267 if rooms:get(room, to) then
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
268 new_nick = nil;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
269 end
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
270 if not new_nick then
820
c20545c0dc4d MUC: Added logging
Waqas Hussain <waqas20@gmail.com>
parents: 818
diff changeset
271 log("debug", "%s couldn't join due to nick conflict: %s", from, to);
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
272 origin.send(st.error_reply(stanza, "cancel", "conflict"));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
273 else
820
c20545c0dc4d MUC: Added logging
Waqas Hussain <waqas20@gmail.com>
parents: 818
diff changeset
274 log("debug", "%s joining as %s", from, to);
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
275 local data;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
276 if not rooms:get(room) and not rooms_info:get(room) then -- new room
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
277 data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
278 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
279 if not data then -- new occupant
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
280 data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
281 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
282 rooms:set(room, to, data);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
283 jid_nick:set(from, room, to);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
284 local r = rooms:get(room);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
285 if r then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
286 for occupant, o_data in pairs(r) do
817
e3e3919b6c7e MUC: Fixed: Presence for user joining the roomi was sent twice to the user
Waqas Hussain <waqas20@gmail.com>
parents: 813
diff changeset
287 if occupant ~= to then
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
288 local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
289 pres.attr.to, pres.attr.from = from, occupant;
833
5da1130054d1 MUC: Removed commented and unused code
Waqas Hussain <waqas20@gmail.com>
parents: 831
diff changeset
290 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
291 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
292 core_route_stanza(component, pres);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
293 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
294 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
295 end
824
b6ee70721783 MUC: Bug fixes and workarounds
Waqas Hussain <waqas20@gmail.com>
parents: 822
diff changeset
296 pr.attr.from = to;
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
297 broadcast_presence_stanza(room, pr);
782
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
298 local history = rooms_info:get(room, 'history'); -- send discussion history
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
299 if history then
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
300 for _, msg in ipairs(history) do
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
301 msg = st.deserialize(msg);
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
302 msg.attr.to=from;
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
303 core_route_stanza(component, msg);
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
304 end
6f9b2a9d6d45 mod_muc: Room history
Waqas Hussain <waqas20@gmail.com>
parents: 779
diff changeset
305 end
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
306 if rooms_info:get(room, 'subject') then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
307 core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject')));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
308 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
309 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
310 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
311 elseif type ~= 'result' then -- bad type
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
312 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
313 end
856
946d0f91bd38 mod_muc: Don't bounce error replies in response to errors
Matthew Wild <mwild1@gmail.com>
parents: 835
diff changeset
314 elseif not current_nick and type ~= "error" then -- not in room
811
863046d84b56 MUC: Private stanzas (private messages, IQs, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 810
diff changeset
315 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
812
1dbcf57154bd MUC: Kick participants sending error messages to other participants
Waqas Hussain <waqas20@gmail.com>
parents: 811
diff changeset
316 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
317 origin.send(st.error_reply(stanza, "modify", "bad-request"));
879
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
318 elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then
834
596a6b425eb9 MUC: Replaced some duplicate code
Waqas Hussain <waqas20@gmail.com>
parents: 833
diff changeset
319 log("debug", "%s kicked from %s for sending an error message", current_nick, room);
596a6b425eb9 MUC: Replaced some duplicate code
Waqas Hussain <waqas20@gmail.com>
parents: 833
diff changeset
320 handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('This participant is kicked from the room because he sent an error message to another occupant')); -- send unavailable
811
863046d84b56 MUC: Private stanzas (private messages, IQs, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 810
diff changeset
321 else -- private stanza
863046d84b56 MUC: Private stanzas (private messages, IQs, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 810
diff changeset
322 local o_data = rooms:get(room, to);
863046d84b56 MUC: Private stanzas (private messages, IQs, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 810
diff changeset
323 if o_data then
820
c20545c0dc4d MUC: Added logging
Waqas Hussain <waqas20@gmail.com>
parents: 818
diff changeset
324 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
813
ebfb904640d8 MUC: Made vCards work by redirecting vCard requests to bare JIDs
Waqas Hussain <waqas20@gmail.com>
parents: 812
diff changeset
325 local jid = o_data.jid;
ebfb904640d8 MUC: Made vCards work by redirecting vCard requests to bare JIDs
Waqas Hussain <waqas20@gmail.com>
parents: 812
diff changeset
326 if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
ebfb904640d8 MUC: Made vCards work by redirecting vCard requests to bare JIDs
Waqas Hussain <waqas20@gmail.com>
parents: 812
diff changeset
327 stanza.attr.to, stanza.attr.from = jid, current_nick;
811
863046d84b56 MUC: Private stanzas (private messages, IQs, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 810
diff changeset
328 core_route_stanza(component, stanza);
879
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
329 elseif type ~= "error" and type ~= "result" then -- recipient not in room
811
863046d84b56 MUC: Private stanzas (private messages, IQs, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 810
diff changeset
330 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
863046d84b56 MUC: Private stanzas (private messages, IQs, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 810
diff changeset
331 end
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
332 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
333 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
334
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
335 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
336 local type = stanza.attr.type;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
337 if stanza.name == "iq" and type == "get" then -- disco requests
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
338 local xmlns = stanza.tags[1].attr.xmlns;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
339 if xmlns == "http://jabber.org/protocol/disco#info" then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
340 origin.send(get_room_disco_info(stanza));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
341 elseif xmlns == "http://jabber.org/protocol/disco#items" then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
342 origin.send(get_room_disco_items(stanza));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
343 else
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
344 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
345 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
346 elseif stanza.name == "message" and type == "groupchat" then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
347 local from, to = stanza.attr.from, stanza.attr.to;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
348 local room = jid_bare(to);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
349 local current_nick = jid_nick:get(from, room);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
350 if not current_nick then -- not in room
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
351 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
352 else
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
353 local from = stanza.attr.from;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
354 stanza.attr.from = current_nick;
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
355 local subject = getText(stanza, {"subject"});
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
356 if subject then
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
357 set_subject(current_nick, room, subject); -- TODO use broadcast_message_stanza
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
358 else
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
359 broadcast_message_stanza(room, stanza, true);
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
360 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
361 end
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
362 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
363 local to = stanza.attr.to;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
364 local current_nick = jid_nick:get(stanza.attr.from, to);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
365 if current_nick then
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
366 stanza.attr.to = current_nick;
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
367 handle_to_occupant(origin, stanza);
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
368 stanza.attr.to = to;
879
2189baddedb8 MUC: Kick participants for error replies only on a selected list of error conditions
Waqas Hussain <waqas20@gmail.com>
parents: 856
diff changeset
369 elseif type ~= "error" and type ~= "result" then
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
370 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
371 end
937
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
372 elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and jid_nick:get(stanza.attr.from, stanza.attr.to)
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
373 and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" and #stanza.tags[1].tags == 1
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
374 and stanza.tags[1].tags[1].name == "invite" and stanza.tags[1].tags[1].attr.to then
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
375 local _from, _to = stanza.attr.from, stanza.attr.to;
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
376 local _invitee = stanza.tags[1].tags[1].attr.to;
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
377 stanza.attr.from, stanza.attr.to = _to, _invitee;
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
378 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = _from, nil;
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
379 core_route_stanza(component, stanza);
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
380 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = nil, _invitee;
40ccaacb2000 Added: MUC: Room invites
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
381 stanza.attr.from, stanza.attr.to = _from, _to;
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
382 else
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
383 if type == "error" or type == "result" then return; end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
384 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
385 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
386 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
387
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
388 function handle_to_domain(origin, stanza)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
389 local type = stanza.attr.type;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
390 if type == "error" or type == "result" then return; end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
391 if stanza.name == "iq" and type == "get" then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
392 local xmlns = stanza.tags[1].attr.xmlns;
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
393 if xmlns == "http://jabber.org/protocol/disco#info" then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
394 origin.send(get_disco_info(stanza));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
395 elseif xmlns == "http://jabber.org/protocol/disco#items" then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
396 origin.send(get_disco_items(stanza));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
397 else
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
398 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
399 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
400 else
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
401 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
402 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
403 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
404
779
ec0eadf4e9ff Changed mod_muc to work with changed component manager
Waqas Hussain <waqas20@gmail.com>
parents: 757
diff changeset
405 register_component(muc_domain, function(origin, stanza)
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
406 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
810
09d6b5fadc84 MUC: Presence and message stanzas now fully work (status messages, xhtml-im, etc)
Waqas Hussain <waqas20@gmail.com>
parents: 782
diff changeset
407 if to_resource and not to_node then
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
408 if type == "error" or type == "result" then return; end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
409 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
410 elseif to_resource then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
411 handle_to_occupant(origin, stanza);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
412 elseif to_node then
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
413 handle_to_room(origin, stanza)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
414 else -- to the main muc domain
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
415 if type == "error" or type == "result" then return; end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
416 handle_to_domain(origin, stanza);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
417 end
779
ec0eadf4e9ff Changed mod_muc to work with changed component manager
Waqas Hussain <waqas20@gmail.com>
parents: 757
diff changeset
418 end);
756
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
419
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
420 module.unload = function()
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
421 deregister_component(muc_domain);
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
422 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
423 module.save = function()
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
424 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
425 end
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
426 module.restore = function(data)
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
427 rooms.data, jid_nick.data, rooms_info.data, persist_list =
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
428 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};
2ca5fa47f317 mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 752
diff changeset
429 end