Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. package application;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.Socket;
  8. import java.net.UnknownHostException;
  9.  
  10. public class MyBot {
  11. // what do you know about the IRC connections?
  12. // uhhh i use port 6667 and i connect to irc.strictfp.com
  13. // lol mk do you know anything about sockets?
  14. // nope
  15.  
  16. private Socket socket; // this is the connection created by java to the
  17. // server;
  18. private BufferedReader serverIn; // reads the raw information coming from
  19. // the socket
  20. private PrintWriter botOut; // this will send information from the bot to
  21. // the server
  22. private boolean connectionClosed = false; // will stop threads processing
  23. // info when the bot closes
  24. // connection to the serv
  25.  
  26. static int port = 6667; // could be called in the methods itself, but so i
  27. // dont have to comment later we will make it
  28. // initalized here
  29. static String server = "irc.strictfp.com";
  30. String nick = "Turtle", user = "Informant";
  31.  
  32. public MyBot(String serv, int p) {
  33. try {
  34. System.out.println("Attempting to connect");
  35. socket = new Socket(serv, p); // creates the connection to the
  36. // server
  37. serverIn = new BufferedReader(new InputStreamReader(
  38. socket.getInputStream()));
  39. botOut = new PrintWriter(socket.getOutputStream());
  40. } catch (UnknownHostException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. } catch (IOException e) {
  44. // TODO Auto-generated catch block
  45. e.printStackTrace();
  46. }
  47.  
  48. if (socket == null) {
  49. System.out.println("Connection failed.");
  50. connectionClosed = true;
  51. }
  52.  
  53. if (!connectionClosed) { // make sure connection is even vaild
  54. System.out.println("Connection is valid, attempting to sign in");
  55. String lineIn = "";
  56. botOut.write("/nick " + nick + "\r\n"); // set nick to nick
  57. botOut.write("/user " + user + " * : " + user + "\r\n"); // set
  58. // user
  59. // as
  60. // user
  61. botOut.flush(); // clear the printwriter
  62. botOut.write("/join #rscode\r\n");
  63. botOut.flush(); // clear printwriter
  64. try {
  65. while ((lineIn = serverIn.readLine()) != null) { // this will
  66. // iterate
  67. // only when
  68. // a string
  69. // from a
  70. // server is
  71. // read. are
  72. // you still
  73. // following?
  74. try {
  75. if (lineIn.contains("PRIVMSG")) {
  76. System.out.println(lineIn); // this will print the
  77. // raw message
  78. // are you waiting for me to type something? my bad,
  79. // just reading up on stuff
  80. String fromNick = lineIn.split(":")[1].split("!")[0]; // btw
  81. // when
  82. // getting
  83. // a
  84. // RAW
  85. // message
  86. // it
  87. // looks
  88. // like
  89. // :nick~user@host
  90. // PRIVMASG
  91. // #channel
  92. // :message
  93. // so we split it into manaable parts
  94. String message = lineIn.split("PRIVMSG #rscode :")[1];
  95. String fromHost = lineIn.split("@")[1].split(" ")[0];
  96. processCommand(fromNick, fromHost, message); // this
  97. // will
  98. // run
  99. // through
  100. // the
  101. // message
  102. // to
  103. // see
  104. // if
  105. // it
  106. // has
  107. // a
  108. // valid
  109. // command
  110. }
  111. } catch (Exception ignore) {
  112. }
  113. }
  114. } catch (IOException e) {
  115. // TODO Auto-generated catch block
  116. e.printStackTrace();
  117. }
  118. }
  119. }
  120.  
  121. public static void main(String[] args) {
  122. new MyBot(server, port);
  123.  
  124. }
  125.  
  126. public void processCommand(String nick, String host, String message) {
  127. if (message.startsWith(".quit")) {
  128. System.exit(0);
  129. }
  130. }
  131.  
  132. public void sendMessage(String location, String message) { // use as
  133. // sendMessage("#channel",
  134. // "message here");
  135. botOut.write("PRIVMSG " + location + " :" + message);
  136. }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement