summaryrefslogtreecommitdiff
path: root/src/Client/ParsedLine.java
blob: ebc45b15e7bfb53f8e33f0a5afebdec29f3fa7a8 (plain)
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
package Client;
import java.lang.NumberFormatException;
import java.util.Hashtable;

public class ParsedLine
{
	private String line;
	private Hashtable <String, String> dict;
	
	/** constructor, returning dictionary. 
	 * @param text line	
	 * for example input:  ":sweetmorn.cohens.org.il 352 tzafrir #chat-dor ~tzafrir localhost"
	 * output: a dictionary with the following entries:
	 * - server: sweetmorn.cohens.org.il
	 * - command: 352
	 * - nick: tzafrir
	 * - message:  #chat-dor ~tzafrir localhost
	 */
	public ParsedLine(String line)
	{
		this.line = line;
		this.dict = new Hashtable <String, String>();
		
		String[] parsed = this.line.split("[ \t]", 3);
		
		int messageCode = -1;
		try {
			messageCode = Integer.getInteger(parsed[1], 10);
		} catch (NumberFormatException e) {};
		
		if (parsed[1].compareTo("PRIVMSG") == 0)
		{
			dict.put("user", RemoveFirst(parsed[0]));
			String[] parsedUser = dict.get("user").split("!", 2);
			dict.put("nick", parsedUser[0]);
			
			dict.put("command", parsed[1]);
			
			String[] parsedArgs = parsed[2].split("[ \t]", 2);
			dict.put("channel", parsedArgs[0]);
			dict.put("message",RemoveFirst(parsedArgs[1]));
		}
		else if (parsed[1].compareTo("NICK") == 0)
		{
			dict.put("user", RemoveFirst(parsed[0]));
			String[] parsedUser = dict.get("user").split("!", 2);
			dict.put("nick", parsedUser[0]);

			dict.put("command", parsed[1]);
			
			dict.put("newnick", RemoveFirst(parsed[2]));
		}
		
		else if (parsed[1].compareTo("JOIN") == 0)
		{
			dict.put("user", RemoveFirst(parsed[0]));
			String[] parsedUser = dict.get("user").split("!", 2);
			dict.put("nick", parsedUser[0]);

			dict.put("command", parsed[1]);
			
			dict.put("channel", parsed[2]);
		}
		
		else if (parsed[0].compareTo("PING") == 0)
		{
			dict.put("server", RemoveFirst(parsed[1]));
			dict.put("command", parsed[0]);
		}
		else if (messageCode > 0) {
			dict.put("server", RemoveFirst(parsed[0]));
			dict.put("command", parsed[1]);
			String[] parsedArgs = parsed[2].split("[ \t]", 2);
			dict.put("nick", parsedArgs[0]);
			dict.put("message", parsedArgs[1]); // FIXME: remove ":". starts at various places for various codes
		}
		else
			System.err.println("Notice: failed to parse message message: [" + line + "]");
	}
	
	/** getting the results from the dictionary
	@param property	 */
	public String get(String property)
	{
		return dict.get(property);
	}
	
	/** Remove the first char from the input (this char is irelevant). TODO: move to Common.*/
	public static String RemoveFirst(String st)
	{
		return st.substring(1 , st.length());
	}
}