Comparison

plugins/muc/muc.lib.lua @ 6202:64ed7f538f81

plugins/muc/muc.lib: Refactor out process_form into hooks
author daurnimator <quae@daurnimator.com>
date Tue, 01 Apr 2014 17:45:03 -0400
parent 6201:b9e8f5268c97
child 6203:b6ffce01e6cf
comparison
equal deleted inserted replaced
6201:b9e8f5268c97 6202:64ed7f538f81
1002 value = tostring(event.room:get_historylength()) 1002 value = tostring(event.room:get_historylength())
1003 }); 1003 });
1004 end); 1004 end);
1005 1005
1006 function room_mt:process_form(origin, stanza) 1006 function room_mt:process_form(origin, stanza)
1007 local query = stanza.tags[1]; 1007 local form = stanza.tags[1]:get_child("x", "jabber:x:data");
1008 local form = query:get_child("x", "jabber:x:data") 1008 if form.attr.type == "cancel" then
1009 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end 1009 origin.send(st.reply(stanza));
1010 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end 1010 elseif form.attr.type == "submit" then
1011 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form")); return; end 1011 local fields = self:get_form_layout(stanza.attr.from):data(form);
1012 1012 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then
1013 local fields = self:get_form_layout(stanza.attr.from):data(form); 1013 origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration"));
1014 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration")); return; end 1014 return true;
1015 1015 end
1016 1016
1017 local changed = {}; 1017 local changed = {};
1018 1018
1019 local function handle_option(name, field, allowed) 1019 local function handle_option(name, field, allowed)
1020 local new = fields[field]; 1020 local new = fields[field];
1021 if new == nil then return; end 1021 if new == nil then return; end
1022 if allowed and not allowed[new] then return; end 1022 if allowed and not allowed[new] then return; end
1023 if new == self["get_"..name](self) then return; end 1023 if new == self["get_"..name](self) then return; end
1024 changed[name] = true; 1024 changed[name] = true;
1025 self["set_"..name](self, new); 1025 self["set_"..name](self, new);
1026 end 1026 end
1027 1027
1028 local event = { room = self, fields = fields, changed = changed, stanza = stanza, origin = origin, update_option = handle_option }; 1028 local event = { room = self, fields = fields, changed = changed, stanza = stanza, origin = origin, update_option = handle_option };
1029 module:fire_event("muc-config-submitted", event); 1029 module:fire_event("muc-config-submitted", event);
1030 1030
1031 handle_option("name", "muc#roomconfig_roomname"); 1031 if self.save then self:save(true); end
1032 handle_option("description", "muc#roomconfig_roomdesc"); 1032 if self:is_locked() then
1033 handle_option("persistent", "muc#roomconfig_persistentroom"); 1033 self:unlock();
1034 handle_option("moderated", "muc#roomconfig_moderatedroom"); 1034 end
1035 handle_option("members_only", "muc#roomconfig_membersonly"); 1035 origin.send(st.reply(stanza));
1036 handle_option("public", "muc#roomconfig_publicroom"); 1036
1037 handle_option("changesubject", "muc#roomconfig_changesubject"); 1037 if next(changed) then
1038 handle_option("historylength", "muc#roomconfig_historylength"); 1038 local msg = st.message({type='groupchat', from=self.jid})
1039 handle_option("whois", "muc#roomconfig_whois", valid_whois); 1039 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
1040 handle_option("password", "muc#roomconfig_roomsecret"); 1040 :tag('status', {code = '104'}):up()
1041 1041 :up();
1042 if self.save then self:save(true); end 1042 if changed.whois then
1043 if self:is_locked() then 1043 local code = (self:get_whois() == 'moderators') and "173" or "172";
1044 self:unlock(); 1044 msg.tags[1]:tag('status', {code = code}):up();
1045 end 1045 end
1046 origin.send(st.reply(stanza)); 1046 self:broadcast_message(msg, false)
1047 1047 end
1048 if next(changed) then 1048 else
1049 local msg = st.message({type='groupchat', from=self.jid}) 1049 origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form"));
1050 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}) 1050 end
1051 :tag('status', {code = '104'}):up() 1051 return true;
1052 :up(); 1052 end
1053 if changed.whois then 1053 module:hook("muc-config-submitted", function(event)
1054 local code = (self:get_whois() == 'moderators') and "173" or "172"; 1054 event.update_option("name", "muc#roomconfig_roomname");
1055 msg.tags[1]:tag('status', {code = code}):up(); 1055 end);
1056 end 1056 module:hook("muc-config-submitted", function(event)
1057 self:broadcast_message(msg, false) 1057 event.update_option("description", "muc#roomconfig_roomdesc");
1058 end 1058 end);
1059 end 1059 module:hook("muc-config-submitted", function(event)
1060 event.update_option("persistent", "muc#roomconfig_persistentroom");
1061 end);
1062 module:hook("muc-config-submitted", function(event)
1063 event.update_option("moderated", "muc#roomconfig_moderatedroom");
1064 end);
1065 module:hook("muc-config-submitted", function(event)
1066 event.update_option("members_only", "muc#roomconfig_membersonly");
1067 end);
1068 module:hook("muc-config-submitted", function(event)
1069 event.update_option("public", "muc#roomconfig_publicroom");
1070 end);
1071 module:hook("muc-config-submitted", function(event)
1072 event.update_option("changesubject", "muc#roomconfig_changesubject");
1073 end);
1074 module:hook("muc-config-submitted", function(event)
1075 event.update_option("historylength", "muc#roomconfig_historylength");
1076 end);
1077 module:hook("muc-config-submitted", function(event)
1078 event.update_option("whois", "muc#roomconfig_whois", valid_whois);
1079 end);
1080 module:hook("muc-config-submitted", function(event)
1081 event.update_option("password", "muc#roomconfig_roomsecret");
1082 end);
1060 1083
1061 -- Removes everyone from the room 1084 -- Removes everyone from the room
1062 function room_mt:clear(x) 1085 function room_mt:clear(x)
1063 x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'}); 1086 x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
1064 local occupants_updated = {}; 1087 local occupants_updated = {};
1186 local reason = child:get_child_text("reason"); 1209 local reason = child:get_child_text("reason");
1187 local password = child:get_child_text("password"); 1210 local password = child:get_child_text("password");
1188 self:destroy(newjid, reason, password); 1211 self:destroy(newjid, reason, password);
1189 origin.send(st.reply(stanza)); 1212 origin.send(st.reply(stanza));
1190 return true; 1213 return true;
1214 elseif child.name == "x" and child.attr.xmlns == "jabber:x:data" then
1215 return self:process_form(origin, stanza);
1191 else 1216 else
1192 self:process_form(origin, stanza); 1217 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
1193 return true; 1218 return true;
1194 end 1219 end
1195 end 1220 end
1196 1221
1197 function room_mt:handle_groupchat_to_room(origin, stanza) 1222 function room_mt:handle_groupchat_to_room(origin, stanza)