--- /dev/null
+%% @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}.
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.