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.util.Date;
021
022import javax.xml.namespace.QName;
023
024import org.jivesoftware.smack.packet.ExtensionElement;
025import org.jivesoftware.smack.packet.NamedElement;
026import org.jivesoftware.smack.packet.Stanza;
027import org.jivesoftware.smack.util.XmlStringBuilder;
028
029import org.jxmpp.util.XmppDateTime;
030
031/**
032 * Represents extended presence information whose sole purpose is to signal the ability of
033 * the occupant to speak the MUC protocol when joining a room. If the room requires a password
034 * then the MUCInitialPresence should include one.
035 * <p>
036 * The amount of discussion history provided on entering a room (perhaps because the
037 * user is on a low-bandwidth connection or is using a small-footprint client) could be managed by
038 * setting a configured History instance to the MUCInitialPresence instance.
039 *
040 * @author Gaston Dombiak
041 * @see MUCInitialPresence#setHistory(MUCInitialPresence.History)
042 */
043public class MUCInitialPresence implements ExtensionElement {
044
045    public static final String ELEMENT = "x";
046    public static final String NAMESPACE = "http://jabber.org/protocol/muc";
047    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
048
049    // TODO make those fields final once deprecated setter methods have been removed.
050    private String password;
051    private History history;
052
053    /**
054     * Deprecated constructor.
055     * @deprecated use {@link #MUCInitialPresence(String, int, int, int, Date)} instead.
056     */
057    @Deprecated
058    public MUCInitialPresence() {
059    }
060
061    /**
062     * Construct a new MUC initial presence extension.
063     *
064     * @param password the optional password used to enter the room.
065     * @param maxChars the maximal count of characters of history to request.
066     * @param maxStanzas the maximal count of stanzas of history to request.
067     * @param seconds the last seconds since when to request history.
068     * @param since the date since when to request history.
069     */
070    public MUCInitialPresence(String password, int maxChars, int maxStanzas, int seconds, Date since) {
071        this.password = password;
072        if (maxChars > -1 || maxStanzas > -1 || seconds > -1 || since != null) {
073            this.history = new History(maxChars, maxStanzas, seconds, since);
074        } else {
075            this.history = null;
076        }
077    }
078
079    @Override
080    public String getElementName() {
081        return ELEMENT;
082    }
083
084    @Override
085    public String getNamespace() {
086        return NAMESPACE;
087    }
088
089    @Override
090    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
091        XmlStringBuilder xml = new XmlStringBuilder(this);
092        xml.rightAngleBracket();
093        xml.optElement("password", getPassword());
094        xml.optElement(getHistory());
095        xml.closeElement(this);
096        return xml;
097    }
098
099    /**
100     * Returns the history that manages the amount of discussion history provided on
101     * entering a room.
102     *
103     * @return the history that manages the amount of discussion history provided on
104     * entering a room.
105     */
106    public History getHistory() {
107        return history;
108    }
109
110    /**
111     * Returns the password to use when the room requires a password.
112     *
113     * @return the password to use when the room requires a password.
114     */
115    public String getPassword() {
116        return password;
117    }
118
119    /**
120     * Sets the History that manages the amount of discussion history provided on
121     * entering a room.
122     *
123     * @param history that manages the amount of discussion history provided on
124     * entering a room.
125     * @deprecated use {@link #MUCInitialPresence(String, int, int, int, Date)} instead.
126     */
127    @Deprecated
128    public void setHistory(History history) {
129        this.history = history;
130    }
131
132    /**
133     * Sets the password to use when the room requires a password.
134     *
135     * @param password the password to use when the room requires a password.
136     * @deprecated use {@link #MUCInitialPresence(String, int, int, int, Date)} instead.
137     */
138    @Deprecated
139    public void setPassword(String password) {
140        this.password = password;
141    }
142
143    /**
144     * Retrieve the MUCInitialPresence PacketExtension from packet, if any.
145     *
146     * @param packet TODO javadoc me please
147     * @return the MUCInitialPresence PacketExtension or {@code null}
148     * @deprecated use {@link #from(Stanza)} instead
149     */
150    @Deprecated
151    public static MUCInitialPresence getFrom(Stanza packet) {
152        return from(packet);
153    }
154
155    /**
156     * Retrieve the MUCInitialPresence PacketExtension from packet, if any.
157     *
158     * @param packet TODO javadoc me please
159     * @return the MUCInitialPresence PacketExtension or {@code null}
160     */
161    public static MUCInitialPresence from(Stanza packet) {
162        return packet.getExtension(MUCInitialPresence.class);
163    }
164
165    /**
166     * The History class controls the number of characters or messages to receive
167     * when entering a room.
168     *
169     * @author Gaston Dombiak
170     */
171    public static class History implements NamedElement {
172
173        public static final String ELEMENT = "history";
174
175        private final int maxChars;
176        private final int maxStanzas;
177        private final int seconds;
178        private final Date since;
179
180        public History(int maxChars, int maxStanzas, int seconds, Date since) {
181            if (maxChars < 0 && maxStanzas < 0 && seconds < 0 && since == null) {
182                throw new IllegalArgumentException();
183            }
184            this.maxChars = maxChars;
185            this.maxStanzas = maxStanzas;
186            this.seconds = seconds;
187            this.since = since;
188        }
189
190        /**
191         * Returns the total number of characters to receive in the history.
192         *
193         * @return total number of characters to receive in the history.
194         */
195        public int getMaxChars() {
196            return maxChars;
197        }
198
199        /**
200         * Returns the total number of messages to receive in the history.
201         *
202         * @return the total number of messages to receive in the history.
203         */
204        public int getMaxStanzas() {
205            return maxStanzas;
206        }
207
208        /**
209         * Returns the number of seconds to use to filter the messages received during that time.
210         * In other words, only the messages received in the last "X" seconds will be included in
211         * the history.
212         *
213         * @return the number of seconds to use to filter the messages received during that time.
214         */
215        public int getSeconds() {
216            return seconds;
217        }
218
219        /**
220         * Returns the since date to use to filter the messages received during that time.
221         * In other words, only the messages received since the datetime specified will be
222         * included in the history.
223         *
224         * @return the since date to use to filter the messages received during that time.
225         */
226        public Date getSince() {
227            return since;
228        }
229
230        @Override
231        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
232            XmlStringBuilder xml = new XmlStringBuilder(this);
233            xml.optIntAttribute("maxchars", getMaxChars());
234            xml.optIntAttribute("maxstanzas", getMaxStanzas());
235            xml.optIntAttribute("seconds", getSeconds());
236            if (getSince() != null) {
237                xml.attribute("since", XmppDateTime.formatXEP0082Date(getSince()));
238            }
239            xml.closeEmptyElement();
240            return xml;
241        }
242
243        @Override
244        public String getElementName() {
245            return ELEMENT;
246        }
247    }
248}