Annotate

plugins/muc/muc.lib.lua @ 1741:2919f3b985fc

MUC: Added support for generating unique room names
author Waqas Hussain <waqas20@gmail.com>
date Mon, 07 Sep 2009 20:29:04 +0500
parent 1740:b37ccf9bec89
child 1742:1483a62d69bb
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
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
152 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
153 local current_nick = self._jid_nick[to];
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
154 for occupant, o_data in pairs(self._occupants) do
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 if occupant ~= current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 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
157 pres.attr.to, pres.attr.from = to, occupant;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 :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
160 self:route_stanza(pres);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
164 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
165 local history = self._data['history']; -- send discussion history
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 if history then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 for _, msg in ipairs(history) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 msg = st.deserialize(msg);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 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
170 self:route_stanza(msg);
1734
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 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
174 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
175 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 local function room_get_disco_info(self, stanza) end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 local function room_get_disco_items(self, stanza) end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
180 function room_mt:set_subject(current_nick, subject)
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 -- TODO check nick's authority
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 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
183 self._data['subject'] = subject;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 local msg = st.message({type='groupchat', from=current_nick})
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 :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
186 self:broadcast_message(msg, false);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 return true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
190 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
191 local from, to = stanza.attr.from, stanza.attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 local room = jid_bare(to);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 local current_nick = self._jid_nick[from];
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 local type = stanza.attr.type;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 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
196 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
197 if stanza.name == "presence" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 local pr = get_filtered_presence(stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 pr.attr.from = current_nick;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 if type == "error" then -- error, kick em out!
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 if current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 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
203 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
204 :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
205 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206 elseif type == "unavailable" then -- unavailable
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 if current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 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
209 local data = self._occupants[current_nick];
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 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
211 self:broadcast_presence(pr);
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
212 self._occupants[current_nick] = nil;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 self._jid_nick[from] = nil;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 elseif not type then -- available
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 if current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 --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
218 if current_nick == to then -- simple presence
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 log("debug", "%s broadcasted presence", current_nick);
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
220 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
221 self:broadcast_presence(pr);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 else -- change nick
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
223 if self._occupants[to] then
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 log("debug", "%s couldn't change nick", current_nick);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 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
226 else
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
227 local data = self._occupants[current_nick];
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 local to_nick = select(3, jid_split(to));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 if to_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 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
231 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
232 self:broadcast_presence(p, '303', to_nick);
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
233 self._occupants[current_nick] = nil;
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
234 self._occupants[to] = data;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
235 self._jid_nick[from] = to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 pr.attr.from = to;
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
237 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
238 self:broadcast_presence(pr);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 --TODO malformed-jid
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244 --else -- possible rejoin
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245 -- 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
246 -- 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
247 -- :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
248 -- self:handle_to_occupant(origin, stanza); -- resend available
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249 --end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 else -- enter room
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
251 local new_nick = to;
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
252 if self._occupants[to] then
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
253 new_nick = nil;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
254 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
255 if not new_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
256 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
257 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
258 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259 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
260 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
261 self._affiliations[jid_bare(from)] = "owner";
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
262 end
1740
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
263 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
264 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
265 if role then -- new occupant
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
266 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
267 self._jid_nick[from] = to;
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
268 self:send_occupant_list(from);
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
269 pr.attr.from = to;
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
270 self:broadcast_presence(pr);
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
271 self:send_history(from);
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
272 else -- banned
b37ccf9bec89 MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents: 1739
diff changeset
273 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
274 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
275 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
276 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
277 elseif type ~= 'result' then -- bad type
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
278 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
279 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
280 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
281 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
282 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
283 origin.send(st.error_reply(stanza, "modify", "bad-request"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
284 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
285 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
286 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
287 else -- private stanza
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
288 local o_data = self._occupants[to];
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
289 if o_data then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
290 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
291 local jid = o_data.jid;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
292 -- 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
293 stanza.attr.to, stanza.attr.from = jid, current_nick;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
294 self:route_stanza(stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
295 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
296 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
297 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
298 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
299 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
300
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
301 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
302 local type = stanza.attr.type;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
303 if stanza.name == "iq" and type == "get" then -- disco requests
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
304 local xmlns = stanza.tags[1].attr.xmlns;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
305 if xmlns == "http://jabber.org/protocol/disco#info" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
306 origin.send(room_get_disco_info(self, stanza));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
307 elseif xmlns == "http://jabber.org/protocol/disco#items" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
308 origin.send(room_get_disco_items(self, stanza));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
309 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
310 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
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 elseif stanza.name == "message" and type == "groupchat" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
313 local from, to = stanza.attr.from, stanza.attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
314 local room = jid_bare(to);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
315 local current_nick = self._jid_nick[from];
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
316 if not current_nick then -- not in room
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
317 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
318 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
319 local from = stanza.attr.from;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
320 stanza.attr.from = current_nick;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
321 local subject = getText(stanza, {"subject"});
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
322 if subject then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
323 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
324 else
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
325 self:broadcast_message(stanza, true);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
326 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
327 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
328 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
329 local to = stanza.attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
330 local current_nick = self._jid_nick[stanza.attr.from];
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
331 if current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
332 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
333 self:handle_to_occupant(origin, stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
334 stanza.attr.to = to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
335 elseif type ~= "error" and type ~= "result" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
336 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
337 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
338 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
339 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
340 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
341 local _from, _to = stanza.attr.from, stanza.attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
342 local _invitee = stanza.tags[1].tags[1].attr.to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
343 stanza.attr.from, stanza.attr.to = _to, _invitee;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
344 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
345 self:route_stanza(stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
346 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
347 stanza.attr.from, stanza.attr.to = _from, _to;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
348 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
349 if type == "error" or type == "result" then return; end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
350 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
351 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
352 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
353
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
354 function room_mt:handle_stanza(origin, stanza)
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
355 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
356 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
357 self:handle_to_occupant(origin, stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
358 else
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
359 self:handle_to_room(origin, stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
360 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
361 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
362
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
363 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
364
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
365 function room_mt:get_affiliation(jid)
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
366 local node, host, resource = jid_split(jid);
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
367 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
368 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
369 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
370 return result;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
371 end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
372
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
373 function room_mt:set_affiliation(jid, affiliation)
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
374 local node, host, resource = jid_split(jid);
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
375 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
376 if affiliation == "none" then affiliation = nil; end
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
377 if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then return false; end
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
378 self._affiliations[bare] = affiliation;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
379 -- TODO set roles based on new affiliation
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
380 return true;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
381 end
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
382
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
383 local _M = {}; -- module "muc"
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
384
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
385 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
386 return setmetatable({
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
387 jid = jid;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
388 _jid_nick = {};
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
389 _occupants = {};
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
390 _data = {};
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
391 _affiliations = {};
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
392 }, room_mt);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
393 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
394
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
395 return _M;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
396
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
397 --[[function get_disco_info(stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
398 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
399 :tag("identity", {category='conference', type='text', name=muc_name}):up()
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
400 :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
401 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
402 function get_disco_items(stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
403 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
404 for room in pairs(rooms_info:get()) do
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
405 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
406 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
407 return reply; -- TODO cache disco reply
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
408 end]]
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
409
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
410 --[[function handle_to_domain(origin, stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
411 local type = stanza.attr.type;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
412 if type == "error" or type == "result" then return; end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
413 if stanza.name == "iq" and type == "get" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
414 local xmlns = stanza.tags[1].attr.xmlns;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
415 if xmlns == "http://jabber.org/protocol/disco#info" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
416 origin.send(get_disco_info(stanza));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
417 elseif xmlns == "http://jabber.org/protocol/disco#items" then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
418 origin.send(get_disco_items(stanza));
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
419 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
420 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
421 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
422 else
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
423 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
424 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
425 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
426
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
427 register_component(muc_domain, function(origin, stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
428 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
429 if to_resource and not to_node then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
430 if type == "error" or type == "result" then return; end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
431 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
432 elseif to_resource then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
433 handle_to_occupant(origin, stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
434 elseif to_node then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
435 handle_to_room(origin, stanza)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
436 else -- to the main muc domain
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
437 if type == "error" or type == "result" then return; end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
438 handle_to_domain(origin, stanza);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
439 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
440 end);]]
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
441
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
442 --[[module.unload = function()
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
443 deregister_component(muc_domain);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
444 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
445 module.save = function()
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
446 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
447 end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
448 module.restore = function(data)
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
449 rooms.data, jid_nick.data, rooms_info.data, persist_list =
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
450 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
451 end]]