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: * Created on 24/09/2006 23:04:29
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.api.integration.mail.pop;
044:
045: import java.util.Random;
046: import java.util.regex.Matcher;
047: import java.util.regex.Pattern;
048:
049: import net.jforum.entities.Topic;
050:
051: /**
052: * Represents the In-Reply-To and Message-ID mail header.
053: *
054: * @author Rafael Steil
055: * @version $Id: MessageId.java,v 1.4 2006/10/09 03:05:54 rafaelsteil Exp $
056: */
057: public class MessageId {
058: private static final Random random = new Random(System
059: .currentTimeMillis());
060: private int topicId;
061:
062: /**
063: * Returns the topic id this header holds.
064: *
065: * @return the topic id represented by this instance
066: */
067: public int getTopicId() {
068: return this .topicId;
069: }
070:
071: /**
072: * Constructs the Message-ID header.
073: * The form is "<postId.topicId.forumId.randomNumber@jforum>".
074: *
075: * @param postId the post id of this message
076: * @param topicId the topic id of this message
077: * @param forumId the forum id of this message
078: * @return the Message-ID header
079: */
080: public static String buildMessageId(int postId, int topicId,
081: int forumId) {
082: return new StringBuffer().append('<').append(postId)
083: .append('.').append(topicId).append('.')
084: .append(forumId).append('.').append(
085: System.currentTimeMillis()).append(
086: random.nextInt(999999999)).append("@jforum>")
087: .toString();
088: }
089:
090: /**
091: * Constructs the In-Reply-To header.
092: * The form is "<topicFirstPostId.topicId.forumId.randomNumber@jforum>".
093: *
094: * @param topic The topic we're replying to. If should have at least the
095: * values for {@link Topic#getFirstPostId()}, {@link Topic#getId()}
096: * and {@link Topic#getForumId()}
097: *
098: * @return the In-Reply-To header
099: */
100: public static String buildInReplyTo(Topic topic) {
101: return buildMessageId(topic.getFirstPostId(), topic.getId(),
102: topic.getForumId());
103: }
104:
105: /**
106: * Parses the header, extracting the information it holds
107: * @param header the header's contents to parse
108: * @return the header information parsed
109: */
110: public static MessageId parse(String header) {
111: MessageId messageId = new MessageId();
112:
113: if (header != null) {
114: // <postId.topicId.forumId.randomNumber@host>
115: Matcher matcher = Pattern.compile(
116: "<(.*?)\\.(.*?)\\.(.*?)\\.(.*?)@.*>").matcher(
117: header);
118:
119: if (matcher.matches()) {
120: String s = matcher.group(2);
121:
122: try {
123: messageId.topicId = Integer.parseInt(s);
124: } catch (Exception e) {
125: }
126: }
127: }
128:
129: return messageId;
130: }
131: }
|