1 /** 2 * $RCSfile$ 3 * $Revision$ 4 * $Date$ 5 * 6 * Copyright 2003-2007 Jive Software. 7 * 8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package org.jivesoftware.smackx.packet; 22 23 import org.jivesoftware.smack.packet.PacketExtension; 24 25 import java.text.SimpleDateFormat; 26 import java.util.Date; 27 import java.util.TimeZone; 28 29 /** 30 * Represents extended presence information whose sole purpose is to signal the ability of 31 * the occupant to speak the MUC protocol when joining a room. If the room requires a password 32 * then the MUCInitialPresence should include one.<p> 33 * 34 * The amount of discussion history provided on entering a room (perhaps because the 35 * user is on a low-bandwidth connection or is using a small-footprint client) could be managed by 36 * setting a configured History instance to the MUCInitialPresence instance. 37 * @see MUCInitialPresence#setHistory(MUCInitialPresence.History). 38 * 39 * @author Gaston Dombiak 40 */ 41 public class MUCInitialPresence implements PacketExtension { 42 43 private String password; 44 private History history; 45 46 public String getElementName() { 47 return "x"; 48 } 49 50 public String getNamespace() { 51 return "http://jabber.org/protocol/muc"; 52 } 53 54 public String toXML() { 55 StringBuilder buf = new StringBuilder(); 56 buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( 57 "\">"); 58 if (getPassword() != null) { 59 buf.append("<password>").append(getPassword()).append("</password>"); 60 } 61 if (getHistory() != null) { 62 buf.append(getHistory().toXML()); 63 } 64 buf.append("</").append(getElementName()).append(">"); 65 return buf.toString(); 66 } 67 68 /** 69 * Returns the history that manages the amount of discussion history provided on 70 * entering a room. 71 * 72 * @return the history that manages the amount of discussion history provided on 73 * entering a room. 74 */ 75 public History getHistory() { 76 return history; 77 } 78 79 /** 80 * Returns the password to use when the room requires a password. 81 * 82 * @return the password to use when the room requires a password. 83 */ 84 public String getPassword() { 85 return password; 86 } 87 88 /** 89 * Sets the History that manages the amount of discussion history provided on 90 * entering a room. 91 * 92 * @param history that manages the amount of discussion history provided on 93 * entering a room. 94 */ 95 public void setHistory(History history) { 96 this.history = history; 97 } 98 99 /** 100 * Sets the password to use when the room requires a password. 101 * 102 * @param password the password to use when the room requires a password. 103 */ 104 public void setPassword(String password) { 105 this.password = password; 106 } 107 108 /** 109 * The History class controls the number of characters or messages to receive 110 * when entering a room. 111 * 112 * @author Gaston Dombiak 113 */ 114 public static class History { 115 116 private int maxChars = -1; 117 private int maxStanzas = -1; 118 private int seconds = -1; 119 private Date since; 120 121 /** 122 * Returns the total number of characters to receive in the history. 123 * 124 * @return total number of characters to receive in the history. 125 */ 126 public int getMaxChars() { 127 return maxChars; 128 } 129 130 /** 131 * Returns the total number of messages to receive in the history. 132 * 133 * @return the total number of messages to receive in the history. 134 */ 135 public int getMaxStanzas() { 136 return maxStanzas; 137 } 138 139 /** 140 * Returns the number of seconds to use to filter the messages received during that time. 141 * In other words, only the messages received in the last "X" seconds will be included in 142 * the history. 143 * 144 * @return the number of seconds to use to filter the messages received during that time. 145 */ 146 public int getSeconds() { 147 return seconds; 148 } 149 150 /** 151 * Returns the since date to use to filter the messages received during that time. 152 * In other words, only the messages received since the datetime specified will be 153 * included in the history. 154 * 155 * @return the since date to use to filter the messages received during that time. 156 */ 157 public Date getSince() { 158 return since; 159 } 160 161 /** 162 * Sets the total number of characters to receive in the history. 163 * 164 * @param maxChars the total number of characters to receive in the history. 165 */ 166 public void setMaxChars(int maxChars) { 167 this.maxChars = maxChars; 168 } 169 170 /** 171 * Sets the total number of messages to receive in the history. 172 * 173 * @param maxStanzas the total number of messages to receive in the history. 174 */ 175 public void setMaxStanzas(int maxStanzas) { 176 this.maxStanzas = maxStanzas; 177 } 178 179 /** 180 * Sets the number of seconds to use to filter the messages received during that time. 181 * In other words, only the messages received in the last "X" seconds will be included in 182 * the history. 183 * 184 * @param seconds the number of seconds to use to filter the messages received during 185 * that time. 186 */ 187 public void setSeconds(int seconds) { 188 this.seconds = seconds; 189 } 190 191 /** 192 * Sets the since date to use to filter the messages received during that time. 193 * In other words, only the messages received since the datetime specified will be 194 * included in the history. 195 * 196 * @param since the since date to use to filter the messages received during that time. 197 */ 198 public void setSince(Date since) { 199 this.since = since; 200 } 201 202 public String toXML() { 203 StringBuilder buf = new StringBuilder(); 204 buf.append("<history"); 205 if (getMaxChars() != -1) { 206 buf.append(" maxchars=\"").append(getMaxChars()).append("\""); 207 } 208 if (getMaxStanzas() != -1) { 209 buf.append(" maxstanzas=\"").append(getMaxStanzas()).append("\""); 210 } 211 if (getSeconds() != -1) { 212 buf.append(" seconds=\"").append(getSeconds()).append("\""); 213 } 214 if (getSince() != null) { 215 SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 216 utcFormat.setTimeZone(TimeZone.getTimeZone("UTC")); 217 buf.append(" since=\"").append(utcFormat.format(getSince())).append("\""); 218 } 219 buf.append("/>"); 220 return buf.toString(); 221 } 222 } 223 } 224