~psycojoker/+junk/cerkbot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* 
Copyright Paul James Mutton, 2001-2007, http://www.jibble.org/

This file is part of PircBot.

This software is dual-licensed, allowing you to choose between the GNU
General Public License (GPL) and the www.jibble.org Commercial License.
Since the GPL may be too restrictive for use in a proprietary application,
a commercial license is also provided. Full license information can be
found at http://www.jibble.org/licenses/

*/


package org.jibble.pircbot;

/**
 * The Colors class provides several static fields and methods that you may
 * find useful when writing an IRC Bot.
 *  <p>
 * This class contains constants that are useful for formatting lines
 * sent to IRC servers.  These constants allow you to apply various
 * formatting to the lines, such as colours, boldness, underlining
 * and reverse text.
 *  <p>
 * The class contains static methods to remove colours and formatting
 * from lines of IRC text.
 *  <p>
 * Here are some examples of how to use the contants from within a
 * class that extends PircBot and imports org.jibble.pircbot.*;
 * 
 * <pre> sendMessage("#cs", Colors.BOLD + "A bold hello!");
 *     <b>A bold hello!</b>
 * sendMessage("#cs", Colors.RED + "Red" + Colors.NORMAL + " text");
 *     <font color="red">Red</font> text
 * sendMessage("#cs", Colors.BOLD + Colors.RED + "Bold and red");
 *     <b><font color="red">Bold and red</font></b></pre>
 * 
 * Please note that some IRC channels may be configured to reject any
 * messages that use colours.  Also note that older IRC clients may be
 * unable to correctly display lines that contain colours and other
 * control characters.
 *  <p>
 * Note that this class name has been spelt in the American style in
 * order to remain consistent with the rest of the Java API.
 *
 *
 * @since   0.9.12
 * @author  Paul James Mutton,
 *          <a href="http://www.jibble.org/">http://www.jibble.org/</a>
 * @version    1.4.6 (Build time: Wed Apr 11 19:20:59 2007)
 */
public class Colors {

    
    /**
     * Removes all previously applied color and formatting attributes.
     */
    public static final String NORMAL = "\u000f";


    /**
     * Bold text.
     */
    public static final String BOLD = "\u0002";
    

    /**
     * Underlined text.
     */
    public static final String UNDERLINE = "\u001f";


    /**
     * Reversed text (may be rendered as italic text in some clients).
     */
    public static final String REVERSE = "\u0016";
    

    /**
     * White coloured text.
     */
    public static final String WHITE = "\u000300";
    

    /**
     * Black coloured text.
     */
    public static final String BLACK = "\u000301";
    

    /**
     * Dark blue coloured text.
     */
    public static final String DARK_BLUE = "\u000302";
    

    /**
     * Dark green coloured text.
     */
    public static final String DARK_GREEN = "\u000303";
    

    /**
     * Red coloured text.
     */
    public static final String RED = "\u000304";
    

    /**
     * Brown coloured text.
     */
    public static final String BROWN = "\u000305";
    

    /**
     * Purple coloured text.
     */
    public static final String PURPLE = "\u000306";
    

    /**
     * Olive coloured text.
     */
    public static final String OLIVE = "\u000307";
    

    /**
     * Yellow coloured text.
     */
    public static final String YELLOW = "\u000308";
    

    /**
     * Green coloured text.
     */
    public static final String GREEN = "\u000309";
    

    /**
     * Teal coloured text.
     */
    public static final String TEAL = "\u000310";
    

    /**
     * Cyan coloured text.
     */
    public static final String CYAN = "\u000311";
    

    /**
     * Blue coloured text.
     */
    public static final String BLUE = "\u000312";
    

    /**
     * Magenta coloured text.
     */
    public static final String MAGENTA = "\u000313";
    

    /**
     * Dark gray coloured text.
     */
    public static final String DARK_GRAY = "\u000314";
    

    /**
     * Light gray coloured text.
     */
    public static final String LIGHT_GRAY = "\u000315";

    
    /**
     * This class should not be constructed.
     */
    private Colors() {
        
    }
    
    
    /**
     * Removes all colours from a line of IRC text.
     * 
     * @since PircBot 1.2.0
     * 
     * @param line the input text.
     * 
     * @return the same text, but with all colours removed.
     */
    public static String removeColors(String line) {
        int length = line.length();
        StringBuffer buffer = new StringBuffer();
        int i = 0;
        while (i < length) {
            char ch = line.charAt(i);
            if (ch == '\u0003') {
                i++;
                // Skip "x" or "xy" (foreground color).
                if (i < length) {
                    ch = line.charAt(i);
                    if (Character.isDigit(ch)) {
                        i++;
                        if (i < length) {
                            ch = line.charAt(i);
                            if (Character.isDigit(ch)) {
                                i++;
                            }
                        }
                        // Now skip ",x" or ",xy" (background color).
                        if (i < length) {
                            ch = line.charAt(i);
                            if (ch == ',') {
                                i++;
                                if (i < length) {
                                    ch = line.charAt(i);
                                    if (Character.isDigit(ch)) {
                                        i++;
                                        if (i < length) {
                                            ch = line.charAt(i);
                                            if (Character.isDigit(ch)) {
                                                i++;
                                            }
                                        }
                                    }
                                    else {
                                        // Keep the comma.
                                        i--;
                                    }
                                }
                                else {
                                    // Keep the comma.
                                    i--;
                                }
                            }
                        }
                    }
                }
            }
            else if (ch == '\u000f') {
                i++;
            }
            else {
                buffer.append(ch);
                i++;
            }
        }
        return buffer.toString();
    }
    
    
    /**
     * Remove formatting from a line of IRC text.
     * 
     * @since PircBot 1.2.0
     * 
     * @param line the input text.
     * 
     * @return the same text, but without any bold, underlining, reverse, etc.
     */
    public static String removeFormatting(String line) {
        int length = line.length();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            char ch = line.charAt(i);
            if (ch == '\u000f' || ch == '\u0002' || ch == '\u001f' || ch == '\u0016') {
                // Don't add this character.
            }
            else {
                buffer.append(ch);
            }
        }
        return buffer.toString();
    }
    
    
    /**
     * Removes all formatting and colours from a line of IRC text.
     * 
     * @since PircBot 1.2.0
     *
     * @param line the input text.
     * 
     * @return the same text, but without formatting and colour characters.
     * 
     */
    public static String removeFormattingAndColors(String line) {
        return removeFormatting(removeColors(line));
    }
    
}