Search in sources :

Example 6 with WonURI

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

the class Message2MailAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof MessageFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        Connection con = ((MessageFromOtherNeedEvent) event).getCon();
        URI responseTo = con.getNeedURI();
        URI remoteNeedUri = con.getRemoteNeedURI();
        MimeMessage originalMail = botContextWrapper.getMimeMessageForURI(responseTo);
        logger.debug("Someone sent a message for URI: " + responseTo + " sending a mail to the creator: " + MailContentExtractor.getFromAddressString(originalMail));
        WonMimeMessage answerMessage = mailGenerator.createMessageMail(originalMail, responseTo, remoteNeedUri, con.getConnectionURI());
        botContextWrapper.addMailIdWonURIRelation(answerMessage.getMessageID(), new WonURI(con.getConnectionURI(), UriType.CONNECTION));
        sendChannel.send(new GenericMessage<>(answerMessage));
    } else {
        logger.debug("event was not of type MessageFromOtherNeedEvent");
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) Connection(won.protocol.model.Connection) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI)

Example 7 with WonURI

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

the class CreateNeedFromMailAction method doRun.

protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof CreateNeedFromMailEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        MimeMessage message = ((CreateNeedFromMailEvent) event).getMessage();
        try {
            NeedContentPropertyType type = mailContentExtractor.getNeedType(message);
            String title = mailContentExtractor.getTitle(message);
            String description = mailContentExtractor.getDescription(message);
            String[] tags = mailContentExtractor.getTags(message);
            boolean isUsedForTesting = mailContentExtractor.isUsedForTesting(message);
            boolean isDoNotMatch = mailContentExtractor.isDoNotMatch(message);
            WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
            final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
            final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
            DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needURI.toString());
            needModelWrapper.setTitle(type, title);
            needModelWrapper.setDescription(type, description);
            for (String tag : tags) {
                needModelWrapper.addTag(type, tag);
            }
            for (URI facet : facets) {
                needModelWrapper.addFacetUri(facet.toString());
            }
            Dataset dataset = needModelWrapper.copyDataset();
            logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(dataset), 150));
            WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, dataset, isUsedForTesting, isDoNotMatch);
            EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
            botContextWrapper.addUriMimeMessageRelation(needURI, message);
            EventListener successCallback = new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    logger.debug("need creation successful, new need URI is {}", needURI);
                    String sender = MailContentExtractor.getFromAddressString(botContextWrapper.getMimeMessageForURI(needURI));
                    botContextWrapper.addMailAddressWonURIRelation(sender, new WonURI(needURI, UriType.NEED));
                    logger.debug("created need was from sender: " + sender);
                }
            };
            EventListener failureCallback = new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
                    logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
                    EventBotActionUtils.removeFromList(ctx, needURI, uriListName);
                    botContextWrapper.removeUriMimeMessageRelation(needURI);
                }
            };
            EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
            logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
            ctx.getWonMessageSender().sendWonMessage(createNeedMessage);
            logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
        } catch (MessagingException me) {
            logger.error("messaging exception occurred: {}", me);
        }
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MessagingException(javax.mail.MessagingException) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) NeedContentPropertyType(won.protocol.model.NeedContentPropertyType) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MimeMessage(javax.mail.internet.MimeMessage) WonMessage(won.protocol.message.WonMessage) CreateNeedFromMailEvent(won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) EventListener(won.bot.framework.eventbot.listener.EventListener) CreateNeedFromMailEvent(won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent)

Example 8 with WonURI

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

the class MailCommandAction method retrieveCorrespondingNeedUriFromMailByTitle.

/**
 * This Method tries to find a corresponding open need uri from a user(given by the from adress) and returns the
 * corresponding need uri if there was an open need with the same title
 * @param message used to extract sender adress and subject(title)
 * @return
 */
private URI retrieveCorrespondingNeedUriFromMailByTitle(MimeMessage message) {
    try {
        MailBotContextWrapper botContextWrapper = ((MailBotContextWrapper) getEventListenerContext().getBotContextWrapper());
        String sender = ((InternetAddress) message.getFrom()[0]).getAddress();
        URI needURI = null;
        String titleToClose = mailContentExtractor.getTitle(message).trim();
        if (sender != null) {
            List<WonURI> needUris = botContextWrapper.getWonURIsForMailAddress(sender);
            for (WonURI u : needUris) {
                Dataset needRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(u.getUri());
                DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needRDF);
                String needTitle = StringUtils.trim(needModelWrapper.getSomeTitleFromIsOrAll("en", "de"));
                if (titleToClose.equals(needTitle) && needModelWrapper.getNeedState().equals(NeedState.ACTIVE)) {
                    return u.getUri();
                }
            }
        }
        return needURI;
    } catch (MessagingException me) {
        logger.error("could not extract information from mimemessage");
        return null;
    }
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) InternetAddress(javax.mail.internet.InternetAddress) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MessagingException(javax.mail.MessagingException) Dataset(org.apache.jena.query.Dataset) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI)

Example 9 with WonURI

use of won.bot.framework.eventbot.action.impl.mail.model.WonURI 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)

Example 10 with WonURI

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

the class TelegramMessageReceivedAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventBus bus = getEventListenerContext().getEventBus();
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof TelegramMessageReceivedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
        TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
        Update update = ((TelegramMessageReceivedEvent) event).getUpdate();
        Message message = update.getMessage();
        CallbackQuery callbackQuery = update.getCallbackQuery();
        if (message != null && message.isCommand()) {
            wonTelegramBotHandler.getCommandRegistry().executeCommand(wonTelegramBotHandler, message);
        } else if (callbackQuery != null && update.hasCallbackQuery()) {
            message = callbackQuery.getMessage();
            String data = callbackQuery.getData();
            WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getMessageId());
            AnswerCallbackQuery answerCallbackQuery = new AnswerCallbackQuery();
            answerCallbackQuery.setCallbackQueryId(callbackQuery.getId());
            switch(correspondingURI.getType()) {
                case NEED:
                    break;
                case CONNECTION:
                    if ("0".equals(data)) {
                        // CLOSE CONNECTION
                        Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
                        Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
                        bus.publish(new CloseCommandEvent(con));
                        answerCallbackQuery.setText("Closed Connection");
                    } else if ("1".equals(data)) {
                        // ACCEPT CONNECTION
                        Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
                        URI remoteNeed = WonRdfUtils.ConnectionUtils.getRemoteNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
                        URI localNeed = WonRdfUtils.ConnectionUtils.getLocalNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
                        bus.publish(new ConnectCommandEvent(localNeed, remoteNeed));
                        answerCallbackQuery.setText("Opened Connection");
                    }
                    break;
            }
            wonTelegramBotHandler.answerCallbackQuery(answerCallbackQuery);
            EditMessageReplyMarkup editMessageReplyMarkup = new EditMessageReplyMarkup();
            editMessageReplyMarkup.setMessageId(message.getMessageId());
            editMessageReplyMarkup.setChatId(message.getChatId());
            editMessageReplyMarkup.setReplyMarkup(null);
            wonTelegramBotHandler.editMessageReplyMarkup(editMessageReplyMarkup);
        } else if (message != null && message.isReply() && message.hasText()) {
            WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getReplyToMessage().getMessageId());
            Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
            Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
            Model messageModel = WonRdfUtils.MessageUtils.textMessage(message.getText());
            bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
        }
    }
}
Also used : AnswerCallbackQuery(org.telegram.telegrambots.api.methods.AnswerCallbackQuery) Message(org.telegram.telegrambots.api.objects.Message) CallbackQuery(org.telegram.telegrambots.api.objects.CallbackQuery) Connection(won.protocol.model.Connection) Update(org.telegram.telegrambots.api.objects.Update) EventBus(won.bot.framework.eventbot.bus.EventBus) Model(org.apache.jena.rdf.model.Model) WonTelegramBotHandler(won.bot.framework.eventbot.action.impl.telegram.WonTelegramBotHandler) TelegramMessageReceivedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramMessageReceivedEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ConnectionModelMapper(won.protocol.model.ConnectionModelMapper) TelegramContentExtractor(won.bot.framework.eventbot.action.impl.telegram.util.TelegramContentExtractor) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) AnswerCallbackQuery(org.telegram.telegrambots.api.methods.AnswerCallbackQuery) WonRdfUtils(won.protocol.util.WonRdfUtils) Event(won.bot.framework.eventbot.event.Event) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent) RdfUtils(won.protocol.util.RdfUtils) EventListener(won.bot.framework.eventbot.listener.EventListener) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) EditMessageReplyMarkup(org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup) ConnectCommandEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) CallbackQuery(org.telegram.telegrambots.api.objects.CallbackQuery) AnswerCallbackQuery(org.telegram.telegrambots.api.methods.AnswerCallbackQuery) Message(org.telegram.telegrambots.api.objects.Message) Dataset(org.apache.jena.query.Dataset) Connection(won.protocol.model.Connection) EventBus(won.bot.framework.eventbot.bus.EventBus) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) Update(org.telegram.telegrambots.api.objects.Update) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) ConnectCommandEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent) EditMessageReplyMarkup(org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup) Model(org.apache.jena.rdf.model.Model) ConnectionModelMapper(won.protocol.model.ConnectionModelMapper) TelegramMessageReceivedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramMessageReceivedEvent) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)

Aggregations

WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)12 URI (java.net.URI)11 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)11 MailBotContextWrapper (won.bot.framework.bot.context.MailBotContextWrapper)7 MimeMessage (javax.mail.internet.MimeMessage)6 Connection (won.protocol.model.Connection)6 Dataset (org.apache.jena.query.Dataset)5 Message (org.telegram.telegrambots.api.objects.Message)5 TelegramBotContextWrapper (won.bot.framework.bot.context.TelegramBotContextWrapper)5 WonMessage (won.protocol.message.WonMessage)5 TelegramApiException (org.telegram.telegrambots.exceptions.TelegramApiException)4 Event (won.bot.framework.eventbot.event.Event)4 EventListener (won.bot.framework.eventbot.listener.EventListener)4 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)4 MessagingException (javax.mail.MessagingException)3 InternetAddress (javax.mail.internet.InternetAddress)2 Model (org.apache.jena.rdf.model.Model)2 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 CloseCommandEvent (won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent)2