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