Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ircbot;
- /**
- * JavaBot (version 1.2)
- *
- * MIT License, dydx (Josh Sandlin) <dydx@thenullbyte.org>
- *
- */
- import java.io.*;
- import java.net.*;
- import java.util.regex.*;
- import java.util.Date;
- public class Main
- {
- public static void main( String[] args ) throws IOException
- {
- //connection variables
- String server = "irc.snappeh.com";
- String nick = "JavaBot";
- String login = "JavaBot";
- String channel = "#bots";
- int port = 6667;
- try {
- //our socket we're connected with
- Socket irc = new Socket( server, port );
- //out output stream
- BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( irc.getOutputStream() ) );
- //our input stream
- BufferedReader br = new BufferedReader( new InputStreamReader( irc.getInputStream() ) );
- //authenticate with the server
- bw.write( "NICK " + nick + "\n" );
- bw.write( "USER " + login + " thenullbyte.org JB: Java Bot\n" );
- bw.flush();
- //join a channel
- bw.write( "JOIN " + channel + "\n" );
- bw.write( "PRIVMSG " + channel + " :Whats up everybody?\n" );
- bw.flush();
- System.out.println( "Successfully connected to IRC" );
- String currLine = null;
- while( ( currLine = br.readLine() ) != null )
- {
- //checks for PING, if one is found; return a PONG
- Pattern pingRegex = Pattern.compile( "^PING", Pattern.CASE_INSENSITIVE );
- Matcher ping = pingRegex.matcher( currLine );
- if( ping.find() )
- {
- bw.write( "PONG " + currLine.substring( 5 ) + "\n" );
- bw.flush();
- }
- //!exit - quit current irc room
- Pattern exitRegex = Pattern.compile( "!exit", Pattern.CASE_INSENSITIVE );
- Matcher exit = exitRegex.matcher( currLine );
- if( exit.find() )
- {
- bw.write( "PRIVMSG " + channel + " :Bye Bye\n" );
- bw.write( "PART " + channel + "\n" );
- bw.flush();
- irc.close();
- }
- //!time - return current time
- Pattern timeRegex = Pattern.compile( "!time", Pattern.CASE_INSENSITIVE );
- Matcher time = timeRegex.matcher( currLine );
- if( time.find() )
- {
- Date d = new Date();
- bw.write( "PRIVMSG " + channel + " :" + d +"\n" );
- bw.flush();
- }
- //!sayhi - shows a little message saying hello
- Pattern helloRegex = Pattern.compile( "!sayhi", Pattern.CASE_INSENSITIVE );
- Matcher hello = helloRegex.matcher( currLine );
- if( hello.find() )
- {
- bw.write( "PRIVMSG " + channel + " :Hello, I'm a JavaBot. I was coded by dydx in Java!\n");
- bw.flush();
- }
- //!join <room> - changes to a new room and sets the variables accordingly
- Pattern joinRegex = Pattern.compile( "!join", Pattern.CASE_INSENSITIVE );
- Matcher join = joinRegex.matcher( currLine );
- if( join.find() )
- {
- String[] token = currLine.split( " " );
- bw.write( "PRIVMSG " + channel + " :Im going over to " + token[4] + "\n" );
- bw.write( "PART " + channel + "\n" );
- String oldChannel = channel;
- channel = token[4];
- bw.write( "JOIN " + channel + "\n" );
- bw.flush();
- }
- }
- } catch ( UnknownHostException e ) {
- System.err.println( "No such host" );
- } catch ( IOException e ) {
- System.err.println( "There was an error connecting to the host" );
- }
- }
- }
Add Comment
Please, Sign In to add comment