001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 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.filter;
021    
022    import java.util.*;
023    import java.util.logging.*;
024    
025    import net.jetrix.Message;
026    import net.jetrix.messages.channel.CommandMessage;
027    import net.jetrix.commands.*;
028    
029    /**
030     * A filter executing a specific command. This filter is useful to add
031     * a command to a specific channel. For example:
032     * <p/>
033     * <pre>
034     * &lt;channel name="duel">
035     *   &lt;filters>
036     *     &lt;filter name="command">
037     *       &lt;param name="class" value="net.jetrix.command.ModeCommand">
038     *     &lt;/filter>
039     *   &lt;/filters>
040     * &lt;/channel>
041     * </pre>
042     *
043     * @author Emmanuel Bourg
044     * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
045     */
046    public class CommandFilter extends GenericFilter
047    {
048        private Command command;
049    
050        public void init()
051        {
052            String cls = config.getString("class", null);
053            try
054            {
055                command = (Command) Class.forName(cls).newInstance();
056            }
057            catch (Exception e)
058            {
059                log.log(Level.WARNING, e.getMessage(), e);
060            }
061        }
062    
063        public void onMessage(CommandMessage m, List<Message> out)
064        {
065            for (String alias : command.getAliases())
066            {
067                if (m.getCommand().equals(alias))
068                {
069                    CommandManager.getInstance().execute(m, command);
070                    return;
071                }
072            }
073    
074            out.add(m);
075        }
076    
077        public String getName()
078        {
079            return "Command Filter";
080        }
081    
082        public String getDescription()
083        {
084            return "Enable a command locally";
085        }
086    
087        public String getVersion()
088        {
089            return "1.0";
090        }
091    
092        public String getAuthor()
093        {
094            return "Emmanuel Bourg";
095        }
096    
097    }