001: /*
002: * Created on 26/08/2006 22:20:46
003: */
004: package net.jforum.api.integration.mail.pop;
005:
006: import java.util.HashMap;
007: import java.util.Iterator;
008:
009: import org.apache.log4j.Logger;
010:
011: import freemarker.template.SimpleHash;
012:
013: import net.jforum.JForumExecutionContext;
014: import net.jforum.SessionFacade;
015: import net.jforum.context.JForumContext;
016: import net.jforum.context.RequestContext;
017: import net.jforum.context.standard.StandardRequestContext;
018: import net.jforum.context.standard.StandardSessionContext;
019: import net.jforum.dao.DataAccessDriver;
020: import net.jforum.dao.ForumDAO;
021: import net.jforum.entities.Topic;
022: import net.jforum.entities.User;
023: import net.jforum.entities.UserSession;
024: import net.jforum.util.preferences.ConfigKeys;
025: import net.jforum.view.forum.PostAction;
026:
027: /**
028: * @author Rafael Steil
029: * @version $Id: POPPostAction.java,v 1.10 2007/07/31 13:52:47 rafaelsteil Exp $
030: */
031: public class POPPostAction {
032: private static Logger logger = Logger
033: .getLogger(POPPostAction.class);
034:
035: public void insertMessages(POPParser parser) {
036: long ms = System.currentTimeMillis();
037: int counter = 0;
038:
039: try {
040: JForumExecutionContext ex = JForumExecutionContext.get();
041:
042: RequestContext request = new StandardRequestContext();
043: ex
044: .setForumContext(new JForumContext("/", "",
045: request, null));
046:
047: JForumExecutionContext.set(ex);
048:
049: SessionFacade.setAttribute(ConfigKeys.TOPICS_READ_TIME,
050: new HashMap());
051:
052: for (Iterator iter = parser.getMessages().iterator(); iter
053: .hasNext();) {
054: POPMessage m = (POPMessage) iter.next();
055: String sessionId = ms + m.getSender() + counter++;
056:
057: request.getSessionContext().setAttribute(
058: StandardSessionContext.SESSION_ID, sessionId);
059:
060: User user = this .findUser(m.getSender());
061:
062: if (user == null) {
063: logger
064: .warn("Could not find user with email "
065: + m.getSender()
066: + ". Ignoring his message.");
067: continue;
068: }
069:
070: try {
071: UserSession us = new UserSession();
072: us.setUserId(user.getId());
073: us.setUsername(us.getUsername());
074: us.setSessionId(sessionId);
075:
076: SessionFacade.add(us, sessionId);
077: SessionFacade.setAttribute(ConfigKeys.LOGGED, "1");
078:
079: SessionFacade
080: .removeAttribute(ConfigKeys.LAST_POST_TIME);
081: SessionFacade.setAttribute(
082: ConfigKeys.REQUEST_IGNORE_CAPTCHA, "1");
083:
084: this .insertMessage(m, user);
085: } finally {
086: SessionFacade.remove(sessionId);
087: }
088: }
089: } finally {
090: JForumExecutionContext.finish();
091: }
092: }
093:
094: /**
095: * Calls {@link PostAction#insertSave()}
096: * @param m the mail message
097: * @param user the user who's sent the message
098: */
099: private void insertMessage(POPMessage m, User user) {
100: this .addDataToRequest(m, user);
101:
102: PostAction postAction = new PostAction(JForumExecutionContext
103: .getRequest(), new SimpleHash());
104: postAction.insertSave();
105: }
106:
107: /**
108: * Extracts information from a mail message and adds it to the request context
109: * @param m the mail message
110: * @param user the user who's sending the message
111: */
112: private void addDataToRequest(POPMessage m, User user) {
113: RequestContext request = JForumExecutionContext.getRequest();
114:
115: request.addParameter("forum_id", Integer.toString(this
116: .discoverForumId(m.getListEmail())));
117: request.addParameter("topic_type", Integer
118: .toString(Topic.TYPE_NORMAL));
119: request.addParameter("quick", "1");
120: request.addParameter("subject", m.getSubject());
121: request.addParameter("message", m.getMessage());
122:
123: int topicId = this .discoverTopicId(m);
124:
125: if (topicId > 0) {
126: request.addParameter("topic_id", Integer.toString(topicId));
127: }
128:
129: if (!user.isBbCodeEnabled()) {
130: request.addParameter("disable_bbcode", "on");
131: }
132:
133: if (!user.isSmiliesEnabled()) {
134: request.addParameter("disable_smilies", "on");
135: }
136:
137: if (!user.isHtmlEnabled()) {
138: request.addParameter("disable_html", "on");
139: }
140: }
141:
142: /**
143: * Tries to extract message relationship from the headers
144: * @param m the message to extract headers from
145: * @return the topic id, if found, or 0 (zero) othwerwise
146: */
147: private int discoverTopicId(POPMessage m) {
148: int topicId = 0;
149:
150: String inReplyTo = m.getInReplyTo();
151:
152: if (inReplyTo != null) {
153: topicId = MessageId.parse(inReplyTo).getTopicId();
154: }
155:
156: return topicId;
157: }
158:
159: /**
160: * Given an email address, finds the forum instance associated to it
161: * @param listEmail the forum's email address to search for
162: * @return the forum's id, or 0 (zero) if nothing was found
163: */
164: private int discoverForumId(String listEmail) {
165: ForumDAO dao = DataAccessDriver.getInstance().newForumDAO();
166: return dao.discoverForumId(listEmail);
167: }
168:
169: /**
170: * Finds an user by his email address
171: * @param email the email address to use in the search
172: * @return the matching record, or null if nothing was found
173: */
174: private User findUser(String email) {
175: return DataAccessDriver.getInstance().newUserDAO().findByEmail(
176: email);
177: }
178: }
|