From 59a80375992cf69668ed82cd2716db9adb53fc5e Mon Sep 17 00:00:00 2001
From: jrandom <jrandom>
Date: Mon, 5 Sep 2005 00:00:11 +0000
Subject: [PATCH] allow exporting eepsite destinations from the syndie database
 into userhosts.txt (so the eepproxy can get it)

---
 .../java/src/net/i2p/syndie/BlogManager.java  | 37 +++++++++++++++++++
 apps/syndie/jsp/addresses.jsp                 |  8 +++-
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/apps/syndie/java/src/net/i2p/syndie/BlogManager.java b/apps/syndie/java/src/net/i2p/syndie/BlogManager.java
index 708e3996be..b7c154f68d 100644
--- a/apps/syndie/java/src/net/i2p/syndie/BlogManager.java
+++ b/apps/syndie/java/src/net/i2p/syndie/BlogManager.java
@@ -301,6 +301,43 @@ public class BlogManager {
             return loginResult;
         }
     }
+
+    public String exportHosts(User user) {
+        if (!user.getAuthenticated() || !user.getAllowAccessRemote())
+            return "Not authorized to export the hosts";
+        Map newNames = new HashMap();
+        PetNameDB db = user.getPetNameDB();
+        for (Iterator names = db.getNames().iterator(); names.hasNext(); ) {
+            PetName pn = db.get((String)names.next());
+            if (pn == null) continue;
+            if (pn.getNetwork().equalsIgnoreCase("i2p")) {
+                try {
+                    Destination d = new Destination(pn.getLocation().trim());
+                    newNames.put(pn.getName(), d);
+                } catch (DataFormatException dfe) {
+                    // ignore
+                }
+            }
+        }
+        // horribly inefficient...
+        for (Iterator iter = newNames.keySet().iterator(); iter.hasNext(); ) {
+            String name = (String)iter.next();
+            Destination existing = _context.namingService().lookup(name);
+            if (existing == null) {
+                Destination known = (Destination)newNames.get(name);
+                try {
+                    FileOutputStream fos = new FileOutputStream("userhosts.txt", true);
+                    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
+                    osw.write(name + "=" + known.toBase64() + "\n");
+                    osw.close();
+                } catch (IOException ioe) {
+                    ioe.printStackTrace();
+                    return "Error exporting the hosts: " + ioe.getMessage();
+                }
+            }
+        }
+        return "Hosts exported";
+    }
     
     public BlogURI createBlogEntry(User user, String subject, String tags, String entryHeaders, String sml) {
         return createBlogEntry(user, subject, tags, entryHeaders, sml, null, null, null);
diff --git a/apps/syndie/jsp/addresses.jsp b/apps/syndie/jsp/addresses.jsp
index cbb44f6525..9ada773bc6 100644
--- a/apps/syndie/jsp/addresses.jsp
+++ b/apps/syndie/jsp/addresses.jsp
@@ -52,6 +52,8 @@ if (!user.getAuthenticated()) {
           names.store(user.getAddressbookLocation());
           %><b>Address removed</b><%
         }
+    } else if ( (action != null) && ("Export".equals(action)) ) {
+      %><%=BlogManager.instance().exportHosts(user)%><%
     }
     TreeSet sorted = new TreeSet(names.getNames());
     %><table border="0" width="100%">
@@ -170,7 +172,11 @@ if (!user.getAuthenticated()) {
         <td><input type="text" name="groups" size="10" /></td>
         <td><input type="submit" name="action" value="Add" /></td>
     </form></tr>
-    </table><%
+    <tr><form action="addresses.jsp" method="POST">
+        <td colspan="7">Export the eepsites to your router's userhosts.txt: <input type="submit" name="action" value="Export" /></td>
+        </form></tr>
+    </table>
+    <%
 }
 %>
 </td></tr>
-- 
GitLab