-module(escalus_event).
%% Installation/deinstallation of the event mgr
-export([start/1,
stop/1,
new_client/3,
add_handler/3,
delete_handler/3]).
%% Notifications
-export([incoming_stanza/2,
outgoing_stanza/2,
pop_incoming_stanza/2,
story_start/1,
story_end/1]).
%% History
-export([get_history/1,
print_history/1]).
-export_type([event_client/0, manager/0]).
-include_lib("exml/include/exml.hrl").
-type config() :: escalus_config:config().
-type event_client() :: list({atom(), any()}).
-type manager() :: pid().
-type resource() :: binary().
%% =======================================================================
%% @doc Add an event handler
%% @end
-spec add_handler(manager(), atom() | pid(), [term()]) -> ok.
add_handler(Mgr, Handler, Args) ->
gen_event:add_handler(Mgr, Handler, Args).
%% @doc Delete an event handler
%% @end
-spec delete_handler(manager(), atom() | pid(), [term()]) -> ok.
delete_handler(Mgr, Handler, Args) ->
gen_event:delete_handler(Mgr, Handler, Args).
incoming_stanza(Client, Stanza) ->
notify_stanza(Client, incoming_stanza, Stanza).
pop_incoming_stanza(Client, Stanza) ->
notify_stanza(Client, pop_incoming_stanza, Stanza).
outgoing_stanza(Client, Stanza) ->
notify_stanza(Client, outgoing_stanza, Stanza).
story_start(Config) ->
gen_event:notify(manager(Config), story_start).
story_end(Config) ->
gen_event:notify(manager(Config), story_end).
%% ====================================================================
%% @doc Start the event manager
%% @end
start(Config) ->
{ok, Mgr} = gen_event:start_link(),
add_handler(Mgr, escalus_history_h, []),
[{escalus_event_mgr, Mgr} | Config].
%% @doc Stop the event manager
%% @end
stop(Config) ->
gen_event:stop(manager(Config)),
Config.
get_history(Config) ->
escalus_history_h:get_history(manager(Config)).
print_history(Config) ->
CaseName = proplists:get_value(tc_name, Config),
PrivDir = proplists:get_value(priv_dir, Config),
FileName = atom_to_list(CaseName) ++ ".xml",
FullFileName = filename:join(PrivDir, FileName),
Events = get_history(Config),
write_events(Events, FullFileName),
% escalus_ct:add_log_link(Heading, FileName, Type),
escalus_ct:add_log_link("Stanza Log:", FileName, ""),
ok.
write_events([], _) ->
ok;
write_events(Events, OutFileName) ->
{ok, FD} = file:open(OutFileName, [write]),
BaseTime = base_time(Events),
%% What is ../../../.. ?
%% XML file is written into priv_dir
%% - which is log_private
%% - inside run.DATE_TIME
%% - inside ejabberd_tests.tests.something_SUITE.log
%% - inside ct_run.test@theta.DATE_TIME
%%
%% Absolute URL would not work, because of
%% Cross-Origin Resource Sharing (CORS) in Chrome.
ensure_xv_browser_copied(OutFileName),
file:write(FD, <<"\n">>),
file:write(FD, <<"
The system accepts any term as the event.
%% @end notify_stanza(undefined, _, _) -> ok; notify_stanza(Client, EventName, Stanza) -> Mgr = proplists:get_value(event_manager, Client), Jid = jid(Client), gen_event:notify(Mgr, {EventName, Jid, Stanza}). jid(Client) -> Server = proplists:get_value(server, Client), User = proplists:get_value(username, Client), Resource = proplists:get_value(resource, Client), {User, Server, Resource}. jid_to_binary({User, Server, Resource}) -> <