001: /**
002: * Copyright (C) 2003 Manfred Andres
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Created on 01.10.2003
019: */package freecs.core;
020:
021: import java.nio.ByteBuffer;
022: import java.nio.CharBuffer;
023: import java.nio.charset.CharacterCodingException;
024: import java.util.StringTokenizer;
025:
026: import freecs.Server;
027: import freecs.content.MessageState;
028: import freecs.interfaces.IGroupState;
029: import freecs.interfaces.IUserStates;
030: import freecs.layout.TemplateManager;
031: import freecs.layout.TemplateSet;
032: import freecs.util.EntityDecoder;
033: import freecs.util.HashUtils;
034:
035: /**
036: * @author Manfred Andres
037: *
038: * freecs.core
039: */
040: public class MessageRenderer {
041: /**
042: * renders the given template
043: * @param msgTpl the raw message-template
044: * @return the rendered template as String with all placeholders replaced by their coresponding value
045: */
046: public static String renderTemplate(MessageState msgState,
047: String msgTpl) {
048: return renderTemplate(msgState, msgTpl, msgTpl
049: .equals("list.users") ? false : true);
050: }
051:
052: public static String renderTemplate(MessageState msgState,
053: String msgTpl, boolean addBreak) {
054: if (msgTpl == null)
055: return null;
056: StringBuffer sb = new StringBuffer();
057: for (StringTokenizer st = new StringTokenizer(msgTpl, "§"); st
058: .hasMoreTokens();) {
059: String token = st.nextToken();
060: // String result = evalAlgorithm (token);
061: sb.append(evalVariable(msgState, token));
062: }
063: if (addBreak)
064: sb.append("<br>\r\n");
065: return (sb.toString());
066: }
067:
068: /**
069: * evaluate the given variable
070: * FIXME: this has to go more elegant...
071: * @param token the token to evaluate
072: * @return the evaluated result || the token if nothing was to evaluate
073: */
074: private static String evalVariable(MessageState msgState,
075: String token) {
076: String val = null;
077: String tok = token.toLowerCase();
078: boolean encode = false;
079:
080: if (tok.startsWith("sender.") || tok.startsWith("sender ")
081: || tok.equals("sender") || tok.startsWith("user.")
082: || tok.startsWith("user ") || tok.equals("user")
083: || tok.startsWith("friend.")
084: || tok.startsWith("friend ") || tok.equals("friend")) { // TOKENS WITH USER-CONTEXT
085: User cu = tok.startsWith("sender") ? msgState.sender
086: : msgState.usercontext;
087: int pos = tok.indexOf(".");
088: if (cu != null && pos == -1) { // TOKEN for the username
089: val = renderUserName(msgState, cu);
090: } else if (cu != null) {
091: tok = tok.substring(pos + 1);
092: if (tok.startsWith("name")) {
093: val = cu.getName();
094: } else if (tok.startsWith("awaymessage")) {
095: if (!cu.isAway()) // not away, no awaymessage
096: return "";
097: val = cu.getAwayMessage();
098: encode = true;
099: if (val == null) {
100: val = "";
101: }
102: } else if (tok.startsWith("isaway")) {
103: val = cu.isAway() ? "away" : "";
104: } else if (tok.startsWith("title")) {
105: val = renderTitle(msgState, cu);
106: } else if (tok.startsWith("chattime")
107: && !cu.isUnregistered) {
108: val = renderChattime(cu);
109: } else if (tok.startsWith("questioncounter")) {
110: val = String.valueOf(cu.getQuestionCounter());
111: } else if (tok.startsWith("linkedname")) {
112: User u = msgState.usercontext;
113: msgState.usercontext = cu;
114: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
115: .getTemplateSet()
116: : msgState.sender.getTemplateSet();
117: val = renderTemplate(msgState, ts
118: .getMessageTemplate("constant.linkedName"),
119: false);
120: msgState.usercontext = u;
121: } else if (tok.startsWith("color")) {
122: val = cu.getColCode();
123: encode = true;
124: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
125: .getTemplateSet()
126: : msgState.sender.getTemplateSet();
127: if (val == null)
128: val = ts
129: .getMessageTemplate("constant.defaultColor");
130: } else if (tok.startsWith("reg.text")) {
131: if (!cu.isUnregistered) {
132: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
133: .getTemplateSet()
134: : msgState.sender.getTemplateSet();
135: val = ts
136: .getMessageTemplate("constant.reg.text");
137: }
138: if (val == null)
139: val = "";
140: } else if (tok.startsWith("group.theme")) {
141: val = cu.getGroup().getTheme();
142: if (val == null)
143: val = "";
144: } else if (tok.startsWith("group")
145: || tok.startsWith("group.name")) {
146: if (cu.getGroup() != null)
147: val = cu.getGroup().getName();
148: else
149: val = "";
150: } else if (tok.startsWith("peername")) {
151: if (cu.conn.peerAddress != null) {
152: val = cu.conn.peerAddress
153: .getCanonicalHostName();
154: }
155: if (val == null)
156: val = "";
157: } else if (tok.startsWith("peerip")) {
158: val = cu.conn.peerIp;
159: if (val == null)
160: val = "";
161: } else if (tok.startsWith("cookie")) {
162: // only the sender himself or GOD can read the MD5 Hash of the cookie String
163: if (cu != null
164: && msgState.sender != null
165: && (cu.getName().equals(
166: msgState.sender.getName()) || msgState.sender
167: .hasRight(IUserStates.ROLE_GOD))) {
168: try {
169: val = HashUtils.encodeMD5(cu.getCookie());
170: } catch (Exception e) {
171: val = e.getMessage();
172: }
173: } else {
174: val = "hidden";
175: }
176: if (val == null)
177: val = "";
178: } else if (tok.startsWith("clientname")) {
179: if (cu.conn.clientAddress != null) {
180: val = cu.conn.clientAddress
181: .getCanonicalHostName();
182: }
183: if (val == null)
184: val = "";
185: } else if (tok.startsWith("clientip")) {
186: val = cu.conn.clientIp;
187: if (val == null)
188: val = "";
189: } else if (tok.startsWith("templateset")) {
190: val = cu.getTemplateSet().getName();
191: } else if (tok.startsWith("forwardchain")) {
192: if (cu.conn.fwChain != null
193: && cu.conn.fwChain.length > 0) {
194: StringBuffer sb = new StringBuffer();
195: for (int j = 0; j < cu.conn.fwChain.length; j++) {
196: sb.append(cu.conn.fwChain[j]);
197: if (j < cu.conn.fwChain.length)
198: sb.append(", ");
199: }
200: val = sb.toString();
201: } else {
202: val = "";
203: }
204: } else {
205: int spc = tok.indexOf(" ");
206: Object o = null;
207: if (spc > -1) {
208: o = cu.getProperty(tok.substring(0, spc));
209: } else {
210: o = cu.getProperty(tok);
211: }
212: if (o == null) {
213: val = "";
214: } else {
215: val = o.toString();
216: encode = true;
217: }
218: }
219: } else {
220: val = "";
221: }
222: } else if (tok.startsWith("group")
223: || tok.startsWith("sourcegroup")
224: || tok.startsWith("targetgroup")) { // TOKENS WITH GROUP-CONTEXT
225: Group cg = tok.startsWith("sourcegroup") ? msgState.sourceGroup
226: : msgState.targetGroup;
227: int pos = tok.indexOf(".");
228: if (pos == -1) {
229: if (cg != null) {
230: val = cg.getName();
231: } else {
232: val = null;
233: }
234: } else if (cg != null) {
235: tok = tok.substring(pos + 1);
236: if (tok.startsWith("theme")) {
237: val = cg.getTheme();
238: if (val == null)
239: val = "";
240: } else if (tok.startsWith("lockstate")) {
241: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
242: .getTemplateSet()
243: : msgState.sender.getTemplateSet();
244: if (cg.hasState(IGroupState.OPEN)) {
245: val = ts
246: .getMessageTemplate("constant.openGroup");
247: } else {
248: val = ts
249: .getMessageTemplate("constant.lockedGroup");
250: }
251: if (val == null)
252: val = "";
253: } else if (tok.startsWith("islocked")) {
254: if (!cg.hasState(IGroupState.OPEN)) {
255: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
256: .getTemplateSet()
257: : msgState.sender.getTemplateSet();
258: val = ts
259: .getMessageTemplate("constant.lockedGroup");
260: } else
261: val = "";
262: } else if (tok.startsWith("isopen")) {
263: if (cg.hasState(IGroupState.OPEN)) {
264: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
265: .getTemplateSet()
266: : msgState.sender.getTemplateSet();
267: val = ts
268: .getMessageTemplate("constant.openGroup");
269: } else
270: val = "";
271: } else if (tok.startsWith("questioncounter")) {
272: val = String.valueOf(cg.getQuestionCounter());
273: }
274: } else {
275: val = "";
276: }
277: } else if (tok.startsWith("message")
278: || tok.startsWith("userlist")
279: || tok.startsWith("result")) {
280: if (msgState.message == null) {
281: val = "";
282: } else { // userlist or result could contain html, is that true?
283: val = msgState.message;
284: encode = tok.startsWith("message");
285: }
286: } else if (tok.startsWith("reason") || tok.startsWith("throw")) {
287: val = msgState.reason == null ? "" : msgState.reason;
288: encode = true;
289: } else if (tok.startsWith("reciever")
290: || tok.startsWith("target")
291: || tok.startsWith("friendscount")
292: || tok.startsWith("source") || tok.startsWith("total")
293: || tok.startsWith("param")) {
294: val = msgState.param;
295: encode = true;
296: } else if (tok.startsWith("firstusers")
297: || tok.startsWith("firstmemberships")) {
298: if (msgState.usrList == null || msgState.usrList.length < 2)
299: val = "";
300: else {
301: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
302: .getTemplateSet()
303: : msgState.sender.getTemplateSet();
304: boolean useTpl = (ts
305: .getMessageTemplate("constant.userListItem") != null);
306: msgState.useRenderCache = false;
307: User u = msgState.usercontext;
308: StringBuffer sb = new StringBuffer();
309: for (int i = 0; i < msgState.usrList.length - 1; i++) {
310: Object curr = msgState.usrList[i];
311: if (curr instanceof User) {
312: if (!useTpl) {
313: sb.append(((User) msgState.usrList[i])
314: .getName());
315: encode = true;
316: } else {
317: msgState.usercontext = (User) msgState.usrList[i];
318: sb
319: .append(renderTemplate(
320: msgState,
321: ts
322: .getMessageTemplate("constant.userListItem"),
323: false));
324: }
325: } else
326: sb.append(curr.toString());
327: if (i < msgState.usrList.length - 2)
328: sb.append(", ");
329: }
330: msgState.usercontext = u;
331: val = sb.toString();
332: }
333: } else if (tok.startsWith("lastuser")
334: || tok.startsWith("lastmembership")) {
335: Object item = msgState.usrList[msgState.usrList.length - 1];
336: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
337: .getTemplateSet()
338: : msgState.sender.getTemplateSet();
339: if (item instanceof User) {
340: if (ts.getMessageTemplate("constant.userListItem") == null) {
341: val = ((User) item).getName();
342: encode = true;
343: } else {
344: User u = msgState.usercontext;
345: msgState.usercontext = (User) item;
346: val = renderTemplate(
347: msgState,
348: ts
349: .getMessageTemplate("constant.userListItem"),
350: false);
351: msgState.usercontext = u;
352: }
353: } else
354: val = item.toString();
355: } else if (tok.startsWith("membership")) {
356: if (msgState.usrList != null && msgState.usrList.length > 0) {
357: val = msgState.usrList[0].toString();
358: encode = true;
359: } else
360: val = "";
361: } else if (tok.startsWith("time")) {
362: String fstrg = "[HH:mm]";
363: int pos = tok.indexOf("format=");
364: if (pos != -1) {
365: fstrg = token.substring(pos + 6);
366: fstrg = retrieveParamValue(fstrg)[1];
367: }
368: val = Server.srv.getFormatedTime(fstrg);
369: } else if (tok.startsWith("vipcount")) {
370: val = String
371: .valueOf(UserManager.mgr.onlineVipList().size());
372: } else if (tok.startsWith("onlineusers")) {
373: val = String.valueOf(UserManager.mgr.getActiveUserCount());
374: } else if (tok.startsWith("opengroups")) {
375: val = String.valueOf(GroupManager.mgr.openGroupsCount());
376: } else if (tok.startsWith("constant")) {
377: int i = tok.indexOf(" ");
378: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
379: .getTemplateSet()
380: : msgState.sender.getTemplateSet();
381: String msgTpl;
382: if (i == -1) {
383: msgTpl = token;
384: } else {
385: msgTpl = token.substring(0, i);
386: }
387: msgTpl = ts.getMessageTemplate(msgTpl);
388: if (msgTpl == null) {
389: val = "[Constant not found: " + msgTpl + "]";
390: encode = true;
391: } else {
392: val = renderTemplate(msgState, msgTpl, false);
393: }
394: }
395: if (val == null)
396: return (token);
397: int pos = Math.max(token.indexOf(" "), token.indexOf("."));
398: if (pos == -1)
399: return encode ? EntityDecoder.charToHtml(val) : val;
400: if (tok.indexOf("touppercase") > -1) {
401: val = val.toUpperCase();
402: } else if (tok.indexOf("tolowercase") > -1) {
403: val = val.toLowerCase();
404: }
405: if (encode) {
406: val = EntityDecoder.charToHtml(val);
407: }
408: return renderValue(msgState, val, token.substring(pos).trim());
409: }
410:
411: private static String renderChattime(User cu) {
412: long s = cu.getChattime();
413: long m = s / 60;
414: long h = m / 60;
415: s = s - m * 60;
416: m = m - h * 60;
417: StringBuffer sb = new StringBuffer();
418: if (h > 0)
419: sb.append(h).append("h ");
420: if (m > 0)
421: sb.append(m).append("m ");
422: sb.append(s).append("s");
423: return sb.toString();
424: }
425:
426: /**
427: * renders the given User's title taking the layout of msgState.sender
428: * @param msgState the current message-state containing the sender
429: * @param cu the user whos title should be rendered
430: * @return the rendered string
431: */
432: private static String renderTitle(MessageState msgState, User cu) {
433: StringBuffer sb = new StringBuffer();
434: String val = "";
435: Group sg = cu.getGroup();
436: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
437: .getTemplateSet() : msgState.sender.getTemplateSet();
438:
439: boolean append = false, displayDefaultVipRighttitle = true, displayDefaultModGuestTitle = true;
440: String seperator = ts
441: .getMessageTemplate("constant.title.seperator");
442: if (cu.getCustomTitle() != null
443: && cu.getCustomTitle().length() > 1) {
444: append = true;
445: val = ts.getMessageTemplate("constant.customtitle.prefix");
446: if (val != null)
447: sb.append(val);
448: sb.append(cu.getCustomTitle());
449: val = ts.getMessageTemplate("constant.customtitle.suffix");
450: if (val != null)
451: sb.append(val);
452: } else {
453: Membership defaultMembership = cu.getDefaultMembership();
454: if (defaultMembership != null) {
455: displayDefaultVipRighttitle = defaultMembership
456: .displayDefaultVipRighttitle();
457: displayDefaultModGuestTitle = defaultMembership
458: .displayDefaultModGuesttitle();
459:
460: }
461: if (defaultMembership != null
462: && defaultMembership != MembershipManager.undefined) {
463: val = ts
464: .getMessageTemplate("constant.title.membership."
465: + defaultMembership.key);
466: if (val == null) {
467: val = defaultMembership.getName();
468: if (val.length() <= 0)
469: val = null;
470: }
471: if (val != null) {
472: if (append && seperator != null)
473: sb.append(seperator);
474: sb.append(val);
475: append = true;
476: }
477: }
478: }
479: if (cu.hasRight(IUserStates.IS_MODERATOR)
480: && sg.hasState(IGroupState.MODERATED)) {
481: val = ts.getMessageTemplate("constant.title.moderator");
482: if (val != null && displayDefaultModGuestTitle) {
483: if (append && seperator != null)
484: sb.append(seperator);
485: sb.append(val);
486: append = true;
487: }
488: }
489: if (cu.hasRight(IUserStates.IS_GUEST)
490: && sg.hasState(IGroupState.MODERATED)) {
491: val = ts.getMessageTemplate("constant.title.guest");
492: if (val != null && displayDefaultModGuestTitle) {
493: if (append && seperator != null)
494: sb.append(seperator);
495: sb.append(val);
496: append = true;
497: }
498: }
499: if (cu.hasRole(IUserStates.ROLE_VIP)) {
500: val = ts.getMessageTemplate("constant.title.vip");
501: if (val != null && displayDefaultVipRighttitle) {
502: if (append && seperator != null)
503: sb.append(seperator);
504: sb.append(val);
505: append = true;
506: }
507: }
508: if (cu.hasRole(IUserStates.ROLE_GOD)) {
509: val = ts.getMessageTemplate("constant.title.god");
510: if (val != null && displayDefaultVipRighttitle) {
511: if (append && seperator != null)
512: sb.append(seperator);
513: sb.append(val);
514: append = true;
515: }
516: }
517: if (cu.getGroup() != null && cu.getGroup().usrIsSu(cu)
518: && !cu.hasRight(IUserStates.ROLE_VIP)) {
519: val = ts.getMessageTemplate("constant.title.su");
520: if (val != null) {
521: if (append && seperator != null)
522: sb.append(seperator);
523: sb.append(val);
524: append = true;
525: }
526: }
527:
528: return sb.toString();
529: }
530:
531: /**
532: * renders the username including the visual changes depending on userrights
533: * @param cu the username to render
534: * @return the rendered username
535: */
536: private static String renderUserName(MessageState msgState, User cu) {
537: if (cu == null)
538: return null;
539: StringBuffer tsb = new StringBuffer();
540: Group sg = cu.getGroup();
541: boolean isSu = false, isPunished = cu.isPunished(), setPrefsuffix = false;
542: if (sg != null)
543: isSu = sg.usrIsSu(cu);
544: String pre = "", suf = "";
545: TemplateSet ts = msgState.sender == null ? TemplateManager.mgr
546: .getTemplateSet() : msgState.sender.getTemplateSet();
547: Membership defaultMembership = cu.getDefaultMembership();
548: if (cu.getDefaultMembership() != null) {
549: pre = ts.getMessageTemplate("constant.membership."
550: + defaultMembership.key + ".prefix");
551: suf = ts.getMessageTemplate("constant.membership."
552: + defaultMembership.key + ".suffix");
553: if (pre == null && suf == null) {
554: pre = defaultMembership.getNamePrefix();
555: suf = defaultMembership.getNameSuffix();
556: }
557: if (pre == null && suf == null) {
558: pre = "";
559: suf = "";
560: } else
561: setPrefsuffix = true;
562: }
563: if (cu.hasRight(IUserStates.IS_GUEST)
564: && !cu.hasRole(IUserStates.ROLE_GOD)) {
565: if (sg != null && sg.hasState(IGroupState.MODERATED)) {
566: pre = ts
567: .getMessageTemplate("constant.username.guest.prefix");
568: suf = ts
569: .getMessageTemplate("constant.username.guest.suffix");
570: if (pre == null && suf == null) {
571: pre = Server.srv.UNAME_PREFIX_GUEST;
572: suf = Server.srv.UNAME_SUFFIX_GUEST;
573: }
574: }
575: }
576: if (cu.hasRight(IUserStates.IS_GUEST)
577: && 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: if (sg != null && sg.hasState(IGroupState.MODERATED)) {
585: if ((ts
586: .getMessageTemplate("constant.username.guest.prefix") != null)
587: && (ts
588: .getMessageTemplate("constant.username.vip.prefix") != null))
589: pre = ts
590: .getMessageTemplate("constant.username.vip.prefix")
591: + ts
592: .getMessageTemplate("constant.username.guest.prefix");
593: else
594: pre = null;
595: if ((ts
596: .getMessageTemplate("constant.username.guest.suffix") != null)
597: && (ts
598: .getMessageTemplate("constant.username.vip.suffix") != null))
599: suf = ts
600: .getMessageTemplate("constant.username.guest.suffix")
601: + ts
602: .getMessageTemplate("constant.username.vip.suffix");
603: else
604: suf = null;
605: if (pre == null && suf == null) {
606: pre = Server.srv.UNAME_PREFIX_VIP
607: + Server.srv.UNAME_PREFIX_GUEST;
608: suf = Server.srv.UNAME_SUFFIX_GUEST
609: + Server.srv.UNAME_SUFFIX_VIP;
610: }
611: }
612: } else if (cu.hasRight(IUserStates.IS_MODERATOR)
613: && !cu.hasRight(IUserStates.IS_GUEST)
614: && !cu.hasRole(IUserStates.ROLE_GOD) && !setPrefsuffix) {
615: if (sg != null && sg.hasState(IGroupState.MODERATED)) {
616: pre = ts
617: .getMessageTemplate("constant.username.moderator.prefix");
618: suf = ts
619: .getMessageTemplate("constant.username.moderator.suffix");
620: if (pre == null && suf == null) {
621: pre = Server.srv.UNAME_PREFIX_MODERATOR;
622: suf = Server.srv.UNAME_SUFFIX_MODERATOR;
623: }
624: }
625: if (cu.hasRight(IUserStates.IS_MODERATOR)
626: && cu.hasRole(IUserStates.ROLE_VIP)
627: && !setPrefsuffix) {
628: pre = ts
629: .getMessageTemplate("constant.username.vip.prefix");
630: suf = ts
631: .getMessageTemplate("constant.username.vip.suffix");
632: if (pre == null && suf == null) {
633: pre = Server.srv.UNAME_PREFIX_VIP;
634: suf = Server.srv.UNAME_SUFFIX_VIP;
635: }
636: if (sg != null && sg.hasState(IGroupState.MODERATED)) {
637: if ((ts
638: .getMessageTemplate("constant.username.moderator.prefix") != null)
639: && (ts
640: .getMessageTemplate("constant.username.vip.prefix") != null))
641: pre = ts
642: .getMessageTemplate("constant.username.vip.prefix")
643: + ts
644: .getMessageTemplate("constant.username.moderator.prefix");
645: else
646: pre = null;
647: if ((ts
648: .getMessageTemplate("constant.username.moderator.suffix") != null)
649: && (ts
650: .getMessageTemplate("constant.username.vip.suffix") != null))
651: suf = ts
652: .getMessageTemplate("constant.username.moderator.suffix")
653: + ts
654: .getMessageTemplate("constant.username.vip.suffix");
655: else
656: suf = null;
657: if (pre == null && suf == null) {
658: pre = Server.srv.UNAME_PREFIX_VIP
659: + Server.srv.UNAME_PREFIX_MODERATOR;
660: suf = Server.srv.UNAME_SUFFIX_MODERATOR
661: + Server.srv.UNAME_SUFFIX_VIP;
662: }
663: }
664: }
665: } else if (cu.hasRole(IUserStates.ROLE_GOD) && !setPrefsuffix) {
666: pre = ts.getMessageTemplate("constant.username.god.prefix");
667: suf = ts.getMessageTemplate("constant.username.god.suffix");
668: if (pre == null && suf == null) {
669: pre = Server.srv.UNAME_PREFIX_GOD;
670: suf = Server.srv.UNAME_SUFFIX_GOD;
671: }
672: } else if (cu.hasRole(IUserStates.ROLE_VIP) && !setPrefsuffix) {
673: pre = ts.getMessageTemplate("constant.username.vip.prefix");
674: suf = ts.getMessageTemplate("constant.username.vip.suffix");
675: if (pre == null && suf == null) {
676: pre = Server.srv.UNAME_PREFIX_VIP;
677: suf = Server.srv.UNAME_SUFFIX_VIP;
678: }
679: } else if (isSu && !setPrefsuffix) {
680: pre = ts.getMessageTemplate("constant.username.su.prefix");
681: suf = ts.getMessageTemplate("constant.username.su.suffix");
682: if (pre == null && suf == null) {
683: pre = Server.srv.UNAME_PREFIX_SU;
684: suf = Server.srv.UNAME_SUFFIX_SU;
685: }
686: }
687: if (isPunished) {
688: pre = ts
689: .getMessageTemplate("constant.username.punished.prefix");
690: suf = ts
691: .getMessageTemplate("constant.username.punished.suffix");
692: if (pre == null && suf == null) {
693: pre = Server.srv.UNAME_PREFIX_PUNISHED;
694: suf = Server.srv.UNAME_SUFFIX_PUNISHED;
695: }
696: }
697: tsb.append(pre);
698: tsb.append(EntityDecoder.charToHtml(cu.getName()));
699: tsb.append(suf);
700: return tsb.toString();
701: }
702:
703: /**
704: * renders the value according to the parameters
705: * @param val the value to render
706: * @param params the params for the rendering
707: * @return the rendered value
708: */
709: private static String renderValue(MessageState msgState,
710: String val, String params) {
711: if (params == null || params.length() < 1)
712: return val;
713: StringBuffer sb = new StringBuffer();
714: String prefix = "";
715: String suffix = "";
716: String plower = params.toLowerCase();
717: while (params.length() > 0) {
718: if (params.startsWith("toUpperCase")
719: || params.startsWith("toLowerCase")
720: || params.startsWith("touppercase")
721: || params.startsWith("tolowercase")) {
722: params = params.substring(11);
723: continue;
724: }
725: if (params.startsWith("prefix")) {
726: int start = params.indexOf("=");
727: if (start == -1)
728: return ("syntax");
729: params = params.substring(start);
730: String vals[] = retrieveParamValue(params);
731: params = vals[0];
732: prefix = vals[1];
733: } else if (params.startsWith("suffix")) {
734: int start = params.indexOf("=");
735: if (start == -1)
736: return ("syntax");
737: params = params.substring(start);
738: String vals[] = retrieveParamValue(params);
739: params = vals[0];
740: suffix = vals[1];
741: } else if (params.toLowerCase().startsWith("ifempty")
742: && val.length() < 1) {
743: int start = params.indexOf("=");
744: if (start == -1)
745: return ("syntax");
746: params = params.substring(start);
747: String vals[] = retrieveParamValue(params);
748: return vals[1];
749: } else if (params.startsWith("value") && !val.equals("")) {
750: int start = params.indexOf("=");
751: if (start == -1)
752: return ("syntax");
753: params = params.substring(start);
754: String vals[] = retrieveParamValue(params);
755: params = vals[0];
756: val = vals[1];
757: } else
758: break;
759: }
760: if (val.equals(""))
761: return ("");
762: if (prefix.indexOf("@") > -1) {
763: prefix = renderTemplate(msgState, prefix.replace('@', '§'),
764: false);
765: }
766: if (suffix.indexOf("@") > -1) {
767: suffix = renderTemplate(msgState, suffix.replace('@', '§'),
768: false);
769: }
770: sb.append(prefix);
771: sb.append(val);
772: sb.append(suffix);
773: return sb.toString();
774: }
775:
776: public static String[] retrieveParamValue(String params) {
777: params = params.trim();
778: StringBuffer tsb = new StringBuffer();
779: boolean quote = false, mask = false, firstChar = false;
780: int stop = 0;
781: for (int i = 0; i < params.length(); i++) {
782: char c = params.charAt(i);
783: if (mask) {
784: mask = false;
785: tsb.append(c);
786: } else if (firstChar && !quote && c == ' ') {
787: stop = i;
788: break;
789: } else if (c == '\\') {
790: mask = true;
791: } else if (!mask && !quote && c == '"') {
792: quote = true;
793: } else if (!mask && c == '"') {
794: stop = i + 1;
795: break;
796: } else if (!firstChar && c != ' ') {
797: firstChar = true;
798: } else {
799: tsb.append(c);
800: }
801: }
802: String result[] = new String[2];
803: if (params.length() > stop)
804: result[0] = params.substring(stop).trim();
805: else
806: result[0] = "";
807:
808: result[1] = tsb.toString();
809: return result;
810: }
811:
812: public static ByteBuffer encode(String s) {
813: try {
814: return Server.srv.defaultCsEnc.encode(CharBuffer.wrap(s));
815: } catch (CharacterCodingException cce) {
816: return Server.srv.defaultCs.encode(CharBuffer.wrap(s));
817: }
818: }
819: }
820:
821: /* public String evalAlgorithm (String token) {
822: if (!token.startsWith ("if(") && !token.startsWith ("if (")) return token;
823: String expression = token.substring (token.indexOf ("("), token.lastIndesOf (")"));
824: StringBuffer sb = new StringBuffer ();
825: int i = 0;
826: for (; i<expression.length (); i++) {
827: char c = expression.charAt (i);
828: if (c == '!' || c == '=' || c == ' ' || c == '<' || c == '>')
829: break;
830: sb.append (c);
831: }
832: String left = sb.toString ().trim ();
833: sb = new StringBuffer ();
834: for (; i<expression.length (); i++) {
835: char c = expression.charAt (i);
836: if (c != '!' || c != '=' || c != ' ' || c != '<' || c != '>')
837: break;
838: sb.append (c);
839: }
840: String operand = sb.toString ();
841: sb = new StringBuffer ();
842: for (; i<expression.length (); i++) {
843: char c = expression.charAt (i);
844: if (c == '!' || c == '=' || c == ' ' || c == '<' || c == '>')
845: break;
846: sb.append (c);
847: }
848: String right = sb.toString ();
849: if (left.startsWith ("\"")) {
850: left = left.substring (1, left.length ()-2);
851: } else (
852: String tstr = evalVariable (left);
853: if (tstr.length () < 1) return token;
854: left = tstr;
855: }
856: if (left.length () < 1 || operand.length () < 1 || right.length () < 1)
857: return (token)
858:
859: } */
|