BrightSide Workbench Full Report + Source Code
elephant/src/main/java/org/turro/plugin/contacts/MailContact.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.plugin.contacts;
19 
20 import java.util.Date;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
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;
36 
41 public class MailContact {
42 
43  private IContact contact;
44  private String link, message, exParam;
45 
46  public MailContact(IContact contact, String link, String message, String exParam) {
47  this.contact = contact;
48  this.link = link;
49  this.message = message;
50  this.exParam = exParam;
51  }
52 
53  public String getMessage(Date validUntil, String comment) {
54  if(contact == null) return null;
55 
56  if(message.indexOf("{linkTmpSes}") > -1) {
57  try {
58 // String key = createTmpKey(validUntil);
59 // String server = ElephantContext.getServerUrl("http") +
60 // (exParam == null ? "?" : exParam + "&") + "tmpses=" + key;
61  String server = ElephantContext.getServerUrl("http") + "?" +
63  message = message.replaceAll("\\{linkTmpSes\\}", "<a style=\"color:inherit\" href=\"" + server + "\">" + link + "</a>");
64  } catch (Exception ex) {
65  Logger.getLogger(MailContact.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
66  }
67  }
68  message = processWebLiveLinks(validUntil, message);
69  message = processWebLinks(message);
70  message = processLinkMacros(message);
71  message = processImgWidth(message);
72 
73  message = message.replaceAll("\\{email\\}", contact.getEmail())
74  .replaceAll("\\{name\\}", contact.getName())
75  .replaceAll("\\{comment\\}", comment);
76 
77  String serverBase = ElephantContext.getServerBase("http");
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\\\"");
82  }
83 
84  public String getEmail() {
85  return getEmail(contact);
86  }
87 
88 // public String createTmpKey(Date validUntil) {
89 // if(validUntil == null) {
90 // validUntil = new Date(new Date().getTime() + (2L * 24L * 60L * 60L * 1000L));
91 // }
92 // IUser user = ContextFactory.getUser(HeadlessApplication.getInstance().getConstructor());
93 // if (user.impersonate(contact.getId())) {
94 // return user.createTmpKey(validUntil);
95 // }
96 // return null;
97 // }
98 
99  public static String getEmail(IContact contact) {
100  if (contact == null) {
101  return null;
102  }
103  String email = contact.getConnector(IUser.CONNECTOR_EMAIL);
104  return email == null ? null : (!Contacts.isValidEmail(email) ? null : email);
105  }
106 
107  public static void sendToIPlayers(List<IContact> contacts, String link, String subject,
108  String message, String comment, ILinkSolver exParam, String implementation) {
110  if(mp != null) {
112  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
113 
114  Set<MailContact> emails = new HashSet<>();
115  for(IContact contact : contacts) {
116  emails.add(new MailContact(contact, link, message, exParam != null ? exParam.getLink(contact) : null));
117  }
118  for(MailContact mc : emails) {
119  mp.addToPool(null, mc.getEmail(), null, subject, mc.getMessage(null, comment), "text/html");
120  }
121  mp.sendPool();
122  }
123  }
124 
125  public static void poolToIPlayers(List<IContact> contacts, String link,
126  String message, String comment, ILinkSolver exParam, MessagePool pool) {
127  Set<MailContact> emails = new HashSet<>();
128  for(IContact contact : contacts) {
129  emails.add(new MailContact(contact, link, message, exParam != null ? exParam.getLink(contact) : null));
130  }
131  for(MailContact mc : emails) {
132  pool.addMessage(mc.getEmail(), mc.getMessage(null, comment));
133  }
134  }
135 
136  @Override
137  public boolean equals(Object obj) {
138  if (obj == null) {
139  return false;
140  }
141  if (getClass() != obj.getClass()) {
142  return false;
143  }
144  final MailContact other = (MailContact) obj;
145  if (this.contact.getId() != other.contact.getId() && (this.contact.getId() == null || !this.contact.getId().equals(other.contact.getId()))) {
146  return false;
147  }
148  return true;
149  }
150 
151  @Override
152  public int hashCode() {
153  int hash = 7;
154  hash = 31 * hash + (this.contact != null && this.contact.getId() != null ? this.contact.getId().hashCode() : 0);
155  return hash;
156  }
157 
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("\\:", "&colon;").replaceAll("\\}", "&endkey;"));
162  if(!Strings.isBlank(path)) {
163  sb.append(":");
164  sb.append(path);
165  }
166  sb.append("}");
167  return sb.toString();
168  }
169  return "";
170  }
171 
172  public static String processLinkMacros(String text) {
173  if(!Strings.isBlank(text)) {
174  return text.replaceAll("\\&colon\\;", ":").replaceAll("\\&endkey\\;", "}");
175  }
176  return "";
177  }
178 
179  private String nextParamSep(String params) {
180  if(params.indexOf("?") > -1) {
181  return "&";
182  }
183  return "?";
184  }
185 
186  private String processWebLiveLinks(Date validUntil, String message) {
187 
188  if(message.contains("{livelink")) {
189  Pattern pat = Pattern.compile(LIVE_LINK_PATTERN);
190  Matcher mat = pat.matcher(message);
191  StringBuffer result = new StringBuffer();
192  while(mat.find()) {
193  try {
194 // String key = createTmpKey(validUntil);
195  String currParam = exParam;
196  if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(2))) {
197  currParam = (currParam == null ? "" : currParam) + mat.group(2);
198  }
199 // String server = ElephantContext.getServerUrl("http") +
200 // (currParam == null ? "" : currParam + nextParamSep(currParam)) + "tmpses=" + key;
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);
207  }
208  }
209  mat.appendTail(result);
210  return result.toString();
211  }
212  return message;
213 
214  }
215 
216  private String processWebLinks(String message) {
217 
218  if(message.contains("{link")) {
219  Pattern pat = Pattern.compile(LINK_PATTERN);
220  Matcher mat = pat.matcher(message);
221  StringBuffer result = new StringBuffer();
222  while(mat.find()) {
223  try {
224  String currParam = exParam;
225  if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(2))) {
226  currParam = (currParam == null ? "" : currParam) + mat.group(2);
227  }
228 // String server = ElephantContext.getServerUrl("http") +
229 // (currParam == null ? "" : currParam);
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);
236  }
237  }
238  mat.appendTail(result);
239  return result.toString();
240  }
241  return message;
242 
243  }
244 
245  private static final String LIVE_LINK_PATTERN = "\\{livelink\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
246  private static final String LINK_PATTERN = "\\{link\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
247 
248  private String processImgWidth(String message) {
249  return message.replaceAll("\\<img ", "<img style=\"width:100%\" ");
250  }
251 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static boolean isValidEmail(String email)
Definition: Contacts.java:86
IImplementation getImplementation(String name)
static String getServerBase(String scheme)
static String getServerUrl(String scheme)
void addToPool(String from, String to, String cc, String subject, String message, String content)
void addMessage(String email, String message)
MailContact(IContact contact, String link, String message, String exParam)
static void poolToIPlayers(List< IContact > contacts, String link, String message, String comment, ILinkSolver exParam, MessagePool pool)
static void sendToIPlayers(List< IContact > contacts, String link, String subject, String message, String comment, ILinkSolver exParam, String implementation)
static final String CONNECTOR_EMAIL
Definition: IUser.java:32
String getLink(IContact contact)