Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ircbot;
- import java.io.*; //Needed for socket stuff
- import java.net.*; //Needed for socket stuff
- import java.util.Timer; //For the timer
- import java.util.TimerTask; //For the timer
- import java.util.Date; //For getting the time
- import java.util.Calendar; //For getting the time
- import java.util.Queue; //For storing data send from server
- public class IrcBot
- {
- private Socket socket;
- /**
- * Constructor
- */
- public IrcBot()
- {
- establishConnection();
- //startTimer();
- while (true)
- {
- BufferedReader in = null;
- try
- {
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String userInput;
- //Send message about login here
- sendMsg("NICK testBOT\r\n\",\"USER guest tolmoon tolsun :Ronnie Reagan\r\n");
- //Note: look for ping # in the future, send back ping #
- while ((userInput = in.readLine()) != null) //This is blocking, but dunno what to use instead
- {
- System.out.println("echo: " + in.readLine());
- }
- }
- catch (IOException e)
- {
- System.out.println(e.toString());
- System.exit(1);
- }
- }
- }
- /**
- * Establishes a connection with the IRC server
- * @return the socket now connected to the server
- */
- public void establishConnection()
- {
- //Create the socket
- try
- {
- InetAddress addr = InetAddress.getByName("irc.dynastynet.net");
- int port = 6667;
- socket = new Socket(addr, port);
- socket.setSoTimeout(1000);
- }
- catch (UnknownHostException e)
- {
- System.out.println(e.toString());
- System.exit(1);
- }
- catch (SocketTimeoutException e)
- {
- System.out.println(e.toString());
- System.exit(1);
- }
- catch (IOException e)
- {
- System.out.println(e.toString());
- System.exit(1);
- }
- }
- /**
- * Sends a message to the IRC exit
- * @param msg the message to send to the server
- * @param socket the open socket
- */
- public void sendMsg(String msg)
- {
- try
- {
- BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
- wr.write(msg);
- wr.flush();
- }
- catch (IOException e)
- {
- System.out.println(e.getMessage());
- System.exit(1);
- }
- }
- /**
- * Sets a timer to check if its 11:11 every second
- */
- public void startTimer()
- {
- Timer timer = new Timer();
- int startingTime = 1; //Start immediately
- int delayTime=1000; //Repeat every second
- timer.schedule(new TimerTask() { public void run() {
- Date currentDate = new Date();
- Calendar cal = Calendar.getInstance();
- int hour = cal.get(Calendar.HOUR);
- int minute = cal.get(Calendar.MINUTE);
- int second = cal.get(Calendar.SECOND);
- if (hour == 11 && minute == 11 && second == 11 )
- {
- //Have the bot do someting
- System.out.println("yayness");
- }
- } } , startingTime, delayTime);
- }
- //Figure this out eventually
- /*
- public String readMsg()
- {
- String userInput;
- try
- {
- while ((userInput = stdIn.readLine()) != null)
- {
- out.println(userInput);
- System.out.println("echo: " + in.readLine());
- return userInput;
- }
- }
- catch (IOException e)
- {
- System.out.println(e.getMessage());
- System.exit(1);
- }
- return null;
- }
- */
- }
Add Comment
Please, Sign In to add comment