Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 {
- private Socket socket;
- private BufferedReader serverIn;
- private PrintWriter botOut;
- private boolean connectionClosed = false;
- static int port = 6667;
- static String server = "irc.freenode.net";
- String nick = "IncreasinglyAcid", user = "skutr34",
- password = "";
- private enum messageToSend {
- HELLO, JAVADOC, FEDORA_LINK, WAITING
- }
- public MyBot(String serv, int p) {
- try {
- System.out.println("Attempting to connect");
- socket = new Socket(serv, p);
- serverIn = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- botOut = new PrintWriter(socket.getOutputStream());
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (socket == null) {
- System.out.println("Connection failed.");
- connectionClosed = true;
- }
- if (!connectionClosed) {
- System.out.println("Connection is valid, attempting to sign in");
- String lineIn = "";
- botOut.write("NICK " + nick + "\r\n");
- botOut.write("USER " + user + " 8 * : " + user + "\r\n");
- botOut.flush(); // clear the printwriter
- botOut.write("JOIN #skutr34\r\n");
- botOut.flush();
- botOut.write("NICKSERV IDENTIFY " + password + "\r\n");
- botOut.flush(); // clear printwriter
- try {
- while ((lineIn = serverIn.readLine()) != null) {
- System.out.println(lineIn);
- try {
- if (lineIn.startsWith("PING")) {
- botOut.write("PONG" + lineIn.split(" ")[1] + "\r\n");
- botOut.flush();
- }
- if (lineIn.contains("PRIVMSG")) {
- System.out.println(lineIn);
- String fromNick = lineIn.split(":")[1].split("!")[0];
- String message = lineIn.substring(1).split(":", 2)[1];
- String fromHost = lineIn.split("@")[1].split(" ")[0];
- switch (processCommand(fromNick, fromHost,
- lineIn.split(" ")[2], message)) {
- case HELLO:
- sendMessage("#skutr34", "Hello, " + fromNick); // this line
- // is it okay to use that variable there?
- break;
- }
- }
- if (lineIn.contains("PART")) {
- System.out.print(lineIn);
- printQuitMessage();
- }
- if (lineIn.contains("JOIN")) {
- System.out.println(lineIn);
- String fromNick = lineIn.split(":")[1].split("!")[0];
- onJoin(fromNick);
- }
- } catch (Exception ignore) {
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- new MyBot(server, port);
- }
- public void printQuitMessage() {
- sendMessage("#skutr34", "That kid was a loser anyway :P");
- }
- public void onJoin(String nick) {
- if (!nick.equals("IncreasinglyAcid")) {
- sendMessage(
- "#skutr34",
- nick
- + ": "
- + "Welcome to #skutr34. Our only rule here is to have fun!...");
- }
- }
- public messageToSend processCommand(String nick, String host,
- String channel, String message) {
- if (message.equals("Hello") || message.equals("hey")) {
- return messageToSend.HELLO;
- } else if (message.equals(".fedora")) {
- return messageToSend.FEDORA_LINK;
- } else if (message.equals(".api")) {
- return messageToSend.JAVADOC;
- }
- return messageToSend.WAITING;
- }
- public void sendMessage(String location, String message) {
- botOut.write("PRIVMSG " + location + " :" + message + "\r\n");
- botOut.flush();
- }
- }
Add Comment
Please, Sign In to add comment