CreateMUCRoom.java 6.22 KB
Newer Older
Alex Wenckus's avatar
Alex Wenckus committed
1
/**
2 3 4
 * $RCSfile$
 * $Revision: 3144 $
 * $Date: 2005-12-01 14:20:11 -0300 (Thu, 01 Dec 2005) $
Alex Wenckus's avatar
Alex Wenckus committed
5
 *
6 7
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Alex Wenckus's avatar
Alex Wenckus committed
19
 */
20
package org.jivesoftware.openfire.commands.admin.muc;
Alex Wenckus's avatar
Alex Wenckus committed
21

22 23
import org.dom4j.Element;
import org.jivesoftware.openfire.XMPPServer;
24 25
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
26
import org.jivesoftware.openfire.muc.MUCRoom;
27
import org.jivesoftware.openfire.muc.MultiUserChatService;
28
import org.jivesoftware.openfire.muc.NotAllowedException;
Alex Wenckus's avatar
Alex Wenckus committed
29 30
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
31
import org.xmpp.packet.JID;
Alex Wenckus's avatar
Alex Wenckus committed
32

33 34
import java.util.Arrays;
import java.util.Collection;
Alex Wenckus's avatar
Alex Wenckus committed
35 36 37 38 39 40 41 42 43
import java.util.List;
import java.util.Map;

/**
 * Allows via AdHoc commands the creation of a Multi-User Chat room.
 *
 * @author Alexander Wenckus
 */
public class CreateMUCRoom extends AdHocCommand {
44 45
    @Override
	public String getCode() {
Alex Wenckus's avatar
Alex Wenckus committed
46 47 48
        return "http://jabber.org/protocol/admin#create-muc-room";
    }

49 50
    @Override
	public String getDefaultLabel() {
Alex Wenckus's avatar
Alex Wenckus committed
51 52 53
        return "Create a Multi-user Chat";
    }

54 55
    @Override
	public int getMaxStages(SessionData data) {
Alex Wenckus's avatar
Alex Wenckus committed
56 57 58
        return 1;
    }

59 60
    @Override
	public void execute(SessionData sessionData, Element command) {
Alex Wenckus's avatar
Alex Wenckus committed
61 62 63 64 65 66 67 68 69
        Element note = command.addElement("note");
        Collection<JID> admins = XMPPServer.getInstance().getAdmins();
        if (admins.size() <= 0) {
            note.addAttribute("type", "error");
            note.setText("Server needs admin user to be able to create rooms.");
            return;
        }
        Map<String, List<String>> data = sessionData.getData();

70 71 72 73 74 75 76 77 78 79
        // Let's find the requested MUC service to create the room in
        String servicehostname = get(data, "servicename", 0);
        if (servicehostname == null) {
            note.addAttribute("type", "error");
            note.setText("Service name must be specified.");
            return;
        }
        // Remove the server's domain name from the passed hostname
        String servicename = servicehostname.replace("."+XMPPServer.getInstance().getServerInfo().getXMPPDomain(), "");
        MultiUserChatService mucService;
80 81
        mucService = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(servicename);
        if (mucService == null) {
82 83 84 85
            note.addAttribute("type", "error");
            note.setText("Invalid service name specified.");
            return;
        }
86 87 88 89 90
        if (!mucService.isServiceEnabled()) {
            note.addAttribute("type", "error");
            note.setText("Multi user chat is disabled for specified service.");
            return;
        }
Alex Wenckus's avatar
Alex Wenckus committed
91 92 93 94 95 96 97 98 99 100
        // Let's create the jid and check that they are a local user
        String roomname = get(data, "roomname", 0);
        if (roomname == null) {
            note.addAttribute("type", "error");
            note.setText("Room name must be specified.");
            return;
        }
        JID admin = admins.iterator().next();
        MUCRoom room;
        try {
101
            room = mucService.getChatRoom(roomname, admin);
Alex Wenckus's avatar
Alex Wenckus committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        }
        catch (NotAllowedException e) {
            note.addAttribute("type", "error");
            note.setText("No permission to create rooms.");
            return;
        }

        boolean isPersistent = "1".equals(get(data, "persistent", 0));
        room.setPersistent(isPersistent);

        boolean isPublic = "1".equals(get(data, "public", 0));
        room.setPublicRoom(isPublic);

        String password = get(data, "password", 0);
        if (password != null) {
            room.setPassword(password);
        }
    }

121 122
    @Override
	protected void addStageInformation(SessionData data, Element command) {
Alex Wenckus's avatar
Alex Wenckus committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        DataForm form = new DataForm(DataForm.Type.form);
        form.setTitle("Create a multi-user chat room");
        form.addInstruction("Fill out this form to create a multi-user chat room.");

        FormField field = form.addField();
        field.setType(FormField.Type.hidden);
        field.setVariable("FORM_TYPE");
        field.addValue("http://jabber.org/protocol/admin");

        field = form.addField();
        field.setType(FormField.Type.text_single);
        field.setLabel("The name of the room");
        field.setVariable("roomname");
        field.setRequired(true);

138 139 140 141 142 143
        field = form.addField();
        field.setType(FormField.Type.text_single);
        field.setLabel("The service (hostname) to create the room on");
        field.setVariable("servicename");
        field.setRequired(true);

Alex Wenckus's avatar
Alex Wenckus committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        field = form.addField();
        field.setType(FormField.Type.text_private);
        field.setLabel("The password for this account");
        field.setVariable("password");

        field = form.addField();
        field.setType(FormField.Type.text_private);
        field.setLabel("Retype password");
        field.setVariable("password-verify");

        field = form.addField();
        field.setType(FormField.Type.boolean_type);
        field.setLabel("Room is persistent");
        field.setVariable("persistent");

        field = form.addField();
        field.setType(FormField.Type.boolean_type);
        field.setLabel("Is the room public");
        field.setVariable("public");

        // Add the form to the command
        command.add(form.getElement());
    }

168 169
    @Override
	protected List<Action> getActions(SessionData data) {
Alex Wenckus's avatar
Alex Wenckus committed
170 171 172
        return Arrays.asList(AdHocCommand.Action.complete);
    }

173 174
    @Override
	protected Action getExecuteAction(SessionData data) {
Alex Wenckus's avatar
Alex Wenckus committed
175 176 177 178
        return AdHocCommand.Action.complete;
    }

}