- /**
- * Connexion à un serveur IRC
- * @author Quentin LE DOLEDEC
- */
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import org.apache.axis.client.*;
- import org.apache.axis.encoding.XMLType;
- import javax.xml.rpc.ParameterMode;
- public class IRCBot extends Thread {
- private Socket socket;
- private String host;
- private int port;
- private PrintStream outData;
- /**
- * Constructeur
- * @param host
- * @throws UnknownHostException
- * @throws IOException
- * @throws ClassNotFoundException
- */
- public IRCBot(String host) throws UnknownHostException, IOException, ClassNotFoundException {
- this(host,6667);
- }
- /**
- * Constructeur (2)
- * @param host
- * @param port
- * @throws UnknownHostException
- * @throws IOException
- * @throws ClassNotFoundException
- */
- public IRCBot ( String host, int port ) throws UnknownHostException,
- IOException, ClassNotFoundException {
- this.host = host;
- this.port = port;
- mainLoop();
- }
- /**
- * Connexion à un serveur
- * @throws UnknownHostException
- * @throws IOException
- */
- private void connect() throws UnknownHostException, IOException {
- System.out.println("*** Connecting to " + host + ":" + port);
- socket = new Socket( host, port );
- if (socket == null) {
- System.out.println("*** Impossible de se connecter");
- System.exit(0);
- }
- outData = new PrintStream( socket.getOutputStream() );
- }
- /**
- * Pour s'identifier
- * @throws IOException
- */
- private void register() throws IOException {
- String nickname = "WS_JAVA";
- String localhost = "localhost";
- // PASS <password>
- // send("PASS" + " " + "test");
- // NICK <nickname>
- send("NICK" + " " + nickname);
- // USER <nick> <host> <server> <name>
- send("USER" + " " + nickname + " " + localhost + " " + host + " " + nickname);
- }
- /**
- * Envoi d'une commande au serveur IRC
- * Exemple de commande :
- * - JOIN #test
- * - PRIVMSG #test :Hello
- * - PRIVMSG foo :Message prive
- * @param commandLine
- */
- private void send(String commandLine) {
- System.out.println("*** -SEND- " + commandLine);
- outData.println(commandLine);
- }
- /**
- * Boucle principale avec ecoute sur le port 31337 et reponses au ping/pong
- * @throws UnknownHostException
- * @throws IOException
- * @throws ClassNotFoundException
- */
- @SuppressWarnings("deprecation")
- public void mainLoop() throws UnknownHostException, IOException, ClassNotFoundException, Exception {
- connect();
- register();
- // Pour l'ecoute sur le port 31337
- Thread th = new Thread() {
- public void run() {
- ServerSocket ss = null;
- Socket s = null;
- DataInputStream dis = null;
- String rl = "";
- try {
- ss = new ServerSocket(31337);
- dis = new DataInputStream(s.getInputStream());
- while (true) {
- s = ss.accept();
- try {
- rl = dis.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (!rl.isEmpty()) {
- send(rl);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- };
- // On lance le thread
- th.start();
- boolean connected = false;
- boolean inChannel = false;
- String line = "";
- String[] data = new String[2];
- String[] serverMsg = new String[4];
- String botAnswer = "";
- String[] privmsg = new String[50];
- DataInputStream inData = new DataInputStream(socket.getInputStream());
- // Envoi regulier des PING/PONG, etc
- while (true) {
- line = inData.readLine();
- data = line.split(":");
- serverMsg = data[1].split(" ");
- System.out.println(line);
- // Pour éviter les timeouts lors de l'envoi d'un PING. On réponds par un PONG.
- if (data[0].equalsIgnoreCase("PING ")) {
- System.out.println("*** Sending PONG command with parameter(s) : " + data[1]);
- outData.println("PONG :" + data[1]);
- }
- if (data.length >= 3 && data[2].contains("MOTD")) {
- System.out.println("*** Connected !");
- connected = true;
- }
- if (connected == true) {
- if (!inChannel) {
- send("JOIN #WS_JAVA");
- inChannel = true;
- }
- if (inChannel) {
- serverMsg = data[1].split(" ");
- if (serverMsg.length >= 2) {
- // TheZ!Z@TheZ.users.quakenet.org PRIVMSG #Quentin
- if (serverMsg[1].equalsIgnoreCase("PRIVMSG")) {
- privmsg = data[2].split(" ");
- if (privmsg[0].equalsIgnoreCase("!time")) {
- botAnswer = "The time is now : " + new java.util.Date().toString();
- send("PRIVMSG " + serverMsg[2] + " :" + botAnswer);
- }
- else if (privmsg[0].equalsIgnoreCase("!help")) {
- botAnswer = "My commands : !time, !addAdvertise, !delAdvertise, !getCapabilities, !subscribe, !delSubscribe, !CheckEvent";
- send("PRIVMSG " + serverMsg[2] + " :" + botAnswer);
- }
- // !addAdvertise <service> <guard> <guard> <date_fin>
- else if (privmsg[0].equalsIgnoreCase("!addAdvertise")) {
- if (privmsg.length == 5) {
- String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
- org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
- Call call = (Call) new org.apache.axis.client.Service().createCall();
- call.setTargetEndpointAddress(new URL(endpoint));
- call.setOperationName("Advertise");
- call.addParameter( "ws", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter( "senseur", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter( "guard", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter( "date_fin", XMLType.XSD_STRING, ParameterMode.IN);
- call.setReturnType(XMLType.SOAP_ARRAY);
- String retour = call.invoke(new Object[] {privmsg[1],privmsg[2],privmsg[3],privmsg[4]});
- send("PRIVMSG " + serverMsg[2] + " :" + retour);
- }
- else {
- send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !addAdvertise <service> <guard> <guard> <date_fin>");
- }
- }
- // !delAdvertise <id_advertise>
- else if (privmsg[0].equalsIgnoreCase("!delAdvertise")) {
- if (privmsg.length == 2) {
- String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
- org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
- Call call = (Call) new org.apache.axis.client.Service().createCall();
- call.setTargetEndpointAddress(new URL(endpoint));
- call.setOperationName("CancelAdvertise");
- call.addParameter( "id_advertise", XMLType.XSD_STRING, ParameterMode.IN);
- call.setReturnType(XMLType.SOAP_ARRAY);
- String retour = call.invoke(new Object[] {privmsg[1]});
- send("PRIVMSG " + serverMsg[2] + " :" + retour);
- }
- else {
- send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !delAdvertise <id_advertise>");
- }
- }
- // !getCapabilities
- else if (privmsg[0].equalsIgnoreCase("!getCapabilities")) {
- String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
- org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
- Call call = (Call) new org.apache.axis.client.Service().createCall();
- call.setTargetEndpointAddress(new URL(endpoint));
- call.setOperationName("getCapabilities");
- call.setReturnType(XMLType.SOAP_ARRAY);
- String retour = call.invoke(new Object[] {""});
- send("PRIVMSG " + serverMsg[2] + " :" + retour);
- }
- // !subscribe <id_advertise> <id_user> <date_fin> <id_type>
- else if (privmsg[0].equalsIgnoreCase("!subscribe")) {
- if (privmsg.length == 5) {
- String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
- org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
- Call call = (Call) new org.apache.axis.client.Service().createCall();
- call.setTargetEndpointAddress(new URL(endpoint));
- call.setOperationName("Subscribe");
- call.addParameter( "id_advertise", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter( "id_user", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter( "date_fin", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter( "id_type", XMLType.XSD_STRING, ParameterMode.IN);
- call.setReturnType(XMLType.SOAP_ARRAY);
- String retour = call.invoke(new Object[] {privmsg[1],privmsg[2],privmsg[3],privmsg[4]});
- send("PRIVMSG " + serverMsg[2] + " :" + retour);
- }
- else {
- send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !subscribe <id_advertise> <id_user> <date_fin> <id_type>");
- }
- }
- // !delSubscribe <id_advertise> <id_user>
- else if (privmsg[0].equalsIgnoreCase("!delSubscribe")) {
- if (privmsg.length == 3) {
- String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
- org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
- Call call = (Call) new org.apache.axis.client.Service().createCall();
- call.setTargetEndpointAddress(new URL(endpoint));
- call.setOperationName("CancelSubscription");
- call.addParameter( "id_advertise", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter( "id_user", XMLType.XSD_STRING, ParameterMode.IN);
- call.setReturnType(XMLType.SOAP_ARRAY);
- String retour = call.invoke(new Object[] {privmsg[1],privmsg[2]});
- send("PRIVMSG " + serverMsg[2] + " :" + retour);
- }
- else {
- send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !delSubscribe <id_advertise> <id_user>");
- }
- }
- // !CheckEvent <event> <val>
- else if (privmsg[0].equalsIgnoreCase("!CheckEvent")) {
- if (privmsg.length == 3) {
- String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
- org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
- Call call = (Call) new org.apache.axis.client.Service().createCall();
- call.setTargetEndpointAddress(new URL(endpoint));
- call.setOperationName("CheckEvent");
- call.addParameter( "event", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter( "val", XMLType.XSD_STRING, ParameterMode.IN);
- call.setReturnType(XMLType.SOAP_ARRAY);
- String retour = call.invoke(new Object[] {privmsg[1],privmsg[2]});
- send("PRIVMSG " + serverMsg[2] + " :" + retour);
- }
- else {
- send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !CheckEvent <event> <val>");
- }
- }
- else if (privmsg[0].startsWith("!")) {
- send("PRIVMSG " + serverMsg[2] + " :Unknown command. Try !help");
- }
- }
- }
- }
- }
- }
- }
- public static void main (String[] args) throws UnknownHostException,
- IOException, ClassNotFoundException
- {
- @SuppressWarnings("unused")
- IRCBot test = new IRCBot ("localhost",6667);
- }
- }