Guest User

Untitled

a guest
Aug 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. package ircbot;
  2.  
  3. import java.io.*; //Needed for socket stuff
  4. import java.net.*; //Needed for socket stuff
  5. import java.util.Timer; //For the timer
  6. import java.util.TimerTask; //For the timer
  7. import java.util.Date; //For getting the time
  8. import java.util.Calendar; //For getting the time
  9.  
  10. import java.util.Queue; //For storing data send from server
  11.  
  12.  
  13. public class IrcBot
  14. {
  15.     private Socket socket;
  16.  
  17.     /**
  18.      * Constructor
  19.      */
  20.     public IrcBot()
  21.     {
  22.         establishConnection();
  23.         //startTimer();
  24.        
  25.        
  26.         while (true)
  27.         {
  28.            BufferedReader in = null;
  29.            
  30.             try
  31.             {
  32.                 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  33.                
  34.                 String userInput;
  35.                
  36.                 //Send message about login here
  37.                 sendMsg("NICK testBOT\r\n\",\"USER guest tolmoon tolsun :Ronnie Reagan\r\n");      
  38.                
  39.                 //Note: look for ping # in the future, send back ping #
  40.                
  41.                 while ((userInput = in.readLine()) != null) //This is blocking, but dunno what to use instead
  42.                 {
  43.                     System.out.println("echo: " + in.readLine());
  44.                 }
  45.             }
  46.             catch (IOException e)
  47.             {
  48.                 System.out.println(e.toString());
  49.                 System.exit(1);
  50.             }
  51.         }
  52.     }
  53.    
  54.     /**
  55.      * Establishes a connection with the IRC server
  56.      * @return the socket now connected to the server
  57.      */
  58.     public void establishConnection()
  59.     {
  60.         //Create the socket
  61.         try
  62.         {
  63.             InetAddress addr = InetAddress.getByName("irc.dynastynet.net");
  64.             int port = 6667;
  65.  
  66.             socket = new Socket(addr, port);
  67.             socket.setSoTimeout(1000);
  68.         }
  69.         catch (UnknownHostException e)
  70.         {
  71.             System.out.println(e.toString());
  72.             System.exit(1);
  73.         }
  74.         catch (SocketTimeoutException e)
  75.         {
  76.             System.out.println(e.toString());
  77.             System.exit(1);
  78.         }
  79.         catch (IOException e)
  80.         {
  81.             System.out.println(e.toString());
  82.             System.exit(1);
  83.         }
  84.     }
  85.    
  86.     /**
  87.      * Sends a message to the IRC exit
  88.      * @param msg the message to send to the server
  89.      * @param socket the open socket
  90.      */
  91.     public void sendMsg(String msg)
  92.     {
  93.         try
  94.         {
  95.             BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  96.             wr.write(msg);
  97.             wr.flush();
  98.         }
  99.         catch (IOException e)
  100.         {
  101.             System.out.println(e.getMessage());
  102.             System.exit(1);
  103.         }
  104.     }
  105.    
  106.     /**
  107.      * Sets a timer to check if its 11:11 every second
  108.      */
  109.     public void startTimer()
  110.     {
  111.         Timer timer = new Timer();
  112.         int startingTime = 1; //Start immediately
  113.         int delayTime=1000; //Repeat every second
  114.         timer.schedule(new TimerTask() { public void run() {
  115.                                                                 Date currentDate = new Date();
  116.                                                                 Calendar cal = Calendar.getInstance();
  117.                                                                    
  118.                                                                 int hour = cal.get(Calendar.HOUR);
  119.                                                                 int minute = cal.get(Calendar.MINUTE);
  120.                                                                 int second = cal.get(Calendar.SECOND);
  121.                                                                    
  122.                                                                 if (hour == 11 && minute == 11 &&  second == 11 )
  123.                                                                 {
  124.                                                                     //Have the bot do someting
  125.                                                                     System.out.println("yayness");
  126.                                                                 }
  127.                                                             } } , startingTime, delayTime);
  128.     }
  129.    
  130.     //Figure this out eventually
  131.     /*
  132.     public String readMsg()
  133.     {
  134.         String userInput;
  135.        
  136.         try
  137.         {
  138.             while ((userInput = stdIn.readLine()) != null)
  139.             {
  140.                 out.println(userInput);
  141.                 System.out.println("echo: " + in.readLine());
  142.                
  143.                 return userInput;
  144.             }
  145.         }
  146.         catch (IOException e)
  147.         {
  148.             System.out.println(e.getMessage());
  149.             System.exit(1);
  150.         }
  151.  
  152.         return null;
  153.     }
  154.      */
  155. }
Add Comment
Please, Sign In to add comment