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.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;
43 private String link, message, exParam;
46 this.contact = contact;
48 this.message = message;
49 this.exParam = exParam;
52 public String
getMessage(Date validUntil, String comment) {
53 if(contact ==
null)
return null;
55 if(message.indexOf(
"{linkTmpSes}") > -1) {
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);
67 message = processWebLiveLinks(validUntil, message);
68 message = processWebLinks(message);
71 message = message.replaceAll(
"\\{login\\}", contact.
getLogin())
72 .replaceAll(
"\\{name\\}", contact.
getName())
73 .replaceAll(
"\\{comment\\}", comment);
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\\\"");
98 if (contact ==
null) {
105 public static void sendToIPlayers(List<IContact> contacts, String link, String subject,
106 String message, String comment, String exParam, String implementation) {
112 Set<MailContact> emails =
new HashSet<MailContact>();
114 emails.add(
new MailContact(contact, link, message, exParam));
117 mp.
addToPool(
null, mc.getEmail(),
null, subject, mc.getMessage(
null, comment),
"text/html");
124 String message, String comment, String exParam, MessagePool pool) {
125 Set<MailContact> emails =
new HashSet<MailContact>();
127 emails.add(
new MailContact(contact, link, message, exParam));
130 pool.addMessage(mc.getEmail(), mc.getMessage(
null, comment));
139 if (getClass() != obj.getClass()) {
143 if (this.contact.
getId() != other.contact.
getId() && (this.contact.
getId() ==
null || !this.contact.
getId().equals(other.contact.
getId()))) {
152 hash = 31 * hash + (this.contact !=
null && this.contact.
getId() !=
null ? this.contact.
getId().hashCode() : 0);
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(
"\\:",
":").replaceAll(
"\\}",
"&endkey;"));
160 if(!Strings.isBlank(path)) {
165 return sb.toString();
171 if(!Strings.isBlank(text)) {
172 return text.replaceAll(
"\\&colon\\;",
":").replaceAll(
"\\&endkey\\;",
"}");
177 private String nextParamSep(String params) {
178 if(params.indexOf(
"?") > -1) {
184 private String processWebLiveLinks(Date validUntil, String message) {
186 if(message.contains(
"{livelink")) {
187 Pattern pat = Pattern.compile(LIVE_LINK_PATTERN);
188 Matcher mat = pat.matcher(message);
189 StringBuffer result =
new StringBuffer();
193 String currParam = exParam;
194 if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(2))) {
195 currParam = (currParam ==
null ?
"" : currParam) + mat.group(2);
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);
207 mat.appendTail(result);
208 return result.toString();
214 private String processWebLinks(String message) {
216 if(message.contains(
"{link")) {
217 Pattern pat = Pattern.compile(LINK_PATTERN);
218 Matcher mat = pat.matcher(message);
219 StringBuffer result =
new StringBuffer();
222 String currParam = exParam;
223 if(mat.groupCount() > 1 && !Strings.isBlank(mat.group(2))) {
224 currParam = (currParam ==
null ?
"" : currParam) + mat.group(2);
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);
236 mat.appendTail(result);
237 return result.toString();
243 private static final String LIVE_LINK_PATTERN =
"\\{livelink\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
244 private static final String LINK_PATTERN =
"\\{link\\:([^\\:\\}]*)\\:?([^\\:\\}]*)\\}";
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 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)
static final String CONNECTOR_EMAIL