From 72e2161924b1cf9e661418778a654b883c27aade Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Mon, 19 Dec 2022 10:09:58 -0500
Subject: [PATCH] CLI: Add NewsHandler CLI to fetch news from servers

---
 .../src/net/i2p/router/news/CommandLine.java  |  3 +-
 .../net/i2p/router/update/NewsHandler.java    | 97 ++++++++++++++++++-
 2 files changed, 97 insertions(+), 3 deletions(-)

diff --git a/apps/routerconsole/java/src/net/i2p/router/news/CommandLine.java b/apps/routerconsole/java/src/net/i2p/router/news/CommandLine.java
index d49c172aa3..428f0fca75 100644
--- a/apps/routerconsole/java/src/net/i2p/router/news/CommandLine.java
+++ b/apps/routerconsole/java/src/net/i2p/router/news/CommandLine.java
@@ -18,7 +18,8 @@ public class CommandLine extends net.i2p.router.CommandLine {
     protected static final List<String> NCLASSES = Arrays.asList(new String[] {
         "com.vuze.plugins.mlab.MLabRunner",
         "net.i2p.router.news.BlocklistEntries",
-        "net.i2p.router.news.NewsXMLParser"
+        "net.i2p.router.news.NewsXMLParser",
+        "net.i2p.router.update.NewsHandler"
     });
 
     protected CommandLine() {}
diff --git a/apps/routerconsole/java/src/net/i2p/router/update/NewsHandler.java b/apps/routerconsole/java/src/net/i2p/router/update/NewsHandler.java
index 0bf568c568..1c5fbe6827 100644
--- a/apps/routerconsole/java/src/net/i2p/router/update/NewsHandler.java
+++ b/apps/routerconsole/java/src/net/i2p/router/update/NewsHandler.java
@@ -1,12 +1,18 @@
 package net.i2p.router.update;
 
+import java.io.File;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.List;
 
+import gnu.getopt.Getopt;
+
+import net.i2p.I2PAppContext;
+import net.i2p.crypto.SU3File;
 import net.i2p.router.RouterContext;
 import net.i2p.router.web.ConfigUpdateHelper;
+import net.i2p.util.EepGet;
 import net.i2p.update.*;
 import static net.i2p.update.UpdateType.*;
 import static net.i2p.update.UpdateMethod.*;
@@ -17,9 +23,9 @@ import static net.i2p.update.UpdateMethod.*;
  *
  * Overrides UpdateRunner for convenience, this is not an Updater
  *
- * @since 0.9.4 moved from NewsFetcher
+ * @since 0.9.4 moved from NewsFetcher, public since 0.9.57 for CLI only
  */
-class NewsHandler extends UpdateHandler implements Checker {
+public class NewsHandler extends UpdateHandler implements Checker {
     
     /**
      *  NOTE: If you change, also change in Android:
@@ -64,4 +70,91 @@ class NewsHandler extends UpdateHandler implements Checker {
         UpdateRunner update = new NewsFetcher(_context, _mgr, updateSources);
         return update;
     }
+
+    /**
+     *  CLI fetch
+     *
+     *  @since 0.9.57
+     */
+    public static void main(String[] args) {
+        I2PAppContext ctx = I2PAppContext.getGlobalContext();
+        String url = ConfigUpdateHelper.getNewsURL(ctx);
+        String proxyHost = "127.0.0.1";
+        int proxyPort = 4444;
+        String lang = null;
+        boolean custom = false;
+        boolean error = false;
+
+        Getopt g = new Getopt("newshandler", args, "p:l:u:");
+        int c;
+        while ((c = g.getopt()) != -1) {
+          switch (c) {
+            case 'p':
+                String s = g.getOptarg();
+                int colon = s.indexOf(':');
+                if (colon >= 0) {
+                    if (colon > 0)
+                        proxyHost = s.substring(0, colon);
+                    String port = s.substring(colon + 1);
+                    proxyPort = Integer.parseInt(port);
+                } else {
+                    proxyHost = s;
+                }
+                break;
+
+            case 'l':
+                lang = g.getOptarg();
+                break;
+
+            case 'u':
+                url = g.getOptarg();
+                custom = true;
+                break;
+
+            default:
+                error = true;
+                break;
+          }
+        }
+        if (error || args.length - g.getOptind() != 0) {
+            System.err.println("Usage: newshandler [-p 127.0.0.1[:4444]] [-l en] [-u url]");
+            System.exit(1);
+        }
+
+        if (lang != null)
+            url = url + "?lang=" + lang;
+        File file = new File("news-primary.su3");
+        System.out.println("Fetching news from primary server at " + url);
+        test(ctx, proxyHost, proxyPort, url, file);
+        if (!custom) {
+            url = DEFAULT_BACKUP_NEWS_URL_SU3;
+            if (lang != null)
+                url = url + "?lang=" + lang;
+            file = new File("news-backup.su3");
+            System.out.println("Fetching news from backup server at " + url);
+            test(ctx, proxyHost, proxyPort, url, file);
+        }
+    }
+
+    /**
+     *  CLI fetch
+     *
+     *  @since 0.9.57
+     */
+    private static void test(I2PAppContext ctx, String phost, int pport, String url, File file) {
+        file.delete();
+        String path = file.getAbsolutePath();
+        EepGet get = new EepGet(ctx, phost, pport, 0, file.getAbsolutePath(), url);
+        if (get.fetch()) {
+            int status = get.getStatusCode();
+            if (status == 200) {
+                SU3File.main(new String[] {"showversion", path} );
+                SU3File.main(new String[] {"extract", "-x", path} );
+            } else {
+                System.out.println("Failed to fetch, status " + status + " for " + url);
+            }
+        } else {
+            System.out.println("Failed to fetch " + url);
+        }
+    }
 }
-- 
GitLab