--- /dev/null
+/**\r
+#\r
+# BorkNet Services Core\r
+#\r
+\r
+#\r
+# Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org\r
+#\r
+# This program is free software; you can redistribute it and/or\r
+# modify it under the terms of the GNU General Public License\r
+# as published by the Free Software Foundation; either version 2\r
+# of the License, or (at your option) any later version.\r
+#\r
+# This program is distributed in the hope that it will be useful,\r
+# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+# GNU General Public License for more details.\r
+#\r
+# You should have received a copy of the GNU General Public License\r
+# along with this program; if not, write to the Free Software\r
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+#\r
+*/\r
+\r
+\r
+\r
+/*\r
+This class waits for website connections then creates a thread for each connection\r
+*/\r
+import java.io.*;\r
+import java.net.*;\r
+import java.util.*;\r
+import java.security.*;\r
+import borknet_services.core.*;\r
+\r
+public class Listener extends Thread\r
+{\r
+ private Core C;\r
+ private W Bot;\r
+ private int maxconn;\r
+ private boolean running;\r
+ private ServerSocket listener;\r
+ private String acceptip;\r
+ private WebClient[] clients;\r
+\r
+ public Listener(Core C, W Bot, String bindip, int port, int maxconn, String acceptip)\r
+ {\r
+ this.C = C;\r
+ this.Bot = Bot;\r
+ this.maxconn = maxconn;\r
+ clients=new WebClient[maxconn];\r
+ this.acceptip = acceptip;\r
+ running = true;\r
+ try\r
+ {\r
+ if(bindip.length()>0)\r
+ {\r
+ InetAddress addr = InetAddress.getByName(bindip);\r
+ listener = new ServerSocket(port,0,addr);\r
+ }\r
+ else\r
+ {\r
+ listener = new ServerSocket(port);\r
+ }\r
+ listener.setSoTimeout(1000);\r
+ }\r
+ catch(Exception ex)\r
+ {\r
+ C.printDebug("Error creating webinterface");\r
+ C.debug(ex);\r
+ C.die("Socket error, trying to die gracefully.");\r
+ }\r
+ }\r
+ \r
+ public void run()\r
+ {\r
+ while(running)\r
+ {\r
+ try\r
+ {\r
+ Socket server = listener.accept();\r
+ String ip = server.getInetAddress().getHostAddress();\r
+ if(ip.equals(acceptip))\r
+ {\r
+ Bot.report("Received connection from "+ip);\r
+ cleanupClients();\r
+ WebClient client= new WebClient(C, Bot, server);\r
+ boolean spot=false;\r
+ for(int i=0;i<maxconn;i++)\r
+ {\r
+ if(clients[i]==null)\r
+ {\r
+ clients[i]=client;\r
+ client.myId(i);\r
+ client.start();\r
+ spot=true;\r
+ break;\r
+ }\r
+ }\r
+ if(!spot)\r
+ {\r
+ Bot.report("Could not find an empty slot for "+ip);\r
+ server.close();\r
+ sleep();\r
+ }\r
+ }\r
+ else\r
+ {\r
+ Bot.report("Received unauthorized connection from "+ip);\r
+ server.close();\r
+ sleep();\r
+ }\r
+ }\r
+ catch(Exception ex)\r
+ {\r
+ sleep();\r
+ }\r
+ }\r
+ }\r
+ \r
+ public void cleanupClients()\r
+ {\r
+ for(int i=0;i<maxconn;i++)\r
+ {\r
+ if(clients[i]!=null && clients[i].getFinished())\r
+ {\r
+ clients[i]=null;;\r
+ }\r
+ }\r
+ }\r
+ \r
+ public void sleep()\r
+ {\r
+ try\r
+ {\r
+ Thread.sleep(100);\r
+ }\r
+ catch(InterruptedException e)\r
+ {\r
+ C.printDebug("Error sleeping in webinterface");\r
+ C.debug(e);\r
+ C.die("Socket error, trying to die gracefully.");\r
+ }\r
+ }\r
+ \r
+ public void stopRunning()\r
+ {\r
+ running = false;\r
+ try\r
+ {\r
+ listener.close();\r
+ }\r
+ catch(Exception e)\r
+ {\r
+ C.printDebug("Error closing webinterface");\r
+ C.debug(e);\r
+ }\r
+ }\r
+ \r
+ public void relayNotice(String targetnum, String message)\r
+ {\r
+ for(int i=0;i<maxconn;i++)\r
+ {\r
+ if(clients[i]!=null && targetnum.substring(2).equals(clients[i].getNumeric()))\r
+ {\r
+ clients[i].send(message);\r
+ }\r
+ }\r
+ }\r
+}
\ No newline at end of file