You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/05/02 08:25:45 UTC

[2/2] camel git commit: Add support for listing users of a channel after it has been joined.

Add support for listing users of a channel after
it has been joined.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/abfae23c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/abfae23c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/abfae23c

Branch: refs/heads/master
Commit: abfae23c8e69f118890a1baadcb8beb637200175
Parents: f3f2f34
Author: Wladislaw Mitzel <mi...@tawadi.de>
Authored: Sun May 1 13:05:12 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon May 2 08:24:59 2016 +0200

----------------------------------------------------------------------
 components/camel-irc/src/main/docs/irc.adoc     | 19 +++++
 .../camel/component/irc/IrcConfiguration.java   | 17 +++-
 .../apache/camel/component/irc/IrcEndpoint.java |  3 +
 .../irc/IrcsListUsersIntegrationTest.java       | 90 ++++++++++++++++++++
 .../src/test/resources/it-list-users.properties |  3 +
 5 files changed, 131 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/abfae23c/components/camel-irc/src/main/docs/irc.adoc
----------------------------------------------------------------------
diff --git a/components/camel-irc/src/main/docs/irc.adoc b/components/camel-irc/src/main/docs/irc.adoc
index 07b96e9..5660992 100644
--- a/components/camel-irc/src/main/docs/irc.adoc
+++ b/components/camel-irc/src/main/docs/irc.adoc
@@ -53,6 +53,7 @@ The IRC component supports 23 endpoint options which are listed below:
 | port | common | 6667,6668,6669 | int | Port number for the IRC chat server
 | autoRejoin | common | true | boolean | Whether to auto re-join when being kicked
 | colors | common | true | boolean | Whether or not the server supports color codes.
+| namesOnJoin | common | false | boolean | Whether or not to send the NAMES command after a channel has been joined.
 | nickname | common |  | String | The nickname used in chat.
 | nickPassword | common |  | String | Your IRC server nickname password.
 | onJoin | common | true | boolean | Handle user join events.
@@ -170,6 +171,24 @@ For example we join 3 channels where as only channel 1 and 3 uses a key.
 irc:nick@irc.server.org?channels=#chan1,#chan2,#chan3&keys=chan1Key,,chan3key
 -----------------------------------------------------------------------------
 
+Getting a list of users of the channel
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Using the `namesOnJoin` option one can invoke the IRC-`NAMES` command after the component has joined a channel. 
+The server will reply with `irc.num = 353`. So in order to process the result the property `onReply` has to be `true`.
+Furthermore one has to filter the `onReply` exchanges in order to get the names.
+
+For example we want to get all exchanges that contain the usernames of the channel:
+
+[source,java]
+-----------------------------------------------------------------------------
+from("ircs:nick@myserver:1234/#mychannelname?listOnJoin=true&onReply=true")
+	.choice()
+		.when(header("irc.messageType").isEqualToIgnoreCase("REPLY"))
+			.filter(header("irc.num").isEqualTo("353"))
+			.to("mock:result").stop();
+-----------------------------------------------------------------------------
+
 [[IRC-SeeAlso]]
 See Also
 ^^^^^^^^

http://git-wip-us.apache.org/repos/asf/camel/blob/abfae23c/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcConfiguration.java b/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcConfiguration.java
index 4536ba2..8b107ad 100644
--- a/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcConfiguration.java
+++ b/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcConfiguration.java
@@ -83,6 +83,13 @@ public class IrcConfiguration implements Cloneable {
     private boolean onPrivmsg = true;
     @UriParam(defaultValue = "true")
     private boolean autoRejoin = true;
+	/**
+	 * Sends <code>NAMES</code> command to channel after joining it.<br>
+	 * {@link #onReply} has to be <code>true</code> in order to process the
+	 * result which will have the header value <code>irc.num = '353'</code>.
+	 */
+    @UriParam(defaultValue = "false")
+    private boolean namesOnJoin = false;
     private SSLContextParameters sslContextParameters;
     @UriParam
     private String nickPassword;
@@ -453,8 +460,16 @@ public class IrcConfiguration implements Cloneable {
     public void setNickPassword(String nickPassword) {
         this.nickPassword = nickPassword;
     }
+    
+    public boolean isNamesOnJoin() {
+		return namesOnJoin;
+	}
+
+	public void setNamesOnJoin(boolean namesOnJoin) {
+		this.namesOnJoin = namesOnJoin;
+	}
 
-    public String toString() {
+	public String toString() {
         return "IrcConfiguration[hostname: " + hostname + ", ports=" + Arrays.toString(ports) + ", username=" + username + "]";
     }
     

http://git-wip-us.apache.org/repos/asf/camel/blob/abfae23c/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcEndpoint.java b/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcEndpoint.java
index 97012ae..5ddef4a 100644
--- a/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcEndpoint.java
+++ b/components/camel-irc/src/main/java/org/apache/camel/component/irc/IrcEndpoint.java
@@ -204,6 +204,9 @@ public class IrcEndpoint extends DefaultEndpoint {
             }
             connection.doJoin(chn);
         }
+        if (configuration.isNamesOnJoin()) {
+			connection.doNames(chn);
+		}
     }
 }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/abfae23c/components/camel-irc/src/test/java/org/apache/camel/component/irc/IrcsListUsersIntegrationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-irc/src/test/java/org/apache/camel/component/irc/IrcsListUsersIntegrationTest.java b/components/camel-irc/src/test/java/org/apache/camel/component/irc/IrcsListUsersIntegrationTest.java
new file mode 100644
index 0000000..7bb34db
--- /dev/null
+++ b/components/camel-irc/src/test/java/org/apache/camel/component/irc/IrcsListUsersIntegrationTest.java
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.irc;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Integration test for the {@link IrcConfiguration#isNamesOnJoin()} option.
+ * Joins a channel and asserts that the username of the current test user is
+ * listed for the channel.
+ */
+public class IrcsListUsersIntegrationTest extends CamelTestSupport {
+	
+	private static final Logger LOGGER = LoggerFactory.getLogger(IrcsListUsersIntegrationTest.class);
+
+	/** message code for a reply to a <code>NAMES</code> command. */
+	public static final String IRC_RPL_NAMREPLY = "353";
+
+	/** irc component uri. configured by properties */
+	private static final String PRODUCER_URI = "ircs:{{test.user}}@{{test.server}}/{{test.room}}";
+
+	@EndpointInject(uri = "mock:result")
+	protected MockEndpoint resultEndpoint;
+
+	protected Properties properties;
+	
+	public IrcsListUsersIntegrationTest() throws IOException {
+		super();
+		properties = new Properties();
+		InputStream resourceAsStream = this.getClass().getResourceAsStream("/it-list-users.properties");
+		properties.load(resourceAsStream);
+	}
+	
+	@Override
+	protected RoutesBuilder createRouteBuilder() throws Exception {
+
+		return new RouteBuilder() {
+
+			@Override
+			public void configure() throws Exception {
+				LOGGER.debug("Creating new test route");
+				from(PRODUCER_URI + "?listOnJoin=true&onReply=true")
+					.choice()
+						.when(header("irc.messageType").isEqualToIgnoreCase("REPLY"))
+							.filter(header("irc.num").isEqualTo(IRC_RPL_NAMREPLY)).to("mock:result").stop();
+			}
+		};
+	}
+
+	@Test
+	public void test() throws Exception {
+		resultEndpoint.setMinimumExpectedMessageCount(1);
+		resultEndpoint.assertIsSatisfied();
+		String body = resultEndpoint.getExchanges().get(0).getIn().getBody(String.class);
+		LOGGER.debug("Received usernames: [{}]", body);
+		String username = properties.getProperty("test.user");
+		assertTrue("userlist does not contain test user", body.contains(username));
+	}
+	
+	@Override
+	protected Properties useOverridePropertiesWithPropertiesComponent() {
+		return properties;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/abfae23c/components/camel-irc/src/test/resources/it-list-users.properties
----------------------------------------------------------------------
diff --git a/components/camel-irc/src/test/resources/it-list-users.properties b/components/camel-irc/src/test/resources/it-list-users.properties
new file mode 100644
index 0000000..028c574
--- /dev/null
+++ b/components/camel-irc/src/test/resources/it-list-users.properties
@@ -0,0 +1,3 @@
+test.server=chat.freenode.net:6697
+test.room=#testroom123
+test.user=camel-irc-ituser
\ No newline at end of file