From dfc0469a184ae978f40cac9df20d399fdeca99a3 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 17 Jul 2010 15:49:23 +0200 Subject: Read JID and server name from config file --- src/core/CMakeLists.txt | 1 + src/core/ephraim.erl | 9 +++++++- src/core/ephraim_config.erl | 55 +++++++++++++++++++++++++++++++++++++++++++++ src/core/ephraim_conn.erl | 10 ++++++--- src/core/ephraim_util.erl | 18 --------------- 5 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 src/core/ephraim_config.erl diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 9321f40..9c536aa 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,5 +1,6 @@ erl_target(ephraim-core ephraim.erl + ephraim_config.erl ephraim_conn.erl ephraim_conv.erl ephraim_event.erl diff --git a/src/core/ephraim.erl b/src/core/ephraim.erl index a7a872b..d558eda 100644 --- a/src/core/ephraim.erl +++ b/src/core/ephraim.erl @@ -23,7 +23,14 @@ stop() -> -spec init() -> ok. init() -> register(ephraim, self()), - + + Config = spawn(ephraim_config, init, []), + % Wait for config process + receive + {Config, started} -> + io:format("Reading config file...~n") + end, + GUI = spawn(ephraim_gui, init, []), Conn = spawn(ephraim_conn, init, []), Roster = spawn(ephraim_roster, init, []), diff --git a/src/core/ephraim_config.erl b/src/core/ephraim_config.erl new file mode 100644 index 0000000..6c49351 --- /dev/null +++ b/src/core/ephraim_config.erl @@ -0,0 +1,55 @@ +-module(ephraim_config). +-compile([debug_info, export_all]). + +-spec init() -> ok. +init() -> + register(ephraim_config, self()), + ephraim ! {self(), started}, + + {ok, Terms} = file:consult(config_path() ++ "/ephraim.cfg"), + Config = dict:from_list(lists:map(fun(Term) -> {element(1, Term), Term} end, Terms)), + + loop(Config). + + +-spec config_path() -> string(). +config_path() -> + Path = case os:getenv("XDG_CONFIG_HOME") of + false -> + os:getenv("HOME") ++ "/.config"; + ConfigHome -> + ConfigHome + end ++ "/ephraim", + case file:read_file_info(Path) of + {ok, _} -> + ok; + {error, _} -> + file:make_dir(Path) + end, + Path. + + +-spec get(atom()) -> tuple() | error. +get(Key) -> + ephraim_config ! {self(), get, Key}, + receive + {config, ok, Key, Value} -> + Value; + {config, error, Key} -> + error + end. + +-spec loop(dict()) -> ok. +loop(Config) -> + receive + stop -> + ok; + {From, get, Key} -> + case dict:find(Key, Config) of + {ok, Value} -> + From ! {config, ok, Key, Value}; + error -> + From ! {config, error, Key} + end, + loop(Config) + end. diff --git a/src/core/ephraim_conn.erl b/src/core/ephraim_conn.erl index ee57284..6a34599 100644 --- a/src/core/ephraim_conn.erl +++ b/src/core/ephraim_conn.erl @@ -11,10 +11,14 @@ init() -> application:start(exmpp), Session = exmpp_session:start(), - JID = exmpp_jid:make("ephraim", "localhost", "Bar"), - exmpp_session:auth_basic_digest(Session, JID, "test"), + + {jid, JID} = ephraim_config:get(jid), + {password, Password} = ephraim_config:get(password), + {server, Server, Port} = ephraim_config:get(server), + + exmpp_session:auth_basic_digest(Session, exmpp_jid:parse(JID), Password), io:format("Connecting...~n"), - exmpp_session:connect_TCP(Session, "localhost", 5222), + exmpp_session:connect_TCP(Session, Server, Port), io:format("Connected.~n"), session(#conn_state{session=Session}). diff --git a/src/core/ephraim_util.erl b/src/core/ephraim_util.erl index cdf9cea..73cb7fc 100644 --- a/src/core/ephraim_util.erl +++ b/src/core/ephraim_util.erl @@ -8,21 +8,3 @@ send_all(Dest, Message) -> -spec send_all_values(dict(), term()) -> term(). send_all_values(Dest, Message) -> dict:fold(fun(_,Value,Msg) -> Value ! Msg end, Message, Dest). - --spec config_path() -> string(). -config_path() -> - Path = case os:getenv("XDG_CONFIG_HOME") of - false -> - os:getenv("HOME") ++ "/.config"; - ConfigHome -> - ConfigHome - end ++ "/ephraim", - case file:read_file_info(Path) of - {ok, _} -> - ok; - {error, _} -> - file:make_dir(Path) - end, - Path. - - -- cgit v1.2.3