001/**
002 *
003 * Copyright 2013-2014 Georg Lukas, 2017 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.carbons;
018
019import java.util.Map;
020import java.util.Set;
021import java.util.WeakHashMap;
022import java.util.concurrent.CopyOnWriteArraySet;
023
024import org.jivesoftware.smack.AbstractConnectionListener;
025import org.jivesoftware.smack.ConnectionCreationListener;
026import org.jivesoftware.smack.ExceptionCallback;
027import org.jivesoftware.smack.Manager;
028import org.jivesoftware.smack.SmackException;
029import org.jivesoftware.smack.SmackException.NoResponseException;
030import org.jivesoftware.smack.SmackException.NotConnectedException;
031import org.jivesoftware.smack.StanzaListener;
032import org.jivesoftware.smack.XMPPConnection;
033import org.jivesoftware.smack.XMPPConnectionRegistry;
034import org.jivesoftware.smack.XMPPException;
035import org.jivesoftware.smack.XMPPException.XMPPErrorException;
036import org.jivesoftware.smack.filter.AndFilter;
037import org.jivesoftware.smack.filter.FromMatchesFilter;
038import org.jivesoftware.smack.filter.OrFilter;
039import org.jivesoftware.smack.filter.StanzaExtensionFilter;
040import org.jivesoftware.smack.filter.StanzaFilter;
041import org.jivesoftware.smack.filter.StanzaTypeFilter;
042import org.jivesoftware.smack.packet.IQ;
043import org.jivesoftware.smack.packet.Message;
044import org.jivesoftware.smack.packet.Stanza;
045
046import org.jivesoftware.smackx.carbons.packet.Carbon;
047import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
048import org.jivesoftware.smackx.carbons.packet.CarbonExtension.Direction;
049import org.jivesoftware.smackx.carbons.packet.CarbonExtension.Private;
050import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
051import org.jivesoftware.smackx.forward.packet.Forwarded;
052
053import org.jxmpp.jid.EntityFullJid;
054
055/**
056 * Manager for XEP-0280: Message Carbons. This class implements the manager for registering {@link CarbonExtension}
057 * support, enabling and disabling message carbons, and for {@link CarbonCopyReceivedListener}.
058 * <p>
059 * Note that <b>it is important to match the 'from' attribute of the message wrapping a carbon copy</b>, as otherwise it would
060 * may be possible for others to impersonate users. Smack's CarbonManager takes care of that in
061 * {@link CarbonCopyReceivedListener}s which where registered with
062 * {@link #addCarbonCopyReceivedListener(CarbonCopyReceivedListener)}.
063 * </p>
064 * <p>
065 * You should call enableCarbons() before sending your first undirected presence (aka. the "initial presence").
066 * </p>
067 *
068 * @author Georg Lukas
069 * @author Florian Schmaus
070 */
071public final class CarbonManager extends Manager {
072
073    private static Map<XMPPConnection, CarbonManager> INSTANCES = new WeakHashMap<XMPPConnection, CarbonManager>();
074
075    static {
076        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
077            @Override
078            public void connectionCreated(XMPPConnection connection) {
079                getInstanceFor(connection);
080            }
081        });
082    }
083
084    private static final StanzaFilter CARBON_EXTENSION_FILTER = 
085                    // @formatter:off
086                    new AndFilter(
087                        new OrFilter(
088                            new StanzaExtensionFilter(CarbonExtension.Direction.sent.name(), CarbonExtension.NAMESPACE),
089                            new StanzaExtensionFilter(CarbonExtension.Direction.received.name(), CarbonExtension.NAMESPACE)
090                        ),
091                        StanzaTypeFilter.MESSAGE
092                    );
093                    // @formatter:on
094
095    private final Set<CarbonCopyReceivedListener> listeners = new CopyOnWriteArraySet<>();
096
097    private volatile boolean enabled_state = false;
098
099    private final StanzaListener carbonsListener;
100
101    private CarbonManager(XMPPConnection connection) {
102        super(connection);
103        ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
104        sdm.addFeature(CarbonExtension.NAMESPACE);
105
106        carbonsListener = new StanzaListener() {
107            @Override
108            public void processStanza(final Stanza stanza) throws NotConnectedException, InterruptedException {
109                final Message wrappingMessage = (Message) stanza;
110                final CarbonExtension carbonExtension = CarbonExtension.from(wrappingMessage);
111                final Direction direction = carbonExtension.getDirection();
112                final Forwarded forwarded = carbonExtension.getForwarded();
113                final Message carbonCopy = (Message) forwarded.getForwardedStanza();
114                for (CarbonCopyReceivedListener listener : listeners) {
115                    listener.onCarbonCopyReceived(direction, carbonCopy, wrappingMessage);
116                }
117            }
118        };
119
120        connection.addConnectionListener(new AbstractConnectionListener() {
121            @Override
122            public void connectionClosed() {
123                // Reset the state if the connection was cleanly closed. Note that this is not strictly necessary,
124                // because we also reset in authenticated() if the stream got not resumed, but for maximum correctness,
125                // also reset here.
126                enabled_state = false;
127                boolean removed = connection().removeSyncStanzaListener(carbonsListener);
128                assert (removed);
129            }
130            @Override
131            public void authenticated(XMPPConnection connection, boolean resumed) {
132                if (!resumed) {
133                    // Non-resumed XMPP sessions always start with disabled carbons
134                    enabled_state = false;
135                }
136                addCarbonsListener(connection);
137            }
138        });
139
140        addCarbonsListener(connection);
141    }
142
143    private void addCarbonsListener(XMPPConnection connection) {
144        EntityFullJid localAddress = connection.getUser();
145        if (localAddress == null) {
146            // We where not connected yet and thus we don't know our XMPP address at the moment, which we need to match incoming
147            // carbons securely. Abort here. The ConnectionListener above will eventually setup the carbons listener.
148            return;
149        }
150
151        // XEP-0280 ยง 11. Security Considerations "Any forwarded copies received by a Carbons-enabled client MUST be
152        // from that user's bare JID; any copies that do not meet this requirement MUST be ignored." Otherwise, if
153        // those copies do not get ignored, malicious users may be able to impersonate other users. That is why the
154        // 'from' matcher is important here.
155        connection.addSyncStanzaListener(carbonsListener, new AndFilter(CARBON_EXTENSION_FILTER,
156                        FromMatchesFilter.createBare(localAddress)));
157    }
158
159    /**
160     * Obtain the CarbonManager responsible for a connection.
161     *
162     * @param connection the connection object.
163     *
164     * @return a CarbonManager instance
165     */
166    public static synchronized CarbonManager getInstanceFor(XMPPConnection connection) {
167        CarbonManager carbonManager = INSTANCES.get(connection);
168
169        if (carbonManager == null) {
170            carbonManager = new CarbonManager(connection);
171            INSTANCES.put(connection, carbonManager);
172        }
173
174        return carbonManager;
175    }
176
177    private static IQ carbonsEnabledIQ(final boolean new_state) {
178        IQ request;
179        if (new_state) {
180            request = new Carbon.Enable();
181        } else {
182            request = new Carbon.Disable();
183        }
184        return request;
185    }
186
187    /**
188     * Add a carbon copy received listener.
189     *
190     * @param listener the listener to register.
191     * @return <code>true</code> if the filter was not already registered.
192     * @since 4.2
193     */
194    public boolean addCarbonCopyReceivedListener(CarbonCopyReceivedListener listener) {
195        return listeners.add(listener);
196    }
197
198    /**
199     * Remove a carbon copy received listener.
200     *
201     * @param listener the listener to register.
202     * @return <code>true</code> if the filter was registered.
203     * @since 4.2
204     */
205    public boolean removeCarbonCopyReceivedListener(CarbonCopyReceivedListener listener) {
206        return listeners.remove(listener);
207    }
208
209    /**
210     * Returns true if XMPP Carbons are supported by the server.
211     * 
212     * @return true if supported
213     * @throws NotConnectedException 
214     * @throws XMPPErrorException 
215     * @throws NoResponseException 
216     * @throws InterruptedException 
217     */
218    public boolean isSupportedByServer() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
219        return ServiceDiscoveryManager.getInstanceFor(connection()).serverSupportsFeature(CarbonExtension.NAMESPACE);
220    }
221
222    /**
223     * Notify server to change the carbons state. This method returns
224     * immediately and changes the variable when the reply arrives.
225     *
226     * You should first check for support using isSupportedByServer().
227     *
228     * @param new_state whether carbons should be enabled or disabled
229     * @throws NotConnectedException 
230     * @throws InterruptedException
231     * @deprecated use {@link #enableCarbonsAsync(ExceptionCallback)} or {@link #disableCarbonsAsync(ExceptionCallback)} instead.
232     */
233    @Deprecated
234    public void sendCarbonsEnabled(final boolean new_state) throws NotConnectedException, InterruptedException {
235        sendUseCarbons(new_state, null);
236    }
237
238    /**
239     * Enable carbons asynchronously. If an error occurs as result of the attempt to enable carbons, the optional
240     * <code>exceptionCallback</code> will be invoked.
241     * <p>
242     * Note that although this method is asynchronous, it may block if the outgoing stream element queue is full (e.g.
243     * because of a slow network connection). Thus, if the thread performing this operation is interrupted while the
244     * queue is full, an {@link InterruptedException} is thrown.
245     * </p>
246     * 
247     * @param exceptionCallback the optional exception callback.
248     * @throws InterruptedException if the thread got interrupted while this action is performed.
249     * @since 4.2
250     */
251    public void enableCarbonsAsync(ExceptionCallback exceptionCallback) throws InterruptedException {
252        sendUseCarbons(true, exceptionCallback);
253    }
254
255    /**
256     * Disable carbons asynchronously. If an error occurs as result of the attempt to disable carbons, the optional
257     * <code>exceptionCallback</code> will be invoked.
258     * <p>
259     * Note that although this method is asynchronous, it may block if the outgoing stream element queue is full (e.g.
260     * because of a slow network connection). Thus, if the thread performing this operation is interrupted while the
261     * queue is full, an {@link InterruptedException} is thrown.
262     * </p>
263     * 
264     * @param exceptionCallback the optional exception callback.
265     * @throws InterruptedException if the thread got interrupted while this action is performed.
266     * @since 4.2
267     */
268    public void disableCarbonsAsync(ExceptionCallback exceptionCallback) throws InterruptedException {
269        sendUseCarbons(false, exceptionCallback);
270    }
271
272    private void sendUseCarbons(final boolean use, ExceptionCallback exceptionCallback) throws InterruptedException {
273        IQ setIQ = carbonsEnabledIQ(use);
274
275        try {
276            connection().sendIqWithResponseCallback(setIQ, new StanzaListener() {
277                @Override
278                public void processStanza(Stanza packet) {
279                    enabled_state = use;
280                }
281            }, exceptionCallback);
282        }
283        catch (NotConnectedException e) {
284            if (exceptionCallback != null) {
285                exceptionCallback.processException(e);
286            }
287        }
288    }
289
290    /**
291     * Notify server to change the carbons state. This method blocks
292     * some time until the server replies to the IQ and returns true on
293     * success.
294     *
295     * You should first check for support using isSupportedByServer().
296     *
297     * @param new_state whether carbons should be enabled or disabled
298     * @throws XMPPErrorException 
299     * @throws NoResponseException 
300     * @throws NotConnectedException 
301     * @throws InterruptedException 
302     *
303     */
304    public synchronized void setCarbonsEnabled(final boolean new_state) throws NoResponseException,
305                    XMPPErrorException, NotConnectedException, InterruptedException {
306        if (enabled_state == new_state)
307            return;
308
309        IQ setIQ = carbonsEnabledIQ(new_state);
310
311        connection().createStanzaCollectorAndSend(setIQ).nextResultOrThrow();
312        enabled_state = new_state;
313    }
314
315    /**
316     * Helper method to enable carbons.
317     *
318     * @throws XMPPException 
319     * @throws SmackException if there was no response from the server.
320     * @throws InterruptedException 
321     */
322    public void enableCarbons() throws XMPPException, SmackException, InterruptedException {
323        setCarbonsEnabled(true);
324    }
325
326    /**
327     * Helper method to disable carbons.
328     *
329     * @throws XMPPException 
330     * @throws SmackException if there was no response from the server.
331     * @throws InterruptedException 
332     */
333    public void disableCarbons() throws XMPPException, SmackException, InterruptedException {
334        setCarbonsEnabled(false);
335    }
336
337    /**
338     * Check if carbons are enabled on this connection.
339     *
340     * @return true if carbons are enabled, else false.
341     */
342    public boolean getCarbonsEnabled() {
343        return this.enabled_state;
344    }
345
346    /**
347     * Mark a message as "private", so it will not be carbon-copied.
348     *
349     * @param msg Message object to mark private
350     * @deprecated use {@link Private#addTo(Message)}
351     */
352    @Deprecated
353    public static void disableCarbons(Message msg) {
354        msg.addExtension(Private.INSTANCE);
355    }
356}