Search in sources :

Example 1 with ConnectFromOtherNeedEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent in project webofneeds by researchstudio-sat.

the class OpenConnectionDebugAction method doRun.

@Override
public void doRun(final Event event, EventListener executingListener) throws Exception {
    if (!(event instanceof ConnectFromOtherNeedEvent)) {
        return;
    }
    if (event instanceof WonMessageReceivedOnConnectionEvent) {
        WonMessage msg = ((WonMessageReceivedOnConnectionEvent) event).getWonMessage();
        String message = WonRdfUtils.MessageUtils.getTextMessage(msg);
        if (message == null) {
            message = "";
        }
        Matcher ignoreMatcher = PATTERN_IGNORE.matcher(message);
        if (ignoreMatcher.find()) {
            logger.debug("not reacting to incoming message of type {} as the welcome message contained 'ignore'", msg.getMessageType());
            return;
        }
        Matcher waitMatcher = PATTERN_WAIT.matcher(message);
        final boolean wait = waitMatcher.find();
        int waitSeconds = 15;
        if (wait && waitMatcher.groupCount() == 2) {
            waitSeconds = Integer.parseInt(waitMatcher.group(2));
        }
        Matcher denyMatcher = PATTERN_DENY.matcher(message);
        final boolean deny = denyMatcher.find();
        ConnectionSpecificEvent connectEvent = (ConnectionSpecificEvent) event;
        logger.debug("auto-replying to connect for connection {}", connectEvent.getConnectionURI());
        URI connectionUri = connectEvent.getConnectionURI();
        String finalWelcomeMessage = welcomeMessage;
        if (wait || deny) {
            finalWelcomeMessage = welcomeMessage + " " + (deny ? "Denying" : "Accepting") + " your request " + (wait ? " after a timeout of " + waitSeconds + " seconds" : "");
        } else {
            finalWelcomeMessage = welcomeMessage + " " + welcomeHelpMessage;
        }
        final WonMessage toSend = deny ? createCloseWonMessage(connectionUri, finalWelcomeMessage) : createOpenWonMessage(connectionUri, finalWelcomeMessage);
        Runnable task = () -> {
            getEventListenerContext().getWonMessageSender().sendWonMessage(toSend);
        };
        if (wait) {
            Date when = new Date(System.currentTimeMillis() + waitSeconds * 1000);
            getEventListenerContext().getTaskScheduler().schedule(task, when);
        } else {
            task.run();
        }
    }
}
Also used : ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) WonMessageReceivedOnConnectionEvent(won.bot.framework.eventbot.event.impl.wonmessage.WonMessageReceivedOnConnectionEvent) Matcher(java.util.regex.Matcher) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent) Date(java.util.Date)

Example 2 with ConnectFromOtherNeedEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent in project webofneeds by researchstudio-sat.

the class Connect2TelegramAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof ConnectFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
        TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
        Connection con = ((ConnectFromOtherNeedEvent) event).getCon();
        URI yourNeedUri = con.getNeedURI();
        URI remoteNeedUri = con.getRemoteNeedURI();
        Long chatId = botContextWrapper.getChatIdForURI(yourNeedUri);
        if (chatId == null) {
            logger.error("No chatId found for the specified needUri");
            return;
        }
        try {
            Message message = wonTelegramBotHandler.sendMessage(wonTelegramBotHandler.getTelegramMessageGenerator().getConnectMessage(chatId, remoteNeedUri, yourNeedUri));
            botContextWrapper.addMessageIdWonURIRelation(message.getMessageId(), new WonURI(con.getConnectionURI(), UriType.CONNECTION));
        } catch (TelegramApiException te) {
            logger.error(te.getMessage());
        }
    }
}
Also used : TelegramApiException(org.telegram.telegrambots.exceptions.TelegramApiException) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) Message(org.telegram.telegrambots.api.objects.Message) Connection(won.protocol.model.Connection) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)

Example 3 with ConnectFromOtherNeedEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent in project webofneeds by researchstudio-sat.

the class Connect2MailParserAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof ConnectFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        Connection con = ((ConnectFromOtherNeedEvent) event).getCon();
        URI responseTo = con.getNeedURI();
        URI remoteNeedUri = con.getRemoteNeedURI();
        MimeMessage originalMail = botContextWrapper.getMimeMessageForURI(responseTo);
        logger.debug("Someone issued a connect for URI: " + responseTo + " sending a mail to the creator: " + MailContentExtractor.getFromAddressString(originalMail));
        WonMimeMessage answerMessage = mailGenerator.createConnectMail(originalMail, remoteNeedUri);
        botContextWrapper.addMailIdWonURIRelation(answerMessage.getMessageID(), new WonURI(con.getConnectionURI(), UriType.CONNECTION));
        sendChannel.send(new GenericMessage<>(answerMessage));
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) 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) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)

Example 4 with ConnectFromOtherNeedEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent in project webofneeds by researchstudio-sat.

the class OpenConnectionAction method doRun.

@Override
public void doRun(final Event event, EventListener executingListener) throws Exception {
    if (event instanceof ConnectFromOtherNeedEvent) {
        ConnectionSpecificEvent connectEvent = (ConnectionSpecificEvent) event;
        logger.debug("auto-replying to connect for connection {}", connectEvent.getConnectionURI());
        getEventListenerContext().getWonMessageSender().sendWonMessage(createOpenWonMessage(connectEvent.getConnectionURI()));
        return;
    } else if (event instanceof OpenFromOtherNeedEvent) {
        ConnectionSpecificEvent connectEvent = (ConnectionSpecificEvent) event;
        URI connectionState = WonLinkedDataUtils.getConnectionStateforConnectionURI(connectEvent.getConnectionURI(), getEventListenerContext().getLinkedDataSource());
        if (ConnectionState.REQUEST_RECEIVED.getURI().equals(connectionState)) {
            logger.debug("auto-replying to open(REQUEST_RECEIVED) with open for connection {}", connectEvent.getConnectionURI());
            getEventListenerContext().getWonMessageSender().sendWonMessage(createOpenWonMessage(connectEvent.getConnectionURI()));
        } else {
        // else do not respond - we assume the connection is now established.
        }
        return;
    } else if (event instanceof HintFromMatcherEvent) {
        // TODO: the hint with a match object is not really suitable here. Would be better to
        // use connection object instead
        HintFromMatcherEvent hintEvent = (HintFromMatcherEvent) event;
        logger.debug("opening connection based on hint {}", event);
        getEventListenerContext().getWonMessageSender().sendWonMessage(createConnectWonMessage(hintEvent.getMatch().getFromNeed(), hintEvent.getMatch().getToNeed(), FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI()));
    }
}
Also used : ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) OpenFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent) HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)

Example 5 with ConnectFromOtherNeedEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent in project webofneeds by researchstudio-sat.

the class BATestScriptListener method handleEvent.

@Override
protected void handleEvent(final Event event) throws Exception {
    if (!(event instanceof NeedSpecificEvent && event instanceof ConnectionSpecificEvent)) {
        return;
    }
    logger.debug("handling event: {}", event);
    // extract need URI and connection URI from event
    URI needURI = ((NeedSpecificEvent) event).getNeedURI();
    URI connectionURI = ((ConnectionSpecificEvent) event).getConnectionURI();
    // it could be about another connection of one of the two needs.
    synchronized (filterChangeMonitor) {
        if (!bothConnectionURIsAreKnown()) {
            if (!isConnectionURIKnown(connectionURI)) {
                // we have to check if the connectionURI is relevant
                if (isRelevantEvent(event, needURI, connectionURI)) {
                    // the connectionURI is relevant. remember the connect
                    rememberConnectionURI(needURI, connectionURI);
                    if (bothConnectionURIsAreKnown()) {
                        updateFilterForBothConnectionURIs();
                    }
                } else {
                    addIrrelevantConnectionURIToFilter(connectionURI);
                    logger.debug("omitting event {} as it is not relevant for listener {}", event, this);
                    return;
                }
            }
        } else if (!isConnectionURIKnown(connectionURI)) {
            logger.debug("omitting event {} as it is not relevant for listener {}", event, this);
            return;
        }
    }
    if (event instanceof ConnectFromOtherNeedEvent) {
        // send an automatic open
        logger.debug("sending automatic open in response to connect");
        sendOpen(connectionURI, new Date(System.currentTimeMillis() + millisBetweenMessages));
        synchronized (countMonitor) {
            this.messagesInFlight++;
        }
        return;
    }
    // execute the next script action
    if (this.script.hasNext()) {
        // if there is an action, execute it.
        BATestScriptAction action = this.script.getNextAction();
        logger.debug("executing next script action: {}", action);
        if (action.isNopAction()) {
            logger.debug("not sending any messages for action {}", action);
            // because we don't send one, we won't receive one from ourselves
            if (!script.hasNext()) {
                logger.debug("unsubscribing from all events as last script action is NOP");
                performFinish();
            }
        } else {
            URI fromCon = getConnectionToSendFrom(action.isSenderIsCoordinator());
            URI fromNeed = getNeedToSendFrom(action.isSenderIsCoordinator());
            URI toCon = getConnectionToSendFrom(!action.isSenderIsCoordinator());
            URI toNeed = getNeedToSendFrom(!action.isSenderIsCoordinator());
            logger.debug("sending message for action {} on connection {}", action, fromCon);
            assertCorrectConnectionState(fromCon, action);
            sendMessage(action, fromCon, fromNeed, toCon, toNeed, new Date(System.currentTimeMillis() + millisBetweenMessages));
            synchronized (countMonitor) {
                this.messagesInFlight++;
            }
        }
    } else {
        logger.debug("script has no more actions.");
    }
    // received, which leads to ugly exceptions
    if (event instanceof MessageEvent) {
        // we received a message
        synchronized (countMonitor) {
            this.messagesInFlight--;
        }
    }
}
Also used : URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent) Date(java.util.Date)

Aggregations

URI (java.net.URI)5 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)5 Date (java.util.Date)2 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)2 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)2 ConnectionSpecificEvent (won.bot.framework.eventbot.event.ConnectionSpecificEvent)2 Connection (won.protocol.model.Connection)2 Matcher (java.util.regex.Matcher)1 MimeMessage (javax.mail.internet.MimeMessage)1 Message (org.telegram.telegrambots.api.objects.Message)1 TelegramApiException (org.telegram.telegrambots.exceptions.TelegramApiException)1 MailBotContextWrapper (won.bot.framework.bot.context.MailBotContextWrapper)1 TelegramBotContextWrapper (won.bot.framework.bot.context.TelegramBotContextWrapper)1 HintFromMatcherEvent (won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent)1 OpenFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent)1 WonMessageReceivedOnConnectionEvent (won.bot.framework.eventbot.event.impl.wonmessage.WonMessageReceivedOnConnectionEvent)1 WonMessage (won.protocol.message.WonMessage)1