1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2005 Philippe Detournay */
\r
8 /* All contacts : theplouf@yahoo.com */
\r
10 /* PJIRC is free software; you can redistribute */
\r
11 /* it and/or modify it under the terms of the GNU */
\r
12 /* General Public License as published by the */
\r
13 /* Free Software Foundation; version 2 or later of */
\r
16 /* PJIRC is distributed in the hope that it will */
\r
17 /* be useful, but WITHOUT ANY WARRANTY; without */
\r
18 /* even the implied warranty of MERCHANTABILITY or */
\r
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
\r
20 /* General Public License for more details. */
\r
22 /* You should have received a copy of the GNU */
\r
23 /* General Public License along with PJIRC; if */
\r
24 /* not, write to the Free Software Foundation, */
\r
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
\r
26 /* MA 02111-1307 USA */
\r
28 /*****************************************************/
\r
30 package irc.ident.prv;
\r
32 import irc.IRCConfiguration;
\r
33 import irc.IRCObject;
\r
34 import irc.IRCTextProvider;
\r
35 import irc.ListenerGroup;
\r
36 import irc.ident.IdentListener;
\r
38 import java.io.BufferedReader;
\r
39 import java.io.BufferedWriter;
\r
40 import java.io.InputStreamReader;
\r
41 import java.io.OutputStreamWriter;
\r
42 import java.net.ServerSocket;
\r
43 import java.net.Socket;
\r
44 import java.util.Hashtable;
\r
51 * Create a new LocalInfo
\r
60 public LocalInfo(int alocalPort, String asystem, String aid) {
\r
61 localPort = alocalPort;
\r
69 public int localPort;
\r
73 public String system;
\r
81 * The built-in pjirc ident server.
\r
83 public class IdentServer extends IRCObject implements Runnable {
\r
84 private Thread _thread;
\r
85 private boolean _running;
\r
86 private Hashtable _table;
\r
87 private ServerSocket _serverSocket;
\r
88 private boolean _defaultUser;
\r
89 private String _system;
\r
91 private ListenerGroup _listeners;
\r
95 * Create a new IdentServer.
\r
98 * global irc configuration.
\r
100 public IdentServer(IRCConfiguration config) {
\r
102 resetDefaultUser();
\r
103 _table = new Hashtable();
\r
104 _listeners = new ListenerGroup();
\r
109 * Start the ident server.
\r
111 * @throws Exception
\r
113 public void start() throws Exception {
\r
118 * Erase the default user configuration.
\r
120 public void resetDefaultUser() {
\r
121 _defaultUser = false;
\r
125 * Set the default user configuration.
\r
132 public void setDefaultUser(String system, String id) {
\r
133 _defaultUser = true;
\r
139 * Start the ident server on the given port.
\r
142 * ident server port.
\r
143 * @throws Exception
\r
145 public void start(int port) throws Exception {
\r
148 _serverSocket = _ircConfiguration.getSecurityProvider().getServerSocket(_port);
\r
149 _thread = new Thread(this, "IDENT server");
\r
156 * Stop the execution of the ident server.
\r
158 public void stop() {
\r
159 if (_thread == null)
\r
162 _serverSocket.close();
\r
165 } catch (Exception e) {
\r
166 // e.printStackTrace();
\r
171 * Register a new local connection.
\r
174 * the local port of this connection.
\r
176 * user system on this connection.
\r
178 * user id on this connection.
\r
180 public synchronized void registerLocalConnection(int localPort, String system, String id) {
\r
181 _table.put(new Integer(localPort), new LocalInfo(localPort, system, id));
\r
185 * Unregister a local connection.
\r
190 public synchronized void unregisterLocalConnection(int localPort) {
\r
191 _table.remove(new Integer(localPort));
\r
194 private synchronized LocalInfo processRequest(int localPort) {
\r
195 return (LocalInfo) _table.get(new Integer(localPort));
\r
204 public synchronized void addIdentListener(IdentListener lis) {
\r
205 _listeners.addListener(lis);
\r
209 * Remove a listener.
\r
212 * listener to remove.
\r
214 public synchronized void removeIdentListener(IdentListener lis) {
\r
215 _listeners.removeListener(lis);
\r
219 public void run() {
\r
220 boolean terminated = false;
\r
223 _listeners.sendEventAsync("identRunning", new Integer(_port));
\r
224 while (!terminated) {
\r
226 Socket s = _serverSocket.accept();
\r
227 String from = getText(IRCTextProvider.IDENT_UNKNOWN);
\r
228 int result = IdentListener.IDENT_ERROR;
\r
229 String reply = getText(IRCTextProvider.IDENT_NONE);
\r
232 from = _ircConfiguration.getSecurityProvider().resolve(s.getInetAddress());
\r
233 } catch (Exception e) {
\r
234 from = s.getInetAddress().getHostAddress();
\r
236 BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
\r
237 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
\r
238 String request = reader.readLine();
\r
239 int pos = request.indexOf(',');
\r
240 String serverSide = request.substring(0, pos).trim();
\r
241 String clientSide = request.substring(pos + 1).trim();
\r
243 LocalInfo info = processRequest(new Integer(serverSide).intValue());
\r
244 reply = serverSide + " , " + clientSide + " : ";
\r
245 if (info == null) {
\r
246 if (!_defaultUser) {
\r
247 result = IdentListener.IDENT_NOT_FOUND;
\r
248 reply += "ERROR : NO-USER";
\r
250 result = IdentListener.IDENT_DEFAULT;
\r
251 reply += "USERID : " + _system + " : " + _id;
\r
254 result = IdentListener.IDENT_OK;
\r
255 reply += "USERID : " + info.system + " : " + info.id;
\r
258 writer.write(reply + "\n");
\r
263 _listeners.sendEventAsync("identRequested", from, new Integer(result), reply);
\r
265 } catch (Exception e) {
\r
266 _listeners.sendEventAsync("identRequested", from, new Integer(IdentListener.IDENT_ERROR), e.getMessage());
\r
268 } catch (Exception e) {
\r
269 _listeners.sendEventAsync("identLeaving", e.getMessage());
\r