BrightSide Workbench Full Report + Source Code
elephant-mail/src/main/java/org/turro/mail/message/MailUtils.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.mail.message;
20 
21 import java.net.URLDecoder;
22 import java.nio.charset.StandardCharsets;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import org.turro.action.Actions;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.security.IUser;
28 import org.turro.log.WebLoggers;
29 import org.turro.mail.provider.MailRecipient;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.string.Strings;
32 
37 public class MailUtils {
38 
39  public static String processMacrosForWeb(IContact contact, String message) {
40  message = cleanLinks(message);
41  message = processUser(contact, message);
42  return message;
43  }
44 
45  public static String processMacros(IContact contact, String message) {
46  message = processLinks(contact, message);
47  message = processUser(contact, message);
48  return message;
49  }
50 
51  /* Links */
52 
53  public static String processLinks(IContact contact, String message) {
54  if(contact != null && contact.isValid() && contact.isWebUser()) {
55  String email = contact.getConnector(IUser.CONNECTOR_EMAIL);
56  message = processLinks(email, message);
57  } else {
58  message = cleanLinks(message);
59  }
60  return message;
61  }
62 
63  public static String processLinks(String email, String message) {
64  message = processLiveLinks(email, message);
65  message = processLiveRefs(email, message);
66  return message;
67  }
68 
69  public static String cleanLinks(String message) {
70  message = cleanLiveLinks(message);
71  message = cleanLiveRefs(message);
72  return message;
73  }
74 
75  public static String processLiveLinks(String email, String message) {
76  if(message.contains("{livelink")) {
77  Pattern pat = Pattern.compile(LIVE_LINK_PATTERN);
78  Matcher mat = pat.matcher(message);
79  StringBuffer result = new StringBuffer();
80  while(mat.find()) {
81  try {
82  if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(1)) && !Strings.isBlank(mat.group(2))) {
83  String server = ElephantContext.getServerUrl("http") + "?" +
84  Actions.createActionFromElephant(email, URLDecoder.decode(mat.group(2), StandardCharsets.UTF_8.name()));
85  mat.appendReplacement(result, "<a href=\"" + server + "\">" + mat.group(1) + "</a>");
86  }
87  } catch (Exception ex) {
89  }
90  }
91  mat.appendTail(result);
92  return result.toString();
93  }
94  return message;
95  }
96 
97  public static String processLiveRefs(String email, String message) {
98  if(message.contains("{liveref")) {
99  Pattern pat = Pattern.compile(LIVE_REF_PATTERN);
100  Matcher mat = pat.matcher(message);
101  StringBuffer result = new StringBuffer();
102  while(mat.find()) {
103  try {
104  if(mat.groupCount() > 0 && !Strings.isBlank(mat.group(1))) {
105  String server = ElephantContext.getServerUrl("http") + "?" +
106  Actions.createActionFromElephant(email, URLDecoder.decode(mat.group(1), StandardCharsets.UTF_8.name()));
107  mat.appendReplacement(result, server);
108  }
109  } catch (Exception ex) {
110  WebLoggers.severe(MailUtils.class).exception(ex).log();
111  }
112  }
113  mat.appendTail(result);
114  return result.toString();
115  }
116  return message;
117  }
118 
119  public static String cleanLiveLinks(String message) {
120  if(message.contains("{livelink")) {
121  Pattern pat = Pattern.compile(LIVE_LINK_PATTERN);
122  Matcher mat = pat.matcher(message);
123  StringBuffer result = new StringBuffer();
124  while(mat.find()) {
125  try {
126  if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(1)) && !Strings.isBlank(mat.group(2))) {
127  mat.appendReplacement(result, "<a href='" + mat.group(2) + "'>" + mat.group(1) + "</a>");
128  }
129  } catch (Exception ex) {
130  WebLoggers.severe(MailUtils.class).exception(ex).log();
131  }
132  }
133  mat.appendTail(result);
134  return result.toString();
135  }
136  return message;
137  }
138 
139  public static String cleanLiveRefs(String message) {
140  if(message.contains("{liveref")) {
141  Pattern pat = Pattern.compile(LIVE_REF_PATTERN);
142  Matcher mat = pat.matcher(message);
143  StringBuffer result = new StringBuffer();
144  while(mat.find()) {
145  try {
146  if(mat.groupCount() > 0 && !Strings.isBlank(mat.group(1))) {
147  mat.appendReplacement(result, mat.group(1));
148  }
149  } catch (Exception ex) {
150  WebLoggers.severe(MailUtils.class).exception(ex).log();
151  }
152  }
153  mat.appendTail(result);
154  return result.toString();
155  }
156  return message;
157  }
158 
159  private static final String LIVE_LINK_PATTERN = "\\{livelink\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
160  private static final String LIVE_REF_PATTERN = "\\{liveref\\:([^\\:\\}]*)\\}";
161  private static final String LINK_PATTERN = "\\{link\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
162 
163  /* Images */
164 
165  public static String processImgWidth(String message) {
166  return message.replaceAll("\\<img ", "<img style=\"max-width:100%\" ");
167  }
168 
169  /* User */
170 
171  public static String processUser(IContact contact, String message) {
172  if(contact != null && contact.isValid() && contact.isWebUser()) {
173  message = processUser(MailRecipient.of(contact), message);
174  }
175  return message;
176  }
177 
178  public static String processUser(String email, String name, String message) {
179  return processUser(MailRecipient.of(email, name), message);
180  }
181 
182  public static String processUser(MailRecipient recipient, String message) {
183  if(!recipient.isEmpty()) {
184  message = recipient.parse(message);
185  }
186  return message;
187  }
188 
189  /* Absolutes */
190 
191  public static String toAbsolute(String text) {
192  String serverBase = ElephantContext.getServerBase("http");
193  return text.replaceAll("href=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "href=\\'" + serverBase + "$1$2\\'")
194  .replaceAll("src=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "src=\\'" + serverBase + "$1$2\\'")
195  .replaceAll("href=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "href=\\\"" + serverBase + "$1$2\\\"")
196  .replaceAll("src=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "src=\\\"" + serverBase + "$1$2\\\"");
197  }
198 
199 }
static String createActionFromElephant(String email, String redir)
Definition: Actions.java:86
static String getServerBase(String scheme)
static String getServerUrl(String scheme)
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
static String processUser(String email, String name, String message)
static String processUser(MailRecipient recipient, String message)
static MailRecipient of(String value)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27