Prevent NPE on closing.
[xudocci.git] / src / main / java / net / pterodactylus / irc / Connection.java
index 2feaec4..f48b205 100644 (file)
@@ -59,6 +59,7 @@ import net.pterodactylus.irc.event.NicknameInUseReceived;
 import net.pterodactylus.irc.event.NoNicknameGivenReceived;
 import net.pterodactylus.irc.event.PrivateMessageReceived;
 import net.pterodactylus.irc.event.PrivateNoticeReceived;
+import net.pterodactylus.irc.event.ReplyReceived;
 import net.pterodactylus.irc.event.UnknownReplyReceived;
 import net.pterodactylus.irc.util.RandomNickname;
 import net.pterodactylus.xdcc.util.io.BandwidthCountingInputStream;
@@ -148,6 +149,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
 
        /**
         * Returns the hostname of the remote end of the connection.
+        *
         * @return The remote’s hostname
         */
        public String hostname() {
@@ -156,6 +158,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
 
        /**
         * Returns the port number of the remote end of the connection.
+        *
         * @return The remote’s port number
         */
        public int port() {
@@ -322,7 +325,9 @@ public class Connection extends AbstractExecutionThreadService implements Servic
         *              if an I/O error occurs
         */
        public void close() throws IOException {
-               connectionHandler.close();
+               if (connectionHandler != null) {
+                       connectionHandler.close();
+               }
        }
 
        //
@@ -371,6 +376,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
 
                        while (connected) {
                                Reply reply = connectionHandler.readReply();
+                               eventBus.post(new ReplyReceived(this, reply));
                                logger.finest(String.format("<< %s", reply));
                                String command = reply.command();
                                List<String> parameters = reply.parameters();
@@ -509,6 +515,9 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                } catch (IOException ioe1) {
                        logger.log(Level.WARNING, "I/O error", ioe1);
                        eventBus.post(new ConnectionClosed(this, ioe1));
+               } catch (RuntimeException re1) {
+                       logger.log(Level.SEVERE, "Runtime error", re1);
+                       eventBus.post(new ConnectionClosed(this, re1));
                } finally {
                        established = false;
                        logger.info("Closing Connection.");
@@ -548,7 +557,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                }
                        } else if (messageWords[1].equalsIgnoreCase("ACCEPT")) {
                                Optional<Integer> port = Optional.fromNullable(Ints.tryParse(messageWords[3]));
-                               long position = Optional.fromNullable(Longs.tryParse(messageWords[4])).or(-1L);
+                               long position = (messageWords.length > 4) ? Optional.fromNullable(Longs.tryParse(messageWords[4])).or(-1L) : -1;
                                if (port.isPresent()) {
                                        eventBus.post(new DccAcceptReceived(this, client, messageWords[2], port.get(), position));
                                } else {