001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2001-2004  Emmanuel Bourg
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License
007     * as published by the Free Software Foundation; either version 2
008     * of the License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018     */
019    
020    package net.jetrix.commands;
021    
022    import java.util.*;
023    
024    import net.jetrix.*;
025    import net.jetrix.config.*;
026    import net.jetrix.messages.*;
027    import net.jetrix.messages.channel.CommandMessage;
028    import net.jetrix.messages.channel.PlineMessage;
029    
030    /**
031     * Join or create a channel.
032     *
033     * @author Emmanuel Bourg
034     * @version $Revision: 860 $, $Date: 2010-05-06 13:21:05 +0200 (jeu., 06 mai 2010) $
035     */
036    public class JoinCommand extends AbstractCommand implements ParameterCommand
037    {
038        public String[] getAliases()
039        {
040            return (new String[]{"join", "j"});
041        }
042    
043        public String getUsage(Locale locale)
044        {
045            return "/join <" + Language.getText("command.params.channel_name_num", locale) + ">"
046                    + " <" + Language.getText("command.params.password", locale) + ">";
047        }
048    
049        public int getParameterCount()
050        {
051            return 1;
052        }
053    
054        public void execute(CommandMessage m)
055        {
056            Client client = (Client) m.getSource();
057    
058            Channel channel = m.getChannelParameter(0);
059    
060            // get the password
061            String password = null;
062            if (m.getParameterCount() >= 2)
063            {
064                password = m.getParameter(1);
065            }
066    
067            // the specified channel was found ?
068            if (channel == null)
069            {
070                // no, let's create it if the message comes from an operator
071                if (client.getUser().getAccessLevel() >= AccessLevel.OPERATOR)
072                {
073                    // create the channel
074                    ChannelConfig config = new ChannelConfig();
075                    config.setSettings(new Settings());
076                    config.setName(m.getParameter(0).replaceFirst("#", "")); // todo reject empty names
077                    config.setDescription("");
078                    channel = ChannelManager.getInstance().createChannel(config);
079    
080                    PlineMessage response = new PlineMessage();
081                    response.setKey("command.join.created", m.getParameter(0));
082                    client.send(response);
083                }
084                else
085                {
086                    // unknown channel
087                    PlineMessage response = new PlineMessage();
088                    response.setKey("command.join.unknown", m.getParameter(0));
089                    client.send(response);
090                }
091    
092            }
093    
094            if (channel != null)
095            {
096                ChannelConfig channelConfig = channel.getConfig(); // NPE
097    
098                if (client.getUser().getAccessLevel() < channelConfig.getAccessLevel())
099                {
100                    // deny access
101                    PlineMessage accessDenied = new PlineMessage();
102                    accessDenied.setKey("command.join.denied");
103                    client.send(accessDenied);
104                }
105                else if (channelConfig.isPasswordProtected() && !channelConfig.getPassword().equals(password))
106                {
107                    // wrong password
108                    log.severe(client.getUser().getName() + "(" + client.getInetAddress() + ") "
109                            + "attempted to join the protected channel '" + channelConfig.getName() + "'.");
110                    PlineMessage accessDenied = new PlineMessage();
111                    accessDenied.setKey("command.join.wrong_password");
112                    client.send(accessDenied);
113                }
114                else if (channel.isFull() && client.getUser().isPlayer())
115                {
116                    // sending channel full message
117                    PlineMessage channelfull = new PlineMessage();
118                    channelfull.setKey("command.join.full");
119                    client.send(channelfull);
120                }
121                else if (client.getUser().isPlayer() && !channelConfig.isProtocolAccepted(client.getProtocol().getName()))
122                {
123                    // incompatible speed constraint
124                    String type = channelConfig.getSpeed() == Speed.FAST ? "TetriFast" : "TetriNET";
125                    client.send(new PlineMessage("command.join.speed", type));
126                }
127                else
128                {
129                    // adding the ADDPLAYER message to the queue of the target channel
130                    AddPlayerMessage move = new AddPlayerMessage();
131                    move.setClient((Client) m.getSource());
132                    channel.send(move);
133                }
134            }
135        }
136    
137    }