added:
[mir.git] / source / mircoders / global / Abuse.java
index 02a111c..697d6e2 100755 (executable)
@@ -34,26 +34,40 @@ import mir.config.MirPropertiesConfiguration;
 import mir.entity.Entity;
 import mir.entity.adapter.EntityAdapterModel;
 import mir.log.LoggerWrapper;
+import mir.module.EntityNotFoundExc;
 import mir.session.Request;
-import mir.util.DateTimeFunctions;
+import mir.util.DateTimeRoutines;
 import mir.util.EntityUtility;
 import mir.util.GeneratorFormatAdapters;
-import mir.util.StringRoutines;
 import mircoders.abuse.FilterEngine;
 import mircoders.entity.EntityComment;
 import mircoders.entity.EntityContent;
 import mircoders.localizer.MirAdminInterfaceLocalizer;
+import mircoders.module.ModuleComment;
+import mircoders.module.ModuleContent;
 import org.apache.commons.collections.ExtendedProperties;
 
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletResponse;
 import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
-import java.util.*;
-
-
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ *  This class manages abuse (spam, offending material, etc.). This
+ *  is done by using a set of filters managed by the FilterEngine class.
+ *  Filters may be of different types (IP, throttle, regexp...), 
+ *  but are created and configured in a single user interface (web page),
+ *  and are stored in a single database table called "filter". 
+ */
 public class Abuse {
   private LoggerWrapper logger;
   private int logSize;
@@ -63,7 +77,7 @@ public class Abuse {
   private boolean cookieOnBlock;
   private String articleBlockAction;
   private String commentBlockAction;
-  private List log;
+  private final List log = new ArrayList();
   private File configFile = MirGlobal.config().getFile("Abuse.Config");
   private FilterEngine filterEngine;
 
@@ -71,12 +85,12 @@ public class Abuse {
 
   private static String cookieName = MirGlobal.config().getString("Abuse.CookieName");
   private static int cookieMaxAge = 60 * 60 * MirGlobal.config().getInt("Abuse.CookieMaxAge");
+  private EntityAdapterModel model;
 
   public Abuse(EntityAdapterModel aModel) {
     logger = new LoggerWrapper("Global.Abuse");
     filterEngine = new FilterEngine(aModel);
-
-    log = new ArrayList();
+    model = aModel;
 
     try {
       configuration = MirPropertiesConfiguration.instance();
@@ -127,7 +141,11 @@ public class Abuse {
 
     return false;
   }
-
+  /** Checks if there is a filter that matches a comment and takes 
+   * appropriate action (as configured in the xxxxxaction field of 
+   * the filter table). The actual matching is delegated to the 
+   * FilterEngine class. 
+   */
   public void checkComment(EntityComment aComment, Request aRequest, HttpServletResponse aResponse) {
     try {
       long time = System.currentTimeMillis();
@@ -140,7 +158,7 @@ public class Abuse {
 
         StringBuffer line = new StringBuffer();
 
-        line.append(DateTimeFunctions.advancedDateFormat(
+        line.append(DateTimeRoutines.advancedDateFormat(
             configuration.getString("Mir.DefaultDateTimeFormat"),
             (new GregorianCalendar()).getTime(), configuration.getString("Mir.DefaultTimezone")));
 
@@ -163,7 +181,11 @@ public class Abuse {
       logger.error("Exception thrown while checking comment", t);
     }
   }
-
+  /** Checks if there is a filter that matches an articleand takes 
+   * appropriate action (as configured in the xxxxxaction field of 
+   * the filter table). The actual matching is delegated to the 
+   * FilterEngine class. 
+   */
   public void checkArticle(EntityContent anArticle, Request aRequest, HttpServletResponse aResponse) {
     try {
       long time = System.currentTimeMillis();
@@ -176,7 +198,7 @@ public class Abuse {
 
         StringBuffer line = new StringBuffer();
 
-        line.append(DateTimeFunctions.advancedDateFormat(
+        line.append(DateTimeRoutines.advancedDateFormat(
             configuration.getString("Mir.DefaultDateTimeFormat"),
             (new GregorianCalendar()).getTime(), configuration.getString("Mir.DefaultTimezone")));
 
@@ -260,33 +282,49 @@ public class Abuse {
   }
 
   public List getLog() {
+    ModuleContent contentModule = new ModuleContent();
+    ModuleComment commentModule = new ModuleComment();
+
     synchronized (log) {
-      try {
-        List result = new ArrayList();
-
-        Iterator i = log.iterator();
-        while (i.hasNext()) {
-          LogEntry logEntry = (LogEntry) i.next();
-          Map entry = new HashMap();
-
-          entry.put("ip", logEntry.getIpNumber());
-          entry.put("id", logEntry.getId());
-          entry.put("timestamp", new GeneratorFormatAdapters.DateFormatAdapter(logEntry.getTimeStamp(), MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone")));
-          if (logEntry.getIsArticle())
-            entry.put("type", "content");
-          else
-            entry.put("type", "comment");
-          entry.put("browser", logEntry.getBrowserString());
-          entry.put("filtertag", logEntry.getMatchingFilterTag());
-
-          result.add(entry);
+      List result = new ArrayList();
+
+      Iterator i = log.iterator();
+      while (i.hasNext()) {
+        LogEntry logEntry = (LogEntry) i.next();
+        Map entry = new HashMap();
+
+        entry.put("ip", logEntry.getIpNumber());
+        entry.put("id", logEntry.getId());
+        entry.put("timestamp", new GeneratorFormatAdapters.DateFormatAdapter(logEntry.getTimeStamp(), MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone")));
+
+        if (logEntry.getIsArticle()) {
+          entry.put("type", "content");
+          try {
+            entry.put("object",
+                model.makeEntityAdapter("content", contentModule.getById(logEntry.getId())));
+          }
+          catch (EntityNotFoundExc e) {
+            entry.put("object", null);
+          }
+        }
+        else {
+          entry.put("type", "comment");
+          try {
+            entry.put("object",
+                model.makeEntityAdapter("comment", commentModule.getById(logEntry.getId())));
+          }
+          catch (EntityNotFoundExc e) {
+            entry.put("object", null);
+          }
         }
 
-        return result;
-      }
-      catch (Throwable t) {
-        throw new RuntimeException(t.toString());
+        entry.put("browser", logEntry.getBrowserString());
+        entry.put("filtertag", logEntry.getMatchingFilterTag());
+
+        result.add(entry);
       }
+
+      return result;
     }
   }
 
@@ -324,13 +362,7 @@ public class Abuse {
 
   public synchronized void load() {
     try {
-      ExtendedProperties configuration = new ExtendedProperties();
-
-      try {
-        configuration = new ExtendedProperties(configFile.getAbsolutePath());
-      }
-      catch (FileNotFoundException e) {
-      }
+      ExtendedProperties configuration = new ExtendedProperties(configFile.getAbsolutePath());
 
       setOpenPostingDisabled(configuration.getString("abuse.openPostingDisabled", "0").equals("1"));
       setOpenPostingPassword(configuration.getString("abuse.openPostingPassword", "0").equals("1"));
@@ -370,8 +402,8 @@ public class Abuse {
 
       Iterator i = MirGlobal.localizer().adminInterface().simpleArticleOperations().iterator();
       while (i.hasNext()) {
-        MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation =
-            (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
+        MirAdminInterfaceLocalizer.EntityOperation operation =
+            (MirAdminInterfaceLocalizer.EntityOperation) i.next();
 
         Map action = new HashMap();
         action.put("resource", operation.getName());
@@ -393,8 +425,8 @@ public class Abuse {
 
       Iterator i = MirGlobal.localizer().adminInterface().simpleCommentOperations().iterator();
       while (i.hasNext()) {
-        MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation =
-            (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
+        MirAdminInterfaceLocalizer.EntityOperation operation =
+            (MirAdminInterfaceLocalizer.EntityOperation) i.next();
 
         Map action = new HashMap();
         action.put("resource", operation.getName());
@@ -410,25 +442,6 @@ public class Abuse {
     }
   }
 
-  private String escapeConfigListEntry(String aFilterPart) {
-    return StringRoutines.replaceStringCharacters(aFilterPart,
-        new char[] {'\\', ':'},
-        new String[] {"\\\\", "\\:"});
-  }
-
-  private String escapeFilterPart(String aFilterPart) {
-    return StringRoutines.replaceStringCharacters(aFilterPart,
-        new char[] {'\\', '\n', '\r', '\t', ' '},
-        new String[] {"\\\\", "\\n", "\\r", "\\t", "\\ "});
-  }
-
-  private String deescapeFilterPart(String aFilterPart) {
-    return StringRoutines.replaceEscapedStringCharacters(aFilterPart,
-        '\\',
-        new char[] {'\\', ':', 'n', 'r', 't', ' '},
-        new String[] {"\\", ":", "\n", "\r", "\t", " "});
-  }
-
   private static class LogEntry {
     private String ipNumber;
     private String browserString;
@@ -485,7 +498,7 @@ public class Abuse {
         }
       }
     }
-  };
+  }
 
   private void appendLog(LogEntry anEntry) {
     synchronized (log) {