diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2010-10-05 03:34:02 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2010-10-05 03:34:02 +0200 |
commit | 3d451b3dc69d91b04c1153f63436b63940287b13 (patch) | |
tree | 93c647b492621c39225b25eb185962de60353f30 /src/core | |
parent | 2444ac841fad00133f231e9667ab427c4ce4d39e (diff) | |
download | ephraim-3d451b3dc69d91b04c1153f63436b63940287b13.tar ephraim-3d451b3dc69d91b04c1153f63436b63940287b13.zip |
Sending work now, too
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/core/ephraim_account.erl | 12 | ||||
-rw-r--r-- | src/core/ephraim_account_event_proxy.erl | 6 | ||||
-rw-r--r-- | src/core/ephraim_accounts.erl | 2 | ||||
-rw-r--r-- | src/core/ephraim_conv.erl | 69 | ||||
-rw-r--r-- | src/core/ephraim_conv_handler.erl | 25 | ||||
-rw-r--r-- | src/core/ephraim_conv_man.erl | 6 | ||||
-rw-r--r-- | src/core/ephraim_event_proxy.erl | 27 | ||||
-rw-r--r-- | src/core/ephraim_roster.erl | 47 |
9 files changed, 110 insertions, 85 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 97c7d4e..75055bb 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -8,6 +8,7 @@ erl_target(ephraim-core ephraim_conv.erl ephraim_conv_handler.erl ephraim_conv_man.erl + ephraim_event_proxy.erl ephraim_gui.erl ephraim_roster.erl ephraim_roster_handler.erl diff --git a/src/core/ephraim_account.erl b/src/core/ephraim_account.erl index 8024d68..2e95f88 100644 --- a/src/core/ephraim_account.erl +++ b/src/core/ephraim_account.erl @@ -1,18 +1,18 @@ -module(ephraim_account). -behaviour(supervisor). --export([start_link/0]). +-export([start_link/1]). -export([init/1]). -start_link() -> +start_link(Account) -> {ok, Pid} = supervisor:start_link(?MODULE, []), {ok, EventManager} = supervisor:start_child(Pid, {event_man, {gen_event, start_link, []}, permanent, 1000, worker, [gen_event]}), - ephraim_account_event_proxy:start(EventManager), - - {ok, _} = supervisor:start_child(Pid, {conv_man, {ephraim_conv_man, start_link, [EventManager]}, permanent, 1000, supervisor, [ephraim_conv_man]}), + ephraim_event_proxy:start(Account, EventManager), + ephraim_account_event_proxy:start(Account, EventManager), {ok, Connection} = supervisor:start_child(Pid, {connection, {ephraim_conn, start_link, [EventManager]}, permanent, 10000, worker, [ephraim_conn]}), - {ok, _} = supervisor:start_child(Pid, {roster, {ephraim_roster, start_link, [EventManager, Connection]}, permanent, 1000, worker, [ephraim_roster]}), + {ok, Roster} = supervisor:start_child(Pid, {roster, {ephraim_roster, start_link, [EventManager, Connection]}, permanent, 1000, worker, [ephraim_roster]}), + {ok, _} = supervisor:start_child(Pid, {conv_man, {ephraim_conv_man, start_link, [EventManager, Connection, Roster]}, permanent, 1000, supervisor, [ephraim_conv_man]}), {ok, Pid}. init(_Args) -> diff --git a/src/core/ephraim_account_event_proxy.erl b/src/core/ephraim_account_event_proxy.erl index d9cf94e..726b0e0 100644 --- a/src/core/ephraim_account_event_proxy.erl +++ b/src/core/ephraim_account_event_proxy.erl @@ -1,11 +1,11 @@ -module(ephraim_account_event_proxy). -behaviour(gen_event). --export([start/1]). +-export([start/2]). -export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]). -start(EventManager) -> - gen_event:add_handler(EventManager, ?MODULE, foo_account). +start(Account, EventManager) -> + gen_event:add_handler(EventManager, ?MODULE, Account). init(Account) -> {ok, Account}. diff --git a/src/core/ephraim_accounts.erl b/src/core/ephraim_accounts.erl index a35c486..d3a3694 100644 --- a/src/core/ephraim_accounts.erl +++ b/src/core/ephraim_accounts.erl @@ -8,4 +8,4 @@ start_link() -> init(_Args) -> {ok, {{one_for_one, 1, 60}, - [{account, {ephraim_account, start_link, []}, permanent, infinity, supervisor, [ephraim_account]}]}}. + [{account, {ephraim_account, start_link, [foo_account]}, permanent, infinity, supervisor, [ephraim_account]}]}}. diff --git a/src/core/ephraim_conv.erl b/src/core/ephraim_conv.erl index d9431b2..d6ab742 100644 --- a/src/core/ephraim_conv.erl +++ b/src/core/ephraim_conv.erl @@ -1,39 +1,53 @@ -module(ephraim_conv). -behaviour(gen_server). --export([start_link/2]). +-export([start_link/4]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). --export([received_message/4]). +-export([received_message/4, send_message/3]). -record(conv_state, { - conv_ref :: reference(), event_manager :: pid(), + connection :: pid(), + roster :: pid(), my_jid :: binary(), jid :: binary() }). --spec start_link(pid(), binary()) -> ok. -start_link(EventManager, JID) -> - ConvRef = make_ref(), - io:format("Starting conversation ~p with ~p~n", [ConvRef, JID]), - - gen_event:notify(EventManager, {view_update, {conversation, ConvRef, {new, JID}}}), +-spec start_link(pid(), pid(), pid(), binary()) -> ok. +start_link(EventManager, Conn, Roster, JID) -> + gen_event:notify(EventManager, {view_update, {conversation, JID, new}}), {jid,MyJID} = ephraim_config:get(jid), - gen_server:start_link(?MODULE, #conv_state{conv_ref=ConvRef,event_manager=EventManager,my_jid=list_to_binary(MyJID),jid=JID}, []). + gen_server:start_link(?MODULE, #conv_state{event_manager=EventManager,connection=Conn,roster=Roster,my_jid=list_to_binary(MyJID),jid=JID}, []). init(State) -> process_flag(trap_exit, true), + io:format("Starting conversation ~p with ~p~n", [self(), State#conv_state.jid]), + {ok, State}. received_message(Conv, From, Type, Body) -> gen_server:cast(Conv, {received_message, From, Type, Body}). +send_message(Conv, Type, Body) -> + gen_server:cast(Conv, {send_message, Type, Body}). handle_call(_Msg, _From, State) -> {noreply, State}. +update_view(State, From, Type, Body) -> + Alias = ephraim_roster:get_alias(State#conv_state.roster, From), + gen_event:notify(State#conv_state.event_manager, {view_update, {conversation, State#conv_state.jid, {message, Alias, Type, Body}}}). + handle_cast({received_message, From, Type, Body}, State) -> - gen_event:notify(State#conv_state.event_manager, {view_update, {conversation, State#conv_state.conv_ref, {message, From, Type, Body}}}), + update_view(State, From, Type, Body), + {noreply, State}; +handle_cast({send_message, Type, Body}, State) -> + Packet = exmpp_message:normal(Body), + Packet2 = exmpp_message:set_type(Packet, Type), + Packet3 = exmpp_xml:set_attribute(Packet2, to, State#conv_state.jid), + ephraim_conn:send_packet(State#conv_state.connection, Packet3), + + update_view(State, State#conv_state.my_jid, Type, Body), {noreply, State}; handle_cast(_Msg, State) -> {noreply, State}. @@ -41,38 +55,7 @@ handle_cast(_Msg, State) -> handle_info(_Msg, State) -> {ok, State}. terminate(_Reason, State) -> - io:format("Stopping conversation ~p with ~p~n", [State#conv_state.conv_ref, State#conv_state.jid]), + io:format("Stopping conversation ~p with ~p~n", [self(), State#conv_state.jid]), ok. code_change(_OldVersion, State, _Extra) -> {ok, State}. - - --spec loop(#conv_state{}) -> ok. -loop(State) -> - receive - stop -> - ok; - - {receive_message, Packet} -> - Type = exmpp_message:get_type(Packet), - Body = exmpp_message:get_body(Packet), - if - Body =/= undefined -> - ephraim ! {ui_update, {chat_message, State#conv_state.jid, Type, ephraim:get_alias(State#conv_state.jid), Body}}; - true -> - io:format("Received strange message from ~p:~n~p~n", [State#conv_state.jid, Packet]) - end, - loop(State); - - {send_message, Type, Message} -> - Packet = exmpp_message:normal(Message), - Packet2 = exmpp_message:set_type(Packet, Type), - Packet3 = exmpp_xml:set_attribute(Packet2, to, State#conv_state.jid), - ephraim ! {send_packet, Packet3}, - ephraim ! {ui_update, {chat_message, State#conv_state.jid, Type, ephraim:get_alias(State#conv_state.my_jid), Message}}, - loop(State); - - Msg -> - io:format("ephraim_conv (~p): ~p~n", [State#conv_state.jid, Msg]), - loop(State) - end. diff --git a/src/core/ephraim_conv_handler.erl b/src/core/ephraim_conv_handler.erl index e525d99..681e53a 100644 --- a/src/core/ephraim_conv_handler.erl +++ b/src/core/ephraim_conv_handler.erl @@ -1,20 +1,21 @@ -module(ephraim_conv_handler). -include_lib("exmpp/include/exmpp_client.hrl"). -behaviour(gen_event). --export([start/2]). +-export([start/4]). -export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]). -record(conv_handler_state, { supervisor :: pid(), event_manager :: pid(), + connection :: pid(), + roster :: pid(), convs = dict:new() :: dict() }). -start(Supervisor, EventManager) -> - gen_event:add_handler(EventManager, ?MODULE, {Supervisor, EventManager}). +start(Supervisor, EventManager, Conn, Roster) -> + gen_event:add_handler(EventManager, ?MODULE, #conv_handler_state{supervisor=Supervisor,event_manager=EventManager,connection=Conn,roster=Roster}). -init({Supervisor, EventManager}) -> - State = #conv_handler_state{supervisor=Supervisor,event_manager=EventManager}, +init(State) -> {ok, State}. -spec get_conv(#conv_handler_state{},exmpp_jid:jid()) -> {#conv_handler_state{},pid()|undefined}. @@ -32,12 +33,24 @@ get_conv(State, JID) -> {State, Conv}; error -> {ok, Conv} = supervisor:start_child(State#conv_handler_state.supervisor, - {Key, {ephraim_conv, start_link, [State#conv_handler_state.event_manager, JID]}, transient, 1000, worker, [ephraim_conv]}), + {Key, {ephraim_conv, start_link, [State#conv_handler_state.event_manager, + State#conv_handler_state.connection, State#conv_handler_state.roster, + JID]}, + transient, 1000, worker, [ephraim_conv]}), Dict = dict:store(Key, Conv, State#conv_handler_state.convs), {State#conv_handler_state{convs=Dict}, Conv} end end. +handle_event({view_request, {conversation,JID,new}}, State) -> + {NewState, _} = get_conv(State, JID), + {ok, NewState}; + +handle_event({view_request, {conversation,JID,{send_message,Type,Body}}}, State) -> + {NewState, Conv} = get_conv(State, JID), + ephraim_conv:send_message(Conv, Type, Body), + {ok, NewState}; + handle_event({received_packet, #received_packet{packet_type=message, raw_packet=Packet}}, State) -> From = exmpp_xml:get_attribute(Packet, from, <<"unknown">>), Type = exmpp_message:get_type(Packet), diff --git a/src/core/ephraim_conv_man.erl b/src/core/ephraim_conv_man.erl index 617ccc4..adfa24d 100644 --- a/src/core/ephraim_conv_man.erl +++ b/src/core/ephraim_conv_man.erl @@ -1,11 +1,11 @@ -module(ephraim_conv_man). -behaviour(supervisor). --export([start_link/1]). +-export([start_link/3]). -export([init/1]). -start_link(EventManager) -> +start_link(EventManager, Conn, Roster) -> {ok, Pid} = supervisor:start_link(?MODULE, []), - ephraim_conv_handler:start(Pid, EventManager), + ephraim_conv_handler:start(Pid, EventManager, Conn, Roster), {ok, Pid}. init(_Args) -> diff --git a/src/core/ephraim_event_proxy.erl b/src/core/ephraim_event_proxy.erl new file mode 100644 index 0000000..4b0e35b --- /dev/null +++ b/src/core/ephraim_event_proxy.erl @@ -0,0 +1,27 @@ +-module(ephraim_event_proxy). +-behaviour(gen_event). + +-export([start/2]). +-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]). + +start(Account, EventManager) -> + gen_event:add_handler(ephraim_event_man, ?MODULE, {Account, EventManager}). + +init(State) -> + {ok, State}. + +handle_event({view_request, {account, Account, Event}}, {Account, EventManager}) -> + gen_event:notify(EventManager, {view_request, Event}), + {ok, {Account, EventManager}}; + +handle_event(_Event, State) -> + {ok, State}. + +handle_call(_Msg, State) -> + {noreply, State}. +handle_info(_Msg, State) -> + {ok, State}. +terminate(_Reason, _State) -> + ok. +code_change(_OldVersion, State, _Extra) -> + {ok, State}. diff --git a/src/core/ephraim_roster.erl b/src/core/ephraim_roster.erl index 605d019..6c6a229 100644 --- a/src/core/ephraim_roster.erl +++ b/src/core/ephraim_roster.erl @@ -4,7 +4,7 @@ -export([start_link/2]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). --export([presence/2, roster_iq/2, vcard_iq/3]). +-export([presence/2, roster_iq/2, vcard_iq/3, get_alias/2]). -record(roster_state, { event_manager :: pid(), @@ -176,27 +176,9 @@ handleVCardIQs(VCard, [Item|Rest]) -> VCard2 = handleVCardIQ(VCard, Item), handleVCardIQs(VCard2, Rest). --spec get_alias(#roster_state{}, binary()) -> binary(). +-spec get_alias(pid(), binary()) -> binary(). get_alias(Roster, JID) -> - {Node, Domain, _Resource} = exmpp_jid:to_lower(exmpp_jid:parse(JID)), - BareJID = list_to_binary([Node, <<"@">>, Domain]), - - case dict:find(BareJID, Roster#roster_state.entries) of - {ok, Entry} -> - case Entry#roster_entry.name of - undefined -> - case dict:find('NICKNAME', Entry#roster_entry.vcard) of - {ok, Value} -> - Value; - error -> - Node - end; - Name -> - Name - end; - error -> - Node - end. + gen_server:call(Roster, {get_alias,JID}). presence(Roster, Packet) -> gen_server:cast(Roster, {presence, Packet}). @@ -205,8 +187,27 @@ roster_iq(Roster, Payload) -> vcard_iq(Roster, From, Payload) -> gen_server:cast(Roster, {vcard_iq, From, Payload}). -handle_call(_Msg, _From, State) -> - {noreply, State}. +handle_call({get_alias,JID}, _From, State) -> + {Node, Domain, _Resource} = exmpp_jid:to_lower(exmpp_jid:parse(JID)), + BareJID = list_to_binary([Node, <<"@">>, Domain]), + + Result = case dict:find(BareJID, State#roster_state.entries) of + {ok, Entry} -> + case Entry#roster_entry.name of + undefined -> + case dict:find('NICKNAME', Entry#roster_entry.vcard) of + {ok, Value} -> + Value; + error -> + Node + end; + Name -> + Name + end; + error -> + Node + end, + {reply, Result, State}. handle_cast({presence, Packet}, State) -> %io:format("ephraim_roster: Presence: ~p~n", [Packet]), |