BrightSide Workbench Full Report + Source Code
elephant/src/main/java/org/turro/mail/impl/MailUtils.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.impl;
20 
21 import java.net.URLDecoder;
22 import java.nio.charset.StandardCharsets;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import javax.mail.internet.AddressException;
28 import javax.mail.internet.InternetAddress;
29 import org.turro.string.Strings;
30 import org.turro.action.Actions;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.security.IUser;
33 import org.turro.plugin.contacts.IContact;
34 
39 @Deprecated
40 public class MailUtils {
41 
42  public static boolean silentEmailCheck(String emails) {
43  try {
44  for(InternetAddress ia : InternetAddress.parse(emails, true)) {
45  ia.validate();
46  }
47  return true;
48  } catch (AddressException ex) {
49  return false;
50  }
51  }
52 
53 
54  public static boolean isValidEmailAddress(String emails) {
55  try {
56  for(InternetAddress ia : InternetAddress.parse(emails, true)) {
57  ia.validate();
58  }
59  return true;
60  } catch (AddressException ex) {
61  Logger.getLogger(MailUtils.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(emails), ex);
62  }
63  return false;
64  }
65 
66  public static String processMacrosForWeb(IContact contact, String message) {
67  message = cleanLinks(message);
68  message = processUser(contact, message);
69  return message;
70  }
71 
72  public static String processMacros(IContact contact, String message) {
73  message = processLinks(contact, message);
74  message = processUser(contact, message);
75  return message;
76  }
77 
78  /* Links */
79 
80  public static String processLinks(IContact contact, String message) {
81  if(contact != null && contact.isValid() && contact.isWebUser()) {
82  String email = contact.getConnector(IUser.CONNECTOR_EMAIL);
83  message = processLinks(email, message);
84  } else {
85  message = cleanLinks(message);
86  }
87  return message;
88  }
89 
90  public static String processLinks(String email, String message) {
91  message = processLiveLinks(email, message);
92  message = processLiveRefs(email, message);
93  return message;
94  }
95 
96  public static String cleanLinks(String message) {
97  message = cleanLiveLinks(message);
98  message = cleanLiveRefs(message);
99  return message;
100  }
101 
102  public static String processLiveLinks(String email, String message) {
103  if(message.contains("{livelink")) {
104  Pattern pat = Pattern.compile(LIVE_LINK_PATTERN);
105  Matcher mat = pat.matcher(message);
106  StringBuffer result = new StringBuffer();
107  while(mat.find()) {
108  try {
109  if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(1)) && !Strings.isBlank(mat.group(2))) {
110  String server = ElephantContext.getServerUrl("http") + "?" +
111  Actions.createActionFromElephant(email, URLDecoder.decode(mat.group(2), StandardCharsets.UTF_8.name()));
112  mat.appendReplacement(result, "<a href=\"" + server + "\">" + mat.group(1) + "</a>");
113  }
114  } catch (Exception ex) {
115  Logger.getLogger(MailUtils.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
116  }
117  }
118  mat.appendTail(result);
119  return result.toString();
120  }
121  return message;
122  }
123 
124  public static String processLiveRefs(String email, String message) {
125  if(message.contains("{liveref")) {
126  Pattern pat = Pattern.compile(LIVE_REF_PATTERN);
127  Matcher mat = pat.matcher(message);
128  StringBuffer result = new StringBuffer();
129  while(mat.find()) {
130  try {
131  if(mat.groupCount() > 0 && !Strings.isBlank(mat.group(1))) {
132  String server = ElephantContext.getServerUrl("http") + "?" +
133  Actions.createActionFromElephant(email, URLDecoder.decode(mat.group(1), StandardCharsets.UTF_8.name()));
134  mat.appendReplacement(result, server);
135  }
136  } catch (Exception ex) {
137  Logger.getLogger(MailUtils.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
138  }
139  }
140  mat.appendTail(result);
141  return result.toString();
142  }
143  return message;
144  }
145 
146  public static String cleanLiveLinks(String message) {
147  if(message.contains("{livelink")) {
148  Pattern pat = Pattern.compile(LIVE_LINK_PATTERN);
149  Matcher mat = pat.matcher(message);
150  StringBuffer result = new StringBuffer();
151  while(mat.find()) {
152  try {
153  if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(1)) && !Strings.isBlank(mat.group(2))) {
154  mat.appendReplacement(result, "<a href='" + mat.group(2) + "'>" + mat.group(1) + "</a>");
155  }
156  } catch (Exception ex) {
157  Logger.getLogger(MailUtils.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
158  }
159  }
160  mat.appendTail(result);
161  return result.toString();
162  }
163  return message;
164  }
165 
166  public static String cleanLiveRefs(String message) {
167  if(message.contains("{liveref")) {
168  Pattern pat = Pattern.compile(LIVE_REF_PATTERN);
169  Matcher mat = pat.matcher(message);
170  StringBuffer result = new StringBuffer();
171  while(mat.find()) {
172  try {
173  if(mat.groupCount() > 0 && !Strings.isBlank(mat.group(1))) {
174  mat.appendReplacement(result, mat.group(1));
175  }
176  } catch (Exception ex) {
177  Logger.getLogger(MailUtils.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
178  }
179  }
180  mat.appendTail(result);
181  return result.toString();
182  }
183  return message;
184  }
185 
186  private static final String LIVE_LINK_PATTERN = "\\{livelink\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
187  private static final String LIVE_REF_PATTERN = "\\{liveref\\:([^\\:\\}]*)\\}";
188  private static final String LINK_PATTERN = "\\{link\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
189 
190  /* Images */
191 
192  public static String processImgWidth(String message) {
193  return message.replaceAll("\\<img ", "<img style=\"max-width:100%\" ");
194  }
195 
196  /* User */
197 
198  public static String processUser(IContact contact, String message) {
199  if(contact != null && contact.isValid() && contact.isWebUser()) {
200  message = processUser(Recipient.of(contact), message);
201  }
202  return message;
203  }
204 
205  public static String processUser(String email, String name, String message) {
206  return processUser(Recipient.of(email, name), message);
207  }
208 
209  public static String processUser(Recipient recipient, String message) {
210  if(!recipient.isEmpty()) {
211  message = recipient.parse(message);
212  }
213  return message;
214  }
215 
216 }
static String createActionFromElephant(String email, String redir)
Definition: Actions.java:86
static String getServerUrl(String scheme)
static String processLinks(String email, String message)
static String processLinks(IContact contact, String message)
static String processUser(IContact contact, String message)
static String processLiveRefs(String email, String message)
static String processLiveLinks(String email, String message)
static String processUser(String email, String name, String message)
static String processUser(Recipient recipient, String message)
static String processMacrosForWeb(IContact contact, String message)
static String processMacros(IContact contact, String message)
static Recipient of(IContact contact)
Definition: Recipient.java:81
String parse(String text)
Definition: Recipient.java:63
static final String CONNECTOR_EMAIL
Definition: IUser.java:27