You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
7.9 KiB
Java
234 lines
7.9 KiB
Java
package org.gcube.portal.socialmail;
|
|
|
|
import java.security.GeneralSecurityException;
|
|
import java.util.Date;
|
|
import java.util.Properties;
|
|
import java.util.UUID;
|
|
|
|
import javax.mail.Address;
|
|
import javax.mail.BodyPart;
|
|
import javax.mail.Flags;
|
|
import javax.mail.Folder;
|
|
import javax.mail.Message;
|
|
import javax.mail.MessagingException;
|
|
import javax.mail.Multipart;
|
|
import javax.mail.NoSuchProviderException;
|
|
import javax.mail.Session;
|
|
import javax.mail.Store;
|
|
import javax.mail.internet.InternetAddress;
|
|
import javax.mail.internet.MimeMessage.RecipientType;
|
|
|
|
import org.gcube.application.framework.core.session.ASLSession;
|
|
import org.gcube.application.framework.core.session.SessionManager;
|
|
import org.gcube.application.framework.core.util.GenderType;
|
|
import org.gcube.applicationsupportlayer.social.mailing.EmailPlugin;
|
|
import org.gcube.common.portal.PortalContext;
|
|
import org.gcube.portal.custom.communitymanager.OrganizationsUtil;
|
|
import org.gcube.portal.databook.server.DatabookStore;
|
|
import org.gcube.portal.databook.shared.Comment;
|
|
import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.liferay.portal.kernel.exception.PortalException;
|
|
import com.liferay.portal.kernel.exception.SystemException;
|
|
import com.liferay.portal.model.User;
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
import com.sun.mail.util.MailSSLSocketFactory;
|
|
|
|
/**
|
|
*
|
|
* @author Massimiliano Assante, CNR-ISTI
|
|
*
|
|
*/
|
|
public class PeriodicTask implements Runnable {
|
|
private static final Logger _log = LoggerFactory.getLogger(PeriodicTask.class);
|
|
|
|
private DatabookStore socialStore;
|
|
private String host;
|
|
private String mailserver_username;
|
|
private String password;
|
|
private String portalName;
|
|
|
|
public PeriodicTask(DatabookStore store, String portalName, String host, String mailserver_username, String password) {
|
|
super();
|
|
this.socialStore = store;
|
|
this.portalName = portalName;
|
|
this.host = host;
|
|
this.mailserver_username = mailserver_username;
|
|
this.password = password;
|
|
}
|
|
|
|
|
|
@Override
|
|
public void run() {
|
|
//check("pop.isti.cnr.it", "social.post", "kof9044+");
|
|
check(portalName, host, mailserver_username, password);
|
|
|
|
}
|
|
/**
|
|
* @return a fake session usuful for Notifications
|
|
*/
|
|
private ASLSession getFakeASLSession(String emailAddress) {
|
|
String sessionID = UUID.randomUUID().toString();
|
|
PortalContext context = PortalContext.getConfiguration();
|
|
String scope = "/" + context.getInfrastructureName();
|
|
|
|
String username = "";
|
|
long companyId;
|
|
try {
|
|
companyId = OrganizationsUtil.getCompany().getCompanyId();
|
|
User user = UserLocalServiceUtil.getUserByEmailAddress(companyId, emailAddress);
|
|
username = user.getScreenName();
|
|
SessionManager.getInstance().getASLSession(sessionID, username).setScope(scope);
|
|
|
|
//add the social information needed by apps
|
|
String fullName = user.getFirstName() + " " + user.getLastName();
|
|
String email = user.getEmailAddress();
|
|
String thumbnailURL = "/image/user_male_portrait?img_id="+user.getPortraitId();
|
|
boolean isMale = user.isMale();
|
|
|
|
SessionManager.getInstance().getASLSession(sessionID, username).setUserFullName(fullName);
|
|
SessionManager.getInstance().getASLSession(sessionID, username).setUserEmailAddress(email);
|
|
SessionManager.getInstance().getASLSession(sessionID, username).setUserAvatarId(thumbnailURL);
|
|
SessionManager.getInstance().getASLSession(sessionID, username).setUserGender(isMale? GenderType.MALE : GenderType.FEMALE);
|
|
|
|
} catch (PortalException | SystemException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
|
|
return SessionManager.getInstance().getASLSession(sessionID, username);
|
|
}
|
|
|
|
public void check(String portalName, String host, String user, String password) {
|
|
try {
|
|
MailSSLSocketFactory sf = null;
|
|
try {
|
|
sf = new MailSSLSocketFactory();
|
|
}
|
|
catch (GeneralSecurityException e) {
|
|
e.printStackTrace();
|
|
}
|
|
sf.setTrustAllHosts(true);
|
|
|
|
Properties pop3Props = new Properties();
|
|
pop3Props.setProperty("mail.pop3.ssl.enable", "true");
|
|
pop3Props.setProperty("mail.protocol.ssl.trust", host);
|
|
pop3Props.put("mail.pop3s.ssl.socketFactory", sf);
|
|
pop3Props.setProperty("mail.pop3s.port", "995");
|
|
|
|
|
|
Session emailSession = Session.getDefaultInstance(pop3Props);
|
|
//emailSession.setDebug(true);
|
|
|
|
//create the POP3 socialStore object and connect with the pop server
|
|
Store store = emailSession.getStore("pop3s");
|
|
|
|
store.connect(host, user, password);
|
|
|
|
//create the folder object and open it
|
|
Folder emailFolder = store.getFolder("INBOX");
|
|
emailFolder.open(Folder.READ_WRITE);
|
|
|
|
// retrieve the messages from the folder in an array and print it
|
|
Message[] messages = emailFolder.getMessages();
|
|
_log.debug("Found " + messages.length + " new messages ...");
|
|
|
|
for (int i = 0, n = messages.length; i < n; i++) {
|
|
Message message = messages[i];
|
|
_log.debug("--------------- FOUND EMAIL ------------------");
|
|
String subject = message.getSubject();
|
|
_log.debug("Parsing email of " + message.getFrom()[0] + " with subject: " + subject);
|
|
|
|
String feedId = extractFeedId(message);
|
|
String commentText = extractText(portalName, feedId, message);
|
|
|
|
String escapedCommentText = Utils.escapeHtmlAndTransformUrl(commentText);
|
|
|
|
|
|
Address[] froms = message.getFrom();
|
|
String emailAddress = froms == null ? null : ((InternetAddress) froms[0]).getAddress();
|
|
|
|
ASLSession fakeSession = getFakeASLSession(emailAddress);
|
|
|
|
Comment comment = new Comment(UUID.randomUUID().toString(), fakeSession.getUsername(),
|
|
new Date(), feedId, escapedCommentText, fakeSession.getUserFullName(), fakeSession.getUserAvatarId());
|
|
|
|
_log.debug("Parsed and escapedCommentText =>" + escapedCommentText);
|
|
boolean commentCommitResult = false;
|
|
try {
|
|
if (socialStore.addComment(comment))
|
|
commentCommitResult = true;
|
|
} catch (FeedIDNotFoundException e) {
|
|
_log.error("Related post not found for this comment " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
if (commentCommitResult) { //the notify
|
|
_log.info("Now the NOTIFICATION SHOULD START!!!!!");
|
|
}
|
|
|
|
|
|
message.setFlag(Flags.Flag.DELETED, true);
|
|
System.out.println("Marked DELETE for message: " + subject);
|
|
}
|
|
|
|
//close the socialStore and folder objects
|
|
emailFolder.close(true);
|
|
store.close();
|
|
|
|
} catch (NoSuchProviderException e) {
|
|
e.printStackTrace();
|
|
} catch (MessagingException e) {
|
|
e.printStackTrace();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private String extractFeedId(Message message) throws MessagingException {
|
|
Address[] emails = message.getRecipients(RecipientType.TO);
|
|
String toParse = emails[0].toString();
|
|
int plus = toParse.indexOf('+');
|
|
int at = toParse.indexOf('@');
|
|
if (plus == -1)
|
|
return null;
|
|
return toParse.substring(plus+1, at);
|
|
}
|
|
|
|
private String extractText(String portalName, String subjectId, Message message) throws Exception {
|
|
Address[] emails = message.getRecipients(RecipientType.TO);
|
|
Object messageContent = message.getContent();
|
|
String toParse = null;
|
|
// Check if content is pure text/html or in parts
|
|
if (messageContent instanceof Multipart) {
|
|
_log.debug("Found Multipart Message, getting text part");
|
|
Multipart multipart = (Multipart) messageContent;
|
|
BodyPart part = multipart.getBodyPart(0);
|
|
part.toString();
|
|
toParse = part.getContent().toString();
|
|
}
|
|
else {
|
|
_log.debug("Found g text/plain Message, getting text");
|
|
toParse = messageContent.toString();
|
|
}
|
|
|
|
String[] lines = toParse.split(System.getProperty("line.separator"));
|
|
int until = -1;
|
|
for (int i = 0; i < lines.length; i++) {
|
|
if (lines[i].contains(EmailPlugin.WRITE_ABOVE_TO_REPLY)) {
|
|
until = i;
|
|
break;
|
|
}
|
|
}
|
|
String toReturn = "";
|
|
for (int i = 0; i < until; i++) {
|
|
if (! ( lines[i].contains(portalName) || lines[i].contains(subjectId) || (lines[i].startsWith("> ") && lines[i].trim().length() < 2)) ) {
|
|
toReturn += lines[i];
|
|
}
|
|
}
|
|
return toReturn;
|
|
|
|
}
|
|
}
|