18 package org.turro.plugin.contacts;
20 import java.util.Date;
21 import java.util.HashSet;
22 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import org.amic.util.string.Strings;
29 import org.turro.action.Actions;
30 import org.turro.action.Contacts;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.context.HeadlessApplication;
33 import org.turro.elephant.security.IUser;
34 import org.turro.mail.impl.MailPool;
35 import org.turro.mail.impl.MessagePool;
44 private String link, message, exParam;
47 this.contact = contact;
49 this.message = message;
50 this.exParam = exParam;
53 public String
getMessage(Date validUntil, String comment) {
54 if(contact ==
null)
return null;
56 if(message.indexOf(
"{linkTmpSes}") > -1) {
63 message = message.replaceAll(
"\\{linkTmpSes\\}",
"<a style=\"color:inherit\" href=\"" + server +
"\">" + link +
"</a>");
64 }
catch (Exception ex) {
68 message = processWebLiveLinks(validUntil, message);
69 message = processWebLinks(message);
71 message = processImgWidth(message);
73 message = message.replaceAll(
"\\{email\\}", contact.
getEmail())
74 .replaceAll(
"\\{name\\}", contact.
getName())
75 .replaceAll(
"\\{comment\\}", comment);
78 return message.replaceAll(
"href=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'",
"href=\\'" + serverBase +
"$1$2\\'")
79 .replaceAll(
"src=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'",
"src=\\'" + serverBase +
"$1$2\\'")
80 .replaceAll(
"href=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"",
"href=\\\"" + serverBase +
"$1$2\\\"")
81 .replaceAll(
"src=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"",
"src=\\\"" + serverBase +
"$1$2\\\"");
100 if (contact ==
null) {
107 public static void sendToIPlayers(List<IContact> contacts, String link, String subject,
108 String message, String comment,
ILinkSolver exParam, String implementation) {
114 Set<MailContact> emails =
new HashSet<>();
116 emails.add(
new MailContact(contact, link, message, exParam !=
null ? exParam.
getLink(contact) :
null));
119 mp.
addToPool(
null, mc.getEmail(),
null, subject, mc.getMessage(
null, comment),
"text/html");
127 Set<MailContact> emails =
new HashSet<>();
129 emails.add(
new MailContact(contact, link, message, exParam !=
null ? exParam.
getLink(contact) :
null));
132 pool.
addMessage(mc.getEmail(), mc.getMessage(
null, comment));
141 if (getClass() != obj.getClass()) {
145 if (this.contact.
getId() != other.contact.
getId() && (this.contact.
getId() ==
null || !this.contact.
getId().equals(other.contact.
getId()))) {
154 hash = 31 * hash + (this.contact !=
null && this.contact.
getId() !=
null ? this.contact.
getId().hashCode() : 0);
158 public static String
createLink(String text, String path,
boolean live) {
159 if(!Strings.isBlank(text)) {
160 StringBuilder sb =
new StringBuilder((live ?
"{livelink:" :
"{link:"));
161 sb.append(text.replaceAll(
"\\:",
":").replaceAll(
"\\}",
"&endkey;"));
162 if(!Strings.isBlank(path)) {
167 return sb.toString();
173 if(!Strings.isBlank(text)) {
174 return text.replaceAll(
"\\&colon\\;",
":").replaceAll(
"\\&endkey\\;",
"}");
179 private String nextParamSep(String params) {
180 if(params.indexOf(
"?") > -1) {
186 private String processWebLiveLinks(Date validUntil, String message) {
188 if(message.contains(
"{livelink")) {
189 Pattern pat = Pattern.compile(LIVE_LINK_PATTERN);
190 Matcher mat = pat.matcher(message);
191 StringBuffer result =
new StringBuffer();
195 String currParam = exParam;
196 if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(2))) {
197 currParam = (currParam ==
null ?
"" : currParam) + mat.group(2);
201 currParam = currParam ==
null ?
"" : currParam + nextParamSep(currParam);
202 String server = ElephantContext.getServerUrl(
"http") +
"?" +
203 Actions.createAction(contact.
getConnector(IUser.CONNECTOR_EMAIL), currParam);
204 mat.appendReplacement(result,
"<a href=\"" + server +
"\">" + mat.group(1) +
"</a>");
205 }
catch (Exception ex) {
206 Logger.getLogger(
MailContact.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
209 mat.appendTail(result);
210 return result.toString();
216 private String processWebLinks(String message) {
218 if(message.contains(
"{link")) {
219 Pattern pat = Pattern.compile(LINK_PATTERN);
220 Matcher mat = pat.matcher(message);
221 StringBuffer result =
new StringBuffer();
224 String currParam = exParam;
225 if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(2))) {
226 currParam = (currParam ==
null ?
"" : currParam) + mat.group(2);
230 currParam = currParam ==
null ?
"" : currParam;
231 String server = ElephantContext.getServerUrl(
"http") +
"?" +
232 Actions.createAction(contact.
getConnector(IUser.CONNECTOR_EMAIL), currParam);
233 mat.appendReplacement(result,
"<a href=\"" + server +
"\">" + mat.group(1) +
"</a>");
234 }
catch (Exception ex) {
235 Logger.getLogger(
MailContact.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
238 mat.appendTail(result);
239 return result.toString();
245 private static final String LIVE_LINK_PATTERN =
"\\{livelink\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
246 private static final String LINK_PATTERN =
"\\{link\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
248 private String processImgWidth(String message) {
249 return message.replaceAll(
"\\<img ",
"<img style=\"width:100%\" ");
static String createAction(String email, String redir)
IImplementation getImplementation(String name)
static String getRealPath(String path)
static String getServerBase(String scheme)
static String getServerUrl(String scheme)
static String logMsg(String msg)
static String getEncoding()
static HeadlessApplication getInstance()
void addToPool(String from, String to, String cc, String subject, String message, String content)
void addCssFile(String cssFile)
void setEncoding(String encoding)
void addMessage(String email, String message)
static final String CONNECTOR_EMAIL