aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/muc/password.lib.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2015-03-27 22:24:57 +0000
committerMatthew Wild <mwild1@gmail.com>2015-03-27 22:24:57 +0000
commit85acc757991e3a425e8d8cab4611c7c321dc8425 (patch)
treef7d0930c79494da535857eaf4f2a976bb052fa35 /plugins/muc/password.lib.lua
parent25d886be280a1b6d9965b38cb22aa328354de0c2 (diff)
parentf3797f8dc8d711759fa4bf93a3a13f4dadbfdd7a (diff)
downloadprosody-85acc757991e3a425e8d8cab4611c7c321dc8425.tar.gz
prosody-85acc757991e3a425e8d8cab4611c7c321dc8425.zip
Merge 0.10->trunk
Diffstat (limited to 'plugins/muc/password.lib.lua')
-rw-r--r--plugins/muc/password.lib.lua72
1 files changed, 72 insertions, 0 deletions
diff --git a/plugins/muc/password.lib.lua b/plugins/muc/password.lib.lua
new file mode 100644
index 00000000..48486d73
--- /dev/null
+++ b/plugins/muc/password.lib.lua
@@ -0,0 +1,72 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local st = require "util.stanza";
+
+local function get_password(room)
+ return room._data.password;
+end
+
+local function set_password(room, password)
+ if password == "" then password = nil; end
+ if room._data.password == password then return false; end
+ room._data.password = password;
+ if room.save then room:save(true); end
+ return true;
+end
+
+module:hook("muc-disco#info", function(event)
+ event.reply:tag("feature", {var = get_password(event.room) and "muc_passwordprotected" or "muc_unsecured"}):up();
+end);
+
+module:hook("muc-config-form", function(event)
+ table.insert(event.form, {
+ name = "muc#roomconfig_roomsecret";
+ type = "text-private";
+ label = "Password";
+ value = get_password(event.room) or "";
+ });
+end);
+
+module:hook("muc-config-submitted", function(event)
+ local new = event.fields["muc#roomconfig_roomsecret"];
+ if new ~= nil and set_password(event.room, new) then
+ event.status_codes["104"] = true;
+ end
+end);
+
+-- Don't allow anyone to join room unless they provide the password
+module:hook("muc-occupant-pre-join", function(event)
+ local room, stanza = event.room, event.stanza;
+ local password = stanza:get_child("x", "http://jabber.org/protocol/muc");
+ password = password and password:get_child_text("password", "http://jabber.org/protocol/muc");
+ if not password or password == "" then password = nil; end
+ if get_password(room) ~= password then
+ local from, to = stanza.attr.from, stanza.attr.to;
+ module:log("debug", "%s couldn't join due to invalid password: %s", from, to);
+ local reply = st.error_reply(stanza, "auth", "not-authorized"):up();
+ reply.tags[1].attr.code = "401";
+ event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
+ return true;
+ end
+end, -20);
+
+-- Add password to outgoing invite
+module:hook("muc-invite", function(event)
+ local password = get_password(event.room);
+ if password then
+ local x = event.stanza:get_child("x", "http://jabber.org/protocol/muc#user");
+ x:tag("password"):text(password):up();
+ end
+end);
+
+return {
+ get = get_password;
+ set = set_password;
+};