KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > core > MessageRenderer


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Created on 01.10.2003
19  */

20
21 package freecs.core;
22
23 import java.nio.ByteBuffer JavaDoc;
24 import java.nio.CharBuffer JavaDoc;
25 import java.nio.charset.CharacterCodingException JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
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 /**
38  * @author Manfred Andres
39  *
40  * freecs.core
41  */

42 public class MessageRenderer {
43     /**
44      * renders the given template
45      * @param msgTpl the raw message-template
46      * @return the rendered template as String with all placeholders replaced by their coresponding value
47      */

48    public static String JavaDoc renderTemplate (MessageState msgState, String JavaDoc msgTpl) {
49       return renderTemplate (msgState, msgTpl, msgTpl.equals ("list.users") ? false : true);
50    }
51    public static String JavaDoc renderTemplate (MessageState msgState, String JavaDoc msgTpl, boolean addBreak) {
52       if (msgTpl == null)
53           return null;
54       StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
55       for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (msgTpl, "§"); st.hasMoreTokens (); ) {
56          String JavaDoc token = st.nextToken ();
57          // String result = evalAlgorithm (token);
58
sb.append (evalVariable (msgState, token));
59       }
60       if (addBreak) sb.append ("<br>\r\n");
61       return (sb.toString ());
62    }
63
64    /**
65     * evaluate the given variable
66     * FIXME: this has to go more elegant...
67     * @param token the token to evaluate
68     * @return the evaluated result || the token if nothing was to evaluate
69     */

70     private static String JavaDoc evalVariable (MessageState msgState, String JavaDoc token) {
71         String JavaDoc val = null;
72         String JavaDoc 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")) { // TOKENS WITH USER-CONTEXT
78
User cu = tok.startsWith("sender") ? msgState.sender : msgState.usercontext;
79             int pos = tok.indexOf(".");
80             if (cu != null && pos == -1) { // TOKEN for the username
81
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()) // not away, no awaymessage
88
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                     // only the sender himself or GOD can read the MD5 Hash of the cookie String
149
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 JavaDoc 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 JavaDoc sb = new StringBuffer JavaDoc ();
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 JavaDoc 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")) { // TOKENS WITH GROUP-CONTEXT
206
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 { // userlist or result could contain html, is that true?
259
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 JavaDoc sb = new StringBuffer JavaDoc();
287                 for (int i = 0; i < msgState.usrList.length-1; i++) {
288                     Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc sb = new StringBuffer JavaDoc();
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     /**
394      * renders the given User's title taking the layout of msgState.sender
395      * @param msgState the current message-state containing the sender
396      * @param cu the user whos title should be rendered
397      * @return the rendered string
398      */

399     private static String JavaDoc renderTitle (MessageState msgState, User cu) {
400         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
401         String JavaDoc 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 JavaDoc 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     /**
490      * renders the username including the visual changes depending on userrights
491      * @param cu the username to render
492      * @return the rendered username
493      */

494     private static String JavaDoc renderUserName (MessageState msgState, User cu) {
495         if (cu==null)
496             return null;
497         StringBuffer JavaDoc tsb = new StringBuffer JavaDoc ();
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 JavaDoc 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     /**
607      * renders the value according to the parameters
608      * @param val the value to render
609      * @param params the params for the rendering
610      * @return the rendered value
611      */

612     private static String JavaDoc renderValue (MessageState msgState, String JavaDoc val, String JavaDoc params) {
613         if (params == null || params.length() < 1)
614             return val;
615         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
616         String JavaDoc prefix = "";
617         String JavaDoc suffix = "";
618         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] retrieveParamValue (String JavaDoc params) {
671       params = params.trim ();
672       StringBuffer JavaDoc tsb = new StringBuffer JavaDoc ();
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 JavaDoc result[] = new String JavaDoc[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 JavaDoc encode (String JavaDoc s) {
707         try {
708             return Server.srv.defaultCsEnc.encode (CharBuffer.wrap(s));
709         } catch (CharacterCodingException JavaDoc cce) {
710             return Server.srv.defaultCs.encode(CharBuffer.wrap(s));
711         }
712     }
713 }
714
715
716
717 /* public String evalAlgorithm (String token) {
718 if (!token.startsWith ("if(") && !token.startsWith ("if (")) return token;
719 String expression = token.substring (token.indexOf ("("), token.lastIndesOf (")"));
720 StringBuffer sb = new StringBuffer ();
721 int i = 0;
722 for (; i<expression.length (); i++) {
723      char c = expression.charAt (i);
724      if (c == '!' || c == '=' || c == ' ' || c == '<' || c == '>')
725          break;
726       sb.append (c);
727 }
728 String left = sb.toString ().trim ();
729 sb = new StringBuffer ();
730 for (; i<expression.length (); i++) {
731      char c = expression.charAt (i);
732      if (c != '!' || c != '=' || c != ' ' || c != '<' || c != '>')
733          break;
734       sb.append (c);
735 }
736 String operand = sb.toString ();
737 sb = new StringBuffer ();
738 for (; i<expression.length (); i++) {
739      char c = expression.charAt (i);
740      if (c == '!' || c == '=' || c == ' ' || c == '<' || c == '>')
741          break;
742       sb.append (c);
743 }
744 String right = sb.toString ();
745 if (left.startsWith ("\"")) {
746      left = left.substring (1, left.length ()-2);
747 } else (
748      String tstr = evalVariable (left);
749      if (tstr.length () < 1) return token;
750      left = tstr;
751 }
752 if (left.length () < 1 || operand.length () < 1 || right.length () < 1)
753      return (token)
754
755 } */

756
Popular Tags