Source code: com/wilko/jaim/Utils.java
1 /*
2 * (C) 2002 Paul Wilkinson wilko@users.sourceforge.net
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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 */
19
20 /*
21 * Utils.java
22 *
23 * Created on 3 May 2002, 17:19
24 */
25
26 package com.wilko.jaim;
27
28 /**
29 *
30 * @author paulw
31 * @version $Revision: 1.4 $
32 */
33 public class Utils {
34
35 private static final String roastKey="Tic/Toc";
36 private static final int roastLen=7;
37
38 /** convert a buddy name to normalised format - remove spaces and convert to lower case
39 * @param input The un-normalised buddy name
40 * @return the normalised buddy name
41 */
42 public static String normalise(java.lang.String input) {
43 StringBuffer output=new StringBuffer();
44 String temp=input.toLowerCase();
45 for (int i=0;i<input.length();i++)
46 {
47 char c=temp.charAt(i);
48 if ((c>= '0' && c<='9')||(c>='a' && c<='z'))
49 {
50 output.append(c);
51 }
52 }
53
54 return(output.toString());
55 }
56
57 /** Roast a password using the AOL roasting protocol
58 * @param password The password to be roasted
59 * @return The roasted password
60 */
61 public static String roast(java.lang.String password) {
62 char[] hexChars={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
63
64 StringBuffer temppw=new StringBuffer();
65 temppw.append("0x");
66 for (int i=0;i<password.length();i++)
67 {
68 int roastedByte=password.charAt(i)^roastKey.charAt(i%roastLen);
69
70 temppw.append(hexChars[(roastedByte>>4)&0x0f]);
71 temppw.append(hexChars[roastedByte&0x0f]);
72 }
73 return(temppw.toString());
74 }
75
76 /** This method performs a simple HTML strip on text. It looks for < characters and then skips input until a matching > is found.
77 * This may fail if the HTML tag contains an embedded '>'
78 * @param input The text to have HTML stripped
79 * @return The text stripped of html
80 */
81 public static String stripHTML(java.lang.String input)
82 {
83 StringBuffer output=new StringBuffer();
84 boolean inHTML=false;
85 for (int i=0;i<input.length();i++)
86 {
87 char c=input.charAt(i);
88 if (c=='<')
89 {
90 inHTML=true;
91 }
92 else
93 {
94 if (c=='>') {
95 inHTML=false;
96 }
97 else
98 {
99 if (!inHTML)
100 {
101 output.append(c);
102 }
103 }
104 }
105 }
106 return(output.toString());
107 }
108
109
110 /** Encode a text message so that it is suitable for transmission using toc_send_im
111 *
112 * @param input The text to be encoded
113 * @return The encoded text
114 */
115 public static String encodeText(String input)
116 {
117 StringBuffer output=new StringBuffer("\"");
118 for (int i=0;i<input.length();i++)
119 {
120 char c=input.charAt(i);
121 switch (c)
122 {
123 case '\"':
124 case '(':
125 case ')':
126 case '$':
127 case '\\':
128 case '{':
129 case '}':
130 case '[':
131 case ']':
132 output.append('\\');
133 break;
134 }
135 output.append(c);
136 }
137
138 output.append('\"');
139 return(output.toString());
140 }
141
142
143 }