--- /dev/null
+/*
+ * XdccDownloader - Reply.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.irc;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import com.google.common.base.Optional;
+
+/**
+ * A reply from the IRC server. A reply contains an optional source, a command
+ * name (which may be a numeric code), and a list of parameters.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class Reply {
+
+ /** The source of the reply. */
+ private final Optional<String> source;
+
+ /** The command of the reply (may be numeric). */
+ private final String command;
+
+ /** The parameters of the reply. */
+ private final List<String> parameters;
+
+ /**
+ * Creates a new reply.
+ *
+ * @param source
+ * The source of the reply
+ * @param command
+ * The command of the reply (may be numeric)
+ * @param parameters
+ * The parameters of the reply
+ */
+ private Reply(Optional<String> source, String command, List<String> parameters) {
+ this.source = source;
+ this.command = command;
+ this.parameters = parameters;
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Returns the source of the reply.
+ *
+ * @return The source of the reply, or {@link Optional#absent()}
+ */
+ public Optional<String> source() {
+ return source;
+ }
+
+ /**
+ * Returns the command of the reply.
+ *
+ * @return The command of the reply (may be numeric)
+ */
+ public String command() {
+ return command;
+ }
+
+ /**
+ * Returns the parameters of the reply.
+ *
+ * @return The parameters of the reply
+ */
+ public List<String> parameters() {
+ return Collections.unmodifiableList(parameters);
+ }
+
+ //
+ // STATIC METHODS
+ //
+
+ /**
+ * Parses the given line into a reply.
+ *
+ * @param line
+ * The line to parse
+ * @return The parsed reply
+ */
+ public static Reply parseLine(String line) {
+ String remainingLine = line;
+
+ /* parse source. */
+ Optional<String> source = Optional.absent();
+ if (remainingLine.startsWith(":")) {
+ source = Optional.of(getFirstWord(remainingLine).substring(1));
+ remainingLine = removeFirstWord(remainingLine);
+ }
+
+ /* parse command. */
+ String command = getFirstWord(remainingLine);
+ remainingLine = removeFirstWord(remainingLine);
+
+ /* parse parameters. */
+ List<String> parameters = new ArrayList<String>();
+ while (remainingLine.length() > 0) {
+ if (remainingLine.startsWith(":")) {
+ parameters.add(remainingLine.substring(1));
+ break;
+ }
+ String parameter = getFirstWord(remainingLine);
+ parameters.add(parameter);
+ remainingLine = removeFirstWord(remainingLine);
+ }
+
+ return new Reply(source, command, parameters);
+ }
+
+ /**
+ * Returns the first word of the given line.
+ *
+ * @param line
+ * The line to get the first word of
+ * @return The first word of the line
+ */
+ private static String getFirstWord(String line) {
+ if (line.indexOf(' ') > -1) {
+ return line.substring(0, line.indexOf(' '));
+ }
+ return line;
+ }
+
+ /**
+ * Returns the given line with the first word removed.
+ *
+ * @param line
+ * The line from which to remove the first word
+ * @return The line with the first word removed
+ */
+ private static String removeFirstWord(String line) {
+ if (line.indexOf(' ') > -1) {
+ return line.substring(line.indexOf(' ') + 1);
+ }
+ return "";
+ }
+
+}
--- /dev/null
+/*
+ * XdccDownloader - ReplyTest.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.irc;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import com.google.common.base.Optional;
+import org.testng.annotations.Test;
+
+/**
+ * Tests for {@link Reply}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ReplyTest {
+
+ @Test
+ public void testParser() {
+ Reply reply;
+
+ reply = Reply.parseLine("NOTICE AUTH :*** Processing your connection");
+ assertThat(reply, notNullValue());
+ assertThat(reply.source(), is(Optional.<String>absent()));
+ assertThat(reply.command(), is("NOTICE"));
+ assertThat(reply.parameters(), hasSize(2));
+ assertThat(reply.parameters().get(0), is("AUTH"));
+ assertThat(reply.parameters().get(1), is("*** Processing your connection"));
+
+ reply = Reply.parseLine(":ParaDMON!services@paraphysics.services PRIVMSG QshelTier :\u0001VERSION\u0001");
+ assertThat(reply, notNullValue());
+ assertThat(reply.source(), is(Optional.of("ParaDMON!services@paraphysics.services")));
+ assertThat(reply.command(), is("PRIVMSG"));
+ assertThat(reply.parameters(), hasSize(2));
+ assertThat(reply.parameters().get(0), is("QshelTier"));
+ assertThat(reply.parameters().get(1), is("\u0001VERSION\u0001"));
+ }
+
+}