Software /
code /
prosody
Comparison
plugins/muc/password.lib.lua @ 6208:d724289a5226
plugins/muc: Move password functions to seperate module
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Wed, 02 Apr 2014 16:57:59 -0400 |
child | 6329:6b3eb1611587 |
comparison
equal
deleted
inserted
replaced
6207:a5928fdeaf97 | 6208:d724289a5226 |
---|---|
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 function get_password(room) | |
11 return room._data.password; | |
12 end | |
13 | |
14 local function set_password(room, password) | |
15 if password == "" then password = nil; end | |
16 if room._data.password == password then return false; end | |
17 room._data.password = password; | |
18 if room.save then room:save(true); end | |
19 return true; | |
20 end | |
21 | |
22 module:hook("muc-disco#info", function(event) | |
23 event.reply:tag("feature", {var = get_password(event.room) and "muc_passwordprotected" or "muc_unsecured"}):up(); | |
24 end); | |
25 | |
26 module:hook("muc-config-form", function(event) | |
27 table.insert(event.form, { | |
28 name = "muc#roomconfig_roomsecret"; | |
29 type = "text-private"; | |
30 label = "Password"; | |
31 value = get_password(event.room) or ""; | |
32 }); | |
33 end); | |
34 | |
35 module:hook("muc-config-submitted", function(event) | |
36 local new = event.fields["muc#roomconfig_roomsecret"]; | |
37 if new ~= nil and set_password(event.room, new) then | |
38 event.status_codes["104"] = true; | |
39 end | |
40 end); | |
41 | |
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 local password = stanza:get_child("x", "http://jabber.org/protocol/muc"); | |
46 password = password and password:get_child_text("password", "http://jabber.org/protocol/muc"); | |
47 if not password or password == "" then password = nil; end | |
48 if get_password(room) ~= password then | |
49 local from, to = stanza.attr.from, stanza.attr.to; | |
50 module:log("debug", "%s couldn't join due to invalid password: %s", from, to); | |
51 local reply = st.error_reply(stanza, "auth", "not-authorized"):up(); | |
52 reply.tags[1].attr.code = "401"; | |
53 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
54 return true; | |
55 end | |
56 end, -20); | |
57 | |
58 -- Add password to outgoing invite | |
59 module:hook("muc-invite", function(event) | |
60 local password = get_password(event.room); | |
61 if password then | |
62 local x = event.stanza:get_child("x", "http://jabber.org/protocol/muc#user"); | |
63 x:tag("password"):text(password):up(); | |
64 end | |
65 end); | |
66 | |
67 return { | |
68 get = get_password; | |
69 set = set_password; | |
70 }; |