anchor.diff:    end.
   
   %@doc Returns geosplatial information for given IP
  +lookup_ip(null) -> [{latitude, 0.0}, {longitude, 0.0}];
   lookup_ip(IPAddress) ->
     ?DEBUG("Decoding IP address ~p", [IPAddress]),
     [_|Data] = erlang:tuple_to_list(element(2, egeoip:lookup(IPAddress))),
  
positive.code:                                       host, bson:utf8(Host),
                                        resource, bson:utf8(Resource),
                                        connected, true}),
      NewDoc = doc_close_connection(mongo:next(Cursor), JID, IPAddress),
      mongo:close_cursor(Cursor),
      mongo:save(connections, NewDoc)
    end);
  handle_cast(_Msg, State) ->
    {noreply, State}.
  
negative.code:    connected,    true}.
  
  %@doc Marks connection as closed and appends data from agent's JID
  doc_close_connection({failure, Reason}, _, _) -> {failure, Reason};
  doc_close_connection({ok, {}}, _, _) -> {failure, nodoc};
  doc_close_connection({ok, {Doc}}, {User, _Host, _Resource}, _IPAddress) ->
    % 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))),
    NewData = {connected, false,
               disconnected_at, bson:timenow(),
               agent_data, AgentData},
    {ok, bson:merge(NewData, Doc)}.
  
  %@doc Sends an message as mod_copilot@localhost and performs base64-encoding of the attributes
  route_to_copilot_component(To, Xml) -> route_to_copilot_component(?JID, To, Xml).
  