001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2001-2003  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.protocols;
021    
022    import net.jetrix.*;
023    import net.jetrix.messages.*;
024    import net.jetrix.messages.channel.*;
025    
026    import java.util.*;
027    
028    /**
029     * An abstract protocol to communicate with a client.
030     *
031     * @author Emmanuel Bourg
032     * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
033     */
034    public class TspecProtocol extends TetrinetProtocol
035    {
036        /**
037         * Return the name of this protocol
038         */
039        public String getName()
040        {
041            return "tspec";
042        }
043    
044        /**
045         * Parse the specified string and return the corresponding server
046         * message for this protocol.
047         */
048        public Message getMessage(String line)
049        {
050            Message message = null;
051    
052            if (line.startsWith("pline") && !line.startsWith("plineact"))
053            {
054                // pline message with the tetrix mode
055                
056                SmsgMessage smsg = new SmsgMessage();
057                smsg.setSlot(Integer.parseInt(line.substring(6, 7)));
058    
059                if (line.indexOf("//") == 8)
060                {
061                    // public comment
062                    smsg.setPrivate(false);
063                    if (line.length() > 11)
064                    {
065                        smsg.setText(line.substring(11));
066                    }
067    
068                    message = smsg;
069                }
070                else if (line.indexOf("/") != 8)
071                {
072                    // private comment
073                    smsg.setPrivate(true);
074                    if (line.length() > 8)
075                    {
076                        smsg.setText(line.substring(8));
077                    }
078    
079                    message = smsg;
080                }
081            }
082            else if (line.startsWith("/pline"))
083            {
084                // pline message with the tserv mode
085                SmsgMessage smsg = new SmsgMessage();
086                smsg.setText(line.substring("/pline".length()));
087                smsg.setPrivate(false);
088    
089                message = smsg;
090            }
091            else if (line.startsWith("/"))
092            {
093                // direct command in tserv mode
094                line = "pline 1 " + line;
095            }
096            else if (line.startsWith("<") && line.contains(">"))
097            {
098                // private comment in tserv mode
099                SmsgMessage smsg = new SmsgMessage();
100                smsg.setText(line.substring(line.indexOf(">") + 1).trim());
101                smsg.setPrivate(true);
102    
103                message = smsg;
104            }
105            else if (line.startsWith("speclist"))
106            {
107                SpectatorListMessage slist = new SpectatorListMessage();
108                String[] tokens = line.split(" ");
109                slist.setChannel(tokens[1].substring(1));
110                slist.setSpectators(Arrays.asList(tokens).subList(2, tokens.length));
111    
112                message = slist;
113            }
114    
115            return message != null ? message : super.getMessage(line);
116        }
117    
118        /**
119         * Translate the specified message into a string that will be sent
120         * to a client using this protocol.
121         */
122        public String translate(Message m, Locale locale)
123        {
124            if (m instanceof SpectatorListMessage) { return translate((SpectatorListMessage) m, locale); }
125            if (m instanceof SmsgMessage) { return translate((SmsgMessage) m, locale); }
126            else
127            {
128                return super.translate(m, locale);
129            }
130        }
131    
132        public String translate(SpectatorListMessage m, Locale locale)
133        {
134            StringBuilder message = new StringBuilder();
135            message.append("speclist #");
136            message.append(m.getChannel());
137    
138            for (String spectator : m.getSpectators())
139            {
140                message.append(" ");
141                message.append(spectator);
142            }
143            
144            return message.toString();
145        }
146    
147        public String translate(SmsgMessage m, Locale locale)
148        {
149            StringBuilder message = new StringBuilder();
150            String name = ((Client) m.getSource()).getUser().getName();
151    
152            if (m.isPrivate())
153            {
154                message.append("smsg ");
155                message.append(name);
156                message.append(" ");
157                message.append(m.getText());
158            }
159            else
160            {
161                message.append(super.translate(m, locale));
162            }
163    
164            return message.toString();
165        }
166    
167        public String translate(JoinMessage m, Locale locale)
168        {
169            if (m.getSlot() == 0)
170            {
171                StringBuilder message = new StringBuilder();
172                message.append("specjoin ");
173                message.append(m.getName());
174                return message.toString();
175            }
176            else
177            {
178                return super.translate(m, locale);
179            }
180        }
181    
182        public String translate(LeaveMessage m, Locale locale)
183        {
184            if (m.getSlot() == 0)
185            {
186                StringBuilder message = new StringBuilder();
187                message.append("specleave ");
188                message.append(m.getName());
189                return message.toString();
190            }
191            else
192            {
193                return super.translate(m, locale);
194            }
195        }
196    
197    }