1 19 package freecs.core; 20 21 import java.util.Iterator ; 22 import java.util.Properties ; 23 import java.util.Vector ; 24 25 import freecs.Server; 26 import freecs.interfaces.IContainer; 27 import freecs.interfaces.IMessageDestination; 28 29 30 37 public class Membership implements IMessageDestination { 38 private Vector members = new Vector (); 39 40 public final String key; 41 private volatile String namePrefix, nameSuffix, stringRepresentation; 42 private boolean displayDefaultVipRighttitle = false, displayDefaultModGuestTitle = true, listedAsOnlinevip = true; 43 private int addedStates = 0, removedStates = 0; 44 private int addedDefaultStates = 0, removedDefaultStates = 0; 45 46 public Membership (String key, Properties p) { 47 if (key == null) 48 throw new RuntimeException ("unable to construct membership without key"); 49 this.key = key; 50 if (p == null && !key.equals("undefined")) 51 throw new RuntimeException ("unable to construct membership without properties"); 52 init(p); 53 } 54 55 private void init (Properties p) { 56 if (p==null) 57 return; 58 boolean checkNamePrefix = false,checkNameSuffix = false,checkStringRepresentation = false,checkAddedStates = false 59 ,checkRemovedStates = false,checkAddedDefaultStates = false,checkRemovedDefaultStates = false, 60 checkDisplayDefaultVipRighttitle = false,checkDisplayDefaultModGuestTitle = false,checkListedAsOnlinevip = false; 61 for (Iterator i = p.keySet().iterator(); i.hasNext(); ) { 62 String currKey = (String ) i.next(); 63 if (currKey.equalsIgnoreCase ("usernameprefix")) { 64 namePrefix = p.getProperty(currKey); 65 checkNamePrefix = true; 66 } else if (currKey.equalsIgnoreCase ("usernamesuffix")) { 67 nameSuffix = p.getProperty(currKey); 68 checkNameSuffix = true; 69 } else if (currKey.equalsIgnoreCase ("stringrepresentation")) { 70 stringRepresentation = p.getProperty(currKey); 71 checkStringRepresentation = true; 72 } else if (currKey.equalsIgnoreCase ("addstates")) { 73 String val = p.getProperty(currKey); 74 String [] states = val.split(","); 75 addedStates = 0; 76 for (int j = 0; j < states.length; j++) { 77 addedStates = addedStates | UserManager.resolveState(states[j]); 78 } 79 checkAddedStates = true; 80 } else if (currKey.equalsIgnoreCase ("removestates")) { 81 String val = p.getProperty(currKey); 82 String [] states = val.split(","); 83 removedStates = 0; 84 for (int j = 0; j < states.length; j++) { 85 removedStates = removedStates | UserManager.resolveState(states[j]); 86 } 87 checkRemovedStates = true; 88 } else if (currKey.equalsIgnoreCase ("adddefaultstates")) { 89 String val = p.getProperty(currKey); 90 String [] states = val.split(","); 91 addedDefaultStates = 0; 92 for (int j = 0; j < states.length; j++) { 93 addedDefaultStates = addedDefaultStates | UserManager.resolveState(states[j]); 94 } 95 checkAddedDefaultStates = true; 96 } else if (currKey.equalsIgnoreCase ("removedefaultstates")) { 97 String val = p.getProperty(currKey); 98 String [] states = val.split(","); 99 removedDefaultStates = 0; 100 for (int j = 0; j < states.length; j++) { 101 removedDefaultStates = removedDefaultStates | UserManager.resolveState(states[j]); 102 } 103 checkRemovedDefaultStates = true; 104 } else if (currKey.equalsIgnoreCase ("displaydefaultviprighttitle") 105 && p.getProperty(currKey).equalsIgnoreCase ("true")) { 106 displayDefaultVipRighttitle = true; 107 checkDisplayDefaultVipRighttitle = true; 108 } else if (currKey.equalsIgnoreCase ("displaydefaultmodguesttitle") 109 && p.getProperty(currKey).equalsIgnoreCase ("false")) { 110 displayDefaultModGuestTitle = false; 111 checkDisplayDefaultModGuestTitle = true; 112 } else if (currKey.equalsIgnoreCase ("listedasonlinevip") 113 && p.getProperty(currKey).equalsIgnoreCase ("false")) { 114 listedAsOnlinevip = false; 115 checkListedAsOnlinevip = true; 116 } 117 } 118 119 if (!checkNamePrefix) 120 namePrefix = null; 121 if (!checkNameSuffix) 122 nameSuffix = null; 123 if (!checkStringRepresentation) 124 stringRepresentation = null; 125 if (!checkAddedStates) 126 addedStates = 0; 127 if (!checkRemovedStates) 128 removedStates = 0; 129 if (!checkAddedDefaultStates) 130 addedDefaultStates = 0; 131 if (!checkRemovedDefaultStates) 132 removedDefaultStates = 0; 133 if (!checkDisplayDefaultVipRighttitle) 134 displayDefaultVipRighttitle = false; 135 if (!checkDisplayDefaultModGuestTitle) 136 displayDefaultModGuestTitle = true; 137 if (!checkListedAsOnlinevip) 138 displayDefaultModGuestTitle = true; 139 140 } 141 142 public synchronized void update (Properties p) { 143 init(p); 144 for (Iterator i = members.iterator(); i.hasNext(); ) { 145 User u = (User) i.next(); 146 u.rebuildMemberships(); 147 } 148 } 149 150 public void add (User u) { 151 if (!members.contains(u)) 152 members.add(u); 153 u.addMembership (this.key, this); 154 155 int pMap = u.getPermissionMap(); 157 pMap = pMap | addedStates; 158 pMap = pMap - (pMap & removedStates); 159 Server.log (this, "setting user-state-map for user " + u.toString() + " to value " + pMap, Server.MSG_AUTH, Server.LVL_VERY_VERBOSE); 160 u.setPermission(pMap); 161 162 int dpMap = u.getDefaultMembershipPermissionMap(); 163 dpMap = dpMap | addedDefaultStates; 164 dpMap = dpMap - (dpMap & removedDefaultStates); 165 Server.log (this, "setting user-defaultmembership-state-map for user " + u.toString() + " to value " + dpMap, Server.MSG_AUTH, Server.LVL_VERY_VERBOSE); 166 u.setDefaultMembershipPermission(dpMap); 167 168 if (dpMap >0 && dpMap != pMap) { 169 Server.log (this, "setting user-defaultstate-map for user " + u.toString() + " to value " + dpMap, Server.MSG_AUTH, Server.LVL_VERY_VERBOSE); 170 u.setDefaultPermissionMap(dpMap); 171 } 172 } 173 174 public void remove (User u) { 175 members.remove(u); 176 } 177 178 public void cleanup () { 179 for (Iterator i = members.iterator(); i.hasNext(); ) { 180 User u = (User) i.next(); 181 u.removeMembership(this.key); 182 } 183 } 184 185 186 188 192 public void sendMessage(IContainer mc) { 193 User[] uArr = (User[]) members.toArray(new User[0]); 194 for (int i = 0; i < uArr.length; i++) { 195 uArr[i].sendMessage(mc); 196 } 197 } 198 199 203 public Iterator users() { 204 return members.iterator(); 205 } 206 207 211 public String getName() { 212 if (stringRepresentation != null) 213 return stringRepresentation; 214 return key; 215 } 216 217 public String getNamePrefix() { 218 return namePrefix; 219 } 220 221 public String getNameSuffix() { 222 return nameSuffix; 223 } 224 225 public boolean displayDefaultVipRighttitle() { 226 return displayDefaultVipRighttitle; 227 } 228 229 public boolean displayDefaultModGuesttitle() { 230 return displayDefaultModGuestTitle; 231 } 232 233 public boolean listedAsOnlinevip() { 234 return listedAsOnlinevip; 235 } 236 237 public int hashCode() { 238 return key.hashCode(); 239 } 240 241 public boolean equals (Object o) { 242 if (this == o) 243 return true; 244 if (!(o instanceof Membership)) 245 return false; 246 Membership foreign = (Membership) o; 247 return foreign.key.equals(this.key); 248 } 249 250 public String toString() { 251 return this.stringRepresentation; 252 } 253 } | Popular Tags |