QPaysTaxes QPaysTaxes - 1 year ago 101
Java Question

Transmit message from Bukkit server to IRC with plugin

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.

Answer Source

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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download