1 18 package freecs.core; 19 20 import freecs.Server; 21 import freecs.interfaces.*; 22 import freecs.util.EntityDecoder; 23 24 import java.util.List ; 25 import java.util.Vector ; 26 import java.util.Iterator ; 27 28 public class Group implements IGroupState, IMessageDestination { 29 private IGroupPlugin[] plugins; 30 private String suForbiddenMembership; 31 private String name, key; 32 private String theme; 33 private String saveName, saveTheme; 34 private int state; 35 private int minRight=IUserStates.MAY_JOIN_GROUP; 36 private int questionCntr = 0, joinpunishedCntr = 0; 37 private int timelockSec = 60; 38 private int minSetSuRole = IUserStates.ROLE_USER; 39 private User[] usrArr; 40 private Vector usr; 41 private Vector susers; 42 private Vector banList; 43 private Vector autoSuList; 44 private Membership[] memberRoom; 45 private volatile boolean valid; 46 47 public Group (String name, String theme) { 48 this.name = name; 49 this.saveName = EntityDecoder.charToHtml(name); 50 this.key = name.toLowerCase ().trim(); 51 this.theme = theme; 52 this.saveTheme = EntityDecoder.charToHtml(theme); 53 this.state = OPEN | AUTO_SU_FIRST | ALLOW_SU; 54 this.usr = new Vector (); 55 this.susers = new Vector (); 56 this.banList = new Vector (); 57 this.valid = true; 58 if (Server.TRACE_CREATE_AND_FINALIZE) 59 Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE); 60 } 61 62 public Group (String name, String theme, int state) { 63 this.name = name; 64 this.saveName = EntityDecoder.charToHtml(name); 65 this.key = name.toLowerCase ().trim(); 66 this.theme = theme; 67 this.saveTheme = EntityDecoder.charToHtml(theme); 68 this.state = state; 69 this.usr = new Vector (); 70 this.susers = new Vector (); 71 this.banList = new Vector (); 72 this.valid = true; 73 if (Server.TRACE_CREATE_AND_FINALIZE) 74 Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE); 75 } 76 77 81 public String getName () { 82 return saveName; 83 } 84 85 public String getRawName() { 86 return name; 87 } 88 89 92 public String getKey() { 93 return key; 94 } 95 96 97 public void setPlugins (IGroupPlugin[] plugins) { 98 this.plugins = plugins; 99 } 100 public IGroupPlugin[] getPlugins () { 101 return plugins; 102 } 103 107 public void setTheme (String t) { 108 theme = t; 109 saveTheme = EntityDecoder.charToHtml(t); 110 } 111 115 public String getTheme () { 116 return saveTheme; 117 } 118 119 public void setMemberRoom (Membership[] memberRoom) { 120 this.memberRoom = memberRoom; 121 } 122 123 public Membership[] getMemberships () { 124 return memberRoom; 125 } 126 130 public boolean addUser (User u) { 131 GroupManager.mgr.updateGroupListLastModified(); 132 return (addUser (u, u, false)); 133 } 134 135 public void addLoginUser (User u) { 136 synchronized (this) { 137 usr.addElement(u); 138 usrArr=(User[]) usr.toArray(new User[0]); 139 } 140 141 u.setGroup(this); 142 if (autoSuList != null 143 && autoSuList.contains(u.getName().toLowerCase())) { 144 addToSusers(u); 145 } 146 if (plugins!=null) { 147 for (int i = 0; i<plugins.length; i++) { 148 try { 149 plugins[i].usrJoin(u); 150 } catch (Exception e) { 151 Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR); 152 } 153 } 154 } 155 } 156 162 public boolean addUser (User u, User ru) { 163 return this.addUser (u, ru, false); 164 } 165 public boolean addUser (User u, User ru, boolean invited) { 166 if (u == null || usr.contains (u)) 167 return true; 168 if (!usrMayJoin(ru) 169 && (u.equals(ru) || !invited)) 170 return false; 171 boolean isFirst; 172 synchronized (this) { 173 isFirst = usr.isEmpty(); 174 usr.addElement (u); 175 usrArr=(User[]) usr.toArray(new User[0]); 176 } 177 if ((isFirst 178 && this.hasState(IGroupState.AUTO_SU_FIRST | IGroupState.ALLOW_SU) 179 && !this.hasState(IGroupState.ENTRANCE) && !this.hasState(NO_SU_FIRST)) 180 || (autoSuList != null 181 && autoSuList.contains(u.getName().toLowerCase()) && this.hasState(AUTO_SU_FIRST) )) { 182 addToSusers (u); 183 } 184 u.setGroup(this); 185 if (plugins!=null) { 186 for (int i = 0; i<plugins.length; i++) { 187 try { 188 plugins[i].usrJoin(u); 189 } catch (Exception e) { 190 Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR); 191 } 192 } 193 } 194 return true; 195 } 196 197 201 public void removeUser (User u) { 202 synchronized (this) { 203 if (usr==null) { 204 return; 205 } 206 usr.remove (u); 207 if (usr.size() <= 0) { 208 GroupManager.mgr.removeGroup (this); 209 return; 210 } 211 usrArr=(User[]) usr.toArray(new User[0]); 212 } 213 while (susers.contains (u)) { 214 susers.remove (u); 215 } 216 if (susers.size()<1 217 && !this.hasState(OPEN)) { 218 this.setState(OPEN); 219 MessageParser mp = new MessageParser (); 220 mp.setSender(u); 221 mp.setMessageTemplate("message.ul"); 222 this.sendModeratedMessage(mp); 223 } 224 if (plugins!=null) { 225 for (int i = 0; i<plugins.length; i++) { 226 try { 227 plugins[i].usrLeaving(u); 228 } catch (Exception e) { 229 Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR); 230 } 231 } 232 } 233 } 234 235 239 public boolean addToSusers (User u) { 240 if (!this.hasState(AUTO_SU_FIRST)) 241 return false; 242 if (susers.contains (u)) 243 return true; 244 susers.addElement (u); 245 return true; 246 } 247 248 252 public void removeFromSusers (User u) { 253 while (susers.contains(u)) 254 susers.removeElement (u); 255 } 256 257 262 public void exclusiveSendMessage (IContainer mc, List exclude) { 263 if (usr == null || usrArr==null || usr.size () < 1) 264 return; 265 User[] uarr = usrArr; 266 for (int i = 0; i < uarr.length; i++) { 267 User cu = (User) uarr[i]; 268 if (exclude.contains(cu)) 269 continue; 270 cu.sendMessage (mc); 271 } 272 if (mc instanceof MessageParser && plugins!=null) { 273 MessageParser mp = (MessageParser) mc; 274 for (int i = 0; i<plugins.length; i++) { 275 try { 276 plugins[i].usrAction(mp); 277 } catch (Exception e) { 278 Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR); 279 } 280 } 281 } 282 } 283 284 288 289 297 public void sendMessage (IContainer mc) { 298 if (usr==null || usrArr==null || usr.size () < 1) 299 return; 300 boolean messageToModerate = false; 301 if (mc instanceof MessageParser) { 302 User sender = ((MessageParser) mc).getSender(); 303 messageToModerate = !(sender != null && (sender.hasRight(IUserStates.IS_MODERATOR) || sender.hasRight(IUserStates.IS_GUEST))); 304 } 305 if (messageToModerate && 306 this.hasState(IGroupState.MODERATED)) { 307 User[] uarr = usrArr; 308 for (int i = 0; i<uarr.length; i++) { 309 User cu = uarr[i]; 310 if (!cu.hasRight(IUserStates.IS_MODERATOR)) 311 continue; 312 cu.sendMessage (mc); 313 } 314 return; 315 } 316 sendMsg(mc); 317 } 318 319 private void sendMsg (IContainer mc) { 320 if (usrArr==null) 321 return; 322 User[] uarr = usrArr; 323 for (int i = 0; i<uarr.length; i++) { 324 User cu = uarr[i]; 325 cu.sendMessage (mc); 326 } 327 if (mc instanceof MessageParser && plugins!=null) { 328 MessageParser mp = (MessageParser) mc; 329 for (int i = 0; i<plugins.length; i++) { 330 try { 331 plugins[i].usrAction(mp); 332 } catch (Exception e) { 333 Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR); 334 } 335 } 336 } 337 } 338 339 public void sendModeratorMessage (IContainer mc) { 340 if (usr==null || usrArr==null || usr.size () < 1) 341 return; 342 User sender = ((MessageParser) mc).getSender(); 343 User[] uarr = usrArr; 344 for (int i=0; i<uarr.length; i++) { 345 User cu = uarr[i]; 346 if (sender.equals(cu)) 347 continue; 348 cu.sendMessage (mc); 349 } 350 if (mc instanceof MessageParser && plugins!=null) { 351 MessageParser mp = (MessageParser) mc; 352 for (int i = 0; i<plugins.length; i++) { 353 try { 354 plugins[i].usrAction(mp); 355 } catch (Exception e) { 356 Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR); 357 } 358 } 359 } 360 } 361 362 366 public void sendModeratedMessage (IContainer mc) { 367 if (usr==null || usr.size () < 1) 368 return; 369 sendMsg(mc); 370 } 371 372 376 public Iterator users () { 377 return usr.iterator (); 378 } 379 380 public User[] getUserArray() { 381 return usrArr; 382 } 383 384 389 public boolean usrMayJoin (User u) { 390 if (u==null) 391 return false; 392 if (!hasState(OPEN) 393 && !u.hasRight (IUserStates.MAY_JOIN_LOCKED_GROUP) 394 && !susers.contains (u)) 395 return false; 396 String uname = u.getName().toLowerCase(); 397 if (banList != null && banList.contains(uname)) 398 return false; 399 return true; 400 } 401 402 public boolean usrMaySetSu (User u) { 403 if ((!this.equals (u.getGroup ()) 404 && !u.hasRight (IUserStates.MAY_CHANGE_RIGHT)) 405 || !u.hasRight (this.minSetSuRole) 406 || !usrIsMember(u)) 407 return false; 408 return true; 409 } 410 411 public boolean usrIsMember (User u) { 412 if (this.memberRoom == null 413 || this.memberRoom.length == 0) 414 return true; 415 for (int i = 0; i < this.memberRoom.length; i++) 416 if (u.getMembership(this.memberRoom[i].key)!= null) 417 return true; 418 return false; 419 } 420 421 426 public boolean usrMayLock (User u) { 427 if (this.hasState(IGroupState.LOCKPROTECTED)) 428 return false; 429 if (!u.hasRight(IUserStates.MAY_LOCK_GROUP) 430 && !susers.contains(u)) 431 return false; 432 if (this.hasState(IGroupState.ENTRANCE) 433 && !u.hasRight(IUserStates.MAY_LOCK_STARTING_GROUP)) 434 return false; 435 if (hasState(IGroupState.MODERATED) 436 && !u.hasRight(IUserStates.MAY_LOCK_MODERATED_GROUP)) 437 return false; 438 if (!usrIsMember(u)) 439 return false; 440 return true; 441 } 442 443 public boolean usrMayJoinPunished (User u) { 444 if (this.joinpunishedCntr >= Server.srv.JOIN_PUNISHED_COUNTER) 445 return false; 446 return true; 447 } 448 449 public void incrementJoinPunishedCounter() { 450 if (joinpunishedCntr == Integer.MAX_VALUE) 451 joinpunishedCntr=1; 452 else 453 joinpunishedCntr++; 454 } 455 456 public void resetJoinPunishedCounter() { 457 joinpunishedCntr=0; 458 } 459 460 463 public void incrementQuestionCounter() { 464 if (questionCntr == Integer.MAX_VALUE) 465 questionCntr=1; 466 else 467 questionCntr++; 468 } 469 470 474 public int getQuestionCounter() { 475 return questionCntr; 476 } 477 478 public void resetQuestionCounter() { 479 questionCntr=0; 480 } 481 482 487 public void setBanForUser (String u, boolean on) { 488 if (!this.isValid()) 489 return; 490 String uname = u.toLowerCase(); 491 if (on && !banList.contains (uname)) 492 banList.addElement (uname); 493 else if (!on) 494 banList.removeElement (uname); 495 } 496 497 public Vector bannedUsers () { 498 return (Vector ) banList.clone(); 499 } 500 501 506 public boolean usrIsBaned (User u) { 507 return usrIsBaned(u.getName()); 508 } 509 public boolean usrIsBaned (String u) { 510 if (banList==null) 511 return false; 512 String uname = u.toLowerCase(); 513 return banList.contains (uname); 514 } 515 516 521 public boolean usrIsPresent (User u) { 522 if (usr==null) 523 return false; 524 return usr.contains (u); 525 } 526 527 532 public boolean usrIsSu (User u) { 533 if (susers==null) 534 return false; 535 return susers.contains (u); 536 } 537 538 542 public int suUserCount () { 543 if (susers==null) 544 return 0; 545 return susers.size(); 546 } 547 548 552 public int size() { 553 if (usr==null) 554 return 0; 555 return usr.size(); 556 } 557 558 562 public void setMinRight(int r) { 563 this.minRight = r; 564 } 565 566 570 public void setAutoSu (String [] usrs) { 571 if (autoSuList == null) 572 autoSuList = new Vector (); 573 for (int i = 0; i < usrs.length; i++) { 574 String usr = usrs[i].trim().toLowerCase(); 575 if (!autoSuList.contains(usr)) 576 autoSuList.add(usr); 577 } 578 } 579 580 public void setSuForbiddenMembership (String ship) { 581 suForbiddenMembership = ship ; 582 } 583 public String getSuForbiddenMembership (){ 584 return suForbiddenMembership ; 585 } 586 591 public boolean hasState (int state) { 592 return (this.state & state) == state; 593 } 594 595 599 public void setState (int state) { 600 this.state = this.state | (state - (this.state & state)); 601 } 602 603 607 public void unsetState (int state) { 608 this.state = this.state - (this.state & state); 609 } 610 611 public synchronized void setMinRightSu (int minRight) { 612 this.minSetSuRole = minRight; 613 } 614 public boolean isValid () { 615 return valid; 616 } 617 618 public void invalidate() { 619 usrArr=null; 620 usr=null; 621 susers=null; 622 banList=null; 623 autoSuList=null; 624 valid=false; 625 } 626 630 public boolean equals (Object g) { 631 if (this == g) 632 return true; 633 if (g==null) 634 return false; 635 if (!(g instanceof Group)) 636 return false; 637 String n = ((Group) g).getKey (); 638 if (n == null) 639 return false; 640 return (n.equalsIgnoreCase (this.key)); 641 } 642 643 public String toString () { 644 StringBuffer sb = new StringBuffer (); 645 sb.append ("[Group "); 646 sb.append (key); 647 sb.append (" / state: "); 648 sb.append (state); 649 sb.append ("]"); 650 return sb.toString(); 651 } 652 653 public int hasCode() { 654 return this.key.hashCode(); 655 } 656 657 public void finalize() { 658 if (Server.TRACE_CREATE_AND_FINALIZE) 659 Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE); 660 } 661 664 public int getTimelockSec() { 665 return timelockSec; 666 } 667 670 public void setTimelockSec(int timelockSec) { 671 this.timelockSec = timelockSec; 672 } 673 } | Popular Tags |