001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2001-2005  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.filter;
021    
022    import java.util.*;
023    
024    import net.jetrix.*;
025    import net.jetrix.messages.channel.*;
026    
027    /**
028     * Display the ping of the player if a <tt>team</tt> message is processed
029     * and the player property <tt>command.ping</tt> is set to true. This filter
030     * also intercepts in game messages containing only "t" and send back a "PONG"
031     * message to the player.
032     *
033     * @author Emmanuel Bourg
034     * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
035     */
036    public class PingFilter extends MessageFilter
037    {
038        public final void process(Message m, List<Message> out)
039        {
040            if (m instanceof TeamMessage)
041            {
042                Client client = (Client) m.getSource();
043                User user = client.getUser();
044    
045                if ("true".equals((user.getProperty("command.ping"))))
046                {
047                    long delay = (System.currentTimeMillis() - ((Long) user.getProperty("command.ping.time")).longValue());
048    
049                    client.send(new PlineMessage("command.ping.message", delay));
050    
051                    user.setProperty("command.ping", "false");
052                }
053                else
054                {
055                    out.add(m);
056                }
057            }
058            else if (m instanceof GmsgMessage && m.getSource() != null)
059            {
060                // send back "* PONG" to the player if he just wrote "t"
061                GmsgMessage gmsg = (GmsgMessage) m;
062                Client client = (Client) m.getSource();
063    
064                String text = "<" + client.getUser().getName() + "> t";
065    
066                if (text.equals(gmsg.getText()))
067                {
068                    client.send(new GmsgMessage("* PONG"));
069                }
070                else
071                {
072                    out.add(m);
073                }
074            }
075            else
076            {
077                out.add(m);
078            }
079        }
080    
081        public String getName()
082        {
083            return "Ping";
084        }
085    
086        public String getDescription()
087        {
088            return "Display the ping time.";
089        }
090    
091        public String getVersion()
092        {
093            return "1.1";
094        }
095    
096        public String getAuthor()
097        {
098            return "Emmanuel Bourg";
099        }
100    
101    }