소스 검색

Added altNickname to myinfo

Added default NickInUse handler if one isn't specified
Added Quit,Disconnect and SetNickname methods
Other misc stuff i think.


git-svn-id: http://svn.dmdirc.com/trunk@27 00569f92-eb28-0410-84fd-f71c24880f
tags/0.1
Shane Mc Cormack 17 년 전
부모
커밋
e45b7d9297
1개의 변경된 파일77개의 추가작업 그리고 9개의 파일을 삭제
  1. 77
    9
      src/dmdirc/parser/IRCParser.java

+ 77
- 9
src/dmdirc/parser/IRCParser.java 파일 보기

@@ -28,8 +28,12 @@ import java.util.ArrayList;
28 28
 
29 29
 public class IRCParser implements Runnable {
30 30
 
31
+	public static final int ndInfo = 1;
32
+//	public static final int ndSomething = 2;
33
+
31 34
 	public class MyInfo {
32 35
 		public String sNickname = "IRCParser";
36
+		public String sAltNickname = "IRC-Parser"; // Alternative nickname, if this fails, we start prepending _ to sNickname
33 37
 		public String sRealname = "Java Test IRCParser";
34 38
 		public String sUsername = "IRCParser";
35 39
 	}
@@ -37,18 +41,26 @@ public class IRCParser implements Runnable {
37 41
 	private Socket socket = null;
38 42
 	private PrintWriter out = null;
39 43
 	private BufferedReader in = null;
44
+	
45
+	public MyInfo me = new MyInfo(); // This is what the user wants, nickname here is *not* fact.
40 46
 
41
-	public MyInfo me = new MyInfo();
47
+	private String sThinkNickname; // This is what we want the nickname to be.
48
+	private String sConfirmedNickname; // This is what the nickname actually is.
49
+	private boolean TriedAlt = false;
42 50
 
51
+	private boolean Got001 = false;
52
+	private boolean Got005 = false;
43 53
 	private boolean HasBegan = false;
44 54
 	private boolean IsFirst = true;
45 55
 
46 56
 	// Events
57
+	public interface IDebugInfo { public void onDebug(IRCParser tParser, int nLevel, String sData); }
47 58
 	public interface IMOTDEnd { public void onMOTDEnd(IRCParser tParser); }
48 59
 	public interface IDataIn { public void onDataIn(IRCParser tParser, String sData); }
49 60
 	public interface IDataOut { public void onDataOut(IRCParser tParser, String sData, boolean FromParser); }
50 61
 	public interface INickInUse { public void onNickInUse(IRCParser tParser); }
51 62
 	class AllEvents {
63
+		ArrayList<IDebugInfo> DebugInfo = new ArrayList<IDebugInfo>();
52 64
 		ArrayList<IMOTDEnd> EndOfMOTD = new ArrayList<IMOTDEnd>();
53 65
 		ArrayList<IDataIn> DataIn = new ArrayList<IDataIn>();
54 66
 		ArrayList<IDataOut> DataOut = new ArrayList<IDataOut>();
@@ -68,43 +80,74 @@ public class IRCParser implements Runnable {
68 80
 		}
69 81
 	}
70 82
 
83
+	public void AddDebugInfo(Object eMethod) { AddCallback(eMethod, cb.DebugInfo); }
84
+	public void DelDebugInfo(Object eMethod) { DelCallback(eMethod, cb.DebugInfo); }
85
+	private boolean CallDebugInfo(int level, String data) { 
86
+		boolean bResult = false;
87
+		for (int i = 0; i < cb.DebugInfo.size(); i++) {
88
+			cb.DebugInfo.get(i).onDebug(this, level, data);
89
+			bResult = true;
90
+		}
91
+		return bResult;
92
+	}
93
+
71 94
 	public void AddMOTDEnd(Object eMethod) { AddCallback(eMethod, cb.EndOfMOTD); }
72 95
 	public void DelMOTDEnd(Object eMethod) { DelCallback(eMethod, cb.EndOfMOTD); }
73
-	private void CallMOTDEnd() { 
96
+	private boolean CallMOTDEnd() { 
97
+		boolean bResult = false;
74 98
 		for (int i = 0; i < cb.EndOfMOTD.size(); i++) {
75 99
 			cb.EndOfMOTD.get(i).onMOTDEnd(this);
100
+			bResult = true;
76 101
 		}
102
+		return bResult;
77 103
 	}
78 104
 
79 105
 	public void AddDataIn(Object eMethod) { AddCallback((IDataIn)eMethod, cb.DataIn); }
80 106
 	public void DelDataIn(Object eMethod) { DelCallback((IDataIn)eMethod, cb.DataIn); }
81
-	private void CallDataIn(String data) { 
107
+	private boolean CallDataIn(String data) { 
108
+		boolean bResult = false;
82 109
 		for (int i = 0; i < cb.DataIn.size(); i++) {
83 110
 			cb.DataIn.get(i).onDataIn(this, data);
111
+			bResult = true;
84 112
 		}
113
+		return bResult;
85 114
 	}
86 115
 
87 116
 	public void AddDataOut(Object eMethod) { AddCallback((IDataOut)eMethod, cb.DataOut); }
88 117
 	public void DelDataOut(Object eMethod) { DelCallback((IDataOut)eMethod, cb.DataOut); }
89
-	private void CallDataOut(String data, boolean FromParser) { 
118
+	private boolean CallDataOut(String data, boolean FromParser) { 
119
+		boolean bResult = false;
90 120
 		for (int i = 0; i < cb.DataOut.size(); i++) {
91 121
 			cb.DataOut.get(i).onDataOut(this, data, FromParser);
122
+			bResult = true;
92 123
 		}
124
+		return bResult;
93 125
 	}
94 126
 
95 127
 	public void AddNickInUse(Object eMethod) { AddCallback(eMethod, cb.NickInUse); }
96 128
 	public void DelNickInUse(Object eMethod) { DelCallback(eMethod, cb.NickInUse); }
97
-	private void CallNickInUse() { 
129
+	private boolean CallNickInUse() { 
130
+		boolean bResult = false;
98 131
 		for (int i = 0; i < cb.NickInUse.size(); i++) {
99 132
 			cb.NickInUse.get(i).onNickInUse(this);
133
+			bResult = true;
100 134
 		}
135
+		return bResult;
101 136
 	}
102 137
 
103 138
 	// Constructor.
104 139
 	public IRCParser () { }
105 140
 
141
+
142
+	private void ResetState() {
143
+			TriedAlt = false;
144
+			Got001 = false;
145
+			Got005 = false;
146
+	};
147
+
106 148
 	public void connect(String sHost) throws Exception {
107 149
 		try {
150
+			ResetState();
108 151
 			connect(sHost,6667);
109 152
 		} catch (Exception e) {
110 153
 			throw e;
@@ -132,7 +175,7 @@ public class IRCParser implements Runnable {
132 175
 			try {
133 176
 				line = in.readLine(); // Blocking :/
134 177
 				if (IsFirst) {
135
-					SendString("NICK "+me.sNickname);
178
+					SetNickname(me.sNickname);
136 179
 					SendString("USER "+me.sUsername.toLowerCase()+" * * :"+me.sRealname);
137 180
 					IsFirst = false;
138 181
 				}
@@ -220,12 +263,37 @@ public class IRCParser implements Runnable {
220 263
 	}
221 264
 
222 265
 	private void ProcessEndOfMOTD(String sParam,String token[]) { CallMOTDEnd(); }
223
-	private void ProcessNickInUse(String sParam,String token[]) { CallNickInUse(); }
224 266
 
267
+	private void ProcessNickInUse(String sParam,String token[]) {
268
+		if (!CallNickInUse()) {
269
+			// Manually handle nick in use.
270
+			CallDebugInfo(ndInfo,"No Nick in use Handler.");
271
+			if (!Got001) {
272
+				CallDebugInfo(ndInfo,"Using inbuilt handler");
273
+				// If this is before 001 we will try and get a nickname, else we will leave the nick as-is
274
+				if (!TriedAlt) { SetNickname(me.sAltNickname); TriedAlt = true; }
275
+				else {
276
+					if (sThinkNickname.equalsIgnoreCase(me.sAltNickname)) { sThinkNickname = me.sNickname; }
277
+					SetNickname('_'+sThinkNickname);	
278
+				}
279
+			}
280
+		}
281
+	}
225 282
 
226 283
 
227
-	//-------------------------------------------------------------------------
228 284
 	public void JoinChannel(String sChannelName) {
229
-		SendLine("JOIN "+sChannelName);
285
+		SendString("JOIN "+sChannelName);
230 286
 	}
287
+
288
+	public void SetNickname(String sNewNickName) {
289
+		sThinkNickname = sNewNickName;
290
+		SendString("NICK "+sNewNickName);
291
+	}
292
+
293
+	public void Quit(String sReason) { SendString("QUIT :"+sReason); }
294
+	// Quit and force Disconnection
295
+	public void Disconnect(String sReason) {
296
+		Quit(sReason);
297
+		try { socket.close(); } catch (Exception e) { /* Meh */ };
298
+	}	
231 299
 }

Loading…
취소
저장