Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 11.50 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * Connexion à un serveur IRC
  3.  * @author Quentin LE DOLEDEC
  4.  */
  5.  
  6.  
  7. import java.io.DataInputStream;
  8. import java.io.IOException;
  9. import java.io.PrintStream;
  10.  
  11. import java.net.ServerSocket;
  12. import java.net.Socket;
  13. import java.net.UnknownHostException;
  14.  
  15. import org.apache.axis.client.*;
  16. import org.apache.axis.encoding.XMLType;
  17.  
  18. import javax.xml.rpc.ParameterMode;
  19.  
  20. public class IRCBot extends Thread {
  21.        
  22.         private Socket socket;
  23.        
  24.         private String host;
  25.         private int port;
  26.        
  27.         private PrintStream outData;
  28.        
  29.        
  30.         /**
  31.          * Constructeur
  32.          * @param host
  33.          * @throws UnknownHostException
  34.          * @throws IOException
  35.          * @throws ClassNotFoundException
  36.          */
  37.         public IRCBot(String host) throws UnknownHostException, IOException, ClassNotFoundException {
  38.                 this(host,6667);
  39.         }
  40.        
  41.        
  42.         /**
  43.          * Constructeur (2)
  44.          * @param host
  45.          * @param port
  46.          * @throws UnknownHostException
  47.          * @throws IOException
  48.          * @throws ClassNotFoundException
  49.          */
  50.         public IRCBot ( String host, int port ) throws UnknownHostException,
  51.                                                                                                 IOException, ClassNotFoundException {
  52.                 this.host = host;
  53.                 this.port = port;
  54.                 mainLoop();
  55.         }
  56.        
  57.        
  58.         /**
  59.          * Connexion à un serveur
  60.          * @throws UnknownHostException
  61.          * @throws IOException
  62.          */
  63.         private void connect() throws UnknownHostException, IOException {
  64.                 System.out.println("*** Connecting to " + host + ":" + port);
  65.                 socket = new Socket( host, port );
  66.                 if (socket == null) {
  67.                         System.out.println("*** Impossible de se connecter");
  68.                         System.exit(0);
  69.                 }
  70.                 outData = new PrintStream( socket.getOutputStream() );
  71.         }
  72.        
  73.         /**
  74.          * Pour s'identifier
  75.          * @throws IOException
  76.          */
  77.         private void register() throws IOException      {
  78.                 String nickname = "WS_JAVA";
  79.                 String localhost = "localhost";
  80.                 // PASS <password>
  81. //              send("PASS" + " "  + "test");
  82.                 // NICK <nickname>
  83.                 send("NICK" + " " + nickname);
  84.                 // USER <nick> <host> <server> <name>
  85.                 send("USER" + " " + nickname + " " + localhost + " " + host + " " + nickname);
  86.         }
  87.        
  88.         /**
  89.          * Envoi d'une commande au serveur IRC
  90.          * Exemple de commande :
  91.          *  - JOIN #test
  92.          *  - PRIVMSG #test :Hello
  93.          *  - PRIVMSG foo :Message prive
  94.          * @param commandLine
  95.          */
  96.         private void send(String commandLine) {
  97.                 System.out.println("*** -SEND- " + commandLine);
  98.                 outData.println(commandLine);
  99.         }
  100.                
  101.         /**
  102.          * Boucle principale avec ecoute sur le port 31337 et reponses au ping/pong
  103.          * @throws UnknownHostException
  104.          * @throws IOException
  105.          * @throws ClassNotFoundException
  106.          */
  107.         @SuppressWarnings("deprecation")
  108.         public void mainLoop() throws UnknownHostException, IOException, ClassNotFoundException, Exception {
  109.                 connect();
  110.                 register();
  111.  
  112.                 // Pour l'ecoute sur le port 31337
  113.                 Thread th = new Thread() {
  114.                         public void run() {
  115.                                 ServerSocket ss = null;
  116.                                 Socket s = null;
  117.                                 DataInputStream dis = null;
  118.                                 String rl = "";
  119.                                 try {
  120.                                         ss = new ServerSocket(31337);
  121.                                         dis = new DataInputStream(s.getInputStream());
  122.                                         while (true) {
  123.                                                 s = ss.accept();
  124.                                                 try {
  125.                                                         rl = dis.readLine();
  126.                                                 } catch (IOException e) {
  127.                                                         e.printStackTrace();
  128.                                                 }
  129.                                                 if (!rl.isEmpty()) {
  130.                                                         send(rl);
  131.                                                 }
  132.                                         }
  133.                                 } catch (IOException e) {
  134.                                         e.printStackTrace();
  135.                                 }
  136.  
  137.                         }
  138.                 };
  139.                 // On lance le thread
  140.                 th.start();
  141.                
  142.  
  143.                 boolean connected = false;
  144.                 boolean inChannel = false;
  145.                
  146.                 String line = "";
  147.                 String[] data = new String[2];
  148.                 String[] serverMsg = new String[4];
  149.                 String botAnswer = "";
  150.                 String[] privmsg = new String[50];
  151.                
  152.                
  153.                 DataInputStream inData = new DataInputStream(socket.getInputStream());
  154.                
  155.                 // Envoi regulier des PING/PONG, etc
  156.                 while (true) {
  157.                         line = inData.readLine();
  158.                         data = line.split(":");
  159.                         serverMsg = data[1].split(" ");
  160.                         System.out.println(line);
  161.                         // Pour éviter les timeouts lors de l'envoi d'un PING. On réponds par un PONG.
  162.                         if (data[0].equalsIgnoreCase("PING ")) {
  163.                                 System.out.println("*** Sending PONG command with parameter(s) : " + data[1]);
  164.                                 outData.println("PONG :" + data[1]);
  165.                         }
  166.                         if (data.length >= 3 && data[2].contains("MOTD")) {
  167.                                 System.out.println("*** Connected !");
  168.                                 connected = true;
  169.                         }
  170.                         if (connected == true) {
  171.                                 if (!inChannel) {
  172.                                         send("JOIN #WS_JAVA");
  173.                                         inChannel = true;
  174.                                 }
  175.                                 if (inChannel) {
  176.                                         serverMsg = data[1].split(" ");
  177.                                         if (serverMsg.length >= 2) {
  178.                                                 // TheZ!Z@TheZ.users.quakenet.org PRIVMSG #Quentin
  179.                                                 if (serverMsg[1].equalsIgnoreCase("PRIVMSG")) {
  180.                                                         privmsg = data[2].split(" ");
  181.                                                         if (privmsg[0].equalsIgnoreCase("!time")) {
  182.                                                                 botAnswer = "The time is now : " + new java.util.Date().toString();
  183.                                                                 send("PRIVMSG " + serverMsg[2] + " :" + botAnswer);
  184.                                                         }
  185.                                                         else if (privmsg[0].equalsIgnoreCase("!help")) {
  186.                                                                 botAnswer = "My commands : !time, !addAdvertise, !delAdvertise, !getCapabilities, !subscribe, !delSubscribe, !CheckEvent";
  187.                                                                 send("PRIVMSG " + serverMsg[2] + " :" + botAnswer);
  188.                                                         }
  189.                                                         // !addAdvertise <service> <guard> <guard> <date_fin>
  190.                                                         else if (privmsg[0].equalsIgnoreCase("!addAdvertise")) {
  191.                                                                 if (privmsg.length == 5) {
  192.  
  193.                                                                         String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
  194.                                                                     org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
  195.                                                                     Call call = (Call) new org.apache.axis.client.Service().createCall();
  196.                                                                     call.setTargetEndpointAddress(new URL(endpoint));
  197.                                                                     call.setOperationName("Advertise");
  198.                                                                     call.addParameter( "ws", XMLType.XSD_STRING, ParameterMode.IN);
  199.                                                                     call.addParameter( "senseur", XMLType.XSD_STRING, ParameterMode.IN);
  200.                                                                     call.addParameter( "guard", XMLType.XSD_STRING, ParameterMode.IN);
  201.                                                                     call.addParameter( "date_fin", XMLType.XSD_STRING, ParameterMode.IN);
  202.                                                                     call.setReturnType(XMLType.SOAP_ARRAY);
  203.                                                                     String retour = call.invoke(new Object[] {privmsg[1],privmsg[2],privmsg[3],privmsg[4]});
  204.                                                                        
  205.                                                                     send("PRIVMSG " + serverMsg[2] + " :" + retour);
  206.                                                                        
  207.                                                                 }
  208.                                                                 else {
  209.                                                                         send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !addAdvertise <service> <guard> <guard> <date_fin>");
  210.                                                                 }
  211.                                                         }
  212.                                                        
  213.                                                         // !delAdvertise <id_advertise>
  214.                                                         else if (privmsg[0].equalsIgnoreCase("!delAdvertise")) {
  215.                                                                 if (privmsg.length == 2) {
  216.  
  217.                                                                         String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
  218.                                                                     org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
  219.                                                                     Call call = (Call) new org.apache.axis.client.Service().createCall();
  220.                                                                     call.setTargetEndpointAddress(new URL(endpoint));
  221.                                                                     call.setOperationName("CancelAdvertise");
  222.                                                                     call.addParameter( "id_advertise", XMLType.XSD_STRING, ParameterMode.IN);
  223.                                                                     call.setReturnType(XMLType.SOAP_ARRAY);
  224.                                                                     String retour = call.invoke(new Object[] {privmsg[1]});
  225.                                                                        
  226.                                                                     send("PRIVMSG " + serverMsg[2] + " :" + retour);
  227.                                                                        
  228.                                                                 }
  229.                                                                 else {
  230.                                                                         send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !delAdvertise <id_advertise>");
  231.                                                                 }
  232.                                                         }
  233.                                                        
  234.                                                        
  235.                                                         // !getCapabilities
  236.                                                         else if (privmsg[0].equalsIgnoreCase("!getCapabilities")) {
  237.  
  238.                                                                 String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
  239.                                                                 org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
  240.                                                                 Call call = (Call) new org.apache.axis.client.Service().createCall();
  241.                                                                 call.setTargetEndpointAddress(new URL(endpoint));
  242.                                                                 call.setOperationName("getCapabilities");
  243.                                                                 call.setReturnType(XMLType.SOAP_ARRAY);
  244.                                                                 String retour = call.invoke(new Object[] {""});
  245.                                                                 send("PRIVMSG " + serverMsg[2] + " :" + retour);
  246.                                                         }
  247.                                                        
  248.                                                         // !subscribe <id_advertise> <id_user> <date_fin> <id_type>
  249.                                                         else if (privmsg[0].equalsIgnoreCase("!subscribe")) {
  250.                                                                 if (privmsg.length == 5) {
  251.  
  252.                                                                         String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
  253.                                                                     org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
  254.                                                                     Call call = (Call) new org.apache.axis.client.Service().createCall();
  255.                                                                     call.setTargetEndpointAddress(new URL(endpoint));
  256.                                                                     call.setOperationName("Subscribe");
  257.                                                                     call.addParameter( "id_advertise", XMLType.XSD_STRING, ParameterMode.IN);
  258.                                                                     call.addParameter( "id_user", XMLType.XSD_STRING, ParameterMode.IN);
  259.                                                                     call.addParameter( "date_fin", XMLType.XSD_STRING, ParameterMode.IN);
  260.                                                                     call.addParameter( "id_type", XMLType.XSD_STRING, ParameterMode.IN);
  261.                                                                     call.setReturnType(XMLType.SOAP_ARRAY);
  262.                                                                     String retour = call.invoke(new Object[] {privmsg[1],privmsg[2],privmsg[3],privmsg[4]});
  263.                                                                        
  264.                                                                     send("PRIVMSG " + serverMsg[2] + " :" + retour);
  265.                                                                        
  266.                                                                 }
  267.                                                                 else {
  268.                                                                         send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !subscribe <id_advertise> <id_user> <date_fin> <id_type>");
  269.                                                                 }
  270.                                                         }
  271.                                                        
  272.                                                        
  273.                                                         // !delSubscribe <id_advertise> <id_user>
  274.                                                         else if (privmsg[0].equalsIgnoreCase("!delSubscribe")) {
  275.                                                                 if (privmsg.length == 3) {
  276.  
  277.                                                                         String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
  278.                                                                     org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
  279.                                                                     Call call = (Call) new org.apache.axis.client.Service().createCall();
  280.                                                                     call.setTargetEndpointAddress(new URL(endpoint));
  281.                                                                     call.setOperationName("CancelSubscription");
  282.                                                                     call.addParameter( "id_advertise", XMLType.XSD_STRING, ParameterMode.IN);
  283.                                                                     call.addParameter( "id_user", XMLType.XSD_STRING, ParameterMode.IN);
  284.                                                                     call.setReturnType(XMLType.SOAP_ARRAY);
  285.                                                                     String retour = call.invoke(new Object[] {privmsg[1],privmsg[2]});
  286.                                                                        
  287.                                                                     send("PRIVMSG " + serverMsg[2] + " :" + retour);
  288.                                                                        
  289.                                                                 }
  290.                                                                 else {
  291.                                                                         send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !delSubscribe <id_advertise> <id_user>");
  292.                                                                 }
  293.                                                         }
  294.                                                        
  295.                                                        
  296.                                                         // !CheckEvent <event> <val>
  297.                                                         else if (privmsg[0].equalsIgnoreCase("!CheckEvent")) {
  298.                                                                 if (privmsg.length == 3) {
  299.  
  300.                                                                         String endpoint = "http://172.20.11.10:8080/axis/WS_SAS.jws?wsdl";
  301.                                                                     org.apache.axis.client.Service sv = new org.apache.axis.client.Service();
  302.                                                                     Call call = (Call) new org.apache.axis.client.Service().createCall();
  303.                                                                     call.setTargetEndpointAddress(new URL(endpoint));
  304.                                                                     call.setOperationName("CheckEvent");
  305.                                                                     call.addParameter( "event", XMLType.XSD_STRING, ParameterMode.IN);
  306.                                                                     call.addParameter( "val", XMLType.XSD_STRING, ParameterMode.IN);
  307.                                                                     call.setReturnType(XMLType.SOAP_ARRAY);
  308.                                                                     String retour = call.invoke(new Object[] {privmsg[1],privmsg[2]});
  309.                                                                        
  310.                                                                     send("PRIVMSG " + serverMsg[2] + " :" + retour);
  311.                                                                        
  312.                                                                 }
  313.                                                                 else {
  314.                                                                         send ("PRIVMSG " + serverMsg[2] + " :Wrong number of arguments. Syntax: !CheckEvent <event> <val>");
  315.                                                                 }
  316.                                                         }
  317.                                                        
  318.                                                         else if (privmsg[0].startsWith("!")) {
  319.                                                                 send("PRIVMSG " + serverMsg[2] + " :Unknown command. Try !help");
  320.                                                         }
  321.                                                        
  322.                                                 }
  323.                                         }
  324.                                 }
  325.                         }
  326.                 }
  327.         }
  328.        
  329.  
  330.         public static void main (String[] args) throws UnknownHostException,
  331.                                                                         IOException, ClassNotFoundException
  332.         {
  333.                         @SuppressWarnings("unused")
  334.                         IRCBot test = new IRCBot ("localhost",6667);
  335.         }
  336.        
  337. }