1 20 21 package freecs.core; 22 23 import java.nio.ByteBuffer ; 24 import java.nio.CharBuffer ; 25 import java.nio.charset.CharacterCodingException ; 26 import java.util.StringTokenizer ; 27 28 import freecs.Server; 29 import freecs.content.MessageState; 30 import freecs.interfaces.IGroupState; 31 import freecs.interfaces.IUserStates; 32 import freecs.layout.TemplateManager; 33 import freecs.layout.TemplateSet; 34 import freecs.util.EntityDecoder; 35 import freecs.util.HashUtils; 36 37 42 public class MessageRenderer { 43 48 public static String renderTemplate (MessageState msgState, String msgTpl) { 49 return renderTemplate (msgState, msgTpl, msgTpl.equals ("list.users") ? false : true); 50 } 51 public static String renderTemplate (MessageState msgState, String msgTpl, boolean addBreak) { 52 if (msgTpl == null) 53 return null; 54 StringBuffer sb = new StringBuffer (); 55 for (StringTokenizer st = new StringTokenizer (msgTpl, "§"); st.hasMoreTokens (); ) { 56 String token = st.nextToken (); 57 sb.append (evalVariable (msgState, token)); 59 } 60 if (addBreak) sb.append ("<br>\r\n"); 61 return (sb.toString ()); 62 } 63 64 70 private static String evalVariable (MessageState msgState, String token) { 71 String val = null; 72 String tok = token.toLowerCase(); 73 boolean encode = false; 74 75 if (tok.startsWith ("sender.") || tok.startsWith ("sender ") || tok.equals("sender") 76 || tok.startsWith ("user.") || tok.startsWith ("user ") || tok.equals ("user") 77 || tok.startsWith ("friend.") || tok.startsWith ("friend ") || tok.equals ("friend")) { User cu = tok.startsWith("sender") ? msgState.sender : msgState.usercontext; 79 int pos = tok.indexOf("."); 80 if (cu != null && pos == -1) { val = renderUserName (msgState, cu); 82 } else if (cu != null) { 83 tok = tok.substring (pos+1); 84 if (tok.startsWith ("name")) { 85 val = cu.getName(); 86 } else if (tok.startsWith("awaymessage")) { 87 if (!cu.isAway()) return ""; 89 val = cu.getAwayMessage(); 90 encode=true; 91 if (val == null) { 92 val = ""; 93 } 94 } else if (tok.startsWith("isaway")) { 95 val = cu.isAway() ? "away" : ""; 96 } else if (tok.startsWith("title")) { 97 val = renderTitle(msgState, cu); 98 } else if (tok.startsWith ("chattime") && !cu.isUnregistered) { 99 val = renderChattime (cu); 100 } else if (tok.startsWith ("questioncounter")) { 101 val = String.valueOf(cu.getQuestionCounter()); 102 } else if (tok.startsWith ("linkedname")) { 103 User u = msgState.usercontext; 104 msgState.usercontext = cu; 105 TemplateSet ts = msgState.sender == null 106 ? TemplateManager.mgr.getTemplateSet() 107 : msgState.sender.getTemplateSet(); 108 val = renderTemplate(msgState, ts.getMessageTemplate("constant.linkedName"), false); 109 msgState.usercontext = u; 110 } else if (tok.startsWith ("color")) { 111 val = cu.getColCode(); 112 encode=true; 113 TemplateSet ts = msgState.sender == null 114 ? TemplateManager.mgr.getTemplateSet() 115 : msgState.sender.getTemplateSet(); 116 if (val == null) 117 val = ts.getMessageTemplate("constant.defaultColor"); 118 } else if (tok.startsWith ("reg.text")) { 119 if (!cu.isUnregistered) { 120 TemplateSet ts = msgState.sender == null 121 ? TemplateManager.mgr.getTemplateSet() 122 : msgState.sender.getTemplateSet(); 123 val = ts.getMessageTemplate("constant.reg.text"); 124 } 125 if (val == null) 126 val = ""; 127 } else if (tok.startsWith ("group.theme")) { 128 val = cu.getGroup().getTheme(); 129 if (val == null) 130 val = ""; 131 } else if (tok.startsWith ("group") 132 || tok.startsWith ("group.name")) { 133 if (cu.getGroup()!=null) 134 val = cu.getGroup().getName(); 135 else 136 val = ""; 137 } else if (tok.startsWith ("peername")) { 138 if (cu.conn.peerAddress != null) { 139 val = cu.conn.peerAddress.getCanonicalHostName(); 140 } 141 if (val == null) 142 val = ""; 143 } else if (tok.startsWith ("peerip")) { 144 val = cu.conn.peerIp; 145 if (val == null) 146 val = ""; 147 } else if (tok.startsWith ("cookie")) { 148 if (cu != null && msgState.sender != null && (cu.getName().equals(msgState.sender.getName()) || msgState.sender.hasRight(IUserStates.ROLE_GOD)) ) { 150 try { 151 val = HashUtils.encodeMD5(cu.getCookie()); 152 } catch (Exception e) { 153 val = e.getMessage(); 154 } 155 } else { 156 val = "hidden"; 157 } 158 if (val == null) 159 val = ""; 160 } else if (tok.startsWith ("clientname")) { 161 if (cu.conn.clientAddress != null) { 162 val = cu.conn.clientAddress.getCanonicalHostName(); 163 } 164 if (val == null) 165 val = ""; 166 } else if (tok.startsWith ("clientip")) { 167 val = cu.conn.clientIp; 168 if (val == null) 169 val = ""; 170 } else if (tok.startsWith ("templateset")) { 171 val = cu.getTemplateSet().getName(); 172 } else if (tok.startsWith ("forwardchain")) { 173 if (cu.conn.fwChain!=null 174 && cu.conn.fwChain.length>0) { 175 StringBuffer sb = new StringBuffer (); 176 for (int j = 0; j<cu.conn.fwChain.length; j++) { 177 sb.append (cu.conn.fwChain[j]); 178 if (j < cu.conn.fwChain.length) 179 sb.append (", "); 180 } 181 val = sb.toString(); 182 } else { 183 val = ""; 184 } 185 } else { 186 int spc = tok.indexOf (" "); 187 Object o = null; 188 if (spc > -1) { 189 o = cu.getProperty (tok.substring(0,spc)); 190 } else { 191 o = cu.getProperty (tok); 192 } 193 if (o == null) { 194 val = ""; 195 } else { 196 val = o.toString(); 197 encode=true; 198 } 199 } 200 } else { 201 val = ""; 202 } 203 } else if (tok.startsWith("group") 204 || tok.startsWith ("sourcegroup") 205 || tok.startsWith ("targetgroup")) { Group cg = tok.startsWith ("sourcegroup") ? msgState.sourceGroup : msgState.targetGroup; 207 int pos = tok.indexOf("."); 208 if (pos == -1) { 209 if (cg!=null) { 210 val = cg.getName(); 211 } else { 212 val = null; 213 } 214 } else if (cg != null) { 215 tok = tok.substring(pos+1); 216 if (tok.startsWith("theme")) { 217 val = cg.getTheme(); 218 if (val == null) 219 val = ""; 220 } else if (tok.startsWith ("lockstate")) { 221 TemplateSet ts = msgState.sender == null 222 ? TemplateManager.mgr.getTemplateSet() 223 : msgState.sender.getTemplateSet(); 224 if (cg.hasState(IGroupState.OPEN)) { 225 val = ts.getMessageTemplate("constant.openGroup"); 226 } else { 227 val = ts.getMessageTemplate("constant.lockedGroup"); 228 } 229 if (val == null) 230 val = ""; 231 } else if (tok.startsWith ("islocked")) { 232 if (!cg.hasState(IGroupState.OPEN)) { 233 TemplateSet ts = msgState.sender == null 234 ? TemplateManager.mgr.getTemplateSet() 235 : msgState.sender.getTemplateSet(); 236 val = ts.getMessageTemplate("constant.lockedGroup"); 237 } else 238 val = ""; 239 } else if (tok.startsWith ("isopen")) { 240 if (cg.hasState(IGroupState.OPEN)) { 241 TemplateSet ts = msgState.sender == null 242 ? TemplateManager.mgr.getTemplateSet() 243 : msgState.sender.getTemplateSet(); 244 val = ts.getMessageTemplate("constant.openGroup"); 245 } else 246 val = ""; 247 } else if (tok.startsWith ("questioncounter")) { 248 val = String.valueOf(cg.getQuestionCounter()); 249 } 250 } else { 251 val = ""; 252 } 253 } else if (tok.startsWith("message") 254 || tok.startsWith ("userlist") 255 || tok.startsWith ("result")) { 256 if (msgState.message==null) { 257 val = ""; 258 } else { val = msgState.message; 260 encode=tok.startsWith("message"); 261 } 262 } else if (tok.startsWith ("reason") 263 || tok.startsWith ("throw")) { 264 val = msgState.reason == null ? "" : msgState.reason; 265 encode=true; 266 } else if (tok.startsWith ("reciever") 267 || tok.startsWith ("target") 268 || tok.startsWith ("friendscount") 269 || tok.startsWith ("source") 270 || tok.startsWith ("total") 271 || tok.startsWith ("param")) { 272 val = msgState.param; 273 encode=true; 274 } else if (tok.startsWith ("firstusers") 275 || tok.startsWith ("firstmemberships")) { 276 if (msgState.usrList == null 277 || msgState.usrList.length < 2) 278 val = ""; 279 else { 280 TemplateSet ts = msgState.sender == null 281 ? TemplateManager.mgr.getTemplateSet() 282 : msgState.sender.getTemplateSet(); 283 boolean useTpl = (ts.getMessageTemplate("constant.userListItem")!=null); 284 msgState.useRenderCache=false; 285 User u = msgState.usercontext; 286 StringBuffer sb = new StringBuffer (); 287 for (int i = 0; i < msgState.usrList.length-1; i++) { 288 Object curr = msgState.usrList[i]; 289 if (curr instanceof User) { 290 if (!useTpl) { 291 sb.append (((User) msgState.usrList[i]).getName()); 292 encode=true; 293 } else { 294 msgState.usercontext=(User) msgState.usrList[i]; 295 sb.append (renderTemplate(msgState,ts.getMessageTemplate("constant.userListItem"), false)); 296 } 297 } else 298 sb.append (curr.toString()); 299 if (i < msgState.usrList.length-2) 300 sb.append (", "); 301 } 302 msgState.usercontext=u; 303 val = sb.toString(); 304 } 305 } else if (tok.startsWith ("lastuser") 306 || tok.startsWith ("lastmembership")) { 307 Object item = msgState.usrList[msgState.usrList.length-1]; 308 TemplateSet ts = msgState.sender == null 309 ? TemplateManager.mgr.getTemplateSet() 310 : msgState.sender.getTemplateSet(); 311 if (item instanceof User) { 312 if (ts.getMessageTemplate("constant.userListItem")==null) { 313 val = ((User) item).getName(); 314 encode=true; 315 } else { 316 User u = msgState.usercontext; 317 msgState.usercontext = (User) item; 318 val = renderTemplate(msgState, ts.getMessageTemplate("constant.userListItem"), false); 319 msgState.usercontext = u; 320 } 321 } else 322 val = item.toString(); 323 } else if (tok.startsWith ("membership")) { 324 if (msgState.usrList != null && msgState.usrList.length > 0) { 325 val = msgState.usrList[0].toString(); 326 encode=true; 327 } else 328 val = ""; 329 } else if (tok.startsWith ("time")) { 330 String fstrg = "[HH:mm]"; 331 int pos = tok.indexOf("format="); 332 if (pos != -1) { 333 fstrg = token.substring (pos+6); 334 fstrg = retrieveParamValue (fstrg)[1]; 335 } 336 val = Server.srv.getFormatedTime (fstrg); 337 } else if (tok.startsWith("vipcount")) { 338 val = String.valueOf(UserManager.mgr.onlineVipList().size()); 339 } else if (tok.startsWith("onlineusers")) { 340 val = String.valueOf(UserManager.mgr.getActiveUserCount()); 341 } else if (tok.startsWith("opengroups")) { 342 val = String.valueOf(GroupManager.mgr.openGroupsCount()); 343 } else if (tok.startsWith ("constant")) { 344 int i = tok.indexOf(" "); 345 TemplateSet ts = msgState.sender == null 346 ? TemplateManager.mgr.getTemplateSet() 347 : msgState.sender.getTemplateSet(); 348 String msgTpl; 349 if (i == -1) { 350 msgTpl = token; 351 } else { 352 msgTpl = token.substring (0,i); 353 } 354 msgTpl = ts.getMessageTemplate(msgTpl); 355 if (msgTpl == null) { 356 val = "[Constant not found: " + msgTpl + "]"; 357 encode=true; 358 } else { 359 val = renderTemplate(msgState, msgTpl, false); 360 } 361 } 362 if (val == null) 363 return (token); 364 int pos = Math.max(token.indexOf (" "), token.indexOf(".")); 365 if (pos == -1) 366 return encode ? EntityDecoder.charToHtml(val) : val; 367 if (tok.indexOf("touppercase") > -1) { 368 val = val.toUpperCase(); 369 } else if (tok.indexOf("tolowercase") > -1) { 370 val = val.toLowerCase(); 371 } 372 if (encode) { 373 val = EntityDecoder.charToHtml(val); 374 } 375 return renderValue (msgState, val, token.substring (pos).trim ()); 376 } 377 378 private static String renderChattime (User cu) { 379 long s = cu.getChattime(); 380 long m = s / 60; 381 long h = m / 60; 382 s = s - m * 60; 383 m = m - h * 60; 384 StringBuffer sb = new StringBuffer (); 385 if (h > 0) 386 sb.append (h).append("h "); 387 if (m > 0) 388 sb.append (m).append("m "); 389 sb.append (s).append ("s"); 390 return sb.toString(); 391 } 392 393 399 private static String renderTitle (MessageState msgState, User cu) { 400 StringBuffer sb = new StringBuffer (); 401 String val=""; 402 Group sg = cu.getGroup (); 403 TemplateSet ts = msgState.sender == null 404 ? TemplateManager.mgr.getTemplateSet() 405 : msgState.sender.getTemplateSet(); 406 407 boolean append = false, displayDefaultVipRighttitle = true, displayDefaultModGuestTitle = true; 408 String seperator = ts.getMessageTemplate("constant.title.seperator"); 409 if (cu.getCustomTitle() != null && cu.getCustomTitle().length() > 1){ 410 append = true; 411 val = ts.getMessageTemplate("constant.customtitle.prefix"); 412 if (val != null) 413 sb.append (val); 414 sb.append (cu.getCustomTitle()); 415 val = ts.getMessageTemplate("constant.customtitle.suffix"); 416 if (val != null) 417 sb.append (val); 418 } else { 419 Membership defaultMembership = cu.getDefaultMembership(); 420 if (defaultMembership != null) { 421 displayDefaultVipRighttitle = defaultMembership.displayDefaultVipRighttitle(); 422 displayDefaultModGuestTitle = defaultMembership.displayDefaultModGuesttitle(); 423 424 } 425 if (defaultMembership != null && defaultMembership != MembershipManager.undefined) { 426 val = ts.getMessageTemplate("constant.title.membership."+ defaultMembership.key); 427 if (val == null) { 428 val = defaultMembership.getName(); 429 if (val.length() <= 0) 430 val = null; 431 } 432 if (val != null) { 433 if (append && seperator != null) 434 sb.append (seperator); 435 sb.append (val); 436 append = true; 437 } 438 } 439 } 440 if (cu.hasRight(IUserStates.IS_MODERATOR) && sg.hasState(IGroupState.MODERATED)) { 441 val = ts.getMessageTemplate("constant.title.moderator"); 442 if (val != null && displayDefaultModGuestTitle) { 443 if (append && seperator != null) 444 sb.append (seperator); 445 sb.append (val); 446 append = true; 447 } 448 } 449 if (cu.hasRight(IUserStates.IS_GUEST) && sg.hasState(IGroupState.MODERATED)) { 450 val = ts.getMessageTemplate("constant.title.guest"); 451 if (val != null && displayDefaultModGuestTitle) { 452 if (append && seperator != null) 453 sb.append (seperator); 454 sb.append (val); 455 append = true; 456 } 457 } 458 if (cu.hasRole(IUserStates.ROLE_VIP)) { 459 val = ts.getMessageTemplate("constant.title.vip"); 460 if (val != null && displayDefaultVipRighttitle) { 461 if (append && seperator != null) 462 sb.append (seperator); 463 sb.append (val); 464 append = true; 465 } 466 } 467 if (cu.hasRole(IUserStates.ROLE_GOD)) { 468 val = ts.getMessageTemplate("constant.title.god"); 469 if (val != null && displayDefaultVipRighttitle) { 470 if (append && seperator != null) 471 sb.append (seperator); 472 sb.append (val); 473 append = true; 474 } 475 } 476 if (cu.getGroup() != null && cu.getGroup().usrIsSu(cu) && !cu.hasRight(IUserStates.ROLE_VIP)) { 477 val = ts.getMessageTemplate("constant.title.su"); 478 if (val != null) { 479 if (append && seperator != null) 480 sb.append (seperator); 481 sb.append (val); 482 append = true; 483 } 484 } 485 486 return sb.toString(); 487 } 488 489 494 private static String renderUserName (MessageState msgState, User cu) { 495 if (cu==null) 496 return null; 497 StringBuffer tsb = new StringBuffer (); 498 Group sg = cu.getGroup (); 499 boolean isSu = false, isPunished = cu.isPunished (), setPrefsuffix = false; 500 if (sg != null) 501 isSu = sg.usrIsSu (cu); 502 String pre = "", 503 suf = ""; 504 TemplateSet ts = msgState.sender == null 505 ? TemplateManager.mgr.getTemplateSet() 506 : msgState.sender.getTemplateSet(); 507 Membership defaultMembership = cu.getDefaultMembership(); 508 if (cu.getDefaultMembership() != null) { 509 pre = ts.getMessageTemplate("constant.membership."+ defaultMembership.key + ".prefix"); 510 suf = ts.getMessageTemplate("constant.membership."+ defaultMembership.key + ".suffix"); 511 if (pre == null && suf == null) { 512 pre = defaultMembership.getNamePrefix(); 513 suf = defaultMembership.getNameSuffix(); 514 } 515 if (pre == null && suf == null) { 516 pre = ""; 517 suf = ""; 518 } else setPrefsuffix = true; 519 } 520 if (cu.hasRight(IUserStates.IS_GUEST) && !cu.hasRole(IUserStates.ROLE_GOD)) { 521 if (sg != null && sg.hasState(IGroupState.MODERATED)) { 522 pre = ts.getMessageTemplate("constant.username.guest.prefix"); 523 suf = ts.getMessageTemplate("constant.username.guest.suffix"); 524 if (pre == null && suf == null) { 525 pre = Server.srv.UNAME_PREFIX_GUEST; 526 suf = Server.srv.UNAME_SUFFIX_GUEST; 527 } 528 } 529 } 530 if (cu.hasRight(IUserStates.IS_GUEST) && cu.hasRole(IUserStates.ROLE_VIP) && !setPrefsuffix) { 531 pre = ts.getMessageTemplate("constant.username.vip.prefix"); 532 suf = ts.getMessageTemplate("constant.username.vip.suffix"); 533 if (pre == null && suf == null) { 534 pre = Server.srv.UNAME_PREFIX_VIP; 535 suf = Server.srv.UNAME_SUFFIX_VIP; 536 } 537 if (sg != null && sg.hasState(IGroupState.MODERATED)) { 538 if ((ts.getMessageTemplate("constant.username.guest.prefix") != null)&& (ts.getMessageTemplate("constant.username.vip.prefix")!= null)) pre = ts.getMessageTemplate("constant.username.vip.prefix")+ts.getMessageTemplate("constant.username.guest.prefix"); else pre = null; 539 if ((ts.getMessageTemplate("constant.username.guest.suffix") != null)&& (ts.getMessageTemplate("constant.username.vip.suffix")!= null)) suf = ts.getMessageTemplate("constant.username.guest.suffix")+ts.getMessageTemplate("constant.username.vip.suffix"); else suf = null; 540 if (pre == null && suf == null) { 541 pre = Server.srv.UNAME_PREFIX_VIP+Server.srv.UNAME_PREFIX_GUEST; 542 suf = Server.srv.UNAME_SUFFIX_GUEST+Server.srv.UNAME_SUFFIX_VIP; 543 } 544 } 545 } else if (cu.hasRight(IUserStates.IS_MODERATOR) && !cu.hasRight(IUserStates.IS_GUEST) && !cu.hasRole(IUserStates.ROLE_GOD) && !setPrefsuffix) { 546 if (sg != null && sg.hasState(IGroupState.MODERATED)) { 547 pre = ts.getMessageTemplate("constant.username.moderator.prefix"); 548 suf = ts.getMessageTemplate("constant.username.moderator.suffix"); 549 if (pre == null && suf == null) { 550 pre = Server.srv.UNAME_PREFIX_MODERATOR; 551 suf = Server.srv.UNAME_SUFFIX_MODERATOR; 552 } 553 } 554 if (cu.hasRight(IUserStates.IS_MODERATOR)&& cu.hasRole(IUserStates.ROLE_VIP) && !setPrefsuffix) { 555 pre = ts.getMessageTemplate("constant.username.vip.prefix"); 556 suf = ts.getMessageTemplate("constant.username.vip.suffix"); 557 if (pre == null && suf == null) { 558 pre = Server.srv.UNAME_PREFIX_VIP; 559 suf = Server.srv.UNAME_SUFFIX_VIP; 560 } 561 if (sg != null && sg.hasState(IGroupState.MODERATED)) { 562 if ((ts.getMessageTemplate("constant.username.moderator.prefix") != null) && (ts.getMessageTemplate("constant.username.vip.prefix") != null)) pre = ts.getMessageTemplate("constant.username.vip.prefix") + ts.getMessageTemplate("constant.username.moderator.prefix"); else pre = null; 563 if ((ts.getMessageTemplate("constant.username.moderator.suffix") != null) && (ts.getMessageTemplate("constant.username.vip.suffix") != null)) suf = ts.getMessageTemplate("constant.username.moderator.suffix")+ ts.getMessageTemplate("constant.username.vip.suffix"); else suf = null; 564 if (pre == null && suf == null) { 565 pre = Server.srv.UNAME_PREFIX_VIP+Server.srv.UNAME_PREFIX_MODERATOR; 566 suf = Server.srv.UNAME_SUFFIX_MODERATOR+Server.srv.UNAME_SUFFIX_VIP; 567 } 568 } 569 } 570 } else if (cu.hasRole(IUserStates.ROLE_GOD) && !setPrefsuffix) { 571 pre = ts.getMessageTemplate("constant.username.god.prefix"); 572 suf = ts.getMessageTemplate("constant.username.god.suffix"); 573 if (pre == null && suf == null) { 574 pre = Server.srv.UNAME_PREFIX_GOD; 575 suf = Server.srv.UNAME_SUFFIX_GOD; 576 } 577 } else if (cu.hasRole(IUserStates.ROLE_VIP) && !setPrefsuffix) { 578 pre = ts.getMessageTemplate("constant.username.vip.prefix"); 579 suf = ts.getMessageTemplate("constant.username.vip.suffix"); 580 if (pre == null && suf == null) { 581 pre = Server.srv.UNAME_PREFIX_VIP; 582 suf = Server.srv.UNAME_SUFFIX_VIP; 583 } 584 } else if (isSu && !setPrefsuffix) { 585 pre = ts.getMessageTemplate("constant.username.su.prefix"); 586 suf = ts.getMessageTemplate("constant.username.su.suffix"); 587 if (pre == null && suf == null) { 588 pre = Server.srv.UNAME_PREFIX_SU; 589 suf = Server.srv.UNAME_SUFFIX_SU; 590 } 591 } 592 if (isPunished) { 593 pre = ts.getMessageTemplate("constant.username.punished.prefix"); 594 suf = ts.getMessageTemplate("constant.username.punished.suffix"); 595 if (pre == null && suf == null) { 596 pre = Server.srv.UNAME_PREFIX_PUNISHED; 597 suf = Server.srv.UNAME_SUFFIX_PUNISHED; 598 } 599 } 600 tsb.append (pre); 601 tsb.append (EntityDecoder.charToHtml (cu.getName ())); 602 tsb.append (suf); 603 return tsb.toString (); 604 } 605 606 612 private static String renderValue (MessageState msgState, String val, String params) { 613 if (params == null || params.length() < 1) 614 return val; 615 StringBuffer sb = new StringBuffer (); 616 String prefix = ""; 617 String suffix = ""; 618 String plower = params.toLowerCase(); 619 while (params.length () > 0) { 620 if (params.startsWith ("toUpperCase") 621 || params.startsWith ("toLowerCase") 622 || params.startsWith ("touppercase") 623 || params.startsWith ("tolowercase")) { 624 params = params.substring (11); 625 continue; 626 } 627 if (params.startsWith ("prefix")) { 628 int start = params.indexOf ("="); 629 if (start == -1) return ("syntax"); 630 params = params.substring (start); 631 String vals[] = retrieveParamValue (params); 632 params = vals[0]; 633 prefix = vals[1]; 634 } else if (params.startsWith ("suffix")) { 635 int start = params.indexOf ("="); 636 if (start == -1) return ("syntax"); 637 params = params.substring (start); 638 String vals[] = retrieveParamValue (params); 639 params = vals[0]; 640 suffix = vals[1]; 641 } else if (params.toLowerCase().startsWith ("ifempty") 642 && val.length() < 1) { 643 int start = params.indexOf ("="); 644 if (start == -1) return ("syntax"); 645 params = params.substring (start); 646 String vals[] = retrieveParamValue (params); 647 return vals[1]; 648 } else if (params.startsWith ("value") && !val.equals ("")) { 649 int start = params.indexOf ("="); 650 if (start == -1) return ("syntax"); 651 params = params.substring (start); 652 String vals[] = retrieveParamValue (params); 653 params = vals[0]; 654 val = vals[1]; 655 } else break; 656 } 657 if (val.equals ("")) return (""); 658 if (prefix.indexOf("@") > -1) { 659 prefix = renderTemplate (msgState, prefix.replace('@', '§'), false); 660 } 661 if (suffix.indexOf("@") > -1) { 662 suffix = renderTemplate (msgState, suffix.replace('@', '§'), false); 663 } 664 sb.append (prefix); 665 sb.append (val); 666 sb.append (suffix); 667 return sb.toString (); 668 } 669 670 public static String [] retrieveParamValue (String params) { 671 params = params.trim (); 672 StringBuffer tsb = new StringBuffer (); 673 boolean quote=false, mask=false, firstChar = false; 674 int stop = 0; 675 for (int i = 0; i < params.length (); i++) { 676 char c = params.charAt (i); 677 if (mask) { 678 mask = false; 679 tsb.append (c); 680 } else if (firstChar && !quote && c == ' ') { 681 stop = i; 682 break; 683 } else if (c == '\\') { 684 mask = true; 685 } else if (!mask && !quote && c == '"') { 686 quote = true; 687 } else if (!mask && c == '"') { 688 stop = i+1; 689 break; 690 } else if (!firstChar && c != ' ') { 691 firstChar = true; 692 } else { 693 tsb.append (c); 694 } 695 } 696 String result[] = new String [2]; 697 if (params.length () > stop) 698 result[0] = params.substring (stop).trim (); 699 else 700 result[0] = ""; 701 702 result[1] = tsb.toString (); 703 return result; 704 } 705 706 public static ByteBuffer encode (String s) { 707 try { 708 return Server.srv.defaultCsEnc.encode (CharBuffer.wrap(s)); 709 } catch (CharacterCodingException cce) { 710 return Server.srv.defaultCs.encode(CharBuffer.wrap(s)); 711 } 712 } 713 } 714 715 716 717 756 | Popular Tags |