// Send commment notifications in locale of server
ResourceBundle resources = ResourceBundle.getBundle("ApplicationResources");
WeblogEntryData entry = cd.getWeblogEntry();
WebsiteData site = entry.getWebsite();
UserData user = entry.getCreator();
// Send e-mail to owner and subscribed users (if enabled)
boolean notify = RollerRuntimeConfig.getBooleanProperty("users.comments.emailnotify");
if (notify && site.getEmailComments().booleanValue()) {
log.debug("Comment notification enabled ... preparing email");
// Determine message and addressing options from init parameters
boolean separateMessages =
RollerConfig.getBooleanProperty("comment.notification.separateOwnerMessage");
boolean hideCommenterAddrs =
RollerConfig.getBooleanProperty("comment.notification.hideCommenterAddresses");
//------------------------------------------
// --- Determine the "from" address
// --- Use either the site configured from address or the user's address
String from =
(StringUtils.isEmpty(site.getEmailFromAddress()))
? user.getEmailAddress()
: site.getEmailFromAddress();
//------------------------------------------
// --- Build list of email addresses to send notification to
List comments = null;
try {
WeblogManager wMgr = RollerFactory.getRoller().getWeblogManager();
// get only approved, non spam comments
comments = entry.getComments(true, true);
} catch(RollerException re) {
// should never happen
comments = new ArrayList();
}
// Get all the subscribers to this comment thread
Set subscribers = new TreeSet();
for (Iterator it = comments.iterator(); it.hasNext();) {
CommentData comment = (CommentData) it.next();
if (!StringUtils.isEmpty(comment.getEmail())) {
// If user has commented twice,
// count the most recent notify setting
if (comment.getNotify().booleanValue()) {
// only add those with valid email
if (comment.getEmail().matches(EMAIL_ADDR_REGEXP)) {
subscribers.add(comment.getEmail());
}
} else {
// remove user who doesn't want to be notified
subscribers.remove(comment.getEmail());
}
}
}
// Form array of commenter addrs
String[] commenterAddrs = (String[])subscribers.toArray(new String[0]);
//------------------------------------------
// --- Form the messages to be sent -
// For simplicity we always build separate owner and commenter messages even if sending a single one
// Determine with mime type to use for e-mail
StringBuffer msg = new StringBuffer();
StringBuffer ownermsg = new StringBuffer();
boolean escapeHtml = RollerRuntimeConfig.getBooleanProperty("users.comments.escapehtml");
if (!escapeHtml) {
msg.append("<html><body style=\"background: white; ");
msg.append(" color: black; font-size: 12px\">");
}
if (!StringUtils.isEmpty(cd.getName())) {
msg.append(cd.getName() + " "
+ resources.getString("email.comment.wrote")+": ");
} else {
msg.append(resources.getString("email.comment.anonymous")+": ");
}
msg.append((escapeHtml) ? "\n\n" : "<br /><br />");
msg.append((escapeHtml) ? Utilities.escapeHTML(cd.getContent())
: UtilitiesModel.transformToHTMLSubset(Utilities.escapeHTML(cd.getContent())));
msg.append((escapeHtml) ? "\n\n----\n"
: "<br /><br /><hr /><span style=\"font-size: 11px\">");
msg.append(resources.getString("email.comment.respond") + ": ");
msg.append((escapeHtml) ? "\n" : "<br />");
// Build link back to comment
StringBuffer commentURL = new StringBuffer(rootURL);
commentURL.append(entry.getPermaLink());
commentURL.append("#comments");
if (escapeHtml) {
msg.append(commentURL.toString());
} else {
msg.append("<a href=\""+commentURL+"\">"+commentURL+"</a></span>");
}
ownermsg.append(msg);
// add link to weblog edit page so user can login to manage comments
ownermsg.append((escapeHtml) ? "\n\n----\n" :
"<br /><br /><hr /><span style=\"font-size: 11px\">");
ownermsg.append("Link to comment management page:");
ownermsg.append((escapeHtml) ? "\n" : "<br />");
StringBuffer deleteURL = new StringBuffer(rootURL);
deleteURL.append("/roller-ui/authoring/commentManagement.do?method=query&entryId=" + entry.getId());
if (escapeHtml) {
ownermsg.append(deleteURL.toString());
} else {
ownermsg.append(
"<a href=\"" + deleteURL + "\">" + deleteURL + "</a></span>");
msg.append("</Body></html>");
ownermsg.append("</Body></html>");
}
String subject = null;
if ((subscribers.size() > 1) ||
(StringUtils.equals(cd.getEmail(), user.getEmailAddress()))) {
subject= "RE: "+resources.getString("email.comment.title")+": ";
} else {
subject = resources.getString("email.comment.title") + ": ";
}
subject += entry.getTitle();
//------------------------------------------
// --- Send message to email recipients
try {
Context ctx = (Context)
new InitialContext().lookup("java:comp/env");
Session session = (Session)ctx.lookup("mail/Session");
boolean isHtml = !escapeHtml;
if (separateMessages) {
// Send separate messages to owner and commenters
sendMessage(session, from,
new String[]{user.getEmailAddress()}, null, null, subject, ownermsg.toString(), isHtml);
if (commenterAddrs.length > 0) {
// If hiding commenter addrs, they go in Bcc: otherwise in the To: of the second message
String[] to = hideCommenterAddrs ? null : commenterAddrs;
String[] bcc = hideCommenterAddrs ? commenterAddrs : null;
sendMessage(session, from, to, null, bcc, subject, msg.toString(), isHtml);
}
} else {
// Single message. User in To: header, commenters in either cc or bcc depending on hiding option
String[] cc = hideCommenterAddrs ? null : commenterAddrs;
String[] bcc = hideCommenterAddrs ? commenterAddrs : null;
sendMessage(session, from, new String[]{user.getEmailAddress()}, cc, bcc, subject,
ownermsg.toString(), isHtml);
}
} catch (NamingException ne) {
log.error("Unable to lookup mail session. Check configuration. NamingException: " + ne.getMessage());
} catch (Exception e) {