2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 local st
= require
"util.stanza";
12 local function get_password(room
)
13 return room
._data
.password
;
16 local function set_password(room
, password
)
17 if password
== "" then password
= nil; end
18 if room
._data
.password
== password
then return false; end
19 room
._data
.password
= password
;
23 module
:hook("muc-disco#info", function(event
)
24 event
.reply
:tag("feature", {var
= get_password(event
.room
) and "muc_passwordprotected" or "muc_unsecured"}):up();
27 module
:hook("muc-config-form", function(event
)
28 table.insert(event
.form
, {
29 name
= "muc#roomconfig_roomsecret";
30 type = "text-private";
32 value
= get_password(event
.room
) or "";
36 module
:hook("muc-config-submitted/muc#roomconfig_roomsecret", function(event
)
37 if set_password(event
.room
, event
.value
) then
38 event
.status_codes
["104"] = true;
42 -- Don't allow anyone to join room unless they provide the password
43 module
:hook("muc-occupant-pre-join", function(event
)
44 local room
, stanza
= event
.room
, event
.stanza
;
45 if not get_password(room
) then return end
46 local muc_x
= stanza
:get_child("x", "http://jabber.org/protocol/muc");
47 if not muc_x
then return end
48 local password
= muc_x
:get_child_text("password", "http://jabber.org/protocol/muc");
49 if not password
or password
== "" then password
= nil; end
50 if get_password(room
) ~= password
then
51 local from
, to
= stanza
.attr
.from
, stanza
.attr
.to
;
52 module
:log("debug", "%s couldn't join due to invalid password: %s", from
, to
);
53 local reply
= st
.error_reply(stanza
, "auth", "not-authorized"):up();
54 reply
.tags
[1].attr
.code
= "401";
55 event
.origin
.send(reply
:tag("x", {xmlns
= "http://jabber.org/protocol/muc"}));
60 -- Add password to outgoing invite
61 module
:hook("muc-invite", function(event
)
62 local password
= get_password(event
.room
);
64 local x
= event
.stanza
:get_child("x", "http://jabber.org/protocol/muc#user");
65 x
:tag("password"):text(password
):up();
69 module
:hook("muc-room-pre-create", function (event
)
70 local stanza
, room
= event
.stanza
, event
.room
;
71 local muc_x
= stanza
:get_child("x", "http://jabber.org/protocol/muc");
72 if not muc_x
then return end
73 local password
= muc_x
:get_child_text("password", "http://jabber.org/protocol/muc");
74 set_password(room
, password
);