Home | History | Annotate | Download | only in carbons
      1 /**
      2  * Copyright 2013 Georg Lukas
      3  *
      4  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.jivesoftware.smackx.carbons;
     18 
     19 import org.jivesoftware.smack.packet.Packet;
     20 import org.jivesoftware.smack.packet.PacketExtension;
     21 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     22 import org.jivesoftware.smack.util.PacketParserUtils;
     23 import org.jivesoftware.smackx.forward.Forwarded;
     24 import org.jivesoftware.smackx.packet.DelayInfo;
     25 import org.jivesoftware.smackx.provider.DelayInfoProvider;
     26 import org.xmlpull.v1.XmlPullParser;
     27 
     28 /**
     29  * Packet extension for XEP-0280: Message Carbons. This class implements
     30  * the packet extension and a {@link PacketExtensionProvider} to parse
     31  * message carbon copies from a packet. The extension
     32  * <a href="http://xmpp.org/extensions/xep-0280.html">XEP-0280</a> is
     33  * meant to synchronize a message flow to multiple presences of a user.
     34  *
     35  * <p>The {@link Carbon.Provider} must be registered in the
     36  * <b>smack.properties</b> file for the elements <b>sent</b> and
     37  * <b>received</b> with namespace <b>urn:xmpp:carbons:2</b></p> to be used.
     38  *
     39  * @author Georg Lukas
     40  */
     41 public class Carbon implements PacketExtension {
     42     public static final String NAMESPACE = "urn:xmpp:carbons:2";
     43 
     44     private Direction dir;
     45     private Forwarded fwd;
     46 
     47     public Carbon(Direction dir, Forwarded fwd) {
     48         this.dir = dir;
     49         this.fwd = fwd;
     50     }
     51 
     52     /**
     53      * get the direction (sent or received) of the carbon.
     54      *
     55      * @return the {@link Direction} of the carbon.
     56      */
     57     public Direction getDirection() {
     58         return dir;
     59     }
     60 
     61     /**
     62      * get the forwarded packet.
     63      *
     64      * @return the {@link Forwarded} message contained in this Carbon.
     65      */
     66     public Forwarded getForwarded() {
     67         return fwd;
     68     }
     69 
     70     @Override
     71     public String getElementName() {
     72         return dir.toString();
     73     }
     74 
     75     @Override
     76     public String getNamespace() {
     77         return NAMESPACE;
     78     }
     79 
     80     @Override
     81     public String toXML() {
     82         StringBuilder buf = new StringBuilder();
     83         buf.append("<").append(getElementName()).append(" xmlns=\"")
     84                 .append(getNamespace()).append("\">");
     85 
     86         buf.append(fwd.toXML());
     87 
     88         buf.append("</").append(getElementName()).append(">");
     89         return buf.toString();
     90     }
     91 
     92     /**
     93      * An enum to display the direction of a {@link Carbon} message.
     94      */
     95     public static enum Direction {
     96         received,
     97         sent
     98     }
     99 
    100     public static class Provider implements PacketExtensionProvider {
    101 
    102         public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
    103             Direction dir = Direction.valueOf(parser.getName());
    104             Forwarded fwd = null;
    105 
    106             boolean done = false;
    107             while (!done) {
    108                 int eventType = parser.next();
    109                 if (eventType == XmlPullParser.START_TAG && parser.getName().equals("forwarded")) {
    110                     fwd = (Forwarded)new Forwarded.Provider().parseExtension(parser);
    111                 }
    112                 else if (eventType == XmlPullParser.END_TAG && dir == Direction.valueOf(parser.getName()))
    113                     done = true;
    114             }
    115             if (fwd == null)
    116                 throw new Exception("sent/received must contain exactly one <forwarded> tag");
    117             return new Carbon(dir, fwd);
    118         }
    119     }
    120 
    121     /**
    122      * Packet extension indicating that a message may not be carbon-copied.
    123      */
    124     public static class Private implements PacketExtension {
    125         public static final String ELEMENT = "private";
    126 
    127         public String getElementName() {
    128             return ELEMENT;
    129         }
    130 
    131         public String getNamespace() {
    132             return Carbon.NAMESPACE;
    133         }
    134 
    135         public String toXML() {
    136             return "<" + ELEMENT + " xmlns=\"" + Carbon.NAMESPACE + "\"/>";
    137         }
    138     }
    139 }
    140