aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/otr/OtrTransformLayer.java
blob: 1fe632401e8fda2b8e569d8e2422a89d197896e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.otr;

import net.java.otr4j.io.*;
import net.java.sip.communicator.plugin.otr.OtrContactManager.OtrContact;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;

/**
 * The Off-the-Record {@link TransformLayer} implementation.
 *
 * @author George Politis
 */
public class OtrTransformLayer
    implements TransformLayer
{
    /*
     * Implements TransformLayer#messageDelivered(MessageDeliveredEvent).
     */
    public MessageDeliveredEvent messageDelivered(MessageDeliveredEvent evt)
    {
        if (OtrActivator.scOtrEngine.isMessageUIDInjected(evt
            .getSourceMessage().getMessageUID()))
            // If this is a message otr4j injected earlier, don't display it,
            // this may have to change when we add support for fragmentation..
            return null;
        else
            return evt;
    }

    /*
     * Implements
     * TransformLayer#messageDeliveryFailed(MessageDeliveryFailedEvent).
     */
    public MessageDeliveryFailedEvent messageDeliveryFailed(
        MessageDeliveryFailedEvent evt)
    {
        return evt;
    }

    /*
     * Implements TransformLayer#messageDeliveryPending(MessageDeliveredEvent).
     */
    public MessageDeliveredEvent[] messageDeliveryPending(
        MessageDeliveredEvent evt)
    {
        Contact contact = evt.getDestinationContact();
        OtrContact otrContact =
            OtrContactManager.getOtrContact(contact, evt.getContactResource());

        // If this is a message otr4j injected earlier, return the event as is.
        if (OtrActivator.scOtrEngine.isMessageUIDInjected(evt
            .getSourceMessage().getMessageUID()))
            return new MessageDeliveredEvent[] {evt};

        // Process the outgoing message.
        String msgContent = evt.getSourceMessage().getContent();
        String[] processedMessageContent =
            OtrActivator.scOtrEngine.transformSending(otrContact, msgContent);

        if (processedMessageContent == null
            || processedMessageContent.length <= 0
            || processedMessageContent[0].length() < 1)
            return new MessageDeliveredEvent[0];

        if (processedMessageContent.length == 1
            && processedMessageContent[0].equals(msgContent))
            return new MessageDeliveredEvent[] {evt};

        final MessageDeliveredEvent[] processedEvents =
            new MessageDeliveredEvent[processedMessageContent.length];
        for (int i = 0; i < processedMessageContent.length; i++)
        {
            final String fragmentContent = processedMessageContent[i];
            // Forge a new message based on the new contents.
            OperationSetBasicInstantMessaging imOpSet =
                contact.getProtocolProvider().getOperationSet(
                    OperationSetBasicInstantMessaging.class);
            Message processedMessage =
                imOpSet.createMessage(fragmentContent, evt
                    .getSourceMessage().getContentType(), evt
                    .getSourceMessage().getEncoding(), evt.getSourceMessage()
                    .getSubject());

            // Create a new event and return.
            final MessageDeliveredEvent processedEvent =
                new MessageDeliveredEvent(processedMessage, contact,
                    evt.getTimestamp());

            if (processedMessage.getContent().contains(
                SerializationConstants.HEAD))
            {
                processedEvent.setMessageEncrypted(true);
            }

            processedEvents[i] = processedEvent;
        }

        return processedEvents;
    }

    /*
     * Implements TransformLayer#messageReceived(MessageReceivedEvent).
     */
    public MessageReceivedEvent messageReceived(MessageReceivedEvent evt)
    {
        Contact contact = evt.getSourceContact();
        OtrContact otrContact =
            OtrContactManager.getOtrContact(contact, evt.getContactResource());

        // Process the incoming message.
        String msgContent = evt.getSourceMessage().getContent();

        String processedMessageContent =
            OtrActivator.scOtrEngine.transformReceiving(otrContact, msgContent);

        if (processedMessageContent == null
            || processedMessageContent.length() < 1)
            return null;

        if (processedMessageContent.equals(msgContent))
            return evt;

        // Forge a new message based on the new contents.
        OperationSetBasicInstantMessaging imOpSet =
            contact.getProtocolProvider().getOperationSet(
                OperationSetBasicInstantMessaging.class);
        Message processedMessage =
            imOpSet.createMessageWithUID(
                processedMessageContent,
                evt.getSourceMessage().getContentType(),
                evt.getSourceMessage().getMessageUID());

        // Create a new event and return.
        MessageReceivedEvent processedEvent =
            new MessageReceivedEvent(processedMessage, contact, evt
                .getContactResource(), evt.getTimestamp(),
                evt.getCorrectedMessageUID());

        return processedEvent;
    }
}