1 /*****************************************************/
2 /* This java file is a part of the */
4 /* - Plouf's Java IRC Client - */
6 /* Copyright (C) 2002 - 2005 Philippe Detournay */
8 /* All contacts : theplouf@yahoo.com */
10 /* PJIRC is free software; you can redistribute */
11 /* it and/or modify it under the terms of the GNU */
12 /* General Public License as published by the */
13 /* Free Software Foundation; version 2 or later of */
16 /* PJIRC is distributed in the hope that it will */
17 /* be useful, but WITHOUT ANY WARRANTY; without */
18 /* even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
20 /* General Public License for more details. */
22 /* You should have received a copy of the GNU */
23 /* General Public License along with PJIRC; if */
24 /* not, write to the Free Software Foundation, */
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
26 /* MA 02111-1307 USA */
28 /*****************************************************/
32 import java.io.BufferedInputStream;
33 import java.io.BufferedOutputStream;
34 import java.net.Socket;
35 import java.util.Vector;
38 * The raw irc server protocol handler.
40 public class ServerProtocol extends IRCObject implements Runnable {
41 private ListenerGroup _listeners;
44 private Socket _socket;
45 private CodingHandler _handler;
46 private Thread _thread;
47 private boolean _connected;
48 private boolean _connecting;
51 * Create a new ServerProtocol.
54 * global irc configuration.
56 public ServerProtocol(IRCConfiguration config) {
60 _listeners = new ListenerGroup();
64 * Connect to the server.
71 public void connect(String host, int port) {
77 _thread = new Thread(this, "Read thread");
82 * Return connected status.
84 * @return true if connected, false otherwise.
86 public boolean connected() {
91 * Return connecting status.
93 * @return true if connecting, false otherwise.
95 public boolean connecting() {
100 * Disconnect from server.
102 public synchronized void disconnect() {
110 } catch (Exception e) {
111 // System.out.println("disconnection");
112 // System.out.println(e);
115 _listeners.sendEvent("disconnected", _host);
119 * Get the local, client-side tcp port for the current connection.
121 * @return the local port.
123 public int getLocalPort() {
124 return _socket.getLocalPort();
127 private void decodeLine(String line) {
128 Vector res = new Vector();
129 while (line.length() != 0) {
130 if (line.startsWith(":") && (res.size() != 0)) {
131 res.insertElementAt(line.substring(1), res.size());
134 int pos = line.indexOf(' ');
136 res.insertElementAt(StringParser.trim(line), res.size());
139 String part = StringParser.trim(line.substring(0, pos));
140 line = StringParser.trim(line.substring(pos + 1));
141 res.insertElementAt(part, res.size());
149 if (((String) (res.elementAt(0))).startsWith(":")) {
150 source = (String) res.elementAt(0);
151 source = source.substring(1);
152 res.removeElementAt(0);
157 String cmd = (String) res.elementAt(0);
158 res.removeElementAt(0);
160 String[] param = new String[res.size()];
161 for (int i = 0; i < res.size(); i++)
162 param[i] = (String) res.elementAt(i);
164 if ((cmd.charAt(0) >= '0') && (cmd.charAt(0) <= '9')) {
166 _listeners.sendEventAsync("replyReceived", source, cmd, param);
169 _listeners.sendEventAsync("messageReceived", source, cmd, param);
176 _socket = _ircConfiguration.getSecurityProvider().getSocket(_host, _port);
178 _handler = new CodingHandler(_ircConfiguration, new BufferedInputStream(_socket.getInputStream()),
179 new BufferedOutputStream(_socket.getOutputStream()));
182 _listeners.sendEventAsync("connected", _host);
183 } catch (Exception e) {
184 // e.printStackTrace();
187 if (e.getMessage() != null)
188 _listeners.sendEventAsync("connectionFailed", e.getMessage(), _host);
190 _listeners.sendEventAsync("connectionFailed", e.getClass().getName(), _host);
194 boolean terminated = false;
195 while (!terminated) {
197 String line = _handler.read();
199 throw new Exception();
200 // System.out.println("--> "+line);
204 } catch (Exception e) {
205 _ircConfiguration.internalError("internal error", e, "plouf@pjirc.com");
207 } catch (Exception e) {
211 EventDispatcher.dispatchEventAsync(this, "disconnect", new Object[0]);
220 public void addServerProtocolListener(ServerProtocolListener lis) {
221 _listeners.addListener(lis);
228 * listener to remove.
230 public void removeServerProtocolListener(ServerProtocolListener lis) {
231 _listeners.removeListener(lis);
235 * Send a raw string to the server.
240 * if something went wrong.
242 public void sendString(String str) throws Exception {
244 throw new Exception(getText(IRCTextProvider.SERVER_NOT_CONNECTED));
246 // System.out.println("<-- "+str);
250 * Send a formatted string to the server.
255 * parameters list. Only the last parameter can contain blanks.
257 * if something went wrong.
259 public void sendCommand(String command, String params[]) throws Exception {
260 String toSend = command;
262 for (int i = 0; i < params.length; i++) {
264 if (params[i].indexOf(' ') != -1)