Commits

Kenneth Endfinger  committed b4166b5

Initial Commit

  • Participants

Comments (0)

Files changed (14)

+/*.iml

File impl/irc4j/impl/TestBot.java

+package irc4j.impl;
+
+import irc4j.IRC4J;
+import irc4j.IRC4JConfig;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+public class TestBot {
+
+    public static void main(String[] args) {
+        IRC4JConfig config = new IRC4JConfig();
+        config.setServer("irc.esper.net", 6667);
+        config.setNickname("IRC4J");
+        config.setRealName("IRC4J");
+        config.setUsername("IRC4J");
+        IRC4J bot = config.create();
+        bot.setEventListener(new TestEventListener());
+        try {
+            bot.connect();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

File impl/irc4j/impl/TestEventListener.java

+package irc4j.impl;
+
+import irc4j.EventListener;
+import irc4j.event.ConnectEvent;
+import irc4j.event.MessageEvent;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class TestEventListener extends EventListener {
+    @Override
+    public void onMessageEvent(MessageEvent event) {
+        System.out.println("Message: " + event.getUser() + " Channel: " + event.getChannel() + " Message: " + event.getMessage());
+        List<String> message = Arrays.asList(event.getMessage().split(" "));
+        if (event.getMessage().startsWith("!")) {
+            if (message.get(0).equals("!example")) {
+                event.getBot().sendRawLine("PRIVMSG " + event.getChannel() + " :" + event.getUser().substring(1, event.getUser().indexOf('!')) + ": Hi!");
+            } else if (message.get(0).equals("!quit")) {
+                event.getBot().disconnect();
+            }
+        }
+    }
+
+    @Override
+    public void onConnectEvent(ConnectEvent event) {
+        event.getBot().sendRawLine("JOIN #kaendfinger");
+    }
+}

File src/irc4j/Event.java

+package irc4j;
+
+public class Event {
+    
+}

File src/irc4j/EventListener.java

+package irc4j;
+
+import irc4j.event.ConnectEvent;
+import irc4j.event.MessageEvent;
+
+public class EventListener {
+
+    public void onMessageEvent(MessageEvent event) {
+
+    }
+
+    public void onConnectEvent(ConnectEvent event) {
+
+    }
+}

File src/irc4j/IRC4J.java

+package irc4j;
+
+import irc4j.event.ConnectEvent;
+import irc4j.event.MessageEvent;
+import irc4j.io.InputThread;
+import irc4j.io.OutputThread;
+import irc4j.socket.SocketHandler;
+import irc4j.utils.IRCUtils;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.util.List;
+
+public class IRC4J {
+
+    private SocketHandler socketHandler;
+    private InputThread inputThread;
+    private OutputThread outputThread;
+    private IRC4JConfig config;
+    private EventListener listener;
+    private boolean connectionReady = false;
+    private boolean hasConnected = false;
+
+    protected IRC4J(IRC4JConfig config) {
+        this.config = config;
+        this.socketHandler = new SocketHandler(this, config.getSocketAddress());
+    }
+
+    public void connect() throws IOException {
+        socketHandler.connect();
+        this.inputThread = socketHandler.createInputThread();
+        this.outputThread = socketHandler.createOutputThread();
+        inputThread.start();
+        try {
+            Thread.sleep(1000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        outputThread.sendRawLine("NICK " + config.getNickname());
+        outputThread.sendRawLine("USER " + config.getUsername() + " 8 * : " + config.getRealname());
+    }
+
+    public void setEventListener(EventListener listener) {
+        this.listener = listener;
+    }
+
+    public Socket getSocket() {
+        return socketHandler.getSocket();
+    }
+
+    public void handleLine(String line) {
+        List<String> raw = IRCUtils.tokenizeLine(line);
+
+        if (raw.get(0).equals("PING")) {
+            outputThread.sendRawLine("PONG :" + raw.get(1));
+            if (!connectionReady) {
+                this.connectionReady = true;
+            }
+        } else if (raw.get(1).equals("NOTICE")) {
+            StringBuilder builder = new StringBuilder();
+            for (int i = 3; i<raw.size(); i++) {
+                builder.append(raw.get(i)).append(" ");
+            }
+            System.out.println("NOTICE FROM " + raw.get(0) + ": " + builder.toString());
+        } else if (raw.get(1).equals("PRIVMSG")) {
+            MessageEvent event = new MessageEvent(this, raw.get(0), raw.get(2), raw.get(3));
+            listener.onMessageEvent(event);
+        }
+        if (connectionReady || !hasConnected) {
+            listener.onConnectEvent(new ConnectEvent(this));
+            this.connectionReady = false;
+            this.hasConnected = true;
+        }
+    }
+
+    public void disconnect() {
+        sendRawLine("QUIT :IRC4J has disconnected.");
+        inputThread.stop();
+        this.hasConnected = false;
+        this.connectionReady = false;
+    }
+
+    public boolean isConnected() {
+        return socketHandler.isConnected();
+    }
+
+    public void sendRawLine(String line) {
+        outputThread.sendRawLine(line);
+    }
+}

File src/irc4j/IRC4JConfig.java

+package irc4j;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+
+public class IRC4JConfig {
+
+    private InetSocketAddress address;
+    private String nickname;
+    private String username;
+    private String realname;
+
+    public IRC4JConfig() {
+
+    }
+
+    public void setServer(String hostname, int port) {
+        this.address = new InetSocketAddress(hostname, port);
+    }
+
+    public void setNickname(String nickname) {
+        this.nickname = nickname;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public void setRealName(String realName) {
+        this.realname = realName;
+    }
+
+    public InetSocketAddress getSocketAddress() {
+        return address;
+    }
+
+    public IRC4J create() {
+        return new IRC4J(this);
+    }
+
+    public String getNickname() {
+        return nickname;
+    }
+
+    public String getRealname() {
+        return realname;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+}

File src/irc4j/User.java

+package irc4j;
+
+public class User {
+    private String nickname;
+    private String realName;
+    private String hostmask;
+
+    public User(String nickname) {
+
+    }
+
+    public String getNick() {
+        return nickname;
+    }
+}

File src/irc4j/event/ConnectEvent.java

+package irc4j.event;
+
+import irc4j.Event;
+import irc4j.IRC4J;
+
+public class ConnectEvent extends Event {
+
+    private IRC4J bot;
+
+    public ConnectEvent(IRC4J bot) {
+        this.bot = bot;
+    }
+
+    public IRC4J getBot() {
+        return bot;
+    }
+}

File src/irc4j/event/MessageEvent.java

+package irc4j.event;
+
+import irc4j.Event;
+import irc4j.IRC4J;
+
+public class MessageEvent extends Event {
+    private String user;
+    private String message;
+    private String channel;
+    private IRC4J bot;
+
+    public MessageEvent(IRC4J bot, String user, String channel, String message) {
+        this.user = user;
+        this.channel = channel;
+        this.message = message;
+        this.bot = bot;
+    }
+
+    public String getUser() {
+        return user;
+    }
+
+    public String getChannel() {
+        return channel;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public IRC4J getBot() {
+        return bot;
+    }
+}

File src/irc4j/io/InputThread.java

+package irc4j.io;
+
+import irc4j.IRC4J;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+
+public class InputThread implements Runnable {
+
+    private BufferedReader reader;
+    private IRC4J irc4J;
+    private Thread runThread;
+    private boolean stopThread;
+
+    public InputThread(IRC4J irc4J, BufferedReader reader) {
+        this.reader = reader;
+        this.irc4J = irc4J;
+    }
+
+    public void start() {
+        this.runThread = new Thread(this);
+        runThread.start();
+    }
+
+    public void stop() {
+        this.stopThread = true;
+        if (runThread.isAlive()) {
+            runThread.interrupt();
+        }
+    }
+
+    @Override
+    public void run() {
+        String line;
+        this.stopThread = false;
+        try {
+            while ((line = reader.readLine()) != null) {
+                if (!irc4J.getSocket().isConnected()) {
+                    this.stopThread = true;
+                }
+                if (stopThread) {
+                    break;
+                }
+                Thread handleLineThread = new Thread(new HandleLineThread(line));
+                handleLineThread.start();
+                if (stopThread) {
+                    break;
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public class HandleLineThread implements Runnable {
+
+        private String line;
+
+        public HandleLineThread(String line) {
+            this.line = line;
+        }
+
+        @Override
+        public void run() {
+            irc4J.handleLine(line);
+        }
+    }
+
+}

File src/irc4j/io/OutputThread.java

+package irc4j.io;
+
+import irc4j.IRC4J;
+
+import java.io.PrintWriter;
+import java.util.concurrent.LinkedBlockingQueue;
+
+public class OutputThread {
+
+    private PrintWriter writer;
+    private IRC4J irc4J;
+    private LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
+
+
+    public OutputThread(IRC4J irc4J, PrintWriter writer) {
+        this.writer = writer;
+        this.irc4J = irc4J;
+    }
+
+    public PrintWriter getWriter() {
+        return writer;
+    }
+
+    public void sendRawLine(String line) {
+        System.out.println("SENT: " + line);
+        writer.println(line);
+        writer.flush();
+    }
+
+    private void connectionCheck() {
+        if (!irc4J.isConnected()) {
+            throw new RuntimeException("IRC4J has not connected to the IRC server!");
+        }
+    }
+}

File src/irc4j/socket/SocketHandler.java

+package irc4j.socket;
+
+import irc4j.IRC4J;
+import irc4j.io.InputThread;
+import irc4j.io.OutputThread;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+
+public class SocketHandler {
+
+    private Socket socket;
+    private InetSocketAddress address;
+    private IRC4J irc4J;
+
+    public SocketHandler() {
+        this.socket = new Socket();
+    }
+
+    public SocketHandler(IRC4J irc4J, InetSocketAddress address) {
+        this.socket = new Socket();
+        this.irc4J = irc4J;
+        this.address = address;
+    }
+
+    public void connect(String host, int port) throws IOException {
+        socket.connect(new InetSocketAddress(host, port));
+    }
+
+    public void connect() throws IOException {
+        socket.connect(address);
+    }
+
+    public Socket getSocket() {
+        return socket;
+    }
+
+    public boolean isConnected() {
+        return socket.isConnected();
+    }
+
+    public OutputThread createOutputThread() throws IOException {
+        return new OutputThread(irc4J, new PrintWriter(socket.getOutputStream()));
+    }
+
+    public InputThread createInputThread() throws IOException {
+        return new InputThread(irc4J, new BufferedReader(new InputStreamReader(socket.getInputStream())));
+    }
+
+}

File src/irc4j/utils/IRCUtils.java

+package irc4j.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class IRCUtils {
+    public static List<String> tokenizeLine(String input) {
+        List<String> retn = new ArrayList<String>();
+
+        if (input == null || input.length() == 0)
+            return retn;
+
+        String temp = input;
+
+        while (true) {
+            if (temp.startsWith(":") && retn.size() > 0) {
+                retn.add(temp.substring(1));
+
+                return retn;
+            }
+
+            String[] split = temp.split(" ", 2);
+            retn.add(split[0]);
+
+            if (split.length > 1)
+                temp = split[1];
+            else
+                break;
+        }
+
+        return retn;
+    }
+}