Software / code / prosody
Comparison
plugins/muc/muc.lib.lua @ 6224:2a9aff163545
plugins/muc: Move subject code to seperate module
| author | daurnimator <quae@daurnimator.com> |
|---|---|
| date | Wed, 16 Apr 2014 13:54:51 -0400 |
| parent | 6223:2a7ce69844ca |
| child | 6225:12537f1c1fec |
comparison
equal
deleted
inserted
replaced
| 6223:2a7ce69844ca | 6224:2a9aff163545 |
|---|---|
| 294 reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up(); | 294 reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up(); |
| 295 end | 295 end |
| 296 return reply; | 296 return reply; |
| 297 end | 297 end |
| 298 | 298 |
| 299 function room_mt:get_subject() | |
| 300 return self._data.subject_from, self._data.subject; | |
| 301 end | |
| 302 local function create_subject_message(from, subject) | |
| 303 return st.message({from = from; type = "groupchat"}) | |
| 304 :tag('subject'):text(subject):up(); | |
| 305 end | |
| 306 function room_mt:send_subject(to) | |
| 307 local msg = create_subject_message(self:get_subject()); | |
| 308 msg.attr.to = to; | |
| 309 self:route_stanza(msg); | |
| 310 end | |
| 311 function room_mt:set_subject(current_nick, subject) | |
| 312 if subject == "" then subject = nil; end | |
| 313 self._data['subject'] = subject; | |
| 314 self._data['subject_from'] = current_nick; | |
| 315 if self.save then self:save(); end | |
| 316 local msg = create_subject_message(current_nick, subject); | |
| 317 self:broadcast_message(msg); | |
| 318 return true; | |
| 319 end | |
| 320 | |
| 321 function room_mt:handle_kickable(origin, stanza) | 299 function room_mt:handle_kickable(origin, stanza) |
| 322 local real_jid = stanza.attr.from; | 300 local real_jid = stanza.attr.from; |
| 323 local occupant = self:get_occupant_by_real_jid(real_jid); | 301 local occupant = self:get_occupant_by_real_jid(real_jid); |
| 324 if occupant == nil then return nil; end | 302 if occupant == nil then return nil; end |
| 325 local type, condition, text = stanza:get_error(); | 303 local type, condition, text = stanza:get_error(); |
| 360 return not self:get_hidden(); | 338 return not self:get_hidden(); |
| 361 end | 339 end |
| 362 function room_mt:set_public(public) | 340 function room_mt:set_public(public) |
| 363 return self:set_hidden(not public); | 341 return self:set_hidden(not public); |
| 364 end | 342 end |
| 365 function room_mt:set_changesubject(changesubject) | |
| 366 changesubject = changesubject and true or nil; | |
| 367 if self._data.changesubject ~= changesubject then | |
| 368 self._data.changesubject = changesubject; | |
| 369 if self.save then self:save(true); end | |
| 370 end | |
| 371 end | |
| 372 function room_mt:get_changesubject() | |
| 373 return self._data.changesubject; | |
| 374 end | |
| 375 | 343 |
| 376 -- Give the room creator owner affiliation | 344 -- Give the room creator owner affiliation |
| 377 module:hook("muc-room-pre-create", function(event) | 345 module:hook("muc-room-pre-create", function(event) |
| 378 event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner"); | 346 event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner"); |
| 379 end, -1); | 347 end, -1); |
| 397 event.room:send_occupant_list(real_jid, function(nick, occupant) | 365 event.room:send_occupant_list(real_jid, function(nick, occupant) |
| 398 -- Don't include self | 366 -- Don't include self |
| 399 return occupant:get_presence(real_jid) == nil; | 367 return occupant:get_presence(real_jid) == nil; |
| 400 end); | 368 end); |
| 401 end, 80); | 369 end, 80); |
| 402 | |
| 403 -- Send subject to joining user | |
| 404 module:hook("muc-occupant-joined", function(event) | |
| 405 event.room:send_subject(event.stanza.attr.from); | |
| 406 end, 20); | |
| 407 | 370 |
| 408 function room_mt:handle_presence_to_occupant(origin, stanza) | 371 function room_mt:handle_presence_to_occupant(origin, stanza) |
| 409 local type = stanza.attr.type; | 372 local type = stanza.attr.type; |
| 410 if type == "error" then -- error, kick em out! | 373 if type == "error" then -- error, kick em out! |
| 411 return self:handle_kickable(origin, stanza) | 374 return self:handle_kickable(origin, stanza) |
| 666 value = not event.room:get_hidden() | 629 value = not event.room:get_hidden() |
| 667 }); | 630 }); |
| 668 end); | 631 end); |
| 669 module:hook("muc-config-form", function(event) | 632 module:hook("muc-config-form", function(event) |
| 670 table.insert(event.form, { | 633 table.insert(event.form, { |
| 671 name = 'muc#roomconfig_changesubject', | |
| 672 type = 'boolean', | |
| 673 label = 'Allow Occupants to Change Subject?', | |
| 674 value = event.room:get_changesubject() | |
| 675 }); | |
| 676 end); | |
| 677 module:hook("muc-config-form", function(event) | |
| 678 table.insert(event.form, { | |
| 679 name = 'muc#roomconfig_moderatedroom', | 634 name = 'muc#roomconfig_moderatedroom', |
| 680 type = 'boolean', | 635 type = 'boolean', |
| 681 label = 'Make Room Moderated?', | 636 label = 'Make Room Moderated?', |
| 682 value = event.room:get_moderated() | 637 value = event.room:get_moderated() |
| 683 }); | 638 }); |
| 726 module:hook("muc-config-submitted", function(event) | 681 module:hook("muc-config-submitted", function(event) |
| 727 event.update_option("moderated", "muc#roomconfig_moderatedroom"); | 682 event.update_option("moderated", "muc#roomconfig_moderatedroom"); |
| 728 end); | 683 end); |
| 729 module:hook("muc-config-submitted", function(event) | 684 module:hook("muc-config-submitted", function(event) |
| 730 event.update_option("public", "muc#roomconfig_publicroom"); | 685 event.update_option("public", "muc#roomconfig_publicroom"); |
| 731 end); | |
| 732 module:hook("muc-config-submitted", function(event) | |
| 733 event.update_option("changesubject", "muc#roomconfig_changesubject"); | |
| 734 end); | 686 end); |
| 735 | 687 |
| 736 -- Removes everyone from the room | 688 -- Removes everyone from the room |
| 737 function room_mt:clear(x) | 689 function room_mt:clear(x) |
| 738 x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'}); | 690 x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'}); |
| 875 else | 827 else |
| 876 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | 828 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
| 877 return true; | 829 return true; |
| 878 end | 830 end |
| 879 end | 831 end |
| 880 | |
| 881 module:hook("muc-subject-change", function(event) | |
| 882 local room, stanza = event.room, event.stanza; | |
| 883 local occupant = room:get_occupant_by_real_jid(stanza.attr.from); | |
| 884 if occupant.role == "moderator" or | |
| 885 ( occupant.role == "participant" and room:get_changesubject() ) then -- and participant | |
| 886 local subject = stanza:get_child_text("subject"); | |
| 887 room:set_subject(occupant.nick, subject); | |
| 888 else | |
| 889 event.origin.send(st.error_reply(stanza, "auth", "forbidden")); | |
| 890 end | |
| 891 return true; | |
| 892 end); | |
| 893 | 832 |
| 894 function room_mt:handle_groupchat_to_room(origin, stanza) | 833 function room_mt:handle_groupchat_to_room(origin, stanza) |
| 895 -- Prosody has made the decision that messages with <subject/> are exclusively subject changes | 834 -- Prosody has made the decision that messages with <subject/> are exclusively subject changes |
| 896 -- e.g. body will be ignored; even if the subject change was not allowed | 835 -- e.g. body will be ignored; even if the subject change was not allowed |
| 897 if stanza:get_child("subject") then | 836 if stanza:get_child("subject") then |
| 1202 | 1141 |
| 1203 local persistent = module:require "muc/persistent"; | 1142 local persistent = module:require "muc/persistent"; |
| 1204 room_mt.get_persistent = persistent.get; | 1143 room_mt.get_persistent = persistent.get; |
| 1205 room_mt.set_persistent = persistent.set; | 1144 room_mt.set_persistent = persistent.set; |
| 1206 | 1145 |
| 1146 local subject = module:require "muc/subject"; | |
| 1147 room_mt.get_changesubject = subject.get_changesubject; | |
| 1148 room_mt.set_changesubject = subject.set_changesubject; | |
| 1149 room_mt.get_subject = subject.get; | |
| 1150 room_mt.set_subject = subject.set; | |
| 1151 room_mt.send_subject = subject.send; | |
| 1152 | |
| 1207 local history = module:require "muc/history"; | 1153 local history = module:require "muc/history"; |
| 1208 room_mt.send_history = history.send; | 1154 room_mt.send_history = history.send; |
| 1209 room_mt.get_historylength = history.get_length; | 1155 room_mt.get_historylength = history.get_length; |
| 1210 room_mt.set_historylength = history.set_length; | 1156 room_mt.set_historylength = history.set_length; |
| 1211 | 1157 |