A plugin to change the message body

Hello,

I want to write a plugin to be able to modify the body of the messages and the message history unless the user has a paid subscription.

I already wrote a custom plugin using a packet interceptor to check if the user has a paid subscription, and if they don’t I change the body of the messages to “[please subscribe]”.

However the problem is that the messages are stored in the database by the MAM plugin as [please subscribe], so if the user pays their subscription tomorrow they will not be able to read them.

What I want is the MAM pluin to store the original message, but the on the other side of the conversation the user will receive a [please subscribe] message so they will not be able to read the message unless they subscribe.

I guess this is impossible with PacketInterceptor. What other way is there to write such a plugin?

This is tricky to get right, and will depend on a lot of moving parts. I’m not sure if it is achievable, without retaining some kind of duplicate administration of the original message.

One thing that you could try is to apply the packet interceptor not on the inbound stanza, but on the outbound one - only replacing the content before it is being delivered.

That’s what I tried. I’m targeting only packets that are processed and outgoing:

        @Override
        public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
            if (!processed || packet.getTo() == null || StringUtils.isEmpty(packet.getTo().getNode())) {
                return;
            }

            boolean isValidOutgoingMessage = isValidOutgoingMessageTargetPacket(packet, incoming);
            boolean isMemberMissingSubscription = XXX;

            //the baby sends a message to the daddy but the daddy hasn't paid
            if (isValidOutgoingMessage && isMemberMissingSubscription) {
                Log.error("Updating " + (incoming ? "incoming" : "outgoing") + " message " + ((Message) packet).getBody());
                Message message = (Message) packet;
                message.setBody("[upgrade to read]");
            }
        }

The result is messages are delivered with original body and stored in the db as [upgrade to read] - exactly the opposite of what I want.

And if I try to act on the messages that are not processed then I receive [upgrade to read] on the other side, and still the MAM archives the message as [upgrade to read] :frowning: