From a3840d52cd599926144224d2ae030f78daa88537 Mon Sep 17 00:00:00 2001 From: Matt Mullins Date: Fri, 26 Aug 2011 15:23:54 -0500 Subject: [PATCH] Added AMQP support to the irc application. --- irc/irc_amqp_listener.erl | 39 +++++++++++++++++++++++++++++++++++++++ irc/irc_conn.erl | 3 ++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 irc/irc_amqp_listener.erl diff --git a/irc/irc_amqp_listener.erl b/irc/irc_amqp_listener.erl new file mode 100644 index 0000000..b8c02a2 --- /dev/null +++ b/irc/irc_amqp_listener.erl @@ -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}. diff --git a/irc/irc_conn.erl b/irc/irc_conn.erl index f15cb8f..318838a 100644 --- a/irc/irc_conn.erl +++ b/irc/irc_conn.erl @@ -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. -- 2.11.0