BrightSide Workbench Full Report + Source Code
elephant-plugins/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.turro.string.Strings;
29 import org.turro.action.Actions;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.context.HeadlessApplication;
32 import org.turro.elephant.security.IUser;
33 import org.turro.mail.impl.MailPool;
34 import org.turro.mail.impl.MessagePool;
35 
40 public class MailContact {
41 
42  private IContact contact;
43  private String link, message, exParam;
44 
45  public MailContact(IContact contact, String link, String message, String exParam) {
46  this.contact = contact;
47  this.link = link;
48  this.message = message;
49  this.exParam = exParam;
50  }
51 
52  public String getMessage(Date validUntil, String comment) {
53  if(contact == null) return null;
54 
55  if(message.indexOf("{linkTmpSes}") > -1) {
56  try {
57 // String key = createTmpKey(validUntil);
58 // String server = ElephantContext.getServerUrl("http") +
59 // (exParam == null ? "?" : exParam + "&") + "tmpses=" + key;
60  String server = ElephantContext.getServerUrl("http") + "?" +
62  message = message.replaceAll("\\{linkTmpSes\\}", "<a style=\"color:inherit\" href=\"" + server + "\">" + link + "</a>");
63  } catch (Exception ex) {
64  Logger.getLogger(MailContact.class.getName()).log(Level.SEVERE, null, ex);
65  }
66  }
67  message = processWebLiveLinks(validUntil, message);
68  message = processWebLinks(message);
69  message = processLinkMacros(message);
70 
71  message = message.replaceAll("\\{login\\}", contact.getLogin())
72  .replaceAll("\\{name\\}", contact.getName())
73  .replaceAll("\\{comment\\}", comment);
74 
75  String serverBase = ElephantContext.getServerBase("http");
76  return message.replaceAll("href=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "href=\\'" + serverBase + "$1$2\\'")
77  .replaceAll("src=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "src=\\'" + serverBase + "$1$2\\'")
78  .replaceAll("href=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "href=\\\"" + serverBase + "$1$2\\\"")
79  .replaceAll("src=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "src=\\\"" + serverBase + "$1$2\\\"");
80  }
81 
82  public String getEmail() {
83  return getEmail(contact);
84  }
85 
86 // public String createTmpKey(Date validUntil) {
87 // if(validUntil == null) {
88 // validUntil = new Date(new Date().getTime() + (2L * 24L * 60L * 60L * 1000L));
89 // }
90 // IUser user = ContextFactory.getUser(DummyApplication.getInstance().getConstructor());
91 // if (user.impersonate(contact.getLogin())) {
92 // return user.createTmpKey(validUntil);
93 // }
94 // return null;
95 // }
96 
97  public static String getEmail(IContact contact) {
98  if (contact == null) {
99  return null;
100  }
101  String email = contact.getConnector(IUser.CONNECTOR_EMAIL);
102  return email == null ? null : (!Contacts.isValidEmail(email) ? null : email);
103  }
104 
105  public static void sendToIPlayers(List<IContact> contacts, String link, String subject,
106  String message, String comment, String exParam, String implementation) {
108  if(mp != null) {
110  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
111 
112  Set<MailContact> emails = new HashSet<MailContact>();
113  for(IContact contact : contacts) {
114  emails.add(new MailContact(contact, link, message, exParam));
115  }
116  for(MailContact mc : emails) {
117  mp.addToPool(null, mc.getEmail(), null, subject, mc.getMessage(null, comment), "text/html");
118  }
119  mp.sendPool();
120  }
121  }
122 
123  public static void poolToIPlayers(List<IContact> contacts, String link,
124  String message, String comment, String exParam, MessagePool pool) {
125  Set<MailContact> emails = new HashSet<MailContact>();
126  for(IContact contact : contacts) {
127  emails.add(new MailContact(contact, link, message, exParam));
128  }
129  for(MailContact mc : emails) {
130  pool.addMessage(mc.getEmail(), mc.getMessage(null, comment));
131  }
132  }
133 
134  @Override
135  public boolean equals(Object obj) {
136  if (obj == null) {
137  return false;
138  }
139  if (getClass() != obj.getClass()) {
140  return false;
141  }
142  final MailContact other = (MailContact) obj;
143  if (this.contact.getId() != other.contact.getId() && (this.contact.getId() == null || !this.contact.getId().equals(other.contact.getId()))) {
144  return false;
145  }
146  return true;
147  }
148 
149  @Override
150  public int hashCode() {
151  int hash = 7;
152  hash = 31 * hash + (this.contact != null && this.contact.getId() != null ? this.contact.getId().hashCode() : 0);
153  return hash;
154  }
155 
156  public static String createLink(String text, String path, boolean live) {
157  if(!Strings.isBlank(text)) {
158  StringBuilder sb = new StringBuilder((live ? "{livelink:" : "{link:"));
159  sb.append(text.replaceAll("\\:", "&colon;").replaceAll("\\}", "&endkey;"));
160  if(!Strings.isBlank(path)) {
161  sb.append(":");
162  sb.append(path);
163  }
164  sb.append("}");
165  return sb.toString();
166  }
167  return "";
168  }
169 
170  public static String processLinkMacros(String text) {
171  if(!Strings.isBlank(text)) {
172  return text.replaceAll("\\&colon\\;", ":").replaceAll("\\&endkey\\;", "}");
173  }
174  return "";
175  }
176 
177  private String nextParamSep(String params) {
178  if(params.indexOf("?") > -1) {
179  return "&";
180  }
181  return "?";
182  }
183 
184  private String processWebLiveLinks(Date validUntil, String message) {
185 
186  if(message.contains("{livelink")) {
187  Pattern pat = Pattern.compile(LIVE_LINK_PATTERN);
188  Matcher mat = pat.matcher(message);
189  StringBuffer result = new StringBuffer();
190  while(mat.find()) {
191  try {
192 // String key = createTmpKey(validUntil);
193  String currParam = exParam;
194  if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(2))) {
195  currParam = (currParam == null ? "" : currParam) + mat.group(2);
196  }
197 // String server = ElephantContext.getServerUrl("http") +
198 // (currParam == null ? "" : currParam + nextParamSep(currParam)) + "tmpses=" + key;
199  currParam = currParam == null ? "" : currParam + nextParamSep(currParam);
200  String server = ElephantContext.getServerUrl("http") + "?" +
201  Actions.createAction(contact.getConnector(IUser.CONNECTOR_EMAIL), currParam);
202  mat.appendReplacement(result, "<a href=\"" + server + "\">" + mat.group(1) + "</a>");
203  } catch (Exception ex) {
204  Logger.getLogger(MailContact.class.getName()).log(Level.SEVERE, null, ex);
205  }
206  }
207  mat.appendTail(result);
208  return result.toString();
209  }
210  return message;
211 
212  }
213 
214  private String processWebLinks(String message) {
215 
216  if(message.contains("{link")) {
217  Pattern pat = Pattern.compile(LINK_PATTERN);
218  Matcher mat = pat.matcher(message);
219  StringBuffer result = new StringBuffer();
220  while(mat.find()) {
221  try {
222  String currParam = exParam;
223  if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(2))) {
224  currParam = (currParam == null ? "" : currParam) + mat.group(2);
225  }
226 // String server = ElephantContext.getServerUrl("http") +
227 // (currParam == null ? "" : currParam);
228  currParam = currParam == null ? "" : currParam;
229  String server = ElephantContext.getServerUrl("http") + "?" +
230  Actions.createAction(contact.getConnector(IUser.CONNECTOR_EMAIL), currParam);
231  mat.appendReplacement(result, "<a href=\"" + server + "\">" + mat.group(1) + "</a>");
232  } catch (Exception ex) {
233  Logger.getLogger(MailContact.class.getName()).log(Level.SEVERE, null, ex);
234  }
235  }
236  mat.appendTail(result);
237  return result.toString();
238  }
239  return message;
240 
241  }
242 
243  private static final String LIVE_LINK_PATTERN = "\\{livelink\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
244  private static final String LINK_PATTERN = "\\{link\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
245 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static boolean isValidEmail(String email)
Definition: Contacts.java:99
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)
MailContact(IContact contact, String link, String message, String exParam)
static void sendToIPlayers(List< IContact > contacts, String link, String subject, String message, String comment, String exParam, String implementation)
static void poolToIPlayers(List< IContact > contacts, String link, String message, String comment, String exParam, MessagePool pool)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27