<commit_before>%%%-------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @copyright (C) 2017, <NAME>
%%% @doc
%%%
%%% @end
%%% Created : 16. Mar 2017 by <NAME> <<EMAIL>>
%%%-------------------------------------------------------------------
-module(eSMTP).
-author("<EMAIL>").

-behaviour(gen_server).

%% API
-export([start_link/0,
         updateConfig/0,
         setUser/1,
         sendMail/3]).

%% gen_server callbacks
-export([init/1,
         handle_call/3,
         handle_cast/2,
         handle_info/2,
         terminate/2,
         code_change/3]).

-define(SERVER, ?MODULE).

-record(state, {host, port, auth, smtpUser, smtpPwd,
                active = false, user, localHost}).

-include_lib("eTodo/include/eTodo.hrl").

%%%===================================================================
%%% API
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @end
%%--------------------------------------------------------------------
-spec(start_link() ->
             {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

updateConfig() ->
    gen_server:cast(?SERVER, updateConfig).

setUser(User) ->
    gen_server:cast(?SERVER, {setUser, User}).

sendMail(From, To, Msg) ->
    gen_server:cast(?SERVER, {sendMail, From, To, Msg}).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%%                     {ok, State, Timeout} |
%%                     ignore |
%%                     {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-spec(init(Args :: term()) ->
             {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
             {stop, Reason :: term()} | ignore).
init([]) ->
    updateConfig(),
    {ok, #state{}}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @end
%%--------------------------------------------------------------------
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
                  State :: #state{}) ->
             {reply, Reply :: term(), NewState :: #state{}} |
             {reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
             {noreply, NewState :: #state{}} |
             {noreply, NewState :: #state{}, timeout() | hibernate} |
             {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
             {stop, Reason :: term(), NewState :: #state{}}).
handle_call(_Request, _From, State) ->
    {reply, ok, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @end
%%--------------------------------------------------------------------
-spec(handle_cast(Request :: term(), State :: #state{}) ->
             {noreply, NewState :: #state{}} |
             {noreply, NewState :: #state{}, timeout() | hibernate} |
             {stop, Reason :: term(), NewState :: #state{}}).
handle_cast(updateConfig, State = #state{user = undefined}) ->
    {noreply, State};
handle_cast(updateConfig, State) ->
    UserCfg = eTodoDB:readUserCfg(State#state.user),
    ConCfg  = eTodoDB:getConnection(State#state.user),
    {noreply, State#state{host      = UserCfg#userCfg.smtpServer,
                          port      = UserCfg#userCfg.smtpPort,
                          smtpUser  = UserCfg#userCfg.smtpUser,
                          smtpPwd   = UserCfg#userCfg.smtpPwd,
                          auth      = UserCfg#userCfg.smtpAuth,
                          active    = UserCfg#userCfg.smtpEnabled,
                          localHost = ConCfg#conCfg.host}};
handle_cast({setUser, User}, State) ->
    {noreply, State#state{user = User}};
handle_cast(_Msg, State = #state{active = false}) ->
    {noreply, State};
handle_cast({sendMail, From, To, Msg}, State) ->
    Result = doSendMail(From, To, Msg, State),
    case Result of
        {error, {Type, Reason}} ->
            eLog:log(debug, ?MODULE, sendAndLog, [From, To, Result],
                     "doSendMail result", ?LINE),
            ErrorTxt = io_lib:format("Failed to send mail: ~p~n~n```~n~p~n```~n",
                                    [Type, Reason]),
            eTodo:systemEntry(system, ErrorTxt);
        _ ->
            ok
    end,
    {noreply, State};
handle_cast(_Request, State) ->
    {noreply, State}.
<commit_msg>Handle unexpected conCfg.<commit_after>%%%-------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @copyright (C) 2017, <NAME>
%%% @doc
%%%
%%% @end
%%% Created : 16. Mar 2017 by <NAME> <<EMAIL>>
%%%-------------------------------------------------------------------
-module(eSMTP).
-author("<EMAIL>").

-behaviour(gen_server).

%% API
-export([start_link/0,
         updateConfig/0,
         setUser/1,
         sendMail/3]).

%% gen_server callbacks
-export([init/1,
         handle_call/3,
         handle_cast/2,
         handle_info/2,
         terminate/2,
         code_change/3]).

-define(SERVER, ?MODULE).

-record(state, {host, port, auth, smtpUser, smtpPwd,
                active = false, user, localHost}).

-include_lib("eTodo/include/eTodo.hrl").

%%%===================================================================
%%% API
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @end
%%--------------------------------------------------------------------
-spec(start_link() ->
             {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

updateConfig() ->
    gen_server:cast(?SERVER, updateConfig).

setUser(User) ->
    gen_server:cast(?SERVER, {setUser, User}).

sendMail(From, To, Msg) ->
    gen_server:cast(?SERVER, {sendMail, From, To, Msg}).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%%                     {ok, State, Timeout} |
%%                     ignore |
%%                     {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-spec(init(Args :: term()) ->
             {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
             {stop, Reason :: term()} | ignore).
init([]) ->
    updateConfig(),
    {ok, #state{}}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @end
%%--------------------------------------------------------------------
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
                  State :: #state{}) ->
             {reply, Reply :: term(), NewState :: #state{}} |
             {reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
             {noreply, NewState :: #state{}} |
             {noreply, NewState :: #state{}, timeout() | hibernate} |
             {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
             {stop, Reason :: term(), NewState :: #state{}}).
handle_call(_Request, _From, State) ->
    {reply, ok, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @end
%%--------------------------------------------------------------------
-spec(handle_cast(Request :: term(), State :: #state{}) ->
             {noreply, NewState :: #state{}} |
             {noreply, NewState :: #state{}, timeout() | hibernate} |
             {stop, Reason :: term(), NewState :: #state{}}).
handle_cast(updateConfig, State = #state{user = undefined}) ->
    {noreply, State};
handle_cast(updateConfig, State) ->
    UserCfg = eTodoDB:readUserCfg(State#state.user),
    ConCfg  = eTodoUtils:default(eTodoDB:getConnection(State#state.user), #conCfg{}),
    {noreply, State#state{host      = UserCfg#userCfg.smtpServer,
                          port      = UserCfg#userCfg.smtpPort,
                          smtpUser  = UserCfg#userCfg.smtpUser,
                          smtpPwd   = UserCfg#userCfg.smtpPwd,
                          auth      = UserCfg#userCfg.smtpAuth,
                          active    = UserCfg#userCfg.smtpEnabled,
                          localHost = ConCfg#conCfg.host}};
handle_cast({setUser, User}, State) ->
    {noreply, State#state{user = User}};
handle_cast(_Msg, State = #state{active = false}) ->
    {noreply, State};
handle_cast({sendMail, From, To, Msg}, State) ->
    Result = doSendMail(From, To, Msg, State),
    case Result of
        {error, {Type, Reason}} ->
            eLog:log(debug, ?MODULE, sendAndLog, [From, To, Result],
                     "doSendMail result", ?LINE),
            ErrorTxt = io_lib:format("Failed to send mail: ~p~n~n```~n~p~n```~n",
                                    [Type, Reason]),
            eTodo:systemEntry(system, ErrorTxt);
        _ ->
            ok
    end,
    {noreply, State};
handle_cast(_Request, State) ->
    {noreply, State}.

{'fraction_non_alphanumeric': 0.3470076726342711, 'fraction_numerical': 0.004092071611253197, 'mean_word_length': 1.6202090592334495, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7006253', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2585, 'n_words': 547}