Merge 0.10->trunk
[prosody.git] / plugins / muc / password.lib.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 local st = require "util.stanza";
11
12 local function get_password(room)
13         return room._data.password;
14 end
15
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;
20         if room.save then room:save(true); end
21         return true;
22 end
23
24 module:hook("muc-disco#info", function(event)
25         event.reply:tag("feature", {var = get_password(event.room) and "muc_passwordprotected" or "muc_unsecured"}):up();
26 end);
27
28 module:hook("muc-config-form", function(event)
29         table.insert(event.form, {
30                 name = "muc#roomconfig_roomsecret";
31                 type = "text-private";
32                 label = "Password";
33                 value = get_password(event.room) or "";
34         });
35 end);
36
37 module:hook("muc-config-submitted/muc#roomconfig_roomsecret", function(event)
38         if set_password(event.room, event.value) then
39                 event.status_codes["104"] = true;
40         end
41 end);
42
43 -- Don't allow anyone to join room unless they provide the password
44 module:hook("muc-occupant-pre-join", function(event)
45         local room, stanza = event.room, event.stanza;
46         local password = stanza:get_child("x", "http://jabber.org/protocol/muc");
47         password = password and password:get_child_text("password", "http://jabber.org/protocol/muc");
48         if not password or password == "" then password = nil; end
49         if get_password(room) ~= password then
50                 local from, to = stanza.attr.from, stanza.attr.to;
51                 module:log("debug", "%s couldn't join due to invalid password: %s", from, to);
52                 local reply = st.error_reply(stanza, "auth", "not-authorized"):up();
53                 reply.tags[1].attr.code = "401";
54                 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
55                 return true;
56         end
57 end, -20);
58
59 -- Add password to outgoing invite
60 module:hook("muc-invite", function(event)
61         local password = get_password(event.room);
62         if password then
63                 local x = event.stanza:get_child("x", "http://jabber.org/protocol/muc#user");
64                 x:tag("password"):text(password):up();
65         end
66 end);
67
68 return {
69         get = get_password;
70         set = set_password;
71 };