Added AMQP support to the irc application.
authorMatt Mullins <mmullins@mmlx.us>
Fri, 26 Aug 2011 20:23:54 +0000 (15:23 -0500)
committerMatt Mullins <mmullins@mmlx.us>
Sun, 28 Aug 2011 19:55:49 +0000 (14:55 -0500)
irc/irc_amqp_listener.erl [new file with mode: 0644]
irc/irc_conn.erl

diff --git a/irc/irc_amqp_listener.erl b/irc/irc_amqp_listener.erl
new file mode 100644 (file)
index 0000000..b8c02a2
--- /dev/null
@@ -0,0 +1,39 @@
+%% @doc AMQP listener that takes events and sends them out as lines of text
+%% as PRIVMSG to a particular destination
+-module(irc_amqp_listener).
+-behavior(gen_server).
+-vsn(1).
+
+-export([
+          start_link/3
+        ]).
+
+-export([
+          init/1,
+          handle_info/2
+        ]).
+
+-include("irc_util.hrl").
+-include_lib("amqp_client/include/amqp_client.hrl").
+
+start_link(ConnectionPid, Destination, RoutingKey) ->
+    gen_server:start_link(?MODULE, {ConnectionPid, Destination, RoutingKey}, []).
+
+init({ConnectionPid, Destination, RoutingKey}) ->
+    {ok, _} = amqp_bot_listener:listen_for_events(RoutingKey),
+    {ok, {ConnectionPid, Destination}}.
+
+%% Ignore the message indicating that the listener is operating correctly
+handle_info(#'basic.consume_ok'{}, State) ->
+    {noreply, State};
+
+handle_info(
+        { #'basic.deliver'{routing_key = RoutingKey},
+          #amqp_msg{payload = BinContent} },
+        State = { ConnectionPid, Destination }) ->
+    Content = binary_to_list(BinContent),
+    Command = #irc_command{ command = "PRIVMSG",
+                            middles = [ Destination ],
+                            trailing = Content },
+    gen_server:cast(ConnectionPid, {send_command, Command}),
+    {noreply, State}.
index f15cb8f..318838a 100644 (file)
@@ -111,7 +111,8 @@ send_command(Command) ->
 
 join_channel(Channel) ->
     Command = #irc_command{command = "JOIN", middles = [Channel]},
-    send_command(Command).
+    send_command(Command),
+    irc_amqp_listener:start_link(self(), Channel, "foobar"). %% TODO: fix routing key
 
 %% @doc Splits a buffer into full lines, keeping whatever is left over
 %% Returns either {ok, [Line], Remaining} or none.