View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2001-2004  Emmanuel Bourg
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package net.jetrix.commands;
21  
22  import java.util.*;
23  
24  import net.jetrix.*;
25  import net.jetrix.config.*;
26  import net.jetrix.messages.*;
27  import net.jetrix.messages.channel.CommandMessage;
28  import net.jetrix.messages.channel.PlineMessage;
29  
30  /***
31   * Join or create a channel.
32   *
33   * @author Emmanuel Bourg
34   * @version $Revision: 860 $, $Date: 2010-05-06 13:21:05 +0200 (jeu., 06 mai 2010) $
35   */
36  public class JoinCommand extends AbstractCommand implements ParameterCommand
37  {
38      public String[] getAliases()
39      {
40          return (new String[]{"join", "j"});
41      }
42  
43      public String getUsage(Locale locale)
44      {
45          return "/join <" + Language.getText("command.params.channel_name_num", locale) + ">"
46                  + " <" + Language.getText("command.params.password", locale) + ">";
47      }
48  
49      public int getParameterCount()
50      {
51          return 1;
52      }
53  
54      public void execute(CommandMessage m)
55      {
56          Client client = (Client) m.getSource();
57  
58          Channel channel = m.getChannelParameter(0);
59  
60          // get the password
61          String password = null;
62          if (m.getParameterCount() >= 2)
63          {
64              password = m.getParameter(1);
65          }
66  
67          // the specified channel was found ?
68          if (channel == null)
69          {
70              // no, let's create it if the message comes from an operator
71              if (client.getUser().getAccessLevel() >= AccessLevel.OPERATOR)
72              {
73                  // create the channel
74                  ChannelConfig config = new ChannelConfig();
75                  config.setSettings(new Settings());
76                  config.setName(m.getParameter(0).replaceFirst("#", "")); // todo reject empty names
77                  config.setDescription("");
78                  channel = ChannelManager.getInstance().createChannel(config);
79  
80                  PlineMessage response = new PlineMessage();
81                  response.setKey("command.join.created", m.getParameter(0));
82                  client.send(response);
83              }
84              else
85              {
86                  // unknown channel
87                  PlineMessage response = new PlineMessage();
88                  response.setKey("command.join.unknown", m.getParameter(0));
89                  client.send(response);
90              }
91  
92          }
93  
94          if (channel != null)
95          {
96              ChannelConfig channelConfig = channel.getConfig(); // NPE
97  
98              if (client.getUser().getAccessLevel() < channelConfig.getAccessLevel())
99              {
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 }