Don’t link to unknown posts.
[Sone.git] / src / main / java / net / pterodactylus / sone / text / FreenetLinkParser.java
index cea4452..4b88acc 100644 (file)
@@ -127,6 +127,18 @@ public class FreenetLinkParser implements Parser<FreenetLinkParserContext> {
                        }
                        emptyLines = 0;
                        boolean lineComplete = true;
+
+                       /* filter http(s) links to own node. */
+                       String hostHeader = (context.getRequest() != null) ? context.getRequest().getHttpRequest().getHeader("host") : null;
+                       logger.log(Level.FINEST, "hostHeader: %s", hostHeader);
+                       if (hostHeader != null) {
+                               for (String toRemove : new String[] { "http://" + hostHeader + "/", "https://" + hostHeader + "/", "http://" + hostHeader, "https://" + hostHeader }) {
+                                       while (line.indexOf(toRemove) != -1) {
+                                               line = line.replace(toRemove, "");
+                                       }
+                               }
+                       }
+
                        while (line.length() > 0) {
                                int nextKsk = line.indexOf("KSK@");
                                int nextChk = line.indexOf("CHK@");
@@ -253,7 +265,7 @@ public class FreenetLinkParser implements Parser<FreenetLinkParserContext> {
                                        } else if (linkType == LinkType.POST) {
                                                String postId = link.substring(7);
                                                Post post = core.getPost(postId, false);
-                                               if (post != null) {
+                                               if ((post != null) && (post.getSone() != null)) {
                                                        String postText = post.getText();
                                                        postText = postText.substring(0, Math.min(postText.length(), 20)) + "…";
                                                        Sone postSone = post.getSone();