001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 04/03/2004 - 20:32:13
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.util.mail;
044:
045: import java.text.MessageFormat;
046: import java.util.List;
047:
048: import net.jforum.api.integration.mail.pop.MessageId;
049: import net.jforum.entities.Post;
050: import net.jforum.entities.Topic;
051: import net.jforum.util.preferences.ConfigKeys;
052: import net.jforum.util.preferences.SystemGlobals;
053: import net.jforum.view.forum.common.PostCommon;
054: import net.jforum.view.forum.common.ViewCommon;
055: import freemarker.template.SimpleHash;
056:
057: /**
058: * Notify users of replies to existing topics
059: * @author Rafael Steil
060: * @version $Id: TopicReplySpammer.java,v 1.5 2007/08/20 19:35:51 rafaelsteil Exp $
061: */
062: public class TopicReplySpammer extends Spammer {
063: /**
064: * Creates a new instance with a message's contents send
065: * @param topic the topic we are replying to
066: * @param post the post instance, with the message's contents. If null,
067: * only a notification will be sent
068: * @param users list of users who'll be notified
069: */
070: public TopicReplySpammer(Topic topic, Post post, List users) {
071: // Make the topic url
072: StringBuffer page = new StringBuffer();
073: int postsPerPage = SystemGlobals
074: .getIntValue(ConfigKeys.POSTS_PER_PAGE);
075:
076: if (topic.getTotalReplies() >= postsPerPage) {
077: page.append(
078: ((topic.getTotalReplies() / postsPerPage))
079: * postsPerPage).append('/');
080: }
081:
082: String forumLink = ViewCommon.getForumLink();
083:
084: String path = this .messageLink(topic, page, forumLink);
085: String unwatch = this .unwatchLink(topic, forumLink);
086:
087: SimpleHash params = new SimpleHash();
088:
089: params.put("topic", topic);
090: params.put("path", path);
091: params.put("forumLink", forumLink);
092: params.put("unwatch", unwatch);
093:
094: if (post != null) {
095: this .setMessageId(MessageId.buildMessageId(post.getId(),
096: topic.getId(), topic.getForumId()));
097:
098: post = PostCommon.preparePostForDisplay(post);
099: params.put("message", post.getText());
100: }
101:
102: this .setUsers(users);
103:
104: if (topic.getFirstPostId() != post.getId()) {
105: this .setInReplyTo(MessageId.buildInReplyTo(topic));
106: }
107:
108: this .setTemplateParams(params);
109: String subject = SystemGlobals
110: .getValue(ConfigKeys.MAIL_NEW_ANSWER_SUBJECT);
111:
112: this .prepareMessage(MessageFormat.format(subject,
113: new Object[] { topic.getTitle() }), SystemGlobals
114: .getValue(ConfigKeys.MAIL_NEW_ANSWER_MESSAGE_FILE));
115: }
116:
117: /**
118: * Creates the "unwatch" link for the current topic
119: * @param topic the topic
120: * @param forumLink the forum's link
121: * @return the unwath link
122: */
123: private String unwatchLink(Topic topic, String forumLink) {
124: return new StringBuffer(128).append(forumLink).append(
125: "posts/unwatch/").append(topic.getId()).append(
126: SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION))
127: .toString();
128: }
129:
130: /**
131: * Creates the link to read the message online
132: * @param topic the topic
133: * @param page the current topic's page
134: * @param forumLink the forum's link
135: * @return the link to the message
136: */
137: private String messageLink(Topic topic, StringBuffer page,
138: String forumLink) {
139: return new StringBuffer(128).append(forumLink).append(
140: "posts/list/").append(page.toString()).append(
141: topic.getId()).append(
142: SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION))
143: .append('#').append(topic.getLastPostId()).toString();
144: }
145: }
|