001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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 */
017
018package org.jivesoftware.smackx.muc.packet;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.packet.Stanza;
024import org.jivesoftware.smack.provider.ExtensionElementProvider;
025import org.jivesoftware.smack.util.XmlStringBuilder;
026
027import org.xmlpull.v1.XmlPullParser;
028import org.xmlpull.v1.XmlPullParserException;
029
030/**
031 * A group chat invitation stanza extension, which is used to invite other
032 * users to a group chat room. To invite a user to a group chat room, address
033 * a new message to the user and set the room name appropriately, as in the
034 * following code example:
035 *
036 * <pre>
037 * Message message = new Message("user@chat.example.com");
038 * message.setBody("Join me for a group chat!");
039 * message.addExtension(new GroupChatInvitation("room@chat.example.com"););
040 * con.sendStanza(message);
041 * </pre>
042 *
043 * To listen for group chat invitations, use a StanzaExtensionFilter for the
044 * <tt>x</tt> element name and <tt>jabber:x:conference</tt> namespace, as in the
045 * following code example:
046 *
047 * <pre>
048 * PacketFilter filter = new StanzaExtensionFilter("x", "jabber:x:conference");
049 * // Create a stanza collector or stanza listeners using the filter...
050 * </pre>
051 *
052 * <b>Note</b>: this protocol is outdated now that the Multi-User Chat (MUC) XEP is available
053 * (<a href="http://www.xmpp.org/extensions/jep-0045.html">XEP-45</a>). However, most
054 * existing clients still use this older protocol. Once MUC support becomes more
055 * widespread, this API may be deprecated.
056 *
057 * @author Matt Tucker
058 */
059public class GroupChatInvitation implements ExtensionElement {
060
061    /**
062     * Element name of the stanza extension.
063     */
064    public static final String ELEMENT = "x";
065
066    /**
067     * Namespace of the stanza extension.
068     */
069    public static final String NAMESPACE = "jabber:x:conference";
070
071    private final String roomAddress;
072
073    /**
074     * Creates a new group chat invitation to the specified room address.
075     * GroupChat room addresses are in the form <tt>room@service</tt>,
076     * where <tt>service</tt> is the name of group chat server, such as
077     * <tt>chat.example.com</tt>.
078     *
079     * @param roomAddress the address of the group chat room.
080     */
081    public GroupChatInvitation(String roomAddress) {
082        this.roomAddress = roomAddress;
083    }
084
085    /**
086     * Returns the address of the group chat room. GroupChat room addresses
087     * are in the form <tt>room@service</tt>, where <tt>service</tt> is
088     * the name of group chat server, such as <tt>chat.example.com</tt>.
089     *
090     * @return the address of the group chat room.
091     */
092    public String getRoomAddress() {
093        return roomAddress;
094    }
095
096    @Override
097    public String getElementName() {
098        return ELEMENT;
099    }
100
101    @Override
102    public String getNamespace() {
103        return NAMESPACE;
104    }
105
106    @Override
107    public XmlStringBuilder toXML(String enclosingNamespace) {
108        XmlStringBuilder xml = new XmlStringBuilder(this);
109        xml.attribute("jid", getRoomAddress());
110        xml.closeEmptyElement();
111        return xml;
112    }
113
114    /**
115     * Deprecated.
116     * @param packet
117     * @return the GroupChatInvitation or null
118     * @deprecated use {@link #from(Stanza)} instead
119     */
120    @Deprecated
121    public static GroupChatInvitation getFrom(Stanza packet) {
122        return from(packet);
123    }
124
125    /**
126     * Get the group chat invitation from the given stanza.
127     * @param packet
128     * @return the GroupChatInvitation or null
129     */
130    public static GroupChatInvitation from(Stanza packet) {
131        return packet.getExtension(ELEMENT, NAMESPACE);
132    }
133
134    public static class Provider extends ExtensionElementProvider<GroupChatInvitation> {
135
136        @Override
137        public GroupChatInvitation parse(XmlPullParser parser,
138                        int initialDepth) throws XmlPullParserException,
139                        IOException {
140            String roomAddress = parser.getAttributeValue("", "jid");
141            // Advance to end of extension.
142            parser.next();
143            return new GroupChatInvitation(roomAddress);
144        }
145    }
146}