Search in sources :

Example 1 with ActionType

use of won.bot.framework.eventbot.action.impl.mail.model.ActionType in project webofneeds by researchstudio-sat.

the class MailCommandAction method processNonReferenceMailCommand.

private void processNonReferenceMailCommand(MimeMessage message) throws IOException, MessagingException {
    EventBus bus = getEventListenerContext().getEventBus();
    ActionType mailAction = mailContentExtractor.getMailAction(message);
    switch(mailAction) {
        case SUBSCRIBE:
            bus.publish(new SubscribeUnsubscribeEvent(message, SubscribeStatus.SUBSCRIBED));
            break;
        case UNSUBSCRIBE:
            bus.publish(new SubscribeUnsubscribeEvent(message, SubscribeStatus.UNSUBSCRIBED));
            break;
        case CLOSE_NEED:
            /*A need can be closed with a mail that matches the takenCmdPattern in its subject and has the same title
                 as a previously created need by the user*/
            URI needUri = retrieveCorrespondingNeedUriFromMailByTitle(message);
            if (needUri != null) {
                bus.publish(new DeactivateNeedCommandEvent(needUri));
            }
            break;
        case NO_ACTION:
        default:
            // INVALID COMMAND
            logger.error("No command was given or assumed");
            break;
    }
}
Also used : ActionType(won.bot.framework.eventbot.action.impl.mail.model.ActionType) SubscribeUnsubscribeEvent(won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent) DeactivateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI)

Example 2 with ActionType

use of won.bot.framework.eventbot.action.impl.mail.model.ActionType in project webofneeds by researchstudio-sat.

the class MailCommandAction method processReferenceMailCommands.

private void processReferenceMailCommands(MimeMessage message, WonURI wonUri) {
    MailBotContextWrapper botContextWrapper = ((MailBotContextWrapper) getEventListenerContext().getBotContextWrapper());
    EventBus bus = getEventListenerContext().getEventBus();
    try {
        if (wonUri == null) {
            throw new NullPointerException("No corresponding wonUri found");
        }
        URI needUri;
        URI remoteNeedUri = null;
        Dataset connectionRDF = null;
        switch(wonUri.getType()) {
            case CONNECTION:
                connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(wonUri.getUri());
                needUri = WonRdfUtils.ConnectionUtils.getLocalNeedURIFromConnection(connectionRDF, wonUri.getUri());
                remoteNeedUri = WonRdfUtils.ConnectionUtils.getRemoteNeedURIFromConnection(connectionRDF, wonUri.getUri());
                break;
            case NEED:
            default:
                needUri = wonUri.getUri();
                break;
        }
        MimeMessage originalMessage = botContextWrapper.getMimeMessageForURI(needUri);
        if (originalMessage == null) {
            throw new NullPointerException("no originalmessage found");
        }
        logger.debug("Validate mailorigin with originalmail:");
        logger.debug("Command Message Sender: " + message.getFrom());
        logger.debug("Original Message Sender: " + originalMessage.getFrom());
        String senderNew = ((InternetAddress) message.getFrom()[0]).getAddress();
        String senderOriginal = ((InternetAddress) originalMessage.getFrom()[0]).getAddress();
        if (!senderNew.equals(senderOriginal)) {
            throw new AccessControlException("Sender of original and command mail are not equal");
        } else {
            logger.debug("Sender of original and command mail are not equal, continue with command processing");
        }
        ActionType actionType = determineAction(getEventListenerContext(), message, wonUri);
        logger.debug("Executing " + actionType + " on uri: " + wonUri.getUri() + " of type " + wonUri.getType());
        Connection con;
        switch(actionType) {
            case CLOSE_CONNECTION:
                con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
                bus.publish(new CloseCommandEvent(con));
                break;
            case OPEN_CONNECTION:
                bus.publish(new ConnectCommandEvent(needUri, remoteNeedUri));
                break;
            case IMPLICIT_OPEN_CONNECTION:
                bus.publish(new ConnectCommandEvent(needUri, remoteNeedUri, mailContentExtractor.getTextMessage(message)));
                break;
            case SENDMESSAGE:
                con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
                Model messageModel = WonRdfUtils.MessageUtils.textMessage(mailContentExtractor.getTextMessage(message));
                bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
                break;
            case CLOSE_NEED:
                bus.publish(new DeactivateNeedCommandEvent(needUri));
                break;
            case NO_ACTION:
            default:
                // INVALID COMMAND
                logger.error("No command was given or assumed");
                break;
        }
    } catch (AccessControlException ace) {
        logger.error("ACCESS RESTRICTION: sender of original and command mail are not equal, command will be blocked");
    } catch (Exception e) {
        logger.error("no reply mail was set or found: " + e.getMessage());
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Connection(won.protocol.model.Connection) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) EventBus(won.bot.framework.eventbot.bus.EventBus) MailCommandEvent(won.bot.framework.eventbot.event.impl.mail.MailCommandEvent) MessagingException(javax.mail.MessagingException) Model(org.apache.jena.rdf.model.Model) InternetAddress(javax.mail.internet.InternetAddress) SubscribeUnsubscribeEvent(won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ConnectionModelMapper(won.protocol.model.ConnectionModelMapper) NeedState(won.protocol.model.NeedState) SubscribeStatus(won.bot.framework.eventbot.action.impl.mail.model.SubscribeStatus) ActionType(won.bot.framework.eventbot.action.impl.mail.model.ActionType) IOException(java.io.IOException) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) MimeMessage(javax.mail.internet.MimeMessage) WonRdfUtils(won.protocol.util.WonRdfUtils) List(java.util.List) Event(won.bot.framework.eventbot.event.Event) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent) RdfUtils(won.protocol.util.RdfUtils) AccessControlException(java.security.AccessControlException) EventListener(won.bot.framework.eventbot.listener.EventListener) DeactivateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) ConnectCommandEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent) InternetAddress(javax.mail.internet.InternetAddress) ActionType(won.bot.framework.eventbot.action.impl.mail.model.ActionType) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) Dataset(org.apache.jena.query.Dataset) Connection(won.protocol.model.Connection) AccessControlException(java.security.AccessControlException) DeactivateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) MimeMessage(javax.mail.internet.MimeMessage) ConnectCommandEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent) Model(org.apache.jena.rdf.model.Model) ConnectionModelMapper(won.protocol.model.ConnectionModelMapper) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)

Aggregations

URI (java.net.URI)2 ActionType (won.bot.framework.eventbot.action.impl.mail.model.ActionType)2 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 DeactivateNeedCommandEvent (won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent)2 SubscribeUnsubscribeEvent (won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent)2 IOException (java.io.IOException)1 AccessControlException (java.security.AccessControlException)1 List (java.util.List)1 MessagingException (javax.mail.MessagingException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 StringUtils (org.apache.commons.lang.StringUtils)1 Dataset (org.apache.jena.query.Dataset)1 Model (org.apache.jena.rdf.model.Model)1 MailBotContextWrapper (won.bot.framework.bot.context.MailBotContextWrapper)1 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 SubscribeStatus (won.bot.framework.eventbot.action.impl.mail.model.SubscribeStatus)1 Event (won.bot.framework.eventbot.event.Event)1