Permalink
Browse files

api gateway work

  • Loading branch information...
1 parent 6609f9d commit 0acc7d94bfb8faf5369146ca1dc13fdaff7efd67 xitiomet committed Nov 28, 2011
View
Binary file not shown.
@@ -404,6 +404,24 @@ private String getMembersSerial()
}
return returnMembers.toString();
}
+
+ public String[] getMemberStringArray()
+ {
+ Vector<String> returnMembers = new Vector<String>();
+ for (Enumeration<IrcUser> e = this.members.elements(); e.hasMoreElements(); )
+ {
+ IrcUser u = e.nextElement();
+ String nick = u.getNick();
+ if (hasMode(u, "o"))
+ {
+ nick = "@" + nick;
+ } else if (hasMode(u, "v")) {
+ nick = "+" + nick;
+ }
+ returnMembers.add(nick);
+ }
+ return returnMembers.toArray(new String[returnMembers.size()]);
+ }
private boolean hasMode(IrcUser member, String mode)
{
@@ -497,4 +515,4 @@ public String toString()
return this.channel_name;
}
-}
+}
@@ -5,6 +5,7 @@
import org.openstatic.irc.gateways.IrcGateway;
import org.openstatic.irc.gateways.WebGateway;
import org.openstatic.irc.gateways.CLIGateway;
+import org.openstatic.irc.gateways.APIGateway;
import java.io.File;
import java.io.FileInputStream;
import java.io.ByteArrayOutputStream;
@@ -20,7 +21,8 @@ public static void help()
System.err.println("");
System.err.println(" --debug Turn debugging output on.");
System.err.println(" --irc [port] Specify IRC listening port");
- System.err.println(" --web [port] Start web administration on given port");
+ System.err.println(" --web [port] Start web gateway on given port");
+ System.err.println(" --api [port] Start api gateway on given port");
System.err.println(" --complex-chan [ini] Create a channel from an ini file");
System.err.println(" --chan [channel name] create chanel (can be used multiple times)");
System.err.println(" --motd [file] specify the motd filename");
@@ -110,6 +112,15 @@ public static void main(String[] args) throws Exception
irc.addGateway(new WebGateway(web_port));
} catch (Exception wa) {}
}
+
+ if (arg.equals("--api") && arg_p1 != null)
+ {
+ try
+ {
+ int api_port = Integer.valueOf(arg_p1).intValue();
+ irc.addGateway(new APIGateway(api_port));
+ } catch (Exception wa) {}
+ }
if (arg.equals("--complex-chan") && arg_p1 != null)
{
@@ -144,4 +155,4 @@ public static void main(String[] args) throws Exception
irc.join();
}
}
-}
+}
@@ -1,24 +1,45 @@
package org.openstatic.irc.gateways;
import org.openstatic.irc.IrcServer;
+import org.openstatic.irc.IrcUser;
+import org.openstatic.irc.IrcChannel;
import org.openstatic.irc.Gateway;
-import org.openstatic.http.PlaceboHttpServer;
-import org.openstatic.http.PlaceboSession;
+import org.openstatic.http.*;
+
+import java.util.Vector;
+import java.util.Enumeration;
+import java.util.Random;
+
+import org.json.*;
public class APIGateway extends Thread implements Gateway
{
private int port;
private boolean keep_running;
private IrcServer ircServer;
private PlaceboHttpServer httpServer;
+ private Vector<APIGatewayConnection> clients;
public APIGateway(int port)
{
this.port = port;
this.ircServer = null;
+ this.clients = new Vector<APIGatewayConnection>();
}
-
+
+ private static String generateBigAlphaKey(int key_length)
+ {
+ Random n = new Random(System.currentTimeMillis());
+ String alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ StringBuffer return_key = new StringBuffer();
+ for (int i = 0; i < key_length; i++)
+ {
+ return_key.append(alpha.charAt(n.nextInt(alpha.length())));
+ }
+ return return_key.toString();
+ }
+
public boolean initGateway(IrcServer ircServer)
{
this.ircServer = ircServer;
@@ -32,6 +53,17 @@ public boolean initGateway(IrcServer ircServer)
return false;
}
}
+
+ public APIGatewayConnection findClient(String token_id)
+ {
+ for(Enumeration<APIGatewayConnection> e = clients.elements(); e.hasMoreElements(); )
+ {
+ APIGatewayConnection ce = e.nextElement();
+ if (ce.getTokenId().equals(token_id))
+ return ce;
+ }
+ return null;
+ }
public void shutdownGateway()
{
@@ -58,13 +90,109 @@ public void run()
{
try
{
- PlaceboSession new_connection = httpServer.getNextSession();
- APIGatewayConnection wagc = new APIGatewayConnection(new_connection, this.ircServer);
- wagc.start();
- } catch (Exception x) {}
+ final HttpRequest nr = httpServer.getNextRequest();
+ Thread t = new Thread()
+ {
+ public void run()
+ {
+ HttpResponse response = new HttpResponse();
+ response.setContentType("text/javascript");
+ try
+ {
+ JSONObject response_json = APIGateway.this.doRequest(nr);
+ response.setData(response_json);
+ } catch (Exception e) {
+ System.err.println(e.toString() + " / " + e.getMessage());
+ e.printStackTrace(System.err);
+ response.setData(e.toString() + " / " + e.getMessage());
+ }
+ nr.sendResponse(response);
+ }
+ };
+ t.start();
+ } catch (Exception x) {
+ System.err.println(x.toString() + " / " + x.getMessage());
+ x.printStackTrace(System.err);
+ }
}
ircServer.log("PORT: " + String.valueOf(this.port), 1, "APIGateway Shutdown");
}
+
+ public JSONObject doRequest(HttpRequest request) throws Exception
+ {
+ JSONObject response = new JSONObject();
+ String client_ip = request.getHttpHeader("X-Real-IP");
+ String token_id = request.getGetValue("token_id");
+ String timeout_string = request.getGetValue("timeout");
+ int timeout = 60;
+ if (timeout_string != null)
+ timeout = Integer.valueOf(timeout_string).intValue();
+ APIGatewayConnection connection = null;
+ if (token_id != null)
+ {
+ connection = findClient(token_id);
+ }
+
+ if (request.getPath().equals("/irc/connect/"))
+ {
+ if (token_id == null)
+ token_id = generateBigAlphaKey(15);
+ String nickname = request.getGetValue("nick");
+ if (this.ircServer.findUser(nickname) == null)
+ {
+ connection = new APIGatewayConnection(this.ircServer, token_id, timeout, client_ip, request.getServerHostname());
+ this.clients.add(connection);
+ connection.connect(nickname, request.getGetValue("password"));
+ response.put("token_id", token_id);
+ } else {
+ response.put("error", "NICK_IN_USE");
+ }
+ } else if (request.getPath().startsWith("/irc/queue/")) {
+ if (connection != null)
+ response.put("events", connection.getMessageQueue());
+ } else if (request.getPath().startsWith("/irc/channel/")) {
+ String channel_name = request.getPathArray()[2];
+ IrcChannel channel = this.ircServer.findChannel(channel_name);
+ if (channel != null)
+ {
+ response.put("topic", channel.getTopic());
+ response.put("users", new JSONArray(channel.getMemberStringArray()));
+ }
+ if (connection != null)
+ {
+ if (request.getPath().endsWith("/join/"))
+ {
+ connection.join(channel_name);
+ } else if (request.getPath().endsWith("/part/")) {
+ connection.part(channel_name);
+ } else if (request.getPath().endsWith("/message/")) {
+ connection.privmsg(channel_name, request.getRawPost());
+ }
+ }
+ } else if (request.getPath().startsWith("/irc/user/")) {
+ String nick_name = request.getPathArray()[2];
+ IrcUser nick = this.ircServer.findUser(nick_name);
+ if (nick != null)
+ {
+ response.put("user", nick.toString());
+ }
+ if (connection != null)
+ {
+ if (request.getPath().endsWith("/message/"))
+ {
+ connection.privmsg(nick_name, request.getRawPost());
+ }
+ }
+ } else {
+
+ }
+ if (connection != null)
+ {
+ response.put("ping_remaining", connection.getPingRemaining());
+ response.put("nick", connection.getIrcUser().getNick());
+ }
+ return response;
+ }
public String toString()
{
Oops, something went wrong. Retry.

0 comments on commit 0acc7d9

Please sign in to comment.