Annotate

plugins/muc/muc.lib.lua @ 1756:b2291156a9c2

MUC: Added service discovery replies for rooms.
author Waqas Hussain <waqas20@gmail.com>
date Mon, 07 Sep 2009 20:54:53 +0500
parent 1755:1614e8e62ad5
child 1757:157e438823ba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 -- Prosody IM
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local datamanager = require "util.datamanager";
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local datetime = require "util.datetime";
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local jid_split = require "util.jid".split;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local jid_bare = require "util.jid".bare;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local st = require "util.stanza";
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local log = require "util.logger".init("mod_muc");
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local multitable_new = require "util.multitable".new;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local t_insert, t_remove = table.insert, table.remove;
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
18 local setmetatable = setmetatable;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local muc_domain = nil; --module:get_host();
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local history_length = 20;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 ------------
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local function filter_xmlns_from_array(array, filters)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local count = 0;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 for i=#array,1,-1 do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 local attr = array[i].attr;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 if filters[attr and attr.xmlns] then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 t_remove(array, i);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 count = count + 1;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 return count;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 local function filter_xmlns_from_stanza(stanza, filters)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 if filters then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 return stanza, filter_xmlns_from_array(stanza, filters);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 return stanza, 0;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 local function get_filtered_presence(stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 return filter_xmlns_from_stanza(st.clone(stanza), presence_filters);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 local kickable_error_conditions = {
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 ["gone"] = true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 ["internal-server-error"] = true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 ["item-not-found"] = true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 ["jid-malformed"] = true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 ["recipient-unavailable"] = true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 ["redirect"] = true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 ["remote-server-not-found"] = true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 ["remote-server-timeout"] = true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 ["service-unavailable"] = true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 };
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 local function get_kickable_error(stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 for _, tag in ipairs(stanza.tags) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 if tag.name == "error" and tag.attr.xmlns == "jabber:client" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 for _, cond in ipairs(tag.tags) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 return kickable_error_conditions[cond.name] and cond.name;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 return true; -- malformed error message
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 return true; -- malformed error message
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 local function getUsingPath(stanza, path, getText)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 local tag = stanza;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 for _, name in ipairs(path) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 if type(tag) ~= 'table' then return; end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 tag = tag:child_with_name(name);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 if tag and getText then tag = table.concat(tag); end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 return tag;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 local function getTag(stanza, path) return getUsingPath(stanza, path); end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 local function getText(stanza, path) return getUsingPath(stanza, path, true); end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 -----------
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 --[[function get_room_disco_info(room, stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 :tag("identity", {category='conference', type='text', name=room._data["name"]):up()
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 function get_room_disco_items(room, stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 end -- TODO allow non-private rooms]]
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 --
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
95 local room_mt = {};
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
96 room_mt.__index = room_mt;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
97
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
98 function room_mt:get_default_role(affiliation)
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
99 if affiliation == "owner" or affiliation == "admin" then
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
100 return "moderator";
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
101 elseif affiliation == "member" or not affiliation then
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
102 return "participant";
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
103 end
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
104 end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
105
1736
98f833669d7f MUC: Fixed function declarations.
Waqas Hussain <waqas20@gmail.com>
parents: 1735
diff changeset
106 function room_mt:broadcast_presence(stanza, code, nick)
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 stanza = get_filtered_presence(stanza);
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
108 local data = self._occupants[stanza.attr.from];
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 :tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up();
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 if code then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 stanza:tag("status", {code=code}):up();
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 local me;
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
115 for occupant, o_data in pairs(self._occupants) do
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 if occupant ~= stanza.attr.from then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 for jid in pairs(o_data.sessions) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 stanza.attr.to = jid;
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
119 self:route_stanza(stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 me = o_data;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 if me then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 stanza:tag("status", {code='110'});
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 for jid in pairs(me.sessions) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 stanza.attr.to = jid;
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
129 self:route_stanza(stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 end
1736
98f833669d7f MUC: Fixed function declarations.
Waqas Hussain <waqas20@gmail.com>
parents: 1735
diff changeset
133 function room_mt:broadcast_message(stanza, historic)
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
134 for occupant, o_data in pairs(self._occupants) do
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 for jid in pairs(o_data.sessions) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 stanza.attr.to = jid;
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
137 self:route_stanza(stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 if historic then -- add to history
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
141 local history = self._data['history'];
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
142 if not history then history = {}; self._data['history'] = history; end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 -- stanza = st.clone(stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 t_insert(history, st.clone(st.preserialize(stanza)));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 while #history > history_length do t_remove(history, 1) end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
150 function room_mt:broadcast_except_nick(stanza, nick)
1751
55ee6e792e3e MUC: Fixed a variable scoping bug causing problems with presence routing on affiliation/role change.
Waqas Hussain <waqas20@gmail.com>
parents: 1750
diff changeset
151 for rnick, occupant in pairs(self._occupants) do
55ee6e792e3e MUC: Fixed a variable scoping bug causing problems with presence routing on affiliation/role change.
Waqas Hussain <waqas20@gmail.com>
parents: 1750
diff changeset
152 if rnick ~= nick then
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
153 for jid in pairs(occupant.sessions) do
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
154 stanza.attr.to = jid;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
155 self:route_stanza(stanza);
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
156 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
157 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
158 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
159 end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
161 function room_mt:send_occupant_list(to)
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
162 local current_nick = self._jid_nick[to];
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
163 for occupant, o_data in pairs(self._occupants) do
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 if occupant ~= current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 pres.attr.to, pres.attr.from = to, occupant;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
169 self:route_stanza(pres);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
173 function room_mt:send_history(to)
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
174 local history = self._data['history']; -- send discussion history
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 if history then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 for _, msg in ipairs(history) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 msg = st.deserialize(msg);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 msg.attr.to=to;
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
179 self:route_stanza(msg);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
182 if self._data['subject'] then
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
183 self:route_stanza(st.message({type='groupchat', from=self.jid, to=to}):tag("subject"):text(self._data['subject']));
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186
1756
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
187 local function room_get_disco_info(self, stanza)
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
188 return st.reply(stanza):query("http://jabber.org/protocol/disco#info"):tag("identity", {category="conference", type="text"});
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
189 end
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
190 local function room_get_disco_items(self, stanza)
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
191 return st.reply(stanza):query("http://jabber.org/protocol/disco#items");
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
192 end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
193 function room_mt:set_subject(current_nick, subject)
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 -- TODO check nick's authority
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 if subject == "" then subject = nil; end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
196 self._data['subject'] = subject;
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
197 if self.save then self:save(); end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 local msg = st.message({type='groupchat', from=current_nick})
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 :tag('subject'):text(subject):up();
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
200 self:broadcast_message(msg, false);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 return true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
204 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 local from, to = stanza.attr.from, stanza.attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206 local room = jid_bare(to);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 local current_nick = self._jid_nick[from];
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 local type = stanza.attr.type;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag());
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 if stanza.name == "presence" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 local pr = get_filtered_presence(stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 pr.attr.from = current_nick;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 if type == "error" then -- error, kick em out!
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 if current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 log("debug", "kicking %s from %s", current_nick, room);
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
217 self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to})
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
218 :tag('status'):text('This participant is kicked from the room because he sent an error presence')); -- send unavailable
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 elseif type == "unavailable" then -- unavailable
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 if current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 log("debug", "%s leaving %s", current_nick, room);
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
223 local data = self._occupants[current_nick];
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 data.role = 'none';
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
225 self:broadcast_presence(pr);
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
226 self._occupants[current_nick] = nil;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 self._jid_nick[from] = nil;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 elseif not type then -- available
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 if current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 if current_nick == to then -- simple presence
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 log("debug", "%s broadcasted presence", current_nick);
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
234 self._occupants[current_nick].sessions[from] = pr;
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
235 self:broadcast_presence(pr);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 else -- change nick
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
237 if self._occupants[to] then
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 log("debug", "%s couldn't change nick", current_nick);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 origin.send(st.error_reply(stanza, "cancel", "conflict"):tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 else
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
241 local data = self._occupants[current_nick];
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 local to_nick = select(3, jid_split(to));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 if to_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244 log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245 local p = st.presence({type='unavailable', from=current_nick});
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
246 self:broadcast_presence(p, '303', to_nick);
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
247 self._occupants[current_nick] = nil;
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
248 self._occupants[to] = data;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249 self._jid_nick[from] = to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 pr.attr.from = to;
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
251 self._occupants[to].sessions[from] = pr;
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
252 self:broadcast_presence(pr);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
253 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
254 --TODO malformed-jid
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
255 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
256 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
257 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
258 --else -- possible rejoin
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259 -- log("debug", "%s had connection replaced", current_nick);
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
260 -- self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to})
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
261 -- :tag('status'):text('Replaced by new connection'):up()); -- send unavailable
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
262 -- self:handle_to_occupant(origin, stanza); -- resend available
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
263 --end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
264 else -- enter room
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
265 local new_nick = to;
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
266 if self._occupants[to] then
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
267 new_nick = nil;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
268 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
269 if not new_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
270 log("debug", "%s couldn't join due to nick conflict: %s", from, to);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
271 origin.send(st.error_reply(stanza, "cancel", "conflict"):tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
272 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
273 log("debug", "%s joining as %s", from, to);
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
274 if not next(self._affiliations) then -- new room, no owners
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
275 self._affiliations[jid_bare(from)] = "owner";
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
276 end
1740
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
277 local affiliation = self:get_affiliation(from);
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
278 local role = self:get_default_role(affiliation)
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
279 if role then -- new occupant
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
280 self._occupants[to] = {affiliation=affiliation, role=role, jid=from, sessions={[from]=get_filtered_presence(stanza)}};
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
281 self._jid_nick[from] = to;
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
282 self:send_occupant_list(from);
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
283 pr.attr.from = to;
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
284 self:broadcast_presence(pr);
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
285 self:send_history(from);
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
286 else -- banned
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
287 origin.send(st.error_reply(stanza, "auth", "forbidden"):tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
288 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
289 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
290 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
291 elseif type ~= 'result' then -- bad type
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
292 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
293 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
294 elseif not current_nick and type ~= "error" and type ~= "result" then -- not in room
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
295 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
296 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
297 origin.send(st.error_reply(stanza, "modify", "bad-request"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
298 elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
299 log("debug", "%s kicked from %s for sending an error message", current_nick, room);
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
300 self: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
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
301 else -- private stanza
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
302 local o_data = self._occupants[to];
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
303 if o_data then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
304 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
305 local jid = o_data.jid;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
306 -- TODO if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
307 stanza.attr.to, stanza.attr.from = jid, current_nick;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
308 self:route_stanza(stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
309 elseif type ~= "error" and type ~= "result" then -- recipient not in room
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
310 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
311 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
312 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
313 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
314
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
315 function room_mt:handle_form(origin, stanza)
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
316 if self:get_affiliation(stanza.attr.from) ~= "owner" then origin.send(st.error_reply(nil, "auth", "forbidden")); return; end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
317 if stanza.attr.type == "get" then
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
318 local title = "Configuration for "..self.jid;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
319 origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner")
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
320 :tag("x", {xmlns='jabber:x:data', type='form'})
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
321 :tag("title"):text(title):up()
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
322 :tag("instructions"):text(title):up()
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
323 :tag("field", {type='hidden', var='FORM_TYPE'}):tag("value"):text("http://jabber.org/protocol/muc#roomconfig"):up():up()
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
324 :tag("field", {type='boolean', label='Make Room Persistent?', var='muc#roomconfig_persistentroom'})
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
325 :tag("value"):text(self._data.persistent and "1" or "0"):up()
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
326 :up()
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
327 :tag("field", {type='boolean', label='Make Room Publicly Searchable?', var='muc#roomconfig_publicroom'})
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
328 :tag("value"):text(self._data.hidden and "0" or "1"):up()
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
329 :up()
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
330 );
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
331 elseif stanza.attr.type == "set" then
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
332 local query = stanza.tags[1];
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
333 local form;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
334 for _, tag in ipairs(query.tags) do if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then form = tag; break; end end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
335 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
336 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
337 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
338 local fields = {};
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
339 for _, field in pairs(form.tags) do
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
340 if field.name == "field" and field.attr.var and field.tags[1].name == "value" and #field.tags[1].tags == 0 then
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
341 fields[field.attr.var] = field.tags[1][1] or "";
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
342 end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
343 end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
344 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
345
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
346 local persistent = fields['muc#roomconfig_persistentroom'];
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
347 if persistent == "0" or persistent == "false" then persistent = nil; elseif persistent == "1" or persistent == "true" then persistent = true;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
348 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
349 self._data.persistent = persistent;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
350 module:log("debug", "persistent=%s", tostring(persistent));
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
351
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
352 local public = fields['muc#roomconfig_publicroom'];
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
353 if public == "0" or public == "false" then public = nil; elseif public == "1" or public == "true" then public = true;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
354 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
355 self._data.hidden = not public and true or nil;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
356
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
357 if self.save then self:save(true); end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
358 origin.send(st.reply(stanza));
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
359 end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
360 end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
361
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
362 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
363 local type = stanza.attr.type;
1745
15039fac3693 MUC: Some fixes for minor bugs in IQ handling.
Waqas Hussain <waqas20@gmail.com>
parents: 1744
diff changeset
364 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
1753
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
365 if stanza.name == "iq" then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
366 if xmlns == "http://jabber.org/protocol/disco#info" and type == "get" then
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
367 origin.send(room_get_disco_info(self, stanza));
1753
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
368 elseif xmlns == "http://jabber.org/protocol/disco#items" and type == "get" then
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
369 origin.send(room_get_disco_items(self, stanza));
1753
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
370 elseif xmlns == "http://jabber.org/protocol/muc#admin" then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
371 local actor = stanza.attr.from;
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
372 local affiliation = self:get_affiliation(actor);
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
373 local current_nick = self._jid_nick[actor];
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
374 local role = current_nick and self._occupants[current_nick].role or self:get_default_role(affiliation);
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
375 local item = stanza.tags[1].tags[1];
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
376 if item and item.name == "item" then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
377 if type == "set" then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
378 local callback = function() origin.send(st.reply(stanza)); end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
379 if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
380 local occupant = self._occupants[self.jid.."/"..item.attr.nick];
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
381 if occupant then item.attr.jid = occupant.jid; end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
382 end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
383 if item.attr.affiliation and item.attr.jid and not item.attr.role then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
384 local success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, callback);
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
385 if not success then origin.send(st.error_reply(stanza, errtype, err)); end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
386 elseif item.attr.role and item.attr.nick and not item.attr.affiliation then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
387 local success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, callback);
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
388 if not success then origin.send(st.error_reply(stanza, errtype, err)); end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
389 else
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
390 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
391 end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
392 elseif type == "get" then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
393 local _aff = item.attr.affiliation;
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
394 local _rol = item.attr.role;
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
395 if _aff and not _rol then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
396 if affiliation == "owner" or (affiliation == "admin" and _aff ~= "owner" and _aff ~= "admin") then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
397 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
398 for jid, affiliation in pairs(self._affiliations) do
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
399 if affiliation == _aff then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
400 reply:tag("item", {affiliation = _aff, jid = jid}):up();
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
401 end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
402 end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
403 origin.send(reply);
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
404 else
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
405 origin.send(st.error_reply(stanza, "auth", "forbidden"));
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
406 end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
407 elseif _rol and not _aff then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
408 if role == "moderator" then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
409 -- TODO allow admins and owners not in room? Provide read-only access to everyone who can see the participants anyway?
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
410 if _rol == "none" then _rol = nil; end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
411 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
412 for nick, occupant in pairs(self._occupants) do
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
413 if occupant.role == _rol then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
414 reply:tag("item", {nick = nick, role = _rol or "none", affiliation = occupant.affiliation or "none", jid = occupant.jid}):up();
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
415 end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
416 end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
417 origin.send(reply);
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
418 else
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
419 origin.send(st.error_reply(stanza, "auth", "forbidden"));
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
420 end
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
421 else
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
422 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
423 end
1744
265863a5612b MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents: 1743
diff changeset
424 end
1753
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
425 elseif type == "set" or type == "get" then
a84901db4085 MUC: Refactored IQ handling to be more easily extensible.
Waqas Hussain <waqas20@gmail.com>
parents: 1752
diff changeset
426 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
427 end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
428 elseif xmlns == "http://jabber.org/protocol/muc#owner" and (type == "get" or type == "set") and stanza.tags[1].name == "query" then
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
429 self:handle_form(origin, stanza);
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
430 elseif type == "set" or type == "get" then
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
431 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
432 end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
433 elseif stanza.name == "message" and type == "groupchat" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
434 local from, to = stanza.attr.from, stanza.attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
435 local room = jid_bare(to);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
436 local current_nick = self._jid_nick[from];
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
437 if not current_nick then -- not in room
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
438 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
439 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
440 local from = stanza.attr.from;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
441 stanza.attr.from = current_nick;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
442 local subject = getText(stanza, {"subject"});
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
443 if subject then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
444 self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
445 else
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
446 self:broadcast_message(stanza, true);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
447 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
448 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
449 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
450 local to = stanza.attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
451 local current_nick = self._jid_nick[stanza.attr.from];
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
452 if current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
453 stanza.attr.to = current_nick;
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
454 self:handle_to_occupant(origin, stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
455 stanza.attr.to = to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
456 elseif type ~= "error" and type ~= "result" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
457 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
458 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
459 elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and self._jid_nick[stanza.attr.from]
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
460 and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" and #stanza.tags[1].tags == 1
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
461 and stanza.tags[1].tags[1].name == "invite" and stanza.tags[1].tags[1].attr.to then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
462 local _from, _to = stanza.attr.from, stanza.attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
463 local _invitee = stanza.tags[1].tags[1].attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
464 stanza.attr.from, stanza.attr.to = _to, _invitee;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
465 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = _from, nil;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
466 self:route_stanza(stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
467 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = nil, _invitee;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
468 stanza.attr.from, stanza.attr.to = _from, _to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
469 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
470 if type == "error" or type == "result" then return; end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
471 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
472 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
473 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
474
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
475 function room_mt:handle_stanza(origin, stanza)
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
476 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
477 if to_resource then
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
478 self:handle_to_occupant(origin, stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
479 else
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
480 self:handle_to_room(origin, stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
481 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
482 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
483
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
484 function room_mt:route_stanza(stanza) end -- Replace with a routing function, e.g., function(room, stanza) core_route_stanza(origin, stanza); end
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
485
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
486 function room_mt:get_affiliation(jid)
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
487 local node, host, resource = jid_split(jid);
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
488 local bare = node and node.."@"..host or host;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
489 local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID.
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
490 if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
491 return result;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
492 end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
493 function room_mt:set_affiliation(actor, jid, affiliation, callback)
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
494 jid = jid_bare(jid);
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
495 if affiliation == "none" then affiliation = nil; end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
496 if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
497 return nil, "modify", "not-acceptable";
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
498 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
499 if self:get_affiliation(actor) ~= "owner" then return nil, "cancel", "not-allowed"; end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
500 if jid_bare(actor) == jid then return nil, "cancel", "not-allowed"; end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
501 self._affiliations[jid] = affiliation;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
502 local role = self:get_default_role(affiliation);
1750
a1c18470eeee MUC: Fixed: Unavailable presence was being sent for all role and affiliation changes. Now sent only for kicked occupants.
Waqas Hussain <waqas20@gmail.com>
parents: 1749
diff changeset
503 local p = st.presence()
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
504 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
1744
265863a5612b MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents: 1743
diff changeset
505 :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up();
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
506 local x = p.tags[1];
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
507 local item = x.tags[1];
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
508 if not role then -- getting kicked
1750
a1c18470eeee MUC: Fixed: Unavailable presence was being sent for all role and affiliation changes. Now sent only for kicked occupants.
Waqas Hussain <waqas20@gmail.com>
parents: 1749
diff changeset
509 p.attr.type = "unavailable";
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
510 if affiliation == "outcast" then
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
511 x:tag("status", {code="301"}):up(); -- banned
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
512 else
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
513 x:tag("status", {code="321"}):up(); -- affiliation change
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
514 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
515 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
516 local modified_nicks = {};
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
517 for nick, occupant in pairs(self._occupants) do
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
518 if jid_bare(occupant.jid) == jid then
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
519 if not role then -- getting kicked
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
520 self._occupants[nick] = nil;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
521 else
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
522 t_insert(modified_nicks, nick);
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
523 occupant.affiliation, occupant.role = affiliation, role;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
524 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
525 p.attr.from = nick;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
526 for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
527 if not role then self._jid_nick[jid] = nil; end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
528 p.attr.to = jid;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
529 self:route_stanza(p);
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
530 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
531 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
532 end
1755
1614e8e62ad5 MUC: Fixed an undefined global access.
Waqas Hussain <waqas20@gmail.com>
parents: 1754
diff changeset
533 if self.save then self:save(); end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
534 if callback then callback(); end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
535 for _, nick in ipairs(modified_nicks) do
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
536 p.attr.from = nick;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
537 self:broadcast_except_nick(p, nick);
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
538 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
539 return true;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
540 end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
541
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
542 function room_mt:get_role(nick)
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
543 local session = self._occupants[nick];
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
544 return session and session.role or nil;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
545 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
546 function room_mt:set_role(actor, nick, role, callback)
1752
4db786919805 MUC: Added kicking support.
Waqas Hussain <waqas20@gmail.com>
parents: 1751
diff changeset
547 if role == "none" then role = nil; end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
548 if role and role ~= "moderator" and role ~= "participant" and role ~= "visitor" then return nil, "modify", "not-acceptable"; end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
549 if self:get_affiliation(actor) ~= "owner" then return nil, "cancel", "not-allowed"; end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
550 local occupant = self._occupants[nick];
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
551 if not occupant then return nil, "modify", "not-acceptable"; end
1743
d00b144f4bcf MUC: An admin or owner MUST NOT be able to revoke moderation privileges from another admin or owner.
Waqas Hussain <waqas20@gmail.com>
parents: 1742
diff changeset
552 if occupant.affiliation == "owner" or occupant.affiliation == "admin" then return nil, "cancel", "not-allowed"; end
1750
a1c18470eeee MUC: Fixed: Unavailable presence was being sent for all role and affiliation changes. Now sent only for kicked occupants.
Waqas Hussain <waqas20@gmail.com>
parents: 1749
diff changeset
553 local p = st.presence({from = nick})
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
554 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
1744
265863a5612b MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents: 1743
diff changeset
555 :tag("item", {affiliation=occupant.affiliation or "none", nick=nick, role=role or "none"}):up();
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
556 if not role then -- kick
1750
a1c18470eeee MUC: Fixed: Unavailable presence was being sent for all role and affiliation changes. Now sent only for kicked occupants.
Waqas Hussain <waqas20@gmail.com>
parents: 1749
diff changeset
557 p.attr.type = "unavailable";
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
558 self._occupants[nick] = nil;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
559 for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
560 self._jid_nick[jid] = nil;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
561 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
562 p:tag("status", {code = "307"}):up();
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
563 else
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
564 occupant.role = role;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
565 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
566 for jid in pairs(occupant.sessions) do -- send to all sessions of the nick
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
567 p.attr.to = jid;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
568 self:route_stanza(p);
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
569 end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
570 if callback then callback(); end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
571 self:broadcast_except_nick(p, nick);
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
572 return true;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
573 end
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
574
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
575 local _M = {}; -- module "muc"
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
576
1749
cf2ade983e12 MUC: Changed a MUC library method into a function.
Waqas Hussain <waqas20@gmail.com>
parents: 1746
diff changeset
577 function _M.new_room(jid)
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
578 return setmetatable({
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
579 jid = jid;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
580 _jid_nick = {};
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
581 _occupants = {};
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
582 _data = {};
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
583 _affiliations = {};
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
584 }, room_mt);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
585 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
586
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
587 return _M;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
588
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
589 --[[function get_disco_info(stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
590 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
591 :tag("identity", {category='conference', type='text', name=muc_name}):up()
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
592 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
593 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
594 function get_disco_items(stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
595 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
596 for room in pairs(rooms_info:get()) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
597 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up();
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
598 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
599 return reply; -- TODO cache disco reply
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
600 end]]
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
601
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
602 --[[function handle_to_domain(origin, stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
603 local type = stanza.attr.type;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
604 if type == "error" or type == "result" then return; end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
605 if stanza.name == "iq" and type == "get" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
606 local xmlns = stanza.tags[1].attr.xmlns;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
607 if xmlns == "http://jabber.org/protocol/disco#info" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
608 origin.send(get_disco_info(stanza));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
609 elseif xmlns == "http://jabber.org/protocol/disco#items" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
610 origin.send(get_disco_items(stanza));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
611 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
612 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
613 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
614 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
615 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
616 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
617 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
618
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
619 register_component(muc_domain, function(origin, stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
620 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
621 if to_resource and not to_node then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
622 if type == "error" or type == "result" then return; end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
623 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
624 elseif to_resource then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
625 handle_to_occupant(origin, stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
626 elseif to_node then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
627 handle_to_room(origin, stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
628 else -- to the main muc domain
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
629 if type == "error" or type == "result" then return; end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
630 handle_to_domain(origin, stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
631 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
632 end);]]
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
633
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
634 --[[module.unload = function()
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
635 deregister_component(muc_domain);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
636 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
637 module.save = function()
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
638 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
639 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
640 module.restore = function(data)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
641 rooms.data, jid_nick.data, rooms_info.data, persist_list =
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
642 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
643 end]]