1 11 12 package org.jivesoftware.messenger.muc; 13 14 import java.text.DateFormat ; 15 import java.text.ParseException ; 16 import java.text.SimpleDateFormat ; 17 import java.util.Date ; 18 import java.util.Iterator ; 19 import java.util.LinkedList ; 20 import java.util.ListIterator ; 21 import java.util.TimeZone ; 22 23 import org.jivesoftware.messenger.muc.spi.MUCRoleImpl; 24 import org.jivesoftware.util.Log; 25 import org.jivesoftware.util.JiveConstants; 26 import org.dom4j.Element; 27 import org.xmpp.packet.Message; 28 29 41 public class HistoryRequest { 42 43 private static final DateFormat formatter = new SimpleDateFormat (JiveConstants.XMPP_DATETIME_FORMAT); 44 private static final DateFormat delayedFormatter = new SimpleDateFormat ("yyyyMMdd'T'HH:mm:ss"); 45 static { 46 delayedFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0")); 47 formatter.setTimeZone(TimeZone.getTimeZone("UTC")); 48 } 49 50 private int maxChars = -1; 51 private int maxStanzas = -1; 52 private int seconds = -1; 53 private Date since; 54 55 public HistoryRequest(Element userFragment) { 56 Element history = userFragment.element("history"); 57 if (history != null) { 58 if (history.attribute("maxchars") != null) { 59 this.maxChars = Integer.parseInt(history.attributeValue("maxchars")); 60 } 61 if (history.attribute("maxstanzas") != null) { 62 this.maxStanzas = Integer.parseInt(history.attributeValue("maxstanzas")); 63 } 64 if (history.attribute("seconds") != null) { 65 this.seconds = Integer.parseInt(history.attributeValue("seconds")); 66 } 67 if (history.attribute("since") != null) { 68 try { 69 this.since = formatter.parse(history.attributeValue("since")); 71 } 72 catch(ParseException pe) { 73 Log.error("Error parsing date from history management", pe); 74 this.since = null; 75 } 76 } 77 } 78 } 79 80 85 public int getMaxChars() { 86 return maxChars; 87 } 88 89 94 public int getMaxStanzas() { 95 return maxStanzas; 96 } 97 98 105 public int getSeconds() { 106 return seconds; 107 } 108 109 116 public Date getSince() { 117 return since; 118 } 119 120 125 private boolean isConfigured() { 126 return maxChars > -1 || maxStanzas > -1 || seconds > -1 || since != null; 127 } 128 129 135 public void sendHistory(MUCRoleImpl joinRole, MUCRoomHistory roomHistory) { 136 if (!isConfigured()) { 137 Iterator history = roomHistory.getMessageHistory(); 138 while (history.hasNext()) { 139 joinRole.send((Message) history.next()); 140 } 141 } 142 else { 143 if (getMaxChars() == 0) { 144 return; 146 } 147 Message message; 148 int accumulatedChars = 0; 149 int accumulatedStanzas = 0; 150 Element delayInformation; 151 LinkedList historyToSend = new LinkedList (); 152 ListIterator iterator = roomHistory.getReverseMessageHistory(); 153 while (iterator.hasPrevious()) { 154 message = (Message)iterator.previous(); 155 String text = message.getBody() == null ? message.getSubject() : message.getBody(); 157 accumulatedChars += text.length(); 158 if (getMaxChars() > -1 && accumulatedChars > getMaxChars()) { 159 break; 161 } 162 accumulatedStanzas ++; 164 if (getMaxStanzas() > -1 && accumulatedStanzas > getMaxStanzas()) { 165 break; 167 } 168 169 if (getSeconds() > -1 || getSince() != null) { 170 delayInformation = message.getChildElement("x", "jabber:x:delay"); 171 try { 172 Date delayedDate = delayedFormatter.parse(delayInformation.attributeValue("stamp")); 174 if (getSince() != null && delayedDate.before(getSince())) { 175 break; 177 } 178 if (getSeconds() > -1) { 179 Date current = new Date (); 180 long diff = (current.getTime() - delayedDate.getTime()) / 1000; 181 if (getSeconds() <= diff) { 182 break; 184 } 185 } 186 } 187 catch (Exception e) { 188 Log.error("Error parsing date from historic message", e); 189 } 190 191 } 192 193 historyToSend.addFirst(message); 194 } 195 Iterator history = historyToSend.iterator(); 197 while (history.hasNext()) { 198 joinRole.send((Message) history.next()); 199 } 200 } 201 } 202 } 203 | Popular Tags |