Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class MyBot {
- // what do you know about the IRC connections?
- // uhhh i use port 6667 and i connect to irc.strictfp.com
- // lol mk do you know anything about sockets?
- // nope
- private Socket socket; // this is the connection created by java to the
- // server;
- private BufferedReader serverIn; // reads the raw information coming from
- // the socket
- private PrintWriter botOut; // this will send information from the bot to
- // the server
- private boolean connectionClosed = false; // will stop threads processing
- // info when the bot closes
- // connection to the serv
- static int port = 6667; // could be called in the methods itself, but so i
- // dont have to comment later we will make it
- // initalized here
- static String server = "irc.strictfp.com";
- String nick = "Turtle", user = "Informant";
- public MyBot(String serv, int p) {
- try {
- System.out.println("Attempting to connect");
- socket = new Socket(serv, p); // creates the connection to the
- // server
- serverIn = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- botOut = new PrintWriter(socket.getOutputStream());
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if (socket == null) {
- System.out.println("Connection failed.");
- connectionClosed = true;
- }
- if (!connectionClosed) { // make sure connection is even vaild
- System.out.println("Connection is valid, attempting to sign in");
- String lineIn = "";
- botOut.write("/nick " + nick + "\r\n"); // set nick to nick
- botOut.write("/user " + user + " * : " + user + "\r\n"); // set
- // user
- // as
- // user
- botOut.flush(); // clear the printwriter
- botOut.write("/join #rscode\r\n");
- botOut.flush(); // clear printwriter
- try {
- while ((lineIn = serverIn.readLine()) != null) { // this will
- // iterate
- // only when
- // a string
- // from a
- // server is
- // read. are
- // you still
- // following?
- try {
- if (lineIn.contains("PRIVMSG")) {
- System.out.println(lineIn); // this will print the
- // raw message
- // are you waiting for me to type something? my bad,
- // just reading up on stuff
- String fromNick = lineIn.split(":")[1].split("!")[0]; // btw
- // when
- // getting
- // a
- // RAW
- // message
- // it
- // looks
- // like
- // :nick~user@host
- // PRIVMASG
- // #channel
- // :message
- // so we split it into manaable parts
- String message = lineIn.split("PRIVMSG #rscode :")[1];
- String fromHost = lineIn.split("@")[1].split(" ")[0];
- processCommand(fromNick, fromHost, message); // this
- // will
- // run
- // through
- // the
- // message
- // to
- // see
- // if
- // it
- // has
- // a
- // valid
- // command
- }
- } catch (Exception ignore) {
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- new MyBot(server, port);
- }
- public void processCommand(String nick, String host, String message) {
- if (message.startsWith(".quit")) {
- System.exit(0);
- }
- }
- public void sendMessage(String location, String message) { // use as
- // sendMessage("#channel",
- // "message here");
- botOut.write("PRIVMSG " + location + " :" + message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement