Problem with chat filter

Find the proper support area, Saga-Version.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
Bara
Posts: 2
Joined: Sun Mar 29, 2009 8:16 am

Problem with chat filter

Post by Bara »

L2J Server Version: 2943
L2J Datapack Version: 5990
I add ChatFilter.java file to net/sf/l2j/gameserver/handler in l2 core with this code:

Code: Select all

/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. * * http://www.gnu.org/copyleft/gpl.html */ package net.sf.l2j.gameserver.handler; import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.List;import java.util.logging.Logger; import javolution.util.FastList;import net.sf.l2j.Config;import net.sf.l2j.L2DatabaseFactory;import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance; /** * @author Forsaiken */ public class ChatFilter{    private static Logger _log = Logger.getLogger(ChatFilter.class.getName());    private static ChatFilter _instance;        public static ChatFilter getInstance()    {        if (_instance == null)            _instance = new ChatFilter();        return _instance;    }        private List<String> _filterList;        private ChatFilter()    {        loadFilterList();    }        public void loadFilterList()    {        _filterList = new FastList<String>();        java.sql.Connection con = null;        try        {            con = L2DatabaseFactory.getInstance().getConnection();            PreparedStatement statement = con.prepareStatement("SELECT word FROM sayfilter");            ResultSet rset = statement.executeQuery();            while (rset.next())            {                _filterList.add(rset.getString("word").toLowerCase());            }        }        catch (Exception e)        {            _log.warning("Error loading Sayfilter from DB!");        }        finally         {            try            {                con.close();            }            catch(Exception e) {}        }                _log.info("Loaded "+_filterList.size()+" words!");    }        public List<String> getFilterList()    {        return _filterList;    }        public void addWordToFilterList(L2PcInstance activeChar, String word)    {        String tmp = word.toLowerCase();        if (_filterList.contains(tmp))        {            activeChar.sendMessage("Word: '" +tmp+ "' allready exists in table!");            return;        }                if (!_filterList.add(tmp))        {            activeChar.sendMessage("Error adding word: "+tmp+"!");            return;        }                try        {            java.sql.Connection con = L2DatabaseFactory.getInstance().getConnection();            PreparedStatement statement = con.prepareStatement("INSERT INTO sayfilter VALUES (?)");            statement.setString(1, tmp);            statement.execute();            statement.close();            activeChar.sendMessage("Word: "+tmp+" sussecfully added!");        }        catch (Exception e)        {            activeChar.sendMessage("Error adding to DB, word: "+tmp+"!");        }    }        public void deleteWordFromFilterList(L2PcInstance activeChar, String word)    {        String tmp = word.toLowerCase();        if (!_filterList.contains(tmp))        {            activeChar.sendMessage("Word: " +tmp+ " doesen`t exists in table!");            return;        }                if (!_filterList.remove(word))        {            activeChar.sendMessage("Error deleting word: "+tmp+"!");            return;        }                try        {            java.sql.Connection con = L2DatabaseFactory.getInstance().getConnection();            PreparedStatement statement = con.prepareStatement("DELETE FROM sayfilter WHERE word = ?");            statement.setString(1, tmp);            statement.execute();            statement.close();            activeChar.sendMessage("Word: "+tmp+" sussecfully removed!");        }        catch (Exception e)        {            activeChar.sendMessage("Error deleting from DB, word: "+tmp+"!");        }    }        public final String filterText(L2PcInstance activeChar, final String text, final int type)    {        if (!Config.ENABLE_CHAT_FILTER)            return text;                for (final int chatType : Config.FILTERED_CHATS)        {            if (chatType == type)            {                int filteredWords = 0;                String lowerCase = text.toLowerCase();                StringBuilder filtered = new StringBuilder(text);                for (String word : _filterList)                {                    while (lowerCase.contains(word))                    {                        filteredWords++;                        int firstIndex = lowerCase.indexOf(word);                        filtered = filtered.replace(firstIndex, firstIndex + word.length(), "omg");                        lowerCase = filtered.toString().toLowerCase();                    }                }                                if (Config.DEBUG)                    _log.info("Filtered " + filteredWords + "Words, from Character:" + activeChar.getName() + "!");                                return filtered.toString();            }        }                return text;    }}

and add to DP, gameserver\data\scripts\handlers\admincommandhandlers\ file AdminChatFilter.java with this code:

Code: Select all

/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. * * http://www.gnu.org/copyleft/gpl.html */package handlers.admincommandhandlers; import java.util.Collections;import java.util.Comparator;import java.util.List; import net.sf.l2j.gameserver.handler.ChatFilter;import javolution.text.TextBuilder;import javolution.util.FastList;import net.sf.l2j.gameserver.handler.IAdminCommandHandler;import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;  /** * Forsaiken */public final class AdminChatFilter implements IAdminCommandHandler{    private static final String[] ADMIN_COMMANDS =    {        "admin_textfilter", "admin_textfilter_add", "admin_textfilter_remove"    };        public boolean useAdminCommand(String command, L2PcInstance activeChar)    {        if (command.equalsIgnoreCase("admin_textfilter"))            showWordList(activeChar, 0, "ALL");        else if (command.startsWith("admin_textfilter "))        {            String[] params = command.split(" ");            if (params.length != 3)                return false;            showWordList(activeChar, Integer.valueOf(params[1]), params[2]);        }        else if (command.startsWith("admin_textfilter_add "))        {            String[] params = command.split(" ");            if (params.length < 2)                return false;            TextBuilder word = new TextBuilder(params[1]);            if (params.length > 2)            {                for (int i = 2; i < params.length; i++)                {                    word.append(" ").append(params[i]);                }            }            ChatFilter.getInstance().addWordToFilterList(activeChar, word.toString());            showWordList(activeChar, 0, "ALL");        }        else if (command.startsWith("admin_textfilter_delete "))        {            String[] params = command.split(" ");            if (params.length < 2)                return false;            TextBuilder word = new TextBuilder(params[1]);            if (params.length > 2)            {                for (int i = 2; i < params.length; i++)                {                    word.append(" ").append(params[i]);                }            }            ChatFilter.getInstance().deleteWordFromFilterList(activeChar, word.toString());            showWordList(activeChar, 0, "ALL");        }        return true;    }        private List<String> getWordsByFirstLetter(String letter)    {        List<String> sorted = new FastList<String>();        for (String word : ChatFilter.getInstance().getFilterList())        {            if (word.startsWith(letter))                sorted.add(word);        }        return sorted;    }        public static class WordComparator implements Comparator<String>    {        public int compare(String s1, String s2)        {            return s1.compareTo(s2);        }    }        public void showWordList(L2PcInstance activeChar, int page, String letter)    {        List<String> list = null;        if (!letter.equals("ALL"))            list = getWordsByFirstLetter(letter);        else            list = ChatFilter.getInstance().getFilterList();                Collections.sort(list, new WordComparator());                int MaxCharactersPerPage = 10;        int MaxPages = list.size() / MaxCharactersPerPage;        if (list.size() > MaxCharactersPerPage * MaxPages)            MaxPages++;        if (page > MaxPages)            page = MaxPages;        int ListStart = MaxCharactersPerPage * page;        int ListEnd = list.size();        if (ListEnd - ListStart > MaxCharactersPerPage)            ListEnd = ListStart + MaxCharactersPerPage;                TextBuilder replyMSG = new TextBuilder("<html><body><table>");        if (!letter.equals("ALL"))        {            replyMSG.append("<tr><td>Showing only: ").append(letter).append("</td></tr>");        }        replyMSG.append("<tr><td>Show only: <combobox width=90 var=letter list=\"ALL;A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z\"></td></tr>");        replyMSG.append("<tr><td><a action=\"bypass -h admin_textfilter").append(page).append(" $letter\">SHOW</a></td></tr>");        replyMSG.append("<tr><td>Page: ");        for (int x = 0; x < MaxPages; x++)        {            replyMSG.append("<a action=\"bypass -h admin_textfilter").append(x).append(" ").append(letter).append("\">").append(x + 1).append("</a>");            replyMSG.append(", ");        }        replyMSG.append("</td></tr><tr><td><table width=270><tr><td width=200>word:</td><td width=70>delete:</td></tr>");        for (int i = ListStart; i < ListEnd; i++)        {            replyMSG.append("<tr><td width=200>").append(list.get(i)).append("</td><td width=70><a action=\"bypass -h admin_textfilter_delete ").append(list.get(i)).append("\">delete</a></td></tr>");        }        replyMSG.append("<tr></tr><tr><td width=200><edit var=\"word\" width=180></td><td width=70><a action=\"bypass -h admin_textfilter_add $word\">add</a></td></tr>");        replyMSG.append("</table></td></tr></table><br>");                NpcHtmlMessage adminReply = new NpcHtmlMessage(5);        adminReply.setHtml(replyMSG.toString());        activeChar.sendPacket(adminReply);    }        public String[] getAdminCommandList()    {        return ADMIN_COMMANDS;    }} 
For file AdminChatFilter.java I add link's in DP gameserver\data\scripts\handlers\MasterHandler.java

Code: Select all

import handlers.admincommandhandlers.AdminChatFilter;
and here

Code: Select all

public class MasterHandler{    private static Logger _log = Logger.getLogger(MasterHandler.class.getName());        private static void loadAdminHandlers()    { AdminCommandHandler.getInstance().registerAdminCommandHandler(new AdminChatFilter());... 
Filtering word work fine, but then I writing coommand (//textfilter)(GM char, this command add to BD tables ) in client nothing happens. But in console of game server I see this error:

Code: Select all

Client: [Character: Driada - Account: driada - IP: 89.254.199.37] - Failed running: [C] 5b SendBypassBuildCmd - L2J Server Version: 2943 - DP Revision: 5990java.lang.NullPointerException        at java.lang.System.arraycopy(Native Method)        at javolution.text.TextBuilder$2.run(TextBuilder.java:1004)        at javax.realtime.MemoryArea.executeInArea(MemoryArea.java:27)        at javolution.text.TextBuilder.increaseCapacity(TextBuilder.java:998)        at javolution.text.TextBuilder.append(TextBuilder.java:349)        at javolution.text.TextBuilder.append(TextBuilder.java:327)        at javolution.text.TextBuilder.<init>(TextBuilder.java:89)        at handlers.admincommandhandlers.AdminChatFilter.showWordList(Unknown Source)        at handlers.admincommandhandlers.AdminChatFilter.useAdminCommand(Unknown Source)        at net.sf.l2j.gameserver.network.clientpackets.SendBypassBuildCmd.runImpl(SendBypassBuildCmd.java:78)        at net.sf.l2j.gameserver.network.clientpackets.L2GameClientPacket.run(L2GameClientPacket.java:76)        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)        at java.lang.Thread.run(Unknown Source) 
Can any help me???? How I can eliminate errors. Sorry for bad English.
_DS_
L2j Veteran
L2j Veteran
Posts: 3437
Joined: Wed Apr 30, 2008 8:53 am
Location: Russia

Re: Problem with chat filter

Post by _DS_ »

Problem is here:

Code: Select all

            replyMSG.append("<tr><td width=200>").append(list.get(i)).append("</td><td width=70><a action=\"bypass -h admin_textfilter_delete ").append(list.get(i)).append("\">delete</a></td></tr>");
Maybe you will need to add one word manually into db or add nullcheck here.
Commiter of the shit
public static final int PI = 3.1415926535897932384626433832795;
Bara
Posts: 2
Joined: Sun Mar 29, 2009 8:16 am

Re: Problem with chat filter

Post by Bara »

I add 3 word manually and replace of this word work fine. But command //textfilter will return some interface in client to edit tables of this BD.
Sry, I'm not good understand this part of code.
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7005
Joined: Tue Aug 11, 2009 3:36 am

Re: Problem with chat filter

Post by Zoey76 »

Moved to viewforum.php?f=82, viewforum.php?f=69 is for contributions not reports!
Powered by Eclipse 4.30 🌌 | Eclipse Temurin 21 ☕ | MariaDB 11.2.2 🗃️ | L2J Server 2.6.3.0 - High Five 🚀

🔗 Join our Discord! 🎮💬
Post Reply