Commits

Fuco committed 253f0c8

* added admin command prefix
* pulled some common string methods to StringUtils

Comments (0)

Files changed (6)

src/cz/dasnet/dasik/Dasik.java

     }
 
     public String getCommand(String message) {
-        String prefix = config.getProperty("command.prefix", "!");
+        return getCommand(message, false);
+    }
+
+    public String getCommand(String message, boolean admin) {
+        String prefix;
+        if (admin) {
+            prefix = config.getProperty("command.adminPrefix", "@");
+        } else {
+            prefix = config.getProperty("command.prefix", "!");
+        }
+
         if (prefix.contains(message.substring(0, 1))) {
             return message.split("\\s+")[0].substring(1);
         }
     public void onNickChange(String oldNick, String login, String hostname, String newNick) {
         removeNickFromIAL(oldNick);
         sendWhoOnUser(newNick);
-        
+
         for (ChannelEventListener cl : channelListeners.values()) {
             cl.onNickChange(oldNick, login, hostname, newNick, this);
         }

src/cz/dasnet/dasik/plugin/AdminCommand.java

 
     @Override
     public void onMessage(String channel, String sender, String login, String hostname, String message, Dasik bot) {
-        String command = bot.getCommand(message);
+        String command = bot.getCommand(message, true);
         if (command == null) {
             return;
         }

src/cz/dasnet/dasik/plugin/LastAction.java

 import java.util.Date;
 import java.util.List;
 import org.apache.log4j.Logger;
+import static cz.dasnet.dasik.util.StringUtils.shorten;
 
 /**
  * Plugin tracking the last action of a user.
         return u;
     }
 
-    private String shortenMessage(String msg, int length) {
-        if (msg.length() > length) {
-            return msg.substring(0, length) + "... [shortened]";
-        }
-        return msg;
-    }
-
     @Override
     public void onAction(String sender, String login, String hostname, String target, String action, Dasik bot) {
         if (target.startsWith("#")) {
                 String sql = "SELECT DISTINCT * FROM users WHERE lastseen_nick like ?";
                 List<User> users = bot.getUserDao().query(sql, UserDaoImpl.userRowMapper,
                         "%" + token[1] + "%");
-                
+
                 StringBuilder sb = new StringBuilder();
                 for (User u : users) {
                     sb.append(u.getLastseenNick()).append(" on ").
                 if (users.size() >= 1) {
                     sb = sb.delete(sb.length() - 3, sb.length());
                 }
-                
-                String msg = shortenMessage(sb.toString(), 350);
+
+                String msg = shorten(sb.toString(), 350);
                 bot.send(channel, msg);
                 return;
             }
 
         User u = getUser(login + "@" + hostname, bot);
         u.setLastseen(System.currentTimeMillis());
-        u.setLastseenAction("posting on " + channel + ": " + shortenMessage(message, 60));
+        u.setLastseenAction("posting on " + channel + ": " + shorten(message, 60));
         u.setLastseenNick(sender);
         bot.getUserDao().updateUser(u);
     }
     public void onQuit(String sourceNick, String sourceLogin, String sourceHostname, String reason, Dasik bot) {
         User u = getUser(sourceLogin + "@" + sourceHostname, bot);
         u.setLastseen(System.currentTimeMillis());
-        u.setLastseenAction("quitting (" + shortenMessage(reason, 60) + ")");
+        u.setLastseenAction("quitting (" + shorten(reason, 60) + ")");
         u.setLastseenNick(sourceNick);
         bot.getUserDao().updateUser(u);
     }
         User u = getUser(bot.nickToMask(recipientNick), bot);
         u.setLastseen(System.currentTimeMillis());
         u.setLastseenAction("being kicked from " + channel + " by " + kickerNick
-                + " (" + shortenMessage(reason, 60) + ")");
+                + " (" + shorten(reason, 60) + ")");
         u.setLastseenNick(recipientNick);
         bot.getUserDao().updateUser(u);
     }

src/cz/dasnet/dasik/plugin/URLInfo.java

 import org.dom4j.DocumentException;
 import org.dom4j.Node;
 import org.dom4j.io.SAXReader;
+import static cz.dasnet.dasik.util.StringUtils.shorten;
+import static cz.dasnet.dasik.util.StringUtils.decodeEntities;
 
 /**
  * Pull info from posted links.
         return line;
     }
 
-    private String shorten(String msg, int length) {
-        if (msg.length() > length) {
-            return msg.substring(0, length) + "... [shortened]";
-        }
-        return msg;
-    }
-
     private String toMS(String len) {
         int l = Integer.parseInt(len);
         return (l / 60) + "m " + (l % 60) + "s";

src/cz/dasnet/dasik/plugin/WordCounter.java

     }
 
     public String getTop10(Dasik bot, int from) {
-        String sql = "SELECT * FROM users ORDER BY words DESC LIMIT " + from + ", 10";
+        String sql = "SELECT * FROM users WHERE words > 0 ORDER BY words DESC LIMIT " + from + ", 10";
         List<User> users = bot.getUserDao().query(sql, UserDaoImpl.userRowMapper);
 
         StringBuilder sb = new StringBuilder();
-        sb.append("Top spammers [").append(from + 1).append("-").append(from + 11).append("]: ");
+        sb.append("Top spammers [").append(from + 1).append("-").append(from + users.size()).append("]: ");
 
         int i = from + 1;
         for (User u : users) {
     }
 
     public String getTop10Daily(Dasik bot, int from) {
-        String sql = "SELECT * FROM users ORDER BY words_daily DESC LIMIT " + from + ", 10";
+        String sql = "SELECT * FROM users WHERE words_daily > 0 ORDER BY words_daily DESC LIMIT " + from + ", 10";
         List<User> users = bot.getUserDao().query(sql, UserDaoImpl.userRowMapper);
 
         StringBuilder sb = new StringBuilder();
-        sb.append("Today's top spammers [").append(from + 1).append("-").append(from + 11).append("]: ");
+        sb.append("Today's top spammers [").append(from + 1).append("-").append(from + users.size()).append("]: ");
 
         int i = from + 1;
         for (User u : users) {
             bot.describe(channel, getTop10Daily(bot, 0));
             String sql = "UPDATE users SET words_daily = 0";
             int rows = bot.getUserDao().update(sql);
-            log.debug(rows + " affected while reseting daily word stats");
+            log.debug(rows + " users affected while reseting daily word stats");
             bot.getUserDao().invalidateCache();
             u.setWordsDaily(0);
         }

src/cz/dasnet/dasik/util/StringUtils.java

+/*
+ *  Copyright (C) 2011 Matus Goljer
+ *  This file is a part of Project DASik, an IRC bot.
+ *
+ *  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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
+ */
+package cz.dasnet.dasik.util;
+
+/**
+ * A class providing basic string utilities not found in {@code java.lang.String}.
+ *
+ * @author Matus Goljer
+ * @version 1.0
+ */
+public class StringUtils {
+
+    // ensure noninstantiability
+    private StringUtils() {
+    }
+
+    /**
+     * Shorten the given string if it's longer than specified length.
+     *
+     * @param msg Message to be shortened
+     * @param length Max length of the message
+     * @return Shortened string
+     */
+    public static String shorten(String msg, int length) {
+        if (msg.length() > length) {
+            return msg.substring(0, length - 16) + "... [shortened]";
+        }
+        return msg;
+    }
+
+    /**
+     * Decode HTML entities.
+     *
+     * @param line Line to be decoded
+     * @return Decoded line
+     */
+    public static String decodeEntities(String line) {
+        line = line.replace("\n", " ");
+        line = line.replace("&apm;", "&");
+        line = line.replace("&gt;", ">");
+        line = line.replace("&lt;", "<");
+        line = line.replace("&bull;", " - ");
+        line = line.replace("&dash;", " - ");
+        line = line.replace("&mdash;", " - ");
+        line = line.replace("&edash;", " - ");
+        line = line.replace("&#8211;", " - ");
+        line = line.replace("&quot;", "\"");
+        line = line.replace("&#39;", "'");
+        line = line.replace("&apos;", "'");
+        line = line.replace("&#x202a;", "");
+        line = line.replace("&#x202c;", "");
+        line = line.replace("&rlm;", "");
+        line = line.replace("&nbsp;", " ");
+        return line;
+    }
+}