Example of a Java IRC Bot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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;
 
        //for security
        String owner = "dydx";
 
        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 " + channel + "\n" );
                    bw.flush();
                }
 
 
                //check for ownership
                Pattern checkOwner = Pattern.compile( "^:"+owner, Pattern.CASE_INSENSITIVE );
                Matcher ownership = checkOwner.matcher( currLine );
 
                //!exit - quit current irc room
                Pattern exitRegex = Pattern.compile( "!exit", Pattern.CASE_INSENSITIVE );
                Matcher exit = exitRegex.matcher( currLine );
                if( exit.find() && ownership.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()  && ownership.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()  && ownership.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()  && ownership.find() )
                {
                    String[] token = currLine.split( " " );
                    bw.write( "PRIVMSG " + channel + " :Im going over to " + token[4] + "\n" );
                    bw.write( "PART " + channel + "\n" );
                    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" );
        } 
    }
}
</room></dydx>
Client-to-client protocol
Example of a C# IRC Bot