<commit_before>-module(escalus_client).

% Public API
-export([start_session/3,
         login/3,
         start_for/3, start_for_wait/3,
         start/3, start_wait/3,
         send/2, send_wait/2,
         stop/1, stop_wait/1,
         kill/1, kill_wait/1,
         drop_history/1,
         get_stanzas/1,
         wait_for_stanzas/2, wait_for_stanzas/3,
         wait_for_stanza/1, wait_for_stanza/2,
         only_stanza/1,
         is_client/1]).

% spawn exports
-export([client_loop/2]).

-define(WAIT_TIME, 100).
-define(WAIT_FOR_STANZA_TIMOUT, 1000).

-include("escalus.hrl").
-include_lib("exmpp/include/exmpp_jid.hrl").
-include_lib("exmpp/include/exmpp_client.hrl").

%%--------------------------------------------------------------------
%% Public API
%%--------------------------------------------------------------------

start_session(Config, UserSpec, Resource) ->
    {server, Server} = proplists:lookup(server, UserSpec),
    Host = get_config(host, UserSpec, escalus_host, Config, Server),
    Port = get_config(port, UserSpec, escalus_port, Config, 5222),
    [Username, Server, Password] = escalus_users:get_usp(UserSpec),
    JID = exmpp_jid:make(Username, Server, Resource),

    Session = exmpp_session:start({1,0}),
    exmpp_session:auth_info(Session, JID, Password),
    {ok, _, _} = exmpp_session:connect_TCP(Session, Host, Port),
    Session.

login(Config, Session, UserSpec) ->
    AuthMethod = get_config(auth_method, UserSpec,
                            escalus_auth_method, Config,
                            "PLAIN"),
    {ok, _RealJid} = exmpp_session:login(Session, AuthMethod).

start(Config, UserSpec, Resource) ->
    Session = start_session(Config, UserSpec, Resource),
    {ok, JID} = login(Config, Session, UserSpec),
    ClientRef = make_ref(),
    ClientPid = spawn(?MODULE, client_loop, [ClientRef, self()]),
    exmpp_session:set_controlling_process(Session, ClientPid),
    copy_packet_messages(ClientPid),
    Client = #client{session=Session,
                     jid=JID#jid.raw,
                     pid=ClientPid,
                     ref=ClientRef},
    escalus_cleaner:add_client(Config, Client),
    send_initial_presence(Config, UserSpec, Client),
    Client.

start_wait(Config, UserSpec, Resource) ->
    Client = start(Config, UserSpec, Resource),
    wait(),
    Client.

start_for(Config, Username, Resource) ->
    {Username, UserSpec} = escalus_users:get_user_by_name(Username),
    start(Config, UserSpec, Resource).

start_for_wait(Config, Username, Resource) ->
    Client = start_for(Config, Username, Resource),
    wait(),
    Client.

stop(#client{session=Session}) ->
    exmpp_session:stop(Session).

stop_wait(Client) ->
    stop(Client),
    wait().

kill(#client{session=Session}) ->
    erlang:exit(Session, kill).

kill_wait(Client) ->
    Client ! kill.

drop_history(Client) ->
    get_stanzas(Client).

get_stanzas(Client) ->
    get_stanzas(Client, []).

get_stanzas(#client{ref=Ref} = Client, Acc) ->
    receive
        {got_stanza, Ref, Stanza} ->
            get_stanzas(Client, [Stanza|Acc])
    after 0 ->
            lists:reverse(Acc)
    end.

wait_for_stanzas(Client, Count) ->
    wait_for_stanzas(Client, Count, ?WAIT_FOR_STANZA_TIMOUT).

wait_for_stanzas(Client, Count, Timeout) ->
    {ok, Tref} = timer:send_after(Timeout, self(), TimeoutMsg={timeout, make_ref()}),
    Result = do_wait_for_stanzas(Client, Count, TimeoutMsg, []),<commit_msg>Added escalus_client:has_stanzas/1<commit_after>-module(escalus_client).

% Public API
-export([start_session/3,
         login/3,
         start_for/3, start_for_wait/3,
         start/3, start_wait/3,
         send/2, send_wait/2,
         stop/1, stop_wait/1,
         kill/1, kill_wait/1,
         drop_history/1,
         get_stanzas/1,
         has_stanzas/1,
         wait_for_stanzas/2, wait_for_stanzas/3,
         wait_for_stanza/1, wait_for_stanza/2,
         only_stanza/1,
         is_client/1]).

% spawn exports
-export([client_loop/2]).

-define(WAIT_TIME, 100).
-define(WAIT_FOR_STANZA_TIMOUT, 1000).

-include("escalus.hrl").
-include_lib("exmpp/include/exmpp_jid.hrl").
-include_lib("exmpp/include/exmpp_client.hrl").

%%--------------------------------------------------------------------
%% Public API
%%--------------------------------------------------------------------

start_session(Config, UserSpec, Resource) ->
    {server, Server} = proplists:lookup(server, UserSpec),
    Host = get_config(host, UserSpec, escalus_host, Config, Server),
    Port = get_config(port, UserSpec, escalus_port, Config, 5222),
    [Username, Server, Password] = escalus_users:get_usp(UserSpec),
    JID = exmpp_jid:make(Username, Server, Resource),

    Session = exmpp_session:start({1,0}),
    exmpp_session:auth_info(Session, JID, Password),
    {ok, _, _} = exmpp_session:connect_TCP(Session, Host, Port),
    Session.

login(Config, Session, UserSpec) ->
    AuthMethod = get_config(auth_method, UserSpec,
                            escalus_auth_method, Config,
                            "PLAIN"),
    {ok, _RealJid} = exmpp_session:login(Session, AuthMethod).

start(Config, UserSpec, Resource) ->
    Session = start_session(Config, UserSpec, Resource),
    {ok, JID} = login(Config, Session, UserSpec),
    ClientRef = make_ref(),
    ClientPid = spawn(?MODULE, client_loop, [ClientRef, self()]),
    exmpp_session:set_controlling_process(Session, ClientPid),
    copy_packet_messages(ClientPid),
    Client = #client{session=Session,
                     jid=JID#jid.raw,
                     pid=ClientPid,
                     ref=ClientRef},
    escalus_cleaner:add_client(Config, Client),
    send_initial_presence(Config, UserSpec, Client),
    Client.

start_wait(Config, UserSpec, Resource) ->
    Client = start(Config, UserSpec, Resource),
    wait(),
    Client.

start_for(Config, Username, Resource) ->
    {Username, UserSpec} = escalus_users:get_user_by_name(Username),
    start(Config, UserSpec, Resource).

start_for_wait(Config, Username, Resource) ->
    Client = start_for(Config, Username, Resource),
    wait(),
    Client.

stop(#client{session=Session}) ->
    exmpp_session:stop(Session).

stop_wait(Client) ->
    stop(Client),
    wait().

kill(#client{session=Session}) ->
    erlang:exit(Session, kill).

kill_wait(Client) ->
    Client ! kill.

drop_history(Client) ->
    get_stanzas(Client).

get_stanzas(Client) ->
    get_stanzas(Client, []).

get_stanzas(#client{ref=Ref} = Client, Acc) ->
    receive
        {got_stanza, Ref, Stanza} ->
            get_stanzas(Client, [Stanza|Acc])
    after 0 ->
            lists:reverse(Acc)
    end.

has_stanzas(#client{ref=Ref}) ->
    {messages, Msgs} = process_info(self(), messages),
    lists:any(fun ({got_stanza, MRef, _}) when MRef == Ref ->
                      true;
                  (_) ->
                      false
              end, Msgs).

wait_for_stanzas(Client, Count) ->
    wait_for_stanzas(Client, Count, ?WAIT_FOR_STANZA_TIMOUT).

wait_for_stanzas(Client, Count, Timeout) ->
    {ok, Tref} = timer:send_after(Timeout, self(), TimeoutMsg={timeout, make_ref()}),
    Result = do_wait_for_stanzas(Client, Count, TimeoutMsg, []),
{'fraction_non_alphanumeric': 0.16914730838781472, 'fraction_numerical': 0.010015301154541661, 'mean_word_length': 2.746743095362168, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7424265', 'n_tokens_mistral': 2460, 'n_tokens_neox': 2405, 'n_words': 399}