Advertisement
Guest User

Untitled

a guest
May 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStreamWriter;
  5. import java.net.Socket;
  6.  
  7. /**
  8. * Created by IntelliJ IDEA.
  9. * User: Vartan
  10. * Date: Jun 5, 2010
  11. * Time: 5:57:43 PM
  12. * To change this template use File | Settings | File Templates.
  13. */
  14. public class Client {
  15. public static void main(String[] args) {
  16. Client c = new Client("irc.rizon.net", "Montgomery");
  17. }
  18.  
  19. BufferedWriter out;
  20. BufferedReader in;
  21. String name;
  22. String password = "thebrucrewisanigger";
  23.  
  24. public Client(String server, String username) {
  25. int port = 6667;
  26. name = username;
  27. if (server.contains(":")) {
  28. port = Integer.parseInt(server.substring(server.indexOf(":") + 1));
  29. server = server.substring(server.indexOf(":"));
  30. }
  31. try {
  32. Socket irc = new Socket(server, port);
  33. out = new BufferedWriter(new OutputStreamWriter(irc.getOutputStream()));
  34. in = new BufferedReader(new InputStreamReader(irc.getInputStream()));
  35. out.write("NICK " + username);
  36. out.newLine();
  37. out.write("USER MIKE_BOT Mike__ total-anarchy.net JAVA :Mike__'s Bot");
  38. out.newLine();
  39. out.flush();
  40. out.write("JOIN #Montgomery");
  41. out.newLine();
  42. out.write("JOIN #sharpf");
  43. out.newLine();
  44. out.flush();
  45. String message = null;
  46. while ((message = in.readLine()) != null) {
  47. onRawCode(message);
  48. }
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. public void onRawCode(String raw) {
  55. String[] h = raw.split(":", 2);
  56. h[0] = h[0].trim();
  57. if (h[0].length() == 0) {
  58. onRawMessage(h[1]);
  59. return;
  60. }
  61. if (h[0].equalsIgnoreCase("PING")) {
  62. onPing(h[1]);
  63. return;
  64. }
  65. if (h[0].equalsIgnoreCase("ERROR")) {
  66. onError(h[1]);
  67. return;
  68. }
  69. System.out.println("!!!!!!!UNHANDLED RAW CODE!!!!!!!!");
  70. System.out.println(raw);
  71. }
  72.  
  73. public void onRawMessage(String raw) {
  74. try {
  75. String user="";
  76. String hostName="";
  77. String type="PARSE ERROR";
  78. String[] a1= new String[0];
  79. String[] a2= new String[0];
  80. String[] a3= new String[0];
  81. try {
  82. a1 = raw.split(":", 2);
  83. a2 = a1[0].split(" ");
  84. a3 = a2[0].split("!");
  85. user = a3[0];
  86. hostName = a3[1];
  87. type = a2[1];
  88. }catch(Exception e) {
  89.  
  90. }
  91.  
  92. if (type.equalsIgnoreCase("PRIVMSG")) {
  93. String message = a1[1];
  94. String channel = a2[2];
  95.  
  96. onMessage(user, hostName, channel, message);
  97. return;
  98. }
  99. if (type.equalsIgnoreCase("NOTICE")) {
  100. String message = a1[1];
  101. String channel = a2[2];
  102.  
  103. onNotice(user, hostName, channel, message);
  104. return;
  105. }
  106. if (type.equalsIgnoreCase("MODE")) {
  107. String message = a1[1];
  108. String channel = a2[2];
  109.  
  110. onModeChange(user, hostName, channel, message);
  111. return;
  112. }
  113. if (type.equalsIgnoreCase("KICK")) {
  114. String message="";
  115. String channel="";
  116. String recipient="";
  117. try {
  118. message = a1[1];
  119. channel = a2[2];
  120. recipient = a2[3];
  121. }catch(Exception e) {
  122. }
  123. onKick(user, hostName, channel, recipient, message);
  124. return;
  125. }
  126. if (type.equalsIgnoreCase("JOIN")) {
  127. String channel="";
  128. try {
  129. channel = a1[1];
  130. }catch(Exception e) {
  131. }
  132. onJoin(user, channel);
  133. return;
  134. }
  135. if (type.equalsIgnoreCase("PART")) {
  136. String message="";
  137. String channel="";
  138. try {
  139. message = a1[1];
  140. channel = a2[2];
  141. }catch(Exception e) {
  142. }
  143. onPart(user, hostName, channel, message);
  144. return;
  145. }
  146. if (type.equalsIgnoreCase("QUIT")) {
  147. String message="";
  148. try {
  149. message = a1[1];
  150. }catch(Exception e) {
  151.  
  152. }
  153. onQuit(user, hostName, message);
  154. return;
  155. }
  156. if (type.equalsIgnoreCase("NICK")) {
  157. String n = a1[1];
  158. onNickChange(user, n, hostName);
  159. return;
  160. }
  161.  
  162. System.out.println("!!!!!!!!!!UNHANDLED TYPE: " + type + "!!!!!!!!!!");
  163. System.out.println(raw);
  164. } catch (Exception e) {
  165. System.out.println("Unparsable: " + raw);
  166. }
  167. }
  168.  
  169. public void onNickChange(String old, String n, String hostName) {
  170. ghost(n);
  171. if (old.equals(name))
  172. name = n;
  173. System.out.println(old + "[" + hostName + "] changed their name to: " + n);
  174. }
  175.  
  176. public void onJoin(String user, String channel) {
  177. ghost(user);
  178. System.out.println("<" + user + "> joined " + channel + ".");
  179. }
  180.  
  181. public void onPart(String user, String hostName, String channel, String message) {
  182. System.out.println("<" + user + "[" + hostName + "]> has parted from " + channel + " (" + message + ")");
  183. }
  184.  
  185. public void onQuit(String user, String hostName, String message) {
  186. System.out.println("<" + user + "[" + hostName + "]> has quit (" + message + ")");
  187. }
  188.  
  189. public void onMessage(String user, String hostName, String channel, String message) {
  190. System.out.println(channel + " <" + user + ">: " + message);
  191.  
  192. }
  193.  
  194. public void onNotice(String user, String hostName, String channel, String message) {
  195. System.out.println("NOTICE <" + user + ">: " + message);
  196. if (user.equalsIgnoreCase("nickserv")) {
  197. if (message.toLowerCase().contains("/msg nickserv identify")) {
  198. try {
  199. System.out.println("LOGGING IN");
  200. out.write("PRIVMSG Nickserv :identify " + password);
  201. out.newLine();
  202. out.flush();
  203. } catch (Exception e) {
  204.  
  205. }
  206. }
  207. }
  208. }
  209.  
  210. public void onModeChange(String user, String hostName, String channel, String type) {
  211. System.out.println("MODE CHANGE <" + user + ">: " + type);
  212.  
  213. }
  214.  
  215. public void ghost(String user) {
  216. if (user.equalsIgnoreCase("Kosaki_is_gay")) {
  217. try {
  218. out.write("PRIVMSG NICKSERV :ghost " + user + " " + password);
  219. out.newLine();
  220. out.flush();
  221. } catch (Exception e) {
  222. }
  223. }
  224. }
  225.  
  226.  
  227. public void onKick(String user, String hostName, String channel, String recipient, String message) {
  228. try {
  229. System.out.println("<" + recipient + "> has been kicked by " + user + "[" + hostName + "] from " + channel + " (" + message + ")");
  230.  
  231. out.write("JOIN " + channel);
  232. out.newLine();
  233. out.write("NOTICE " + user + " :You are an asshole! I never \"" + message + "\"");
  234. out.newLine();
  235. out.flush();
  236. } catch (Exception e) {
  237.  
  238. }
  239.  
  240. }
  241.  
  242. public void onPing(String host) {
  243. try {
  244. out.write("PONG " + host);
  245. out.newLine();
  246. out.flush();
  247. System.out.println("Handled ping from " + host + " like a champ. Pong, nigger.");
  248. } catch (Exception e) {
  249. e.printStackTrace();
  250. }
  251.  
  252. }
  253.  
  254. public void onError(String error) {
  255. System.out.println("!!!!!!!!!!ERROR!!!!!!!!!");
  256. System.out.println(error);
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement