Browse Source

Started adding new interfaces

git-svn-id: http://svn.dmdirc.com/trunk@4 00569f92-eb28-0410-84fd-f71c24880f
tags/0.1
Chris Smith 17 years ago
parent
commit
8354882748
1 changed files with 200 additions and 0 deletions
  1. 200
    0
      src/dmdirc/parser/IRCParser.java

+ 200
- 0
src/dmdirc/parser/IRCParser.java View File

@@ -0,0 +1,200 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack
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 dmdirc.parser;
24
+
25
+import java.io.*;
26
+import java.net.*;
27
+
28
+interface IDataInEvent { public void onDataIn(IRCParser parser, String data); }
29
+interface IDataOutEvent { public void onDataOut(IRCParser parser, String data); }
30
+
31
+public class IRCParser implements Runnable {
32
+
33
+	class MyInfo {
34
+		String sNickname = "IRCParser";
35
+		String sRealname = "Java Test IRCParser";
36
+		String sUsername = "IRCParser";
37
+	}
38
+
39
+	private Socket socket = null;
40
+	private PrintWriter out = null;
41
+	private BufferedReader in = null;
42
+
43
+	public MyInfo me = new MyInfo();
44
+
45
+	private boolean HasBegan = false;
46
+	private boolean IsFirst = true;
47
+
48
+	// Events
49
+	public interface GeneralIRCEvent { public void GeneralEvent(String sName); }
50
+	public interface LogEvent { public void LogEvent(String sName, String sData); }
51
+	class AllEvents {
52
+		GeneralIRCEvent EndOfMOTD = null;
53
+		LogEvent DataIn = null;
54
+		LogEvent DataOut = null;
55
+
56
+		String sEndOfMOTD = "";
57
+		String sDataIn = "";
58
+		String sDataOut = "";
59
+	}
60
+	public AllEvents cb = new AllEvents();
61
+
62
+	public void SetCallback(String sType, Object eMethod) throws Exception { try { SetCallback(sType,sType,eMethod); } catch (Exception e) { throw e; } }
63
+	public void SetCallback(String sType, String sName, Object eMethod) throws Exception {
64
+		if (sName.equals("")) { sName = sType; }
65
+		sType = sType.toLowerCase();
66
+		if (sType.equals("endofmotd")) { cb.EndOfMOTD = (GeneralIRCEvent)eMethod; cb.sEndOfMOTD = sName; }
67
+		else if (sType.equals("datain")) { cb.DataIn = (LogEvent)eMethod; cb.sDataIn = sName; }
68
+		else if (sType.equals("dataout")) { cb.DataOut = (LogEvent)eMethod; cb.sDataOut = sName; }
69
+		else { throw new Exception("No such callback '"+sType+"'");}
70
+	}
71
+
72
+	// Constructor.
73
+	IRCParser () {}
74
+
75
+	public void connect(String sHost) throws Exception {
76
+		try {
77
+			connect(sHost,6667);		
78
+		} catch (Exception e) {
79
+			throw e;
80
+		}
81
+	}
82
+
83
+	public void connect(String sHost, int nPort) throws Exception {
84
+		if (HasBegan) { return; }
85
+		try {
86
+			socket = new Socket(sHost,nPort);
87
+			out = new PrintWriter(socket.getOutputStream(), true);
88
+			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
89
+		} catch (UnknownHostException e) {
90
+			throw new Exception("Socket Exception");
91
+		} catch (Exception e) {
92
+			throw new Exception("General Exception");
93
+		}
94
+	}
95
+
96
+	public void run() {
97
+		if (HasBegan) { return; } else { HasBegan = true; }
98
+		// :HACK: While true loops really really suck.
99
+		while(true){
100
+			String line = "";
101
+			try {
102
+				line = in.readLine(); // Blocking :/
103
+				if (IsFirst) {
104
+					SendString("NICK "+me.sNickname);
105
+					SendString("USER "+me.sUsername.toLowerCase()+" * * :"+me.sRealname);
106
+					IsFirst = false;
107
+				}
108
+		        ProcessLine(line);
109
+			} catch (IOException e) {
110
+				System.out.println("Socket read failed");
111
+				System.exit(-1);
112
+			}
113
+		}
114
+	}
115
+
116
+	protected void finalize(){
117
+		try{
118
+			socket.close();
119
+		} catch (IOException e) {
120
+			System.out.println("Could not close socket");
121
+			System.exit(-1);
122
+		}
123
+	}
124
+
125
+	private String GetParam(String line) {
126
+		String[] params = null;
127
+		params = line.split(" :",2);
128
+		return params[params.length-1];
129
+	}
130
+
131
+	private String[] IRCTokenise(String line) {
132
+		String[] params = null;
133
+		String[] tokens = null;
134
+		params = line.split(" :",2);
135
+		tokens = params[0].split(" ");
136
+	
137
+		String[] temp = new String[tokens.length+1];
138
+		System.arraycopy(tokens, 0, temp, 0, tokens.length);
139
+		tokens = temp;
140
+		if (params.length == 2) { tokens[tokens.length-1] = params[1]; }
141
+
142
+	    return tokens;
143
+	}
144
+
145
+	public void SendLine(String line) {SendString(line);} // This should do some checks on stuff possible, public event!
146
+	
147
+	// Our Method
148
+	private void SendString(String line) {
149
+		if (!cb.sDataOut.equals("")) {cb.DataOut.LogEvent(cb.sDataOut,line);} // Ugly++
150
+		out.printf("%s\r\n",line);
151
+	}
152
+
153
+	private void ProcessLine(String line) {
154
+		String[] token = IRCTokenise(line);
155
+		String sParam = token[token.length-1];
156
+
157
+		int nParam;
158
+		if (!cb.sDataIn.equals("")) {cb.DataIn.LogEvent(cb.sDataIn,line);} // Ugly++
159
+
160
+		try {nParam = Integer.parseInt(token[1]);} catch (Exception e) { nParam = -1;}
161
+
162
+		if (token[0].equals("PING") || token[1].equals("PING")) { SendString("PONG :"+sParam); }
163
+		else {
164
+			if (token[0].substring(0,1).equals(":")) {
165
+				// Post Connect
166
+				switch (nParam) {
167
+					case -1:
168
+						ProcessStringParam(sParam,token);
169
+						break;
170
+					case 1: // 001
171
+						break;
172
+					case 422: // No MOTD
173
+					case 376: // End of MOTD
174
+						ProcessEndOfMOTD(sParam,token);
175
+						break;
176
+					default: // Unknown
177
+						break;
178
+				}
179
+			} else {
180
+				// Pre Connect
181
+			}
182
+		}
183
+	}
184
+
185
+	private void ProcessStringParam(String sParam,String token[]) {
186
+		// Process a line where the parameter is a string (IE PRIVMSG, NOTICE etc - Not including PING!)
187
+	}
188
+
189
+	private void ProcessEndOfMOTD(String sParam,String token[]) {
190
+		// Process EndOfMOTD
191
+		if (!cb.sEndOfMOTD.equals("")) {cb.EndOfMOTD.GeneralEvent(cb.sEndOfMOTD);} // Ugly++
192
+	}
193
+
194
+
195
+
196
+	//-------------------------------------------------------------------------
197
+	public void JoinChannel(String sChannelName) {
198
+		SendLine("JOIN "+sChannelName);
199
+	}
200
+}

Loading…
Cancel
Save