     state = receive do
       {:internal, msg} ->
         case msg do
-          {:add_hook, id, hook} ->
-            state.add_hook(id, hook)
+          {:add_hook, id, hook, opts} ->
+            state_add_hook(state, id, hook, opts)
           {:remove_hook, id} ->
-            state.remove_hook(id)
+            state_remove_hook(state, id)
           other ->
             raise RuntimeError[message: "unhandled internal msg #{inspect other}"]
         end

  end

  defp resolve_hook_result({:reply, text}, sock) do
    irc_cmd(sock, "PRIVMSG", "\##{@channel} RECEIVER: :#{text}")
  end

  defp resolve_hook_result({:msg, text}, sock) do

  @channel "exligir"
  #@channel "elixir-lang"

  defrecordp :hookrec, [:type, :direct, :fn]
  defrecord State, hooks: []

  defp state_add_hook(state, id, f, opts) do
    hook = Enum.reduce(opts, hookrec(fn: f), fn
      {:in, type}, rec ->
        hookrec(rec, type: type)
      {:direct, flag}, rec ->
        hookrec(rec, direct: flag)
    end)
    state.update_hooks(&( &1 ++ [{id, hook}] ))
  end

  defp state_remove_hook(state=State[hooks: hooks], id) do
    state.hooks(Keyword.delete(hooks, id))
  end


  def start_link() do
    pid = spawn_link(&connect/0)
    Process.register(pid, __MODULE__)
  end

  def add_hook(id, hook, opts \\ []) do
    Process.send(__MODULE__, {:internal, {:add_hook, id, hook, opts}})
  end

  def remove_hook(id) do
