1 11 12 package org.jivesoftware.messenger.muc; 13 14 import java.text.SimpleDateFormat ; 15 import java.util.Date ; 16 import java.util.Iterator ; 17 import java.util.ListIterator ; 18 import java.util.TimeZone ; 19 20 import org.jivesoftware.messenger.user.UserNotFoundException; 21 import org.xmpp.packet.Message; 22 import org.xmpp.packet.JID; 23 import org.dom4j.Element; 24 25 31 public final class MUCRoomHistory { 32 33 private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat ("yyyyMMdd'T'HH:mm:ss"); 34 static { 35 UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0")); 36 } 37 38 private MUCRoom room; 39 40 private HistoryStrategy historyStrategy; 41 42 private boolean isNonAnonymousRoom; 43 44 public MUCRoomHistory(MUCRoom mucRoom, HistoryStrategy historyStrategy) { 45 this.room = mucRoom; 46 this.isNonAnonymousRoom = mucRoom.canAnyoneDiscoverJID(); 47 this.historyStrategy = historyStrategy; 48 } 49 50 public void addMessage(Message packet) { 51 if ((packet.getFrom() == null || packet.getFrom().toString().length() == 0 || 54 packet.getFrom().equals(room.getRole().getRoleAddress())) && 55 packet.getSubject() == null) { 56 return; 57 } 58 Message packetToAdd = (Message) packet.createCopy(); 59 60 62 if (isNonAnonymousRoom != room.canAnyoneDiscoverJID()) { 64 isNonAnonymousRoom = room.canAnyoneDiscoverJID(); 65 Message message; 67 Element delayElement; 68 for (Iterator it = getMessageHistory(); it.hasNext();) { 70 message = (Message) it.next(); 71 delayElement = message.getChildElement("x", "jabber:x:delay"); 72 if (room.canAnyoneDiscoverJID()) { 73 try { 75 MUCRole role = room.getOccupant(message.getFrom().getResource()); 76 delayElement.addAttribute("from", role.getChatUser().getAddress().toString()); 77 } 78 catch (UserNotFoundException e) { 79 } 80 } 81 else { 82 delayElement.addAttribute("from", message.getFrom().toString()); 84 } 85 } 86 87 } 88 89 Element delayInformation = packetToAdd.addChildElement("x", "jabber:x:delay"); 91 Date current = new Date (); 92 delayInformation.addAttribute("stamp", UTC_FORMAT.format(current)); 93 if (room.canAnyoneDiscoverJID()) { 94 try { 96 MUCRole role = room.getOccupant(packet.getFrom().getResource()); 97 delayInformation.addAttribute("from", role.getChatUser().getAddress() 98 .toString()); 99 } 100 catch (UserNotFoundException e) { 101 } 102 } 103 else { 104 delayInformation.addAttribute("from", packet.getFrom().toString()); 106 } 107 historyStrategy.addMessage(packetToAdd); 108 } 109 110 public Iterator getMessageHistory() { 111 return historyStrategy.getMessageHistory(); 112 } 113 114 121 public ListIterator getReverseMessageHistory() { 122 return historyStrategy.getReverseMessageHistory(); 123 } 124 125 136 public void addOldMessage(String senderJID, String nickname, Date sentDate, String subject, 137 String body) 138 { 139 Message message = new Message(); 140 message.setType(Message.Type.groupchat); 141 message.setSubject(subject); 142 message.setBody(body); 143 if (nickname != null && nickname.trim().length() > 0) { 145 JID roomJID = room.getRole().getRoleAddress(); 146 message.setFrom(new JID(roomJID.getNode(), roomJID.getDomain(), 148 nickname)); 149 } 150 else { 151 message.setFrom(room.getRole().getRoleAddress()); 153 } 154 155 Element delayInformation = message.addChildElement("x", "jabber:x:delay"); 157 delayInformation.addAttribute("stamp", UTC_FORMAT.format(sentDate)); 158 if (room.canAnyoneDiscoverJID()) { 159 delayInformation.addAttribute("from", senderJID); 161 } 162 else { 163 delayInformation.addAttribute("from", room.getRole().getRoleAddress().toString()); 165 } 166 historyStrategy.addMessage(message); 167 } 168 169 176 public boolean hasChangedSubject() { 177 return historyStrategy.hasChangedSubject(); 178 } 179 } | Popular Tags |