From 264bd16c937c48b2af77473944217496fae0aa3d Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Thu, 26 Jul 2018 12:42:51 +0300
Subject: Multiple trivial fixes
* https links in posts
* escape reply numbers for telegram
* catch when non safe url can not be stripped
---
.../com/juick/formatters/PlainTextFormatter.java | 2 +-
.../src/main/java/com/juick/util/MessageUtils.java | 34 ++++++++++++++--------
2 files changed, 23 insertions(+), 13 deletions(-)
(limited to 'juick-common/src/main/java/com')
diff --git a/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java
index 91748684..378a523f 100644
--- a/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java
+++ b/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java
@@ -44,7 +44,7 @@ public class PlainTextFormatter {
sb.append(title).append(markdown ? MessageUtils.getMarkdownUser(jmsg.getUser()) : jmsg.getUser().getName()).append(":\n")
.append(subtitle).append("\n");
if (markdown) {
- sb.append(MessageUtils.escapeMarkdown(StringUtils.defaultString(jmsg.getText())));
+ sb.append(MessageUtils.formatMarkdownText(jmsg));
} else {
sb.append(StringUtils.defaultString(jmsg.getText()));
}
diff --git a/juick-common/src/main/java/com/juick/util/MessageUtils.java b/juick-common/src/main/java/com/juick/util/MessageUtils.java
index ae0c2a8b..bac24507 100644
--- a/juick-common/src/main/java/com/juick/util/MessageUtils.java
+++ b/juick-common/src/main/java/com/juick/util/MessageUtils.java
@@ -66,6 +66,8 @@ public class MessageUtils {
private final static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?#12345
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1#$2$3");
+ msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1#$2$3");
// #12345/65
// #12345/65
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1#$2/$3$4");
+ msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1#$2/$3$4");
// *bold*
// bold
@@ -129,15 +131,15 @@ public class MessageUtils {
// /12
// /12
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1/$2$3");
+ msg = msg.replaceAll(replyNumberRegex, "$1/$2$3");
// @username@jabber.org
// @username@jabber.org
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3");
+ msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3");
// @username
// @username
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3");
+ msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3");
// (http://juick.com/last?page=2)
// (juick.com)
@@ -211,6 +213,10 @@ public class MessageUtils {
}
return ret;
}
+ public static String formatMarkdownText(final Message msg) {
+ String s = StringUtils.defaultString(msg.getText()).replaceAll(replyNumberRegex, String.format("$1[/$2](https://juick.com/m/%d#$2)$3", msg.getMid()));
+ return escapeMarkdown(s);
+ }
public static String escapeMarkdown(final String s) {
return s.replace("_", "\\_").replace("*", "\\*")
.replace("`", "\\`");
@@ -280,14 +286,18 @@ public class MessageUtils {
public static String stripNonSafeUrls(String input) {
// strip login urls
- Matcher urlMatcher = Pattern.compile(MessageUtils.urlRegex).matcher(input);
- while (urlMatcher.find()) {
- URI uri = URI.create(urlMatcher.group(0));
- if (uri.getHost().equals("juick.com")) {
- UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(uri);
- uriComponentsBuilder.replaceQueryParam("hash");
- input = input.replace(urlMatcher.group(0), uriComponentsBuilder.build().toUriString());
+ try {
+ Matcher urlMatcher = Pattern.compile(MessageUtils.urlRegex).matcher(input);
+ while (urlMatcher.find()) {
+ URI uri = URI.create(urlMatcher.group(0));
+ if (uri.getHost().equals("juick.com")) {
+ UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(uri);
+ uriComponentsBuilder.replaceQueryParam("hash");
+ input = input.replace(urlMatcher.group(0), uriComponentsBuilder.build().toUriString());
+ }
}
+ } catch (IllegalArgumentException e) {
+ return input;
}
return input;
}
--
cgit v1.2.3