1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.dexterim.msn.io;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23
24 import net.sf.dexterim.msn.message.AddToListMessage;
25 import net.sf.dexterim.msn.message.AnswerMessage;
26 import net.sf.dexterim.msn.message.BuddyPhoneMessage;
27 import net.sf.dexterim.msn.message.CallMessage;
28 import net.sf.dexterim.msn.message.ChallengeMessage;
29 import net.sf.dexterim.msn.message.ChangeScreenNameMessage;
30 import net.sf.dexterim.msn.message.ChangeStatusMessage;
31 import net.sf.dexterim.msn.message.ContactListMessage;
32 import net.sf.dexterim.msn.message.InviteMessage;
33 import net.sf.dexterim.msn.message.JoinMessage;
34 import net.sf.dexterim.msn.message.JoiningMessage;
35 import net.sf.dexterim.msn.message.MessageFormatException;
36 import net.sf.dexterim.msn.message.MimeMessageFactory;
37 import net.sf.dexterim.msn.message.MsnMessage;
38 import net.sf.dexterim.msn.message.OfflineMessage;
39 import net.sf.dexterim.msn.message.OnlineMessage;
40 import net.sf.dexterim.msn.message.PersonalInformationMessage;
41 import net.sf.dexterim.msn.message.ReferMessage;
42 import net.sf.dexterim.msn.message.RemoveFromListMessage;
43 import net.sf.dexterim.msn.message.USRMessage;
44 import net.sf.dexterim.msn.message.UnknownMessage;
45 import net.sf.dexterim.msn.message.UserLeftMessage;
46
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49
50
51 /***
52 *@author Christoph Walcher
53 */
54 public class BufferedMsnMessageReader extends BufferedReader {
55
56 private static final Log log = LogFactory.getLog(BufferedMsnMessageReader.class);
57
58 private MsnMessage workingMessage;
59
60 private boolean closed;
61
62 /***
63 * Description of the Field
64 */
65 public final static String MAGIC_STRING = "Q1P7W2E4J9R8U3S5";
66
67 /***
68 * Description of the Field
69 */
70 public final static String CMD_RING = "RNG";
71
72 /***
73 * Description of the Field
74 */
75 public final static String CMD_REFER = "XFR";
76
77 /***
78 * Description of the Field
79 */
80 public final static String CMD_CHECK = "CHL";
81
82 /***
83 * Description of the Field
84 */
85 public final static String CMD_OFFLINE = "FLN";
86
87 /***
88 * Description of the Field
89 */
90 public final static String CMD_ONLINE = "NLN";
91
92 /***
93 * Description of the Field
94 */
95 public final static String CMD_STATUS_CHANGE = "ILN";
96
97 /***
98 * Description of the Field
99 */
100 public final static String CMD_LIST = "LST";
101
102 /***
103 * Description of the Field
104 */
105 public final static String CMD_USER_MESSAGE = "MSG";
106
107 /***
108 * Description of the Field
109 */
110 public final static String CMD_USER_TYPING = "TypingUser";
111
112 /***
113 * Description of the Field
114 */
115 public final static String CMD_USER_JOINED = "JOI";
116
117 /***
118 * Description of the Field
119 */
120 public final static String CMD_USER_JOINING = "IRO";
121
122 /***
123 * Description of the Field
124 */
125 public final static String CMD_USER_LEFT = "BYE";
126
127 /***
128 * Description of the Field
129 */
130 public final static String CMD_PERSONAL_PHONE_NUMBER = "PRP";
131
132 /***
133 * Description of the Field
134 */
135 public final static String CMD_BUDDY_PHONE_NUMBER = "BPR";
136
137 /***
138 * Description of the Field
139 */
140 public final static String CMD_REMOVE_FROM_LIST = "REM";
141
142 /***
143 * Description of the Field
144 */
145 public final static String CMD_ADD_TO_LIST = "ADD";
146
147 /***
148 * Description of the Field
149 */
150 public final static String CMD_CHANGE_SCREEN_NAME = "REA";
151
152 /***
153 * Description of the Field
154 */
155 public final static String CMD_HOTMAIL_PROFILE = "MSG";
156 public final static String CMD_USR_MESSAGE = "USR";
157 public final static String CMD_CALL_MESSAGE = "CAL";
158 public final static String CMD_ANSWER_MESSAGE = "ANS";
159
160 /***
161 * Creates a new instance of BufferednMsnMessageReader
162 *
163 *@param in Description of the Parameter
164 */
165 public BufferedMsnMessageReader(java.io.Reader in) {
166 super(in);
167 closed = false;
168
169 workingMessage = null;
170 }
171
172 /***
173 * Description of the Method
174 *
175 *@return Description of the Return Value
176 *@exception MessageFormatException Description of the Exception
177 */
178 public MsnMessage readMesage() throws MessageFormatException {
179 while (true) {
180 try {
181 if (workingMessage == null) {
182 String stringMessage = readLine();
183
184 workingMessage = createWorkMessage(stringMessage);
185
186 if (workingMessage == null) {
187 return null;
188 }
189
190 workingMessage = workingMessage.addLine(stringMessage);
191 }
192 else if (workingMessage.getRequiredByteInput() < 0) {
193 String stringMessage = readLine();
194
195 if (stringMessage == null) {
196 return null;
197 }
198
199 workingMessage = workingMessage.addLine(stringMessage);
200 }
201 else {
202 char[] charArray = new char[workingMessage.getRequiredByteInput()];
203
204 read(charArray);
205
206 workingMessage = workingMessage.addCharBlock(charArray);
207 }
208
209 if ((workingMessage != null) && workingMessage.isCompleted()) {
210 net.sf.dexterim.msn.message.MsnMessage tmpMessage = workingMessage;
211
212 workingMessage = null;
213
214 return tmpMessage;
215 }
216 }
217 catch (java.net.SocketTimeoutException stex) {
218 if (log.isDebugEnabled()) {
219 log.debug("SocketTimeout - continuing");
220 }
221 }
222 catch (java.io.IOException ex) {
223 if (log.isDebugEnabled()) {
224 log.debug("Exception thrown while reading from msn Network", ex);
225 }
226
227 return null;
228 }
229 }
230 }
231
232 /***
233 * Description of the Method
234 *
235 *@param messageString Description of the Parameter
236 *@return Description of the Return Value
237 */
238 protected MsnMessage createWorkMessage(String messageString) {
239 if (messageString == null) {
240 return null;
241 }
242
243 if (messageString.startsWith(CMD_REFER)) {
244 return new ReferMessage();
245 }
246 else if (messageString.startsWith(CMD_RING)) {
247 return new InviteMessage();
248 }
249 else if (messageString.startsWith(CMD_CHECK)) {
250 return new ChallengeMessage();
251 }
252 else if (messageString.startsWith(CMD_OFFLINE)) {
253 return new OfflineMessage();
254 }
255 else if (messageString.startsWith(CMD_ONLINE)) {
256 return new OnlineMessage();
257 }
258 else if (messageString.startsWith(CMD_STATUS_CHANGE)) {
259 return new ChangeStatusMessage();
260 }
261 else if (messageString.startsWith(CMD_PERSONAL_PHONE_NUMBER)) {
262 return new PersonalInformationMessage();
263 }
264 else if (messageString.startsWith(CMD_BUDDY_PHONE_NUMBER)) {
265 return new BuddyPhoneMessage();
266 }
267 else if (messageString.startsWith(CMD_REMOVE_FROM_LIST)) {
268 return new RemoveFromListMessage();
269 }
270 else if (messageString.startsWith(CMD_ADD_TO_LIST)) {
271 return new AddToListMessage();
272 }
273 else if (messageString.startsWith(CMD_CHANGE_SCREEN_NAME)) {
274 return new ChangeScreenNameMessage();
275 }
276 else if (messageString.startsWith(CMD_HOTMAIL_PROFILE)) {
277 return new MimeMessageFactory();
278 }
279 else if (messageString.startsWith(CMD_USER_MESSAGE)) {
280 return new MimeMessageFactory();
281 }
282 else if (messageString.startsWith(CMD_USER_JOINED)) {
283 return new JoinMessage();
284 }
285 else if (messageString.startsWith(CMD_USER_JOINING)) {
286 return new JoiningMessage();
287 }
288 else if (messageString.startsWith(CMD_USER_LEFT)) {
289 return new UserLeftMessage();
290 }
291 else if (messageString.startsWith(CMD_LIST)) {
292 return new ContactListMessage();
293 }
294 else if (messageString.startsWith(CMD_USR_MESSAGE)) {
295 return new USRMessage();
296 }
297 else if (messageString.startsWith(CMD_CALL_MESSAGE)) {
298 return new CallMessage();
299 }
300 else if (messageString.startsWith(CMD_ANSWER_MESSAGE)) {
301 return new AnswerMessage();
302 }
303 else {
304 return new UnknownMessage();
305 }
306 }
307
308 public void close() throws IOException {
309 super.close();
310
311 this.closed = true;
312 }
313
314 public boolean isClosed() {
315 return closed;
316 }
317 }