mod_message_log: Add function specifications
This commit is contained in:
		
							parent
							
								
									44464c0f8d
								
							
						
					
					
						commit
						84044c290b
					
				@ -40,13 +40,20 @@
 | 
				
			|||||||
-record(state, {filename = ?DEFAULT_FILENAME :: binary(),
 | 
					-record(state, {filename = ?DEFAULT_FILENAME :: binary(),
 | 
				
			||||||
		iodevice                     :: io:device()}).
 | 
							iodevice                     :: io:device()}).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-type direction() :: incoming | outgoing | offline.
 | 
				
			||||||
 | 
					-type state() :: #state{}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
%% -------------------------------------------------------------------
 | 
					%% -------------------------------------------------------------------
 | 
				
			||||||
%% gen_mod/supervisor callbacks.
 | 
					%% gen_mod/supervisor callbacks.
 | 
				
			||||||
%% -------------------------------------------------------------------
 | 
					%% -------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec start_link(gen_mod:opts()) -> {ok, pid()} | ignore | {error, _}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
start_link(Opts) ->
 | 
					start_link(Opts) ->
 | 
				
			||||||
    gen_server:start_link({local, ?PROCNAME}, ?MODULE, Opts, []).
 | 
					    gen_server:start_link({local, ?PROCNAME}, ?MODULE, Opts, []).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec start(binary(), gen_mod:opts()) -> {ok, _} | {ok, _, _} | {error, _}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
start(Host, Opts) ->
 | 
					start(Host, Opts) ->
 | 
				
			||||||
    ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
 | 
					    ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
 | 
				
			||||||
		       log_packet_send, 42),
 | 
							       log_packet_send, 42),
 | 
				
			||||||
@ -64,6 +71,8 @@ start(Host, Opts) ->
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    supervisor:start_child(ejabberd_sup, Spec).
 | 
					    supervisor:start_child(ejabberd_sup, Spec).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec stop(binary()) -> ok.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
stop(Host) ->
 | 
					stop(Host) ->
 | 
				
			||||||
    ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
 | 
					    ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
 | 
				
			||||||
			  log_packet_send, 42),
 | 
								  log_packet_send, 42),
 | 
				
			||||||
@ -82,6 +91,8 @@ stop(Host) ->
 | 
				
			|||||||
%% gen_server callbacks.
 | 
					%% gen_server callbacks.
 | 
				
			||||||
%% -------------------------------------------------------------------
 | 
					%% -------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec init(gen_mod:opts()) -> {ok, state()}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
init(Opts) ->
 | 
					init(Opts) ->
 | 
				
			||||||
    process_flag(trap_exit, true),
 | 
					    process_flag(trap_exit, true),
 | 
				
			||||||
    ejabberd_hooks:add(reopen_log_hook, ?MODULE, reopen_log, 42),
 | 
					    ejabberd_hooks:add(reopen_log_hook, ?MODULE, reopen_log, 42),
 | 
				
			||||||
@ -90,9 +101,13 @@ init(Opts) ->
 | 
				
			|||||||
    {ok, IoDevice} = file:open(Filename, ?FILE_MODES),
 | 
					    {ok, IoDevice} = file:open(Filename, ?FILE_MODES),
 | 
				
			||||||
    {ok, #state{filename = Filename, iodevice = IoDevice}}.
 | 
					    {ok, #state{filename = Filename, iodevice = IoDevice}}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec handle_call(_, {pid(), _}, state()) -> {noreply, state()}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handle_call(_Request, _From, State) ->
 | 
					handle_call(_Request, _From, State) ->
 | 
				
			||||||
    {noreply, State}.
 | 
					    {noreply, State}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec handle_cast(_, state()) -> {noreply, state()}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handle_cast({message, Direction, From, To, Type}, #state{iodevice = IoDevice} =
 | 
					handle_cast({message, Direction, From, To, Type}, #state{iodevice = IoDevice} =
 | 
				
			||||||
	    State) ->
 | 
						    State) ->
 | 
				
			||||||
    write_log(IoDevice, Direction, From, To, Type),
 | 
					    write_log(IoDevice, Direction, From, To, Type),
 | 
				
			||||||
@ -105,13 +120,19 @@ handle_cast(reopen_log, #state{filename = Filename, iodevice = IoDevice} =
 | 
				
			|||||||
handle_cast(_Request, State) ->
 | 
					handle_cast(_Request, State) ->
 | 
				
			||||||
    {noreply, State}.
 | 
					    {noreply, State}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec handle_info(timeout | _, state()) -> {noreply, state()}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handle_info(_Info, State) ->
 | 
					handle_info(_Info, State) ->
 | 
				
			||||||
    {noreply, State}.
 | 
					    {noreply, State}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec terminate(normal | shutdown | {shutdown, _} | _, _) -> any().
 | 
				
			||||||
 | 
					
 | 
				
			||||||
terminate(_Reason, State) ->
 | 
					terminate(_Reason, State) ->
 | 
				
			||||||
    ejabberd_hooks:delete(reopen_log_hook, ?MODULE, reopen_log, 42),
 | 
					    ejabberd_hooks:delete(reopen_log_hook, ?MODULE, reopen_log, 42),
 | 
				
			||||||
    ok = file:close(State#state.iodevice).
 | 
					    ok = file:close(State#state.iodevice).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec code_change({down, _} | _, state(), _) -> {ok, state()}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
code_change(_OldVsn, State, _Extra) ->
 | 
					code_change(_OldVsn, State, _Extra) ->
 | 
				
			||||||
    {ok, State}.
 | 
					    {ok, State}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -119,15 +140,23 @@ code_change(_OldVsn, State, _Extra) ->
 | 
				
			|||||||
%% ejabberd_hooks callbacks.
 | 
					%% ejabberd_hooks callbacks.
 | 
				
			||||||
%% -------------------------------------------------------------------
 | 
					%% -------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec log_packet_send(jid(), jid(), xmlel()) -> any().
 | 
				
			||||||
 | 
					
 | 
				
			||||||
log_packet_send(From, To, Packet) ->
 | 
					log_packet_send(From, To, Packet) ->
 | 
				
			||||||
    log_packet(outgoing, From, To, Packet).
 | 
					    log_packet(outgoing, From, To, Packet).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec log_packet_receive(jid(), jid(), jid(), xmlel()) -> any().
 | 
				
			||||||
 | 
					
 | 
				
			||||||
log_packet_receive(JID, From, _To, Packet) ->
 | 
					log_packet_receive(JID, From, _To, Packet) ->
 | 
				
			||||||
    log_packet(incoming, From, JID, Packet).
 | 
					    log_packet(incoming, From, JID, Packet).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec log_packet_offline(jid(), jid(), xmlel()) -> any().
 | 
				
			||||||
 | 
					
 | 
				
			||||||
log_packet_offline(From, To, Packet) ->
 | 
					log_packet_offline(From, To, Packet) ->
 | 
				
			||||||
    log_packet(offline, From, To, Packet).
 | 
					    log_packet(offline, From, To, Packet).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec reopen_log() -> any().
 | 
				
			||||||
 | 
					
 | 
				
			||||||
reopen_log() ->
 | 
					reopen_log() ->
 | 
				
			||||||
    gen_server:cast(?PROCNAME, reopen_log).
 | 
					    gen_server:cast(?PROCNAME, reopen_log).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -135,6 +164,8 @@ reopen_log() ->
 | 
				
			|||||||
%% Internal functions.
 | 
					%% Internal functions.
 | 
				
			||||||
%% -------------------------------------------------------------------
 | 
					%% -------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec log_packet(direction(), jid(), jid(), xmlel()) -> any().
 | 
				
			||||||
 | 
					
 | 
				
			||||||
log_packet(Direction, From, To, #xmlel{name = <<"message">>} = Packet) ->
 | 
					log_packet(Direction, From, To, #xmlel{name = <<"message">>} = Packet) ->
 | 
				
			||||||
    case xml:get_subtag(Packet, <<"body">>) of
 | 
					    case xml:get_subtag(Packet, <<"body">>) of
 | 
				
			||||||
      #xmlel{children = Body} when length(Body) > 0 ->
 | 
					      #xmlel{children = Body} when length(Body) > 0 ->
 | 
				
			||||||
@ -152,6 +183,8 @@ log_packet(Direction, From, To, #xmlel{name = <<"message">>} = Packet) ->
 | 
				
			|||||||
log_packet(_Direction, _From, _To, _Packet) ->
 | 
					log_packet(_Direction, _From, _To, _Packet) ->
 | 
				
			||||||
    ok.
 | 
					    ok.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec get_message_type(xmlel()) -> binary().
 | 
				
			||||||
 | 
					
 | 
				
			||||||
get_message_type(#xmlel{attrs = Attrs}) ->
 | 
					get_message_type(#xmlel{attrs = Attrs}) ->
 | 
				
			||||||
    case xml:get_attr_s(<<"type">>, Attrs) of
 | 
					    case xml:get_attr_s(<<"type">>, Attrs) of
 | 
				
			||||||
      <<"">> ->
 | 
					      <<"">> ->
 | 
				
			||||||
@ -160,6 +193,8 @@ get_message_type(#xmlel{attrs = Attrs}) ->
 | 
				
			|||||||
	  Type
 | 
						  Type
 | 
				
			||||||
    end.
 | 
					    end.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec is_carbon(xmlel()) -> {true, direction()} | false.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
is_carbon(Packet) ->
 | 
					is_carbon(Packet) ->
 | 
				
			||||||
    {Direction, SubTag} = case {xml:get_subtag(Packet, <<"sent">>),
 | 
					    {Direction, SubTag} = case {xml:get_subtag(Packet, <<"sent">>),
 | 
				
			||||||
				xml:get_subtag(Packet, <<"received">>)} of
 | 
									xml:get_subtag(Packet, <<"received">>)} of
 | 
				
			||||||
@ -182,6 +217,8 @@ is_carbon(Packet) ->
 | 
				
			|||||||
	  false
 | 
						  false
 | 
				
			||||||
    end.
 | 
					    end.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec write_log(io:device(), direction(), jid(), jid(), binary()) -> ok.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
write_log(IoDevice, Direction, From, To, Type) ->
 | 
					write_log(IoDevice, Direction, From, To, Type) ->
 | 
				
			||||||
    Date = format_date(calendar:local_time()),
 | 
					    Date = format_date(calendar:local_time()),
 | 
				
			||||||
    Record = io_lib:format("~s [~s, ~s] ~s -> ~s~n",
 | 
					    Record = io_lib:format("~s [~s, ~s] ~s -> ~s~n",
 | 
				
			||||||
@ -190,6 +227,8 @@ write_log(IoDevice, Direction, From, To, Type) ->
 | 
				
			|||||||
			    jlib:jid_to_string(To)]),
 | 
								    jlib:jid_to_string(To)]),
 | 
				
			||||||
    ok = file:write(IoDevice, [Record]).
 | 
					    ok = file:write(IoDevice, [Record]).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-spec format_date(calendar:datetime()) -> io_lib:chars().
 | 
				
			||||||
 | 
					
 | 
				
			||||||
format_date({{Year, Month, Day}, {Hour, Minute, Second}}) ->
 | 
					format_date({{Year, Month, Day}, {Hour, Minute, Second}}) ->
 | 
				
			||||||
    Format = "~B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B",
 | 
					    Format = "~B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B",
 | 
				
			||||||
    io_lib:format(Format, [Year, Month, Day, Hour, Minute, Second]).
 | 
					    io_lib:format(Format, [Year, Month, Day, Hour, Minute, Second]).
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user