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 org.jivesoftware.smack.packet.PacketExtension;
021
022import java.text.SimpleDateFormat;
023import java.util.Date;
024import java.util.TimeZone;
025
026/**
027 * Represents extended presence information whose sole purpose is to signal the ability of 
028 * the occupant to speak the MUC protocol when joining a room. If the room requires a password 
029 * then the MUCInitialPresence should include one.
030 * <p>
031 * The amount of discussion history provided on entering a room (perhaps because the 
032 * user is on a low-bandwidth connection or is using a small-footprint client) could be managed by
033 * setting a configured History instance to the MUCInitialPresence instance. 
034 *
035 * @author Gaston Dombiak
036 * @see MUCInitialPresence#setHistory(MUCInitialPresence.History)
037 */
038public class MUCInitialPresence implements PacketExtension {
039
040    private String password;
041    private History history; 
042
043    public String getElementName() {
044        return "x";
045    }
046
047    public String getNamespace() {
048        return "http://jabber.org/protocol/muc";
049    }
050
051    public String toXML() {
052        StringBuilder buf = new StringBuilder();
053        buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
054            "\">");
055        if (getPassword() != null) {
056            buf.append("<password>").append(getPassword()).append("</password>");
057        }
058        if (getHistory() != null) {
059            buf.append(getHistory().toXML());
060        }
061        buf.append("</").append(getElementName()).append(">");
062        return buf.toString();
063    }
064
065    /**
066     * Returns the history that manages the amount of discussion history provided on 
067     * entering a room.
068     * 
069     * @return the history that manages the amount of discussion history provided on 
070     * entering a room.
071     */
072    public History getHistory() {
073        return history;
074    }
075
076    /**
077     * Returns the password to use when the room requires a password.
078     * 
079     * @return the password to use when the room requires a password.
080     */
081    public String getPassword() {
082        return password;
083    }
084
085    /**
086     * Sets the History that manages the amount of discussion history provided on 
087     * entering a room.
088     * 
089     * @param history that manages the amount of discussion history provided on 
090     * entering a room.
091     */
092    public void setHistory(History history) {
093        this.history = history;
094    }
095
096    /**
097     * Sets the password to use when the room requires a password.
098     * 
099     * @param password the password to use when the room requires a password.
100     */
101    public void setPassword(String password) {
102        this.password = password;
103    }
104
105    /**
106     * The History class controls the number of characters or messages to receive
107     * when entering a room.
108     * 
109     * @author Gaston Dombiak
110     */
111    public static class History {
112
113        private int maxChars = -1;
114        private int maxStanzas = -1; 
115        private int seconds = -1; 
116        private Date since; 
117
118        /**
119         * Returns the total number of characters to receive in the history.
120         * 
121         * @return total number of characters to receive in the history.
122         */
123        public int getMaxChars() {
124            return maxChars;
125        }
126
127        /**
128         * Returns the total number of messages to receive in the history.
129         * 
130         * @return the total number of messages to receive in the history.
131         */
132        public int getMaxStanzas() {
133            return maxStanzas;
134        }
135
136        /**
137         * Returns the number of seconds to use to filter the messages received during that time. 
138         * In other words, only the messages received in the last "X" seconds will be included in 
139         * the history.
140         * 
141         * @return the number of seconds to use to filter the messages received during that time.
142         */
143        public int getSeconds() {
144            return seconds;
145        }
146
147        /**
148         * Returns the since date to use to filter the messages received during that time. 
149         * In other words, only the messages received since the datetime specified will be 
150         * included in the history.
151         * 
152         * @return the since date to use to filter the messages received during that time.
153         */
154        public Date getSince() {
155            return since;
156        }
157
158        /**
159         * Sets the total number of characters to receive in the history.
160         * 
161         * @param maxChars the total number of characters to receive in the history.
162         */
163        public void setMaxChars(int maxChars) {
164            this.maxChars = maxChars;
165        }
166
167        /**
168         * Sets the total number of messages to receive in the history.
169         * 
170         * @param maxStanzas the total number of messages to receive in the history.
171         */
172        public void setMaxStanzas(int maxStanzas) {
173            this.maxStanzas = maxStanzas;
174        }
175
176        /**
177         * Sets the number of seconds to use to filter the messages received during that time. 
178         * In other words, only the messages received in the last "X" seconds will be included in 
179         * the history.
180         * 
181         * @param seconds the number of seconds to use to filter the messages received during 
182         * that time.
183         */
184        public void setSeconds(int seconds) {
185            this.seconds = seconds;
186        }
187
188        /**
189         * Sets the since date to use to filter the messages received during that time. 
190         * In other words, only the messages received since the datetime specified will be 
191         * included in the history.
192         * 
193         * @param since the since date to use to filter the messages received during that time.
194         */
195        public void setSince(Date since) {
196            this.since = since;
197        }
198
199        public String toXML() {
200            StringBuilder buf = new StringBuilder();
201            buf.append("<history");
202            if (getMaxChars() != -1) {
203                buf.append(" maxchars=\"").append(getMaxChars()).append("\"");
204            }
205            if (getMaxStanzas() != -1) {
206                buf.append(" maxstanzas=\"").append(getMaxStanzas()).append("\"");
207            }
208            if (getSeconds() != -1) {
209                buf.append(" seconds=\"").append(getSeconds()).append("\"");
210            }
211            if (getSince() != null) {
212                SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
213                utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
214                buf.append(" since=\"").append(utcFormat.format(getSince())).append("\"");
215            }
216            buf.append("/>");
217            return buf.toString();
218        }
219    }
220}