Permalink
Browse files

Build StreamingJsonMiddlewareHandler to allow twitter access as well …

…as user-configurable support for future streaming json apis. Also added demo gardenhose.ini and halloween.ini

--HG--
rename : channels/google.ini => channels/halloween.ini
  • Loading branch information...
1 parent 91a6c84 commit bf51a120ec9c7ccea0813803fd0df621d17cf1ef xitiomet committed Oct 20, 2009
View
@@ -8,6 +8,7 @@ jvm-build
ircd
ircd.jar
.DS_Store
+nohup.out
syntax: regexp
(.*/)?\#[^/]*\#$
View
@@ -76,13 +76,16 @@ build/org/openstatic/irc/middleware/StreamMiddlewareHandler.class: src/org/opens
build/org/openstatic/irc/middleware/TwitterMiddlewareHandler.class: src/org/openstatic/irc/middleware/TwitterMiddlewareHandler.java
$(JC) $(JC_FLAGS) --classpath=$(CLASS_PATH) -C $<
+build/org/openstatic/irc/middleware/StreamingJsonMiddlewareHandler.class: src/org/openstatic/irc/middleware/StreamingJsonMiddlewareHandler.java
+ $(JC) $(JC_FLAGS) --classpath=$(CLASS_PATH) -C $<
+
build/org/openstatic/irc/middleware/JsonHttpCH.class: src/org/openstatic/irc/middleware/JsonHttpCH.java
$(JC) $(JC_FLAGS) --classpath=$(CLASS_PATH) -C $<
# Main Builds
# -------------------------------------------------------------------------------
-ircd.jar: build/org/openstatic/irc/IrcServer.class build/org/openstatic/irc/ReceivedCommand.class build/org/openstatic/irc/IrcUser.class build/org/openstatic/irc/IrcChannel.class build/org/openstatic/irc/MiddlewareHandler.class build/org/openstatic/irc/PreparedCommand.class build/org/openstatic/irc/middleware/DefaultMiddlewareHandler.class build/org/openstatic/irc/middleware/StreamMiddlewareHandler.class build/org/openstatic/irc/middleware/JsonHttpCH.class build/org/openstatic/irc/GatewayConnection.class build/org/openstatic/irc/Gateway.class build/org/openstatic/irc/gateways/IrcGatewayConnection.class build/org/openstatic/irc/gateways/IrcGateway.class build/org/openstatic/irc/gateways/WebAdminGatewayConnection.class build/org/openstatic/irc/gateways/WebAdminGateway.class build/org/openstatic/irc/middleware/TwitterMiddlewareHandler.class build/org/openstatic/Base64Coder.class
+ircd.jar: build/org/openstatic/irc/IrcServer.class build/org/openstatic/irc/ReceivedCommand.class build/org/openstatic/irc/IrcUser.class build/org/openstatic/irc/IrcChannel.class build/org/openstatic/irc/MiddlewareHandler.class build/org/openstatic/irc/PreparedCommand.class build/org/openstatic/irc/middleware/DefaultMiddlewareHandler.class build/org/openstatic/irc/middleware/StreamMiddlewareHandler.class build/org/openstatic/irc/middleware/JsonHttpCH.class build/org/openstatic/irc/GatewayConnection.class build/org/openstatic/irc/Gateway.class build/org/openstatic/irc/gateways/IrcGatewayConnection.class build/org/openstatic/irc/gateways/IrcGateway.class build/org/openstatic/irc/gateways/WebAdminGatewayConnection.class build/org/openstatic/irc/gateways/WebAdminGateway.class build/org/openstatic/irc/middleware/TwitterMiddlewareHandler.class build/org/openstatic/irc/middleware/StreamingJsonMiddlewareHandler.class build/org/openstatic/Base64Coder.class
$(JAR) -cvf $@ -C build org
clean:
View
@@ -0,0 +1,20 @@
+# Example Streaming JSON Middleware
+config=channel
+
+# Server Side Channel Settings
+channel_name=\#gardenhose
+channel_topic=Twitter Gardenhose feed
+middleware=org.openstatic.irc.middleware.StreamingJsonMiddlewareHandler
+
+
+# To Use HTTP Authentication
+username=zander_xi01
+password=zander1032
+
+# How to locate and parse the json stream
+stream_url=http://stream.twitter.com/1/statuses/sample.json
+
+stream_data_username_JSONPATH=user.screen_name
+stream_data_nickname_JSONPATH=user.screen_name
+stream_data_hostname=twitter.com
+stream_data_message_JSONPATH=text
View
@@ -1,13 +0,0 @@
-# Example Twitter Middleware
-config=channel
-
-# Server Side
-channel_name=\#google
-channel_topic=google
-middleware=org.openstatic.irc.middleware.TwitterMiddlewareHandler
-
-
-# For Synchronizing with twitter!
-twitter_username=zander_xi01
-twitter_password=zander1032
-twitter_topic=google
View
@@ -0,0 +1,20 @@
+# Example Streaming JSON Middleware
+config=channel
+
+# Server Side Channel Settings
+channel_name=\#halloween
+channel_topic=halloween
+middleware=org.openstatic.irc.middleware.StreamingJsonMiddlewareHandler
+
+
+# To Use HTTP Authentication
+username=zander_xi01
+password=zander1032
+
+# How to locate and parse the json stream
+stream_url=http://stream.twitter.com/1/statuses/filter.json?track=halloween
+
+stream_data_username_JSONPATH=user.screen_name
+stream_data_nickname_JSONPATH=user.screen_name
+stream_data_hostname=twitter.com
+stream_data_message_JSONPATH=text
@@ -28,9 +28,7 @@ public IrcChannel(Properties setup)
Class<?> c = Class.forName(setup.getProperty("middleware"));
Constructor<?> cons = c.getDeclaredConstructor(Properties.class);
this.myHandler = (MiddlewareHandler) cons.newInstance(setup);
- } catch (Exception e) {
- System.err.println(e.getMessage());
- }
+ } catch (Exception e) {}
}
this.members = new Vector<IrcUser>();
this.pending_joins = new Vector<IrcUser>();
@@ -99,8 +99,10 @@ public void run()
this.ircUser.onGatewayCommand(cmd);
}
} catch (Exception rex) {
+ this.ircUser.getIrcServer().logln("IGC_" + this.clientHostname, "Exception: " + rex.toString() + " / " + rex.getMessage());
}
} catch (Exception x) {
+ this.ircUser.getIrcServer().logln("IGC_" + this.clientHostname, "Exception: " + x.toString() + " / " + x.getMessage());
}
this.ircUser.disconnect();
}
@@ -0,0 +1,145 @@
+package org.openstatic.irc.middleware;
+
+import org.openstatic.irc.MiddlewareHandler;
+import org.openstatic.irc.ReceivedCommand;
+import org.openstatic.irc.IrcUser;
+import org.openstatic.Base64Coder;
+import org.json.*;
+import java.net.URL;
+import java.net.HttpURLConnection;
+import java.net.URLEncoder;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+public class StreamingJsonMiddlewareHandler implements MiddlewareHandler
+{
+ private MiddlewareHandler middlewareHandler;
+ private Properties setup;
+ private Thread read_json;
+
+ public StreamingJsonMiddlewareHandler(Properties setup)
+ {
+
+ this.setup = setup;
+ this.middlewareHandler = null;
+
+ read_json = new Thread()
+ {
+ public void run()
+ {
+ String privmsg_to = StreamingJsonMiddlewareHandler.this.setup.getProperty("channel_name");
+ String username = StreamingJsonMiddlewareHandler.this.setup.getProperty("username");
+ String password = StreamingJsonMiddlewareHandler.this.setup.getProperty("password");
+ String url = StreamingJsonMiddlewareHandler.this.setup.getProperty("stream_url");
+
+ try
+ {
+ HttpURLConnection connection = null;
+ if (username != null && password != null)
+ {
+ connection = authenticate(url, username, password);
+ } else {
+ connection = (HttpURLConnection) (new URL(url)).openConnection();
+ }
+ BufferedReader bread = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+ String newLine = null;
+ do
+ {
+ newLine = bread.readLine();
+ try
+ {
+ JSONObject json_object = new JSONObject(newLine);
+
+ String stream_nickname = StreamingJsonMiddlewareHandler.this.resolveField(json_object, "stream_data_nickname");
+ String stream_hostname = StreamingJsonMiddlewareHandler.this.resolveField(json_object, "stream_data_hostname");
+ String stream_username = StreamingJsonMiddlewareHandler.this.resolveField(json_object, "stream_data_username");
+ String stream_message = StreamingJsonMiddlewareHandler.this.resolveField(json_object, "stream_data_message");
+ String raw_irc = ":" + stream_nickname + "!" + stream_username + "@" + stream_hostname + " PRIVMSG " + privmsg_to + " :" + stream_message;
+ ReceivedCommand rc = new ReceivedCommand(raw_irc);
+ StreamingJsonMiddlewareHandler.this.middlewareHandler.onCommand(rc, StreamingJsonMiddlewareHandler.this);
+ } catch (Exception ve) {}
+ } while (newLine != null);
+ bread.close();
+
+ } catch (Exception nex) {
+ System.err.println("Streaming JSON Middleware Failed: " + nex.getMessage());
+ }
+ }
+ };
+ }
+
+ public String resolveField(JSONObject data, String field) throws JSONException
+ {
+ if (setup.getProperty(field) != null)
+ {
+ return setup.getProperty(field);
+ } else {
+ if (setup.getProperty(field + "_JSONPATH") != null) {
+ String jsonPath = setup.getProperty(field + "_JSONPATH");
+ StringTokenizer json_path = new StringTokenizer(jsonPath, ".");
+ JSONObject co = data;
+ String return_var = null;
+ while (json_path.hasMoreTokens())
+ {
+ String currentToken = json_path.nextToken();
+ if (json_path.hasMoreTokens())
+ {
+ co = co.getJSONObject(currentToken);
+ } else {
+ return_var = co.getString(currentToken);
+ }
+ }
+ return return_var;
+ } else {
+ return null;
+ }
+ }
+ }
+
+ public static HttpURLConnection authenticate(String url, String username, String password) throws Exception
+ {
+ HttpURLConnection c = (HttpURLConnection) (new URL(url)).openConnection();
+ String plain_auth = username + ":" + password;
+ String auth = Base64Coder.encodeString(plain_auth);
+ c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+ c.setRequestProperty("Authorization", "Basic " + auth);
+ return c;
+ }
+
+ public void onCommand(ReceivedCommand command, MiddlewareHandler middlewareHandler)
+ {
+ middlewareHandler.onCommand(command, this);
+ if (this.middlewareHandler == null || read_json.isAlive() == false)
+ {
+ this.middlewareHandler = middlewareHandler;
+ read_json.start();
+ }
+ }
+
+ public IrcUser findMember(String value)
+ {
+ if (this.middlewareHandler != null)
+ {
+ return this.middlewareHandler.findMember(value);
+ } else {
+ return null;
+ }
+ }
+
+ public String getHandlerName()
+ {
+ return "org.openstatic.irc.middleware.StreamingJsonMiddlewareHandler";
+ }
+
+ public String getHandlerDescription()
+ {
+ return "OpenStatic.org Streaming JSON IRC Middleware";
+ }
+
+ public String getHandlerDetails()
+ {
+ return null;
+ }
+}

0 comments on commit bf51a12

Please sign in to comment.