anchor.diff:  doc_close_connection({}, _) -> {failure, nodoc};
   doc_close_connection({Doc}, {User, _Host, _Resource}) ->
     % Converts a list of tuples ([{x, Val}]) into a list of lists ([[x, Val]]) and flattens it ([x, Val]) for BSON
  -  AgentData = erlang:list_to_tuple(list:flatmap(fun erlang:tuple_to_list/1, parse_component_jid(User))),
  +  AgentData = erlang:list_to_tuple(lists:flatmap(fun erlang:tuple_to_list/1, parse_component_jid(User))),
     NewData = {connected, false,
                disconnected_at, bson:timenow(),
                agent_data, AgentData},
  
positive.code: parse_component_jid(User) ->
    case string:tokens(User, "_") of
      ["agent"|_] = Props -> lists:zip([component, version, cores, jobs, uuid, ukn], lists:map(fun bson:utf8/1, Props));
      [Component|_] -> [{component, Component}]
    end.
  
  %@doc Creates a MonoDB document describing the connection
  
negative.code: hash_xml_attribute({"to", _} = Attr) -> Attr;
  hash_xml_attribute({"from", _} = Attr) -> Attr;
  hash_xml_attribute({"noack", _} = Attr) -> Attr;
  hash_xml_attribute({Attr, Value}) -> {Attr, "_BASE64:" ++ base64:encode_to_string(Value)}.
  
  %@doc Prepares the reportEvent command
  prepare_event_command(Event, Host) -> prepare_event_command(Event, "", Host).
  prepare_event_command(Event, Type, Host) -> proplists:delete(Type, prepare_event_command(Event, Type, "", Host)).
  prepare_event_command(Event, Type, Value, Host) ->
    [{"command", "reportEvent" ++ ucfirst(Type)},
     {"component", "copilot.ejabberd." ++ Host},
     {"event", Event},
     {Type, Value}].
  
  %@doc Sends a reportEvent command to the Monitor
  report_event(Host, Event) ->
    To = gen_mod:get_module_opt(Host, ?MODULE, monitor_jid, "mon@localhost"),
    XmlBody = {xmlelement, "message", [{"from", ?JID},
                                       {"to", To},
                                       {"noack", "1"}],
                                       [{xmlelement, "info", prepare_event_command(Event, Host), []}]},
    route_to_copilot_component(To, XmlBody),
    ok.
  
  %@doc Sends a reportEvent{Type} command to the Monitor
  report_event(Host, Event, Value, Type) ->
    To = gen_mod:get_module_opt(Host, ?MODULE, monitor_jid, "mon@localhost"),
    XmlBody = {xmlelement, "message", [{"from", ?JID},
                                       {"to", To},
                                       {"noack", "1"},
                                      [{xmlelement, "info", prepare_event_command(Event, Value, Type, Host), []}]]},
    route_to_copilot_component(To, XmlBody),
    ok.
  