|
@@ -0,0 +1,218 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.parser.irc;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.parser.interfaces.Encoder;
|
|
26
|
+
|
|
27
|
+import java.io.Closeable;
|
|
28
|
+import java.io.IOException;
|
|
29
|
+import java.io.InputStream;
|
|
30
|
+
|
|
31
|
+/**
|
|
32
|
+ * A {@link java.io.BufferedReader}-style reader that is aware of the IRC
|
|
33
|
+ * protocol and can transcode text appropriately.
|
|
34
|
+ *
|
|
35
|
+ * @since 0.6.5
|
|
36
|
+ * @author chris
|
|
37
|
+ */
|
|
38
|
+public class IRCReader implements Closeable {
|
|
39
|
+
|
|
40
|
+ /** Maximum length for an IRC line in bytes. */
|
|
41
|
+ private static final int LINE_LENGTH = 512;
|
|
42
|
+
|
|
43
|
+ /** The input stream to read input from. */
|
|
44
|
+ private final InputStream stream;
|
|
45
|
+
|
|
46
|
+ /** The encoder to use to encode lines. */
|
|
47
|
+ private final Encoder encoder;
|
|
48
|
+
|
|
49
|
+ /**
|
|
50
|
+ * Creates a new IRCReader which will read from the specified stream.
|
|
51
|
+ *
|
|
52
|
+ * @param inputStream The stream to read input from
|
|
53
|
+ * @param encoder The encoder to use to encode lines
|
|
54
|
+ */
|
|
55
|
+ public IRCReader(final InputStream inputStream, final Encoder encoder) {
|
|
56
|
+ this.stream = inputStream;
|
|
57
|
+ this.encoder = encoder;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ /**
|
|
61
|
+ * Reads a line from the underlying input stream, tokenises it, and
|
|
62
|
+ * requests that this reader's encoder encodes the message part of the
|
|
63
|
+ * line, if any.
|
|
64
|
+ *
|
|
65
|
+ * @return A wrapped line tokenised per RFC1459, or null if the stream ends
|
|
66
|
+ * @throws IOException If an IOException is encountered reading the
|
|
67
|
+ * underlying stream
|
|
68
|
+ */
|
|
69
|
+ public ReadLine readLine() throws IOException {
|
|
70
|
+ final byte[] line = new byte[LINE_LENGTH];
|
|
71
|
+ int offset = 0;
|
|
72
|
+ int paramOffset = -1;
|
|
73
|
+ int chr = 0, lastChr = 0;
|
|
74
|
+
|
|
75
|
+ while (offset < 512 && (chr = stream.read()) > -1) {
|
|
76
|
+ line[offset++] = (byte) chr;
|
|
77
|
+
|
|
78
|
+ if (lastChr == ' ' && chr == ':' && paramOffset == -1) {
|
|
79
|
+ // We've found the last param
|
|
80
|
+ paramOffset = offset;
|
|
81
|
+ } else if (lastChr == '\r' && chr == '\n') {
|
|
82
|
+ // End of the line
|
|
83
|
+ break;
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ lastChr = chr;
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ if (chr == -1) {
|
|
90
|
+ // Hit the end of the stream
|
|
91
|
+ return null;
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ return processLine(line, offset - 2, paramOffset);
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ /**
|
|
98
|
+ * Processes the specified line into a wrapped {@link ReadLine} instance.
|
|
99
|
+ *
|
|
100
|
+ * @param line The line as read from the wire
|
|
101
|
+ * @param length The length of the line in bytes
|
|
102
|
+ * @param paramOffset The offset of the first byte of the trailing parameter
|
|
103
|
+ * (i.e., the first byte following the ASCII sequence ' :'), or -1 if no
|
|
104
|
+ * such parameter exists.
|
|
105
|
+ * @return A corresponding {@link ReadLine} instance
|
|
106
|
+ */
|
|
107
|
+ private ReadLine processLine(final byte[] line, final int length, final int paramOffset) {
|
|
108
|
+ final String firstPart = new String(line, 0, paramOffset == -1 ? length : paramOffset - 2);
|
|
109
|
+ final String[] firstTokens = firstPart.split(" ");
|
|
110
|
+
|
|
111
|
+ final String[] tokens;
|
|
112
|
+ if (paramOffset > -1) {
|
|
113
|
+ final String source = getSource(firstTokens);
|
|
114
|
+ final String destination = getDestination(firstTokens);
|
|
115
|
+
|
|
116
|
+ final String lastPart = encoder.encode(source, destination, line,
|
|
117
|
+ paramOffset, length - paramOffset);
|
|
118
|
+ tokens = new String[firstTokens.length + 1];
|
|
119
|
+ System.arraycopy(firstTokens, 0, tokens, 0, firstTokens.length);
|
|
120
|
+ tokens[firstTokens.length] = lastPart;
|
|
121
|
+ } else {
|
|
122
|
+ tokens = firstTokens;
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ return new ReadLine(new String(line, 0, length), tokens);
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ /**
|
|
129
|
+ * Determines the 'source' of a line made up of the specified tokens. A
|
|
130
|
+ * source is described by the first token if and only if that token starts
|
|
131
|
+ * with a colon.
|
|
132
|
+ *
|
|
133
|
+ * @param tokens The tokens to extract a source from
|
|
134
|
+ * @return The relevant source or null if none specified
|
|
135
|
+ */
|
|
136
|
+ private String getSource(final String[] tokens) {
|
|
137
|
+ if (tokens.length > 0 && tokens[0].length() > 1 && tokens[0].charAt(0) == ':') {
|
|
138
|
+ return tokens[0].substring(1);
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ return null;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ /**
|
|
145
|
+ * Determines the 'destination' of a line made up of the specified tokens.
|
|
146
|
+ * A destination exists only if a source exists
|
|
147
|
+ * (see {@link #getSource(java.lang.String[])}), and is contained within
|
|
148
|
+ * the third argument for non-numeric lines, and fourth for numerics.
|
|
149
|
+ *
|
|
150
|
+ * @param tokens The tokens to extract a destination from
|
|
151
|
+ * @return The relevant destination or null if none specified
|
|
152
|
+ */
|
|
153
|
+ private String getDestination(final String[] tokens) {
|
|
154
|
+ if (tokens.length > 0 && tokens[0].length() >= 3 && tokens[0].charAt(0) == ':') {
|
|
155
|
+ final int target = tokens[1].matches("^[0-9]+$") ? 3 : 2;
|
|
156
|
+
|
|
157
|
+ if (tokens.length > target) {
|
|
158
|
+ return tokens[target];
|
|
159
|
+ }
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ return null;
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ /** {@inheritDoc} */
|
|
166
|
+ @Override
|
|
167
|
+ public void close() throws IOException {
|
|
168
|
+ stream.close();
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ /**
|
|
172
|
+ * Represents a line that has been read from the IRC server and encoded
|
|
173
|
+ * appropriately.
|
|
174
|
+ */
|
|
175
|
+ public static class ReadLine {
|
|
176
|
+
|
|
177
|
+ /** A representation of the read-line using a default encoding. */
|
|
178
|
+ private final String line;
|
|
179
|
+ /** The tokens found in the line, individually encoded as appropriate. */
|
|
180
|
+ private final String[] tokens;
|
|
181
|
+
|
|
182
|
+ /**
|
|
183
|
+ * Creates a new instance of {@link ReadLine} with the specified line
|
|
184
|
+ * and tokens.
|
|
185
|
+ *
|
|
186
|
+ * @param line A string representation of the line
|
|
187
|
+ * @param tokens The tokens which make up the line
|
|
188
|
+ */
|
|
189
|
+ public ReadLine(final String line, final String[] tokens) {
|
|
190
|
+ this.line = line;
|
|
191
|
+ this.tokens = tokens;
|
|
192
|
+ }
|
|
193
|
+
|
|
194
|
+ /**
|
|
195
|
+ * Retrieves a string representation of the line that has been read.
|
|
196
|
+ * This may be encoded using a charset which is not appropriate for
|
|
197
|
+ * displaying all of the line's contents, and is intended for debug
|
|
198
|
+ * purposes only.
|
|
199
|
+ *
|
|
200
|
+ * @return A string representation of the line
|
|
201
|
+ */
|
|
202
|
+ public String getLine() {
|
|
203
|
+ return line;
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ /**
|
|
207
|
+ * Retrieves an array of tokens extracted from the specified line.
|
|
208
|
+ * Each token may have a different encoding.
|
|
209
|
+ *
|
|
210
|
+ * @return The line's tokens
|
|
211
|
+ */
|
|
212
|
+ public String[] getTokens() {
|
|
213
|
+ return tokens;
|
|
214
|
+ }
|
|
215
|
+
|
|
216
|
+ }
|
|
217
|
+
|
|
218
|
+}
|