Question

My friend asked me to write a plugin a bit ago, and recently (As in about five minutes ago) he asked me to add a feature where people can log onto a specific IRC on esper.net and chat onto it. Everythin is set up - I have a command that lets them log in and it would have them join with the nickname of their username in MineCraft. The server uses a chat bot to log in users so they can chat and I have everything set up so that when the type in the command (/irc login ) they send the bot the correct message (Specifically, it makes it seem like they wrote /msg identify in IRC chat). Well, I have the plugin set up a string that has everything formatted, but I need to know how to actually send the message to IRC. Below is my code (The whole thing triggers when the user inputs the command "/irc" and "player" is a Player object set up when they enter any command). I would also like some code to receive what the bot sends back so I know if the player was successfully logged on or not.

if(args[0].equalsIgnoreCase("login") && args.length == 3) {
    String msg = "/msg <bot name> login " + args[1] + " " + args[2];
    //args[1] is the username and args[2] is the password
    String userName = args[1];
    //Creating the connection with a nickname of userName
    //Here is where I need to send that message through IRC
}
else {
    String msg;
    for(int i = 0; i <= args.length; i++) {
        msg += args[i] + " ";
    }
}

By the way, I don't yet know the name of the bot, as my friend is still writing the code for it. I supposed I could just put that in when I get it. Also, the domain is still to be determined as he needs to buy the website and set it all up. Any advice to make this code function faster would also be great.

Was it helpful?

Solution

I think you should Look into Java Socket Connections

but here is a basic IRC connection

class IRC
{
      //Connection Details
      String server = "IRC ADDRESS";
      String nick = "NICKNAME";
      String login = "LOGIN";
      String Pass = "PASS";  
      String channel = "CHANNLE";

  //Socket Stuff
  Socket socket;
  BufferedWriter writer;
  BufferedReader reader;


      public void HandleChat() throws Exception 
      {
           socket = new Socket(server, 6667);

           writer = new BufferedWriter(
               new OutputStreamWriter(socket.getOutputStream( )));

           reader = new BufferedReader(
               new InputStreamReader(socket.getInputStream( )));


           // Log on to the server.      
           writer.write("PASS " + Pass + "\r\n");
           writer.write("NICK " + nick + "\r\n");
           writer.write("USER " + login +"\r\n");
           writer.flush( );

           // Read lines from the server until it tells us we have connected.
           String line = null;

           while (((line = reader.readLine( )) != null))
           {
           }            
      }
}

OTHER TIPS

Here is a few classes that can make a connection to server. you can use this and modify it to your needs. all you need to do is create a new instance of ChatClient and give it the right parameters and then it will connect with the server.

ChatCleint

    package com.weebly.foxgenesis.src;
import java.net.*;
import java.io.*;

public final class ChatClient
{  
    private Socket socket = null;
    private DataOutputStream streamOut = null;
    private ChatClientThread client = null;
    private String serverName = "localhost";
    private int serverPort = -1;
    private final ChatReciever output;

/**
 * Create a new ChatClient that connects to a given server and receives UTF-8 encoded messages
 * @param a Client class
 */
public ChatClient(ChatReciever chatReciever)
{ 
    output = chatReciever;
    serverPort = chatReciever.getPort();
    serverName = chatReciever.getHost();
    connect(serverName, serverPort);
}

private void connect(String serverName, int serverPort)
{  
    output.handleLog("Establishing connection. Please wait ...");
    try
    {  
        socket = new Socket(serverName, serverPort);
        output.handle("Connected to chat server");
        open();
    }
    catch(UnknownHostException uhe)
    {  
        output.handleError("Host unknown: " + uhe.getMessage()); 
    }
    catch(IOException ioe)
    {  
        output.handleError("Unexpected exception: " + ioe.getMessage()); 
    } 
}

/**
 * Sends a message to the server through bytes in UTF-8 encoding 
 * @param msg message to send to the server
 */
public void send(String msg) throws IOException
{  
    streamOut.writeUTF(msg); 
    streamOut.flush();
}

/**
 * forces the ChatReciever to listen to a message (NOTE: this does not send anything to the server)
 * @param msg message to send
 */
public void handle(String msg)
{  
    output.handle(msg);
}

private void open() throws IOException
{   
    streamOut = new DataOutputStream(socket.getOutputStream());
    client = new ChatClientThread(this, socket); 
}

/**
 * tries to close 
 */
public void close() throws IOException
{  
    if (streamOut != null)  
        streamOut.close();
    if (socket    != null)  
        socket.close(); 
}

/**
 * closes the client connection
 */
@SuppressWarnings("deprecation")
public void stop()
{
    if(client != null)
        client.stop();
    client = null;
}

/**
 * checks if the ChatClient is currently connected to the server
 * @return Boolean is connected
 */
public boolean isConnected()
{
    return client == null ?(false) : (true);
}
}

ChatServerThread

 package com.weebly.foxgenesis.src;
import java.net.*;
import java.io.*;

public final class ChatClientThread extends Thread
{  
    private Socket           socket   = null;
    private ChatClient       client   = null;
    private DataInputStream  streamIn = null;

    public ChatClientThread(ChatClient _client, Socket _socket)
    {  
        client   = _client;
        socket   = _socket;
        open();  
        start();
    }
    public void open()
    {  
        try
        {  
            streamIn  = new DataInputStream(socket.getInputStream());
        }
        catch(IOException ioe)
        {  
            System.out.println("Error getting input stream: " + ioe);
            client.stop();
        }
    }
    public void close()
    {  
        try
        {  
            if (streamIn != null) streamIn.close();
        }
        catch(IOException ioe)
        {  
            System.out.println("Error closing input stream: " + ioe);
        }
    }
    public void run()
    {  
        while (true)
        {  
            try
            {  
                client.handle(streamIn.readUTF());
            }
            catch(IOException ioe)
            { 
                System.out.println("Listening error: " + ioe.getMessage());
                client.stop();
            }
        }
    }
}

ChatReciever

package com.weebly.foxgenesis.src;

public interface ChatReciever 
{
    /**
     * gets the IP address of the host
     * @return String IP address
     */
    public String getHost();

    /**
     * gets the port of the host
     * @return Integer port of host
     */
    public int getPort();

    /**
     * sends a message from the server to the implementing class
     * @param msg message from the server
     */
    public void handle(String msg);

    /**
     * sends an error to the implementing class
     * @param errorMsg error message
     */
    public void handleError(String errorMsg);

    /**
     * Sends a message to the log
     * @param msg message to send
     */
    public void handleLog(Object msg);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top