001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.pop;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.pop.MessageListener;
020    import com.liferay.portal.kernel.pop.MessageListenerException;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.ObjectValuePair;
024    import com.liferay.portal.kernel.util.PrefsPropsUtil;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.StreamUtil;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.model.Company;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.security.auth.PrincipalException;
031    import com.liferay.portal.security.permission.PermissionCheckerUtil;
032    import com.liferay.portal.service.CompanyLocalServiceUtil;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.UserLocalServiceUtil;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portal.util.PortletKeys;
037    import com.liferay.portal.util.PropsValues;
038    import com.liferay.portlet.messageboards.NoSuchCategoryException;
039    import com.liferay.portlet.messageboards.NoSuchMessageException;
040    import com.liferay.portlet.messageboards.model.MBCategory;
041    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
042    import com.liferay.portlet.messageboards.model.MBMessage;
043    import com.liferay.portlet.messageboards.model.MBMessageConstants;
044    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
045    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
046    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
047    import com.liferay.portlet.messageboards.util.MBMailMessage;
048    import com.liferay.portlet.messageboards.util.MBUtil;
049    
050    import java.io.InputStream;
051    
052    import java.util.List;
053    
054    import javax.mail.Message;
055    import javax.mail.MessagingException;
056    
057    import org.apache.commons.lang.time.StopWatch;
058    
059    /**
060     * @author Brian Wing Shun Chan
061     * @author Jorge Ferrer
062     * @author Michael C. Han
063     */
064    public class MessageListenerImpl implements MessageListener {
065    
066            @Override
067            public boolean accept(String from, String recipient, Message message) {
068                    try {
069                            if (isAutoReply(message)) {
070                                    return false;
071                            }
072    
073                            String messageIdString = getMessageIdString(recipient, message);
074    
075                            if ((messageIdString == null) ||
076                                    !messageIdString.startsWith(
077                                            MBUtil.MESSAGE_POP_PORTLET_PREFIX, getOffset())) {
078    
079                                    return false;
080                            }
081    
082                            Company company = getCompany(messageIdString);
083                            long categoryId = getCategoryId(messageIdString);
084    
085                            MBCategory category = MBCategoryLocalServiceUtil.getCategory(
086                                    categoryId);
087    
088                            if ((category.getCompanyId() != company.getCompanyId()) &&
089                                    !category.isRoot()) {
090    
091                                    return false;
092                            }
093    
094                            if (_log.isDebugEnabled()) {
095                                    _log.debug("Check to see if user " + from + " exists");
096                            }
097    
098                            String pop3User = PrefsPropsUtil.getString(
099                                    PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
100                                    PropsValues.MAIL_SESSION_MAIL_POP3_USER);
101    
102                            if (from.equalsIgnoreCase(pop3User)) {
103                                    return false;
104                            }
105    
106                            UserLocalServiceUtil.getUserByEmailAddress(
107                                    company.getCompanyId(), from);
108    
109                            return true;
110                    }
111                    catch (Exception e) {
112                            if (_log.isErrorEnabled()) {
113                                    _log.error("Unable to process message: " + message, e);
114                            }
115    
116                            return false;
117                    }
118            }
119    
120            @Override
121            public void deliver(String from, String recipient, Message message)
122                    throws MessageListenerException {
123    
124                    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = null;
125    
126                    try {
127                            StopWatch stopWatch = null;
128    
129                            if (_log.isDebugEnabled()) {
130                                    stopWatch = new StopWatch();
131    
132                                    stopWatch.start();
133    
134                                    _log.debug("Deliver message from " + from + " to " + recipient);
135                            }
136    
137                            String messageIdString = getMessageIdString(recipient, message);
138    
139                            Company company = getCompany(messageIdString);
140    
141                            if (_log.isDebugEnabled()) {
142                                    _log.debug("Message id " + messageIdString);
143                            }
144    
145                            long groupId = 0;
146                            long categoryId = getCategoryId(messageIdString);
147    
148                            try {
149                                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(
150                                            categoryId);
151    
152                                    groupId = category.getGroupId();
153    
154                                    if (category.isRoot()) {
155                                            long messageId = getMessageId(messageIdString);
156    
157                                            MBMessage threadMessage =
158                                                    MBMessageLocalServiceUtil.fetchMBMessage(messageId);
159    
160                                            if (threadMessage != null) {
161                                                    groupId = threadMessage.getGroupId();
162                                            }
163                                    }
164                            }
165                            catch (NoSuchCategoryException nsce) {
166                                    groupId = categoryId;
167                                    categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
168                            }
169    
170                            if (_log.isDebugEnabled()) {
171                                    _log.debug("Group id " + groupId);
172                                    _log.debug("Category id " + categoryId);
173                            }
174    
175                            User user = UserLocalServiceUtil.getUserByEmailAddress(
176                                    company.getCompanyId(), from);
177    
178                            long parentMessageId = getParentMessageId(recipient, message);
179    
180                            if (_log.isDebugEnabled()) {
181                                    _log.debug("Parent message id " + parentMessageId);
182                            }
183    
184                            MBMessage parentMessage = null;
185    
186                            try {
187                                    if (parentMessageId > 0) {
188                                            parentMessage = MBMessageLocalServiceUtil.getMessage(
189                                                    parentMessageId);
190                                    }
191                            }
192                            catch (NoSuchMessageException nsme) {
193    
194                                    // If the parent message does not exist we ignore it and post
195                                    // the message as a new thread.
196    
197                            }
198    
199                            if (_log.isDebugEnabled()) {
200                                    _log.debug("Parent message " + parentMessage);
201                            }
202    
203                            String subject = MBUtil.getSubjectWithoutMessageId(message);
204    
205                            MBMailMessage mbMailMessage = new MBMailMessage();
206    
207                            MBUtil.collectPartContent(message, mbMailMessage);
208    
209                            inputStreamOVPs = mbMailMessage.getInputStreamOVPs();
210    
211                            PermissionCheckerUtil.setThreadValues(user);
212    
213                            ServiceContext serviceContext = new ServiceContext();
214    
215                            serviceContext.setAddGroupPermissions(true);
216                            serviceContext.setAddGuestPermissions(true);
217                            serviceContext.setLayoutFullURL(
218                                    PortalUtil.getLayoutFullURL(
219                                            groupId, PortletKeys.MESSAGE_BOARDS));
220                            serviceContext.setScopeGroupId(groupId);
221    
222                            if (parentMessage == null) {
223                                    MBMessageServiceUtil.addMessage(
224                                            groupId, categoryId, subject, mbMailMessage.getBody(),
225                                            MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, false,
226                                            0.0, true, serviceContext);
227                            }
228                            else {
229                                    MBMessageServiceUtil.addMessage(
230                                            parentMessage.getMessageId(), subject,
231                                            mbMailMessage.getBody(), MBMessageConstants.DEFAULT_FORMAT,
232                                            inputStreamOVPs, false, 0.0, true, serviceContext);
233                            }
234    
235                            if (_log.isDebugEnabled()) {
236                                    _log.debug(
237                                            "Delivering message takes " + stopWatch.getTime() + " ms");
238                            }
239                    }
240                    catch (PrincipalException pe) {
241                            if (_log.isDebugEnabled()) {
242                                    _log.debug("Prevented unauthorized post from " + from);
243                            }
244    
245                            throw new MessageListenerException(pe);
246                    }
247                    catch (Exception e) {
248                            _log.error(e, e);
249    
250                            throw new MessageListenerException(e);
251                    }
252                    finally {
253                            if (inputStreamOVPs != null) {
254                                    for (ObjectValuePair<String, InputStream> inputStreamOVP :
255                                                    inputStreamOVPs) {
256    
257                                            InputStream inputStream = inputStreamOVP.getValue();
258    
259                                            StreamUtil.cleanUp(inputStream);
260                                    }
261                            }
262    
263                            PermissionCheckerUtil.setThreadValues(null);
264                    }
265            }
266    
267            @Override
268            public String getId() {
269                    return MessageListenerImpl.class.getName();
270            }
271    
272            protected long getCategoryId(String messageIdString) {
273                    String[] parts = getMessageIdStringParts(messageIdString);
274    
275                    return GetterUtil.getLong(parts[0]);
276            }
277    
278            protected Company getCompany(String messageIdString) throws Exception {
279                    int pos =
280                            messageIdString.indexOf(CharPool.AT) +
281                                    PropsValues.POP_SERVER_SUBDOMAIN.length() + 1;
282    
283                    if (PropsValues.POP_SERVER_SUBDOMAIN.length() > 0) {
284                            pos++;
285                    }
286    
287                    int endPos = messageIdString.indexOf(CharPool.GREATER_THAN, pos);
288    
289                    if (endPos == -1) {
290                            endPos = messageIdString.length();
291                    }
292    
293                    String mx = messageIdString.substring(pos, endPos);
294    
295                    return CompanyLocalServiceUtil.getCompanyByMx(mx);
296            }
297    
298            protected long getMessageId(String messageIdString) {
299                    String[] parts = getMessageIdStringParts(messageIdString);
300    
301                    return GetterUtil.getLong(parts[1]);
302            }
303    
304            protected String getMessageIdString(String recipient, Message message)
305                    throws Exception {
306    
307                    if (PropsValues.POP_SERVER_SUBDOMAIN.length() > 0) {
308                            return recipient;
309                    }
310                    else {
311                            return MBUtil.getParentMessageIdString(message);
312                    }
313            }
314    
315            protected String[] getMessageIdStringParts(String messageIdString) {
316                    int pos = messageIdString.indexOf(CharPool.AT);
317    
318                    String target = messageIdString.substring(
319                            MBUtil.MESSAGE_POP_PORTLET_PREFIX.length() + getOffset(), pos);
320    
321                    return StringUtil.split(target, CharPool.PERIOD);
322            }
323    
324            protected int getOffset() {
325                    if (PropsValues.POP_SERVER_SUBDOMAIN.length() == 0) {
326                            return 1;
327                    }
328    
329                    return 0;
330            }
331    
332            protected long getParentMessageId(String recipient, Message message)
333                    throws Exception {
334    
335                    if (!StringUtil.startsWith(
336                                    recipient, MBUtil.MESSAGE_POP_PORTLET_PREFIX)) {
337    
338                            return MBUtil.getParentMessageId(message);
339                    }
340    
341                    int pos = recipient.indexOf(CharPool.AT);
342    
343                    if (pos < 0) {
344                            return MBUtil.getParentMessageId(message);
345                    }
346    
347                    String target = recipient.substring(
348                            MBUtil.MESSAGE_POP_PORTLET_PREFIX.length(), pos);
349    
350                    String[] parts = StringUtil.split(target, CharPool.PERIOD);
351    
352                    long parentMessageId = 0;
353    
354                    if (parts.length == 2) {
355                            parentMessageId = GetterUtil.getLong(parts[1]);
356                    }
357    
358                    if (parentMessageId > 0) {
359                            return parentMessageId;
360                    }
361    
362                    return MBUtil.getParentMessageId(message);
363            }
364    
365            protected boolean isAutoReply(Message message) throws MessagingException {
366                    String[] autoReply = message.getHeader("X-Autoreply");
367    
368                    if ((autoReply != null) && (autoReply.length > 0)) {
369                            return true;
370                    }
371    
372                    String[] autoReplyFrom = message.getHeader("X-Autoreply-From");
373    
374                    if ((autoReplyFrom != null) && (autoReplyFrom.length > 0)) {
375                            return true;
376                    }
377    
378                    String[] mailAutoReply = message.getHeader("X-Mail-Autoreply");
379    
380                    if ((mailAutoReply != null) && (mailAutoReply.length > 0)) {
381                            return true;
382                    }
383    
384                    return false;
385            }
386    
387            private static Log _log = LogFactoryUtil.getLog(MessageListenerImpl.class);
388    
389    }