Guest User

Untitled

a guest
May 21st, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. package ircbot;
  2.  
  3. /**
  4. * JavaBot (version 1.2)
  5. *
  6. * MIT License, dydx (Josh Sandlin) <dydx@thenullbyte.org>
  7. *
  8. */
  9.  
  10. import java.io.*;
  11. import java.net.*;
  12. import java.util.regex.*;
  13. import java.util.Date;
  14.  
  15. public class Main
  16. {
  17. public static void main( String[] args ) throws IOException
  18. {
  19. //connection variables
  20. String server = "irc.snappeh.com";
  21. String nick = "JavaBot";
  22. String login = "JavaBot";
  23. String channel = "#bots";
  24. int port = 6667;
  25.  
  26. try {
  27.  
  28. //our socket we're connected with
  29. Socket irc = new Socket( server, port );
  30. //out output stream
  31. BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( irc.getOutputStream() ) );
  32. //our input stream
  33. BufferedReader br = new BufferedReader( new InputStreamReader( irc.getInputStream() ) );
  34.  
  35. //authenticate with the server
  36. bw.write( "NICK " + nick + "\n" );
  37. bw.write( "USER " + login + " thenullbyte.org JB: Java Bot\n" );
  38. bw.flush();
  39.  
  40. //join a channel
  41. bw.write( "JOIN " + channel + "\n" );
  42. bw.write( "PRIVMSG " + channel + " :Whats up everybody?\n" );
  43. bw.flush();
  44. System.out.println( "Successfully connected to IRC" );
  45.  
  46. String currLine = null;
  47. while( ( currLine = br.readLine() ) != null )
  48. {
  49. //checks for PING, if one is found; return a PONG
  50. Pattern pingRegex = Pattern.compile( "^PING", Pattern.CASE_INSENSITIVE );
  51. Matcher ping = pingRegex.matcher( currLine );
  52. if( ping.find() )
  53. {
  54. bw.write( "PONG " + currLine.substring( 5 ) + "\n" );
  55. bw.flush();
  56. }
  57.  
  58. //!exit - quit current irc room
  59. Pattern exitRegex = Pattern.compile( "!exit", Pattern.CASE_INSENSITIVE );
  60. Matcher exit = exitRegex.matcher( currLine );
  61. if( exit.find() )
  62. {
  63. bw.write( "PRIVMSG " + channel + " :Bye Bye\n" );
  64. bw.write( "PART " + channel + "\n" );
  65. bw.flush();
  66. irc.close();
  67. }
  68.  
  69. //!time - return current time
  70. Pattern timeRegex = Pattern.compile( "!time", Pattern.CASE_INSENSITIVE );
  71. Matcher time = timeRegex.matcher( currLine );
  72. if( time.find() )
  73. {
  74. Date d = new Date();
  75. bw.write( "PRIVMSG " + channel + " :" + d +"\n" );
  76. bw.flush();
  77. }
  78.  
  79. //!sayhi - shows a little message saying hello
  80. Pattern helloRegex = Pattern.compile( "!sayhi", Pattern.CASE_INSENSITIVE );
  81. Matcher hello = helloRegex.matcher( currLine );
  82. if( hello.find() )
  83. {
  84. bw.write( "PRIVMSG " + channel + " :Hello, I'm a JavaBot. I was coded by dydx in Java!\n");
  85. bw.flush();
  86. }
  87.  
  88. //!join <room> - changes to a new room and sets the variables accordingly
  89. Pattern joinRegex = Pattern.compile( "!join", Pattern.CASE_INSENSITIVE );
  90. Matcher join = joinRegex.matcher( currLine );
  91. if( join.find() )
  92. {
  93. String[] token = currLine.split( " " );
  94. bw.write( "PRIVMSG " + channel + " :Im going over to " + token[4] + "\n" );
  95. bw.write( "PART " + channel + "\n" );
  96. String oldChannel = channel;
  97. channel = token[4];
  98. bw.write( "JOIN " + channel + "\n" );
  99. bw.flush();
  100. }
  101. }
  102. } catch ( UnknownHostException e ) {
  103. System.err.println( "No such host" );
  104. } catch ( IOException e ) {
  105. System.err.println( "There was an error connecting to the host" );
  106. }
  107. }
  108. }
Add Comment
Please, Sign In to add comment