MUC: Assign priorities to config form hooks so they have a consistent order on each...
[prosody.git] / plugins / muc / name.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 jid_split = require "util.jid".split;
11
12 local function get_name(room)
13         return room._data.name or jid_split(room.jid);
14 end
15
16 local function set_name(room, name)
17         if name == "" or name == (jid_split(room.jid)) then name = nil; end
18         if room._data.name == name then return false; end
19         room._data.name = name;
20         return true;
21 end
22
23 module:hook("muc-disco#info", function(event)
24         event.reply:tag("identity", {category="conference", type="text", name=get_name(event.room)}):up();
25 end);
26
27 module:hook("muc-config-form", function(event)
28         table.insert(event.form, {
29                 name = "muc#roomconfig_roomname";
30                 type = "text-single";
31                 label = "Name";
32                 value = get_name(event.room) or "";
33         });
34 end, 100-1);
35
36 module:hook("muc-config-submitted/muc#roomconfig_roomname", function(event)
37         if set_name(event.room, event.value) then
38                 event.status_codes["104"] = true;
39         end
40 end);
41
42 return {
43         get = get_name;
44         set = set_name;
45 };