Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- import java.io.*;
- import java.net.*;
- public class ircbot {
- String server;
- int port;
- String channel;
- String username;
- String password;
- BufferedWriter writer;
- BufferedReader reader;
- Socket socket;
- public ircbot (String server, int port, String username) {
- this.server = server;
- this.port = port;
- this.username = username;
- }
- public ircbot (String server, int port, String username, String channel) {
- this(server, port, username);
- this.channel = channel;
- }
- public ircbot (String server, int port, String username, String channel, String password) {
- this(server, port, username, channel);
- this.password = password;
- }
- public String getServer() {
- return server;
- }
- public int getPort() {
- return port;
- }
- public String getUsername() {
- return username;
- }
- public String getChannel() {
- return channel;
- }
- public void connect () {
- try {
- this.socket = new Socket(server, port);
- this.writer = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream( )));
- this.reader = new BufferedReader( new InputStreamReader( socket.getInputStream( )));
- this.sendLine("NICK " + username + "\r\n");
- this.sendLine("USER " + password + " 8 * : IRC Bot\r\n");
- String line = null;
- while ((line = this.reader.readLine( )) != null) {
- if (line.indexOf("004") >= 0) {
- break;
- } else if (line.indexOf("433") >= 0) {
- System.out.println("Nickname is already in use.");
- return;
- }
- }
- if (channel!=null){
- this.sendLine("JOIN " + channel + "\r\n");
- }
- if (username!=null){
- this.sendLine("PRIVMSG NICKSERV IDENTIFY " + password + "\r\n");
- }
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- public void nick (String username) {
- this.username = username;
- this.sendLine("NICK " + username + "\r\n");
- }
- public void pass (String password) {
- this.password = password;
- this.sendLine("USER " + password + " 8 * : \r\n");
- }
- public void join (String channel) {
- this.channel = channel;
- this.sendLine("JOIN " + channel + "\r\n");
- }
- public void leave () {
- this.sendLine("PART\r\n");
- }
- public void change (String channel) {
- this.channel = channel;
- this.leave();
- this.join(channel);
- }
- public String readLine () {
- String blah = null;
- try {
- blah = this.reader.readLine();
- } catch (IOException e) {
- System.out.println(e);
- }
- return blah;
- }
- public void sendLine (String line) {
- try {
- this.writer.write(line);
- this.writer.flush();
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- public static void main (String[] args) {
- // USE BELOW AS SAMPLE CODE
- ircbot blah = new ircbot("myserver", 6667, "mybot", "#mychannel", "mypass");
- blah.connect();
- String line = null;
- while ((line = blah.readLine( )) != null) {
- System.out.println(line);
- try {
- if (line.contains("PING")) {
- blah.sendLine("PONG " + line.substring(5) + "\r\n");
- } else if (line.contains("hi bot")) {
- String send = "PRIVMSG " + blah.channel + " hi there.\r\n";
- blah.sendLine(send);
- System.out.println("\nRECEIVED - " + line + "\nSENT - " + send);
- } else {
- }
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement