From 7d94f9fb192978fda469bf4391c29bed1a550c2e Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sat, 2 Jun 2012 13:49:14 +0000
Subject: [PATCH] improve opentracker configuration

---
 .../src/org/klomp/snark/I2PSnarkUtil.java     |  8 ++-
 .../src/org/klomp/snark/SnarkManager.java     | 24 +++++---
 .../org/klomp/snark/web/I2PSnarkServlet.java  | 61 +++++++++++++------
 3 files changed, 64 insertions(+), 29 deletions(-)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java b/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java
index 77c10d4028..320b5005a2 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/I2PSnarkUtil.java
@@ -457,14 +457,18 @@ public class I2PSnarkUtil {
         _openTrackerString = ot;
     }
 
+    /** Comma delimited list of open trackers to use as backups
+     *  non-null but possibly empty
+     */
     public String getOpenTrackerString() { 
         if (_openTrackerString == null)
             return DEFAULT_OPENTRACKERS;
         return _openTrackerString;
     }
 
-    /** comma delimited list open trackers to use as backups */
-    /** sorted map of name to announceURL=baseURL */
+    /** List of open trackers to use as backups
+     *  Null if disabled
+     */
     public List<String> getOpenTrackers() { 
         if (!shouldUseOpenTrackers())
             return null;
diff --git a/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java b/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java
index 94e7974563..7c1c421a8c 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java
@@ -365,7 +365,7 @@ public class SnarkManager implements Snark.CompleteListener {
     public void updateConfig(String dataDir, boolean filesPublic, boolean autoStart, String refreshDelay,
                              String startDelay, String seedPct, String eepHost, 
                              String eepPort, String i2cpHost, String i2cpPort, String i2cpOpts,
-                             String upLimit, String upBW, boolean useOpenTrackers, String openTrackers, String theme) {
+                             String upLimit, String upBW, boolean useOpenTrackers, String theme) {
         boolean changed = false;
         //if (eepHost != null) {
         //    // unused, we use socket eepget
@@ -549,14 +549,6 @@ public class SnarkManager implements Snark.CompleteListener {
             _util.setUseOpenTrackers(useOpenTrackers);
             changed = true;
         }
-        if (openTrackers != null) {
-            if (openTrackers.trim().length() > 0 && !openTrackers.trim().equals(_util.getOpenTrackerString())) {
-                _config.setProperty(PROP_OPENTRACKERS, openTrackers.trim());
-                _util.setOpenTrackerString(openTrackers);
-                addMessage(_("Open Tracker list changed - torrent restart required to take effect."));
-                changed = true;
-            }
-        }
         if (theme != null) {
             if(!theme.equals(_config.getProperty(PROP_THEME))) {
                 _config.setProperty(PROP_THEME, theme);
@@ -571,6 +563,20 @@ public class SnarkManager implements Snark.CompleteListener {
         }
     }
     
+    /**
+     *  @param ot null to restore default
+     *  @since 0.9.1
+     */
+    public void saveOpenTrackers(String ot) {
+        _util.setOpenTrackerString(ot);
+        if (ot != null)
+            _config.setProperty(PROP_OPENTRACKERS, ot);
+        else
+            _config.remove(PROP_OPENTRACKERS);
+        addMessage(_("Open Tracker list changed - torrent restart required to take effect."));
+        saveConfig();
+    }
+
     public void saveConfig() {
         try {
             synchronized (_configFile) {
diff --git a/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java b/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java
index 06c0581abc..dd9b14ba3d 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java
@@ -693,11 +693,11 @@ public class I2PSnarkServlet extends DefaultServlet {
             String refreshDel = req.getParameter("refreshDelay");
             String startupDel = req.getParameter("startupDelay");
             boolean useOpenTrackers = req.getParameter("useOpenTrackers") != null;
-            String openTrackers = req.getParameter("openTrackers");
+            //String openTrackers = req.getParameter("openTrackers");
             String theme = req.getParameter("theme");
             _manager.updateConfig(dataDir, filesPublic, autoStart, refreshDel, startupDel,
                                   seedPct, eepHost, eepPort, i2cpHost, i2cpPort, i2cpOpts,
-                                  upLimit, upBW, useOpenTrackers, openTrackers, theme);
+                                  upLimit, upBW, useOpenTrackers, theme);
         } else if ("Save2".equals(action)) {
             String taction = req.getParameter("taction");
             if (taction != null)
@@ -771,26 +771,35 @@ public class I2PSnarkServlet extends DefaultServlet {
 
     /** @since 0.9 */
     private void processTrackerForm(String action, HttpServletRequest req) {
-        if (action.equals(_("Delete selected"))) {
+        if (action.equals(_("Delete selected")) || action.equals(_("Change open trackers"))) {
             boolean changed = false;
             Map<String, Tracker> trackers = _manager.getTrackerMap();
+            StringBuilder openBuf = new StringBuilder(128);
             Enumeration e = req.getParameterNames();
             while (e.hasMoreElements()) {
                  Object o = e.nextElement();
                  if (!(o instanceof String))
                      continue;
                  String k = (String) o;
-                 if (!k.startsWith("delete_"))
-                     continue;
-                 k = k.substring(7);
-                 if (trackers.remove(k) != null) {
-                    _manager.addMessage(_("Removed") + ": " + k);
-                    changed = true;
+                 if (k.startsWith("delete_")) {
+                     k = k.substring(7);
+                     if (trackers.remove(k) != null) {
+                        _manager.addMessage(_("Removed") + ": " + k);
+                        changed = true;
+                    }
+                } else if (k.startsWith("open_")) {
+                     if (openBuf.length() > 0)
+                         openBuf.append(',');
+                     openBuf.append(k.substring(5));
                 }
             }
             if (changed) {
                 _manager.saveTrackerMap();
             }
+            String newOpen = openBuf.toString();
+            if (!newOpen.equals(_manager.util().getOpenTrackerString())) {
+                _manager.saveOpenTrackers(newOpen);
+            }
         } else if (action.equals(_("Add tracker"))) {
             String name = req.getParameter("tname");
             String hurl = req.getParameter("thurl");
@@ -803,6 +812,11 @@ public class I2PSnarkServlet extends DefaultServlet {
                     Map<String, Tracker> trackers = _manager.getTrackerMap();
                     trackers.put(name, new Tracker(name, aurl, hurl));
                     _manager.saveTrackerMap();
+                    if (req.getParameter("_add_open_") != null) {
+                        String oldOpen = _manager.util().getOpenTrackerString();
+                        String newOpen = oldOpen.length() <= 0 ? aurl : oldOpen + ',' + aurl;
+                        _manager.saveOpenTrackers(newOpen);
+                    }
                 } else {
                     _manager.addMessage(_("Enter valid tracker name and URLs"));
                 }
@@ -811,6 +825,7 @@ public class I2PSnarkServlet extends DefaultServlet {
             }
         } else if (action.equals(_("Restore defaults"))) {
             _manager.setDefaultTrackerMap();
+            _manager.saveOpenTrackers(null);
             _manager.addMessage(_("Restored default trackers"));
         } else {
             _manager.addMessage("Unknown POST action: \"" + action + '\"');
@@ -1422,7 +1437,7 @@ public class I2PSnarkServlet extends DefaultServlet {
         boolean filesPublic = _manager.areFilesPublic();
         boolean autoStart = _manager.shouldAutoStart();
         boolean useOpenTrackers = _manager.util().shouldUseOpenTrackers();
-        String openTrackers = _manager.util().getOpenTrackerString();
+        //String openTrackers = _manager.util().getOpenTrackerString();
         //int seedPct = 0;
        
         out.write("<form action=\"/i2psnark/configure\" method=\"POST\">\n" +
@@ -1536,12 +1551,12 @@ public class I2PSnarkServlet extends DefaultServlet {
                   + (useOpenTrackers ? "checked " : "") 
                   + "title=\"");
         out.write(_("If checked, announce torrents to open trackers as well as the tracker listed in the torrent file"));
-        out.write("\" > " +
+        out.write("\" ></td></tr>\n");
 
-                  "<tr><td>");
-        out.write(_("Open tracker announce URLs"));
-        out.write(": <td><input type=\"text\" name=\"openTrackers\" value=\""
-                  + openTrackers + "\" size=\"50\" ><br>\n");
+        //          "<tr><td>");
+        //out.write(_("Open tracker announce URLs"));
+        //out.write(": <td><input type=\"text\" name=\"openTrackers\" value=\""
+        //          + openTrackers + "\" size=\"50\" ><br>\n");
 
         //out.write("\n");
         //out.write("EepProxy host: <input type=\"text\" name=\"eepHost\" value=\""
@@ -1613,27 +1628,37 @@ public class I2PSnarkServlet extends DefaultServlet {
            .append("</th><th>")
            .append(_("Website URL"))
            .append("</th><th>")
+           .append(_("Open Tracker?"))
+           .append("</th><th>")
            .append(_("Announce URL"))
            .append("</th></tr>\n");
+        List<String> openTrackers = _manager.util().getOpenTrackers();
         for (Tracker t : _manager.getSortedTrackers()) {
             String name = t.name;
             String homeURL = t.baseURL;
             String announceURL = t.announceURL.replace("&#61;", "=");
             buf.append("<tr><td align=\"center\"><input type=\"checkbox\" class=\"optbox\" name=\"delete_")
-               .append(name).append("\">" +
+               .append(name).append("\" title=\"").append(_("Delete")).append("\">" +
                        "</td><td align=\"left\">").append(name)
                .append("</td><td align=\"left\">").append(urlify(homeURL, 35))
-               .append("</td><td align=\"left\">").append(urlify(announceURL, 35))
+               .append("</td><td align=\"center\"><input type=\"checkbox\" class=\"optbox\" name=\"open_")
+               .append(announceURL).append("\"");
+            if (openTrackers != null && openTrackers.contains(t.announceURL))
+                buf.append(" checked=\"checked\"");
+            buf.append(">" +
+                       "</td><td align=\"left\">").append(urlify(announceURL, 35))
                .append("</td></tr>\n");
         }
         buf.append("<tr><td align=\"center\"><b>")
            .append(_("Add")).append(":</b></td>" +
                    "<td align=\"left\"><input type=\"text\" size=\"16\" name=\"tname\"></td>" +
                    "<td align=\"left\"><input type=\"text\" size=\"40\" name=\"thurl\"></td>" +
+                   "<td align=\"center\"><input type=\"checkbox\" class=\"optbox\" name=\"_add_open_\"></td>" +
                    "<td align=\"left\"><input type=\"text\" size=\"40\" name=\"taurl\"></td></tr>\n" +
-                   "<tr><td colspan=\"2\"></td><td colspan=\"2\" align=\"left\">\n" +
+                   "<tr><td colspan=\"2\"></td><td colspan=\"3\" align=\"left\">\n" +
                    "<input type=\"submit\" name=\"taction\" class=\"default\" value=\"").append(_("Add tracker")).append("\">\n" +
                    "<input type=\"submit\" name=\"taction\" class=\"delete\" value=\"").append(_("Delete selected")).append("\">\n" +
+                   "<input type=\"submit\" name=\"taction\" class=\"accept\" value=\"").append(_("Change open trackers")).append("\">\n" +
                    // "<input type=\"reset\" class=\"cancel\" value=\"").append(_("Cancel")).append("\">\n" +
                    "<input type=\"submit\" name=\"taction\" class=\"reload\" value=\"").append(_("Restore defaults")).append("\">\n" +
                    "<input type=\"submit\" name=\"taction\" class=\"add\" value=\"").append(_("Add tracker")).append("\">\n" +
-- 
GitLab