Add method to send messages.
[xudocci.git] / src / main / java / net / pterodactylus / irc / Connection.java
index 69aa648..ade43c9 100644 (file)
@@ -32,7 +32,6 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.SynchronousQueue;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.net.SocketFactory;
@@ -52,11 +51,10 @@ import net.pterodactylus.irc.event.PrivateMessageReceived;
 import net.pterodactylus.irc.event.UnknownReplyReceived;
 import net.pterodactylus.irc.util.RandomNickname;
 
-import com.beust.jcommander.internal.Maps;
-import com.beust.jcommander.internal.Sets;
 import com.google.common.base.Optional;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 import com.google.common.eventbus.EventBus;
-import com.google.common.eventbus.Subscribe;
 import com.google.common.io.Closeables;
 import com.google.common.util.concurrent.AbstractExecutionThreadService;
 import com.google.common.util.concurrent.Service;
@@ -162,7 +160,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
         *
         * @param username
         *              The username to use
-        * @return
+        * @return This connection
         */
        public Connection username(String username) {
                this.username = Optional.fromNullable(username);
@@ -202,43 +200,26 @@ public class Connection extends AbstractExecutionThreadService implements Servic
         *
         * @param channel
         *              The channel to join
-        * @return {@code true} if the channel was joined, {@code false} otherwise
+        * @throws IOException
+        *              if an I/O error occurs
         */
-       public boolean joinChannel(final String channel) {
-               final SynchronousQueue<Boolean> result = new SynchronousQueue<Boolean>();
-               Object eventHandler = new Object() {
-
-                       @Subscribe
-                       public void channelJoined(ChannelJoined channelJoined) throws InterruptedException {
-                               if (!channelJoined.channel().equalsIgnoreCase(channel)) {
-                                       return;
-                               }
-                               Optional<String> nickname = channelJoined.client().nick();
-                               if (!nickname.isPresent() || (nickname.isPresent() && nickname().equalsIgnoreCase(nickname.get()))) {
-                                       eventBus.unregister(this);
-                                       result.put(true);
-                               }
-                       }
+       public void joinChannel(final String channel) throws IOException {
+               connectionHandler.sendCommand("JOIN", channel);
+       }
 
-                       @Subscribe
-                       public void channelNotJoined(ChannelNotJoined channelNotJoined) throws InterruptedException {
-                               if (!channelNotJoined.channel().equalsIgnoreCase(channel)) {
-                                       return;
-                               }
-                               eventBus.unregister(this);
-                               result.put(false);
-                       }
-               };
-               eventBus.register(eventHandler);
-               try {
-                       connectionHandler.sendCommand("JOIN", channel);
-                       return result.take();
-               } catch (IOException ioe1) {
-                       eventBus.unregister(eventHandler);
-               } catch (InterruptedException ie1) {
-                       /* TODO - how to handle? */
-               }
-               return false;
+       /**
+        * Sends a message to the given recipient, which may be a channel or another
+        * nickname.
+        *
+        * @param recipient
+        *              The recipient of the message
+        * @param message
+        *              The message
+        * @throws IOException
+        *              if an I/O error occurs
+        */
+       public void sendMessage(String recipient, String message) throws IOException {
+               connectionHandler.sendCommand("PRIVMSG", recipient, message);
        }
 
        //
@@ -294,10 +275,11 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                /* most common events. */
                                if (command.equalsIgnoreCase("PRIVMSG")) {
                                        String recipient = parameters.get(0);
+                                       String message = parameters.get(1);
                                        if (!channelTypes.contains(recipient.charAt(0))) {
-                                               eventBus.post(new PrivateMessageReceived(this, reply.source().get(), parameters.get(1)));
+                                               eventBus.post(new PrivateMessageReceived(this, reply.source().get(), message));
                                        } else {
-                                               eventBus.post(new ChannelMessageReceived(this, recipient, reply.source().get(), parameters.get(1)));
+                                               eventBus.post(new ChannelMessageReceived(this, recipient, reply.source().get(), message));
                                        }
 
                                /* replies 001-004 don’t hold information but they have to be sent on a successful connection. */
@@ -362,7 +344,6 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                } else if (command.equals("332")) {
                                        eventBus.post(new ChannelTopic(this, parameters.get(1), parameters.get(2)));
                                } else if (command.equals("353")) {
-                                       String channel = parameters.get(2);
                                        for (String nickname : parameters.get(3).split(" ")) {
                                                if (nickPrefixes.containsKey(nickname.substring(0, 1))) {
                                                        nicks.add(new Nickname(nickname.substring(1), nickname.substring(0, 1)));
@@ -481,7 +462,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                        setParameters.add(maybeSetParameter.get());
                                }
                        }
-                       sendCommand(command, setParameters.toArray(new String[0]));
+                       sendCommand(command, setParameters.toArray(new String[setParameters.size()]));
                }
 
                /**