X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=modules%2Ferl%2Feuphorik_protocole.erl;h=f56a335af65af640125937a067ddb3266d20cda2;hb=7ed3a03bffb4112ee174b05ef1d7b486d6ad6534;hp=7b9423a394b7fc8de0998375866a2af8c8034128;hpb=13ea96f3600d1c42b22b6ea91dc75055555f72b7;p=euphorik.git diff --git a/modules/erl/euphorik_protocole.erl b/modules/erl/euphorik_protocole.erl index 7b9423a..f56a335 100755 --- a/modules/erl/euphorik_protocole.erl +++ b/modules/erl/euphorik_protocole.erl @@ -1,58 +1,32 @@ % coding: utf-8 -% Ce module gére les différents message envoyé par le client (javascript) +% Ce module gére les différents message envoyé par le client (javascript) via AJAX. % Par exemple le client peut demander les derniers messages du minichat. % Les messages sont au format XML, la plus part des fonctions accepte un xmlDocument() et renvoie un string() % qui est la réponse XML. -% Example XML : http://www.erlang.org/doc/apps/xmerl/xmerl_ug.html -% Auteur : G.Burri -% Date : 29.10.2007 +% Example XML : http://www.erlang.org/doc/apps/xmerl/xmerl_ug.html. +% @author G.Burri -module(euphorik_protocole). -export([ - generation_captcha/1, - nouveau_user_captcha/1, nouveau_user_login/1, login/1, logout/1, profile/1, refreshMessage/1, message/1 -]). +]). + -include_lib("xmerl/include/xmerl.hrl"). -include("../include/euphorik_bd.hrl"). --include("../include/euphorik_defines.hrl"). -%-compile(export_all). - +-include("../include/euphorik_defines.hrl"). -% Génère un nouveau captchat dans ?DOSSIER_CAPTCHA -generation_captcha(_) -> - {Mot_crypt, Nom_fichier} = captcha:create(5, ?DOSSIER_CAPTCHA), - simple_xml_to_string(xml_reponse_generation_captcha(?DOSSIER_CAPTCHA_RELATIF "/" ++ Nom_fichier, Mot_crypt)). - - -% Un nouvel utilisateur doit être créé -% Action est un xmlElement() -nouveau_user_captcha(Action) -> - simple_xml_to_string( - case {xmerl_xpath:string("captchaCrypt", Action), xmerl_xpath:string("captchaInput", Action)} of - {[#xmlElement{content = [#xmlText{value = C1}]}], [#xmlElement{content = [#xmlText{value = C2}]}]} -> - C2_crypt = common:crypt(C2), - if C1 =:= C2_crypt -> - Cookie = generer_cookie(), - User = euphorik_minichat:nouveau_user("Paul", Cookie), - xml_reponse_login_ok(User); - true -> - xml_reponse_login_pas_ok("Captcha incorrect") - end; - _ -> - xml_reponse_login_pas_ok("XML malformé") - end - ). +% Une utilisateur s'enregistre avec un tuple {Login, Password}. +% @spec nouveau_user_login(xmerl:xmlElement()) -> string() nouveau_user_login(Action) -> {Login, Password, Login_deja_pris} = case {xmerl_xpath:string("login", Action), xmerl_xpath:string("password", Action)} of {[#xmlElement{content = [#xmlText{value = L}]}], [#xmlElement{content = [#xmlText{value = P}]}]} -> - {L, P, case euphorik_minichat:get_user_by_login(L) of {ok, _} -> true; _ -> false end}; + {L, P, case euphorik_minichat:user_by_login(L) of {ok, _} -> true; _ -> false end}; _ -> {[], [], false} end, simple_xml_to_string( @@ -66,15 +40,15 @@ nouveau_user_login(Action) -> ). -% Un utilisateur se logge +% Un utilisateur se logge. login(Action) -> case xmerl_xpath:string("cookie", Action) of [#xmlElement{content = [#xmlText{value = Cookie}]}] -> - loginUser(euphorik_minichat:get_user_by_cookie(Cookie)); + loginUser(euphorik_minichat:user_by_cookie(Cookie)); _ -> case {xmerl_xpath:string("login", Action), xmerl_xpath:string("password", Action)} of {[#xmlElement{content = [#xmlText{value = Login}]}], [#xmlElement{content = [#xmlText{value = Password}]}]} -> - loginUser(euphorik_minichat:get_user_by_login_password(Login, Password)); + loginUser(euphorik_minichat:user_by_login_password(Login, Password)); _ -> simple_xml_to_string(xml_reponse_login_pas_ok("XML malformé")) end @@ -108,7 +82,16 @@ profile(Action) -> Pseudo = case xmerl_xpath:string("pseudo", Action) of [#xmlElement{content = [#xmlText{value = P2}]}] -> P2; _ -> Login end, Email = case xmerl_xpath:string("email", Action) of [#xmlElement{content = [#xmlText{value = E}]}] -> E; _ -> undefined end, Css = case xmerl_xpath:string("css", Action) of [#xmlElement{content = [#xmlText{value = C}]}] -> C; _ -> undefined end, - case euphorik_minichat:set_profile(Cookie, Login, Password, Pseudo, Email, Css) of + Page_principale = case xmerl_xpath:string("pagePrincipale", Action) of [#xmlElement{content = [#xmlText{value = P3}]}] -> list_to_integer(P3); _ -> undefined end, + Conversations = lists:map( + fun(Conv) -> + [#xmlElement{content = [#xmlText{value = Id_racine_str}]}] = xmerl_xpath:string("racine", Conv), + [#xmlElement{content = [#xmlText{value = Page_conv_str}]}] = xmerl_xpath:string("page", Conv), + {erlang:list_to_integer(Id_racine_str, 36), list_to_integer(Page_conv_str)} + end, + xmerl_xpath:string("conversation", Action) + ), + case euphorik_minichat:set_profile(Cookie, Login, Password, Pseudo, Email, Css, Page_principale, Conversations) of ok -> xml_reponse_profile_ok(); login_deja_pris -> @@ -125,74 +108,77 @@ profile(Action) -> % Renvoie les messages appropriés. refreshMessage(Action) -> simple_xml_to_string( - case { - xmerl_xpath:string("nombreMessage", Action), % le nombre de message qu'affiche le client - xmerl_xpath:string("page", Action) % la page désiré, la première (les derniers messages) étant la 1 - } of - { - [#xmlElement{content = [#xmlText{value = Nb_message_str}]}], - [#xmlElement{content = [#xmlText{value = Page_str}]}] - } -> + case xmerl_xpath:string("nombreMessage", Action) of % le nombre de message qu'affiche le client + [#xmlElement{content = [#xmlText{value = Nb_message_str}]}] -> Nb_message = list_to_integer(Nb_message_str), - Page = list_to_integer(Page_str), Dernier_id = case xmerl_xpath:string("dernierMessageId", Action) of % l'id du dernier message que connait le client [#xmlElement{content = [#xmlText{value = D}]}] -> erlang:list_to_integer(D, 36); _ -> 0 - end, + end, User = case xmerl_xpath:string("cookie", Action) of [#xmlElement{content = [#xmlText{value = Cookie}]}] -> - case euphorik_minichat:get_user_by_cookie(Cookie) of + case euphorik_minichat:user_by_cookie(Cookie) of {ok, U} -> U; _ -> inconnu end; _ -> inconnu - end, - % accrochez-vous ca va siouxer ;) - Mess = lists:map( - fun(Mess) -> - Est_proprietaire = User =/= inconnu andalso User#user.id =:= Mess#minichat.auteur_id, - A_repondu_a_message = User =/= inconnu andalso euphorik_minichat:a_repondu_a_message(User#user.id, Mess#minichat.id), - Est_une_reponse_a_user = User =/= inconnu andalso euphorik_minichat:est_une_reponse_a_user(User#user.id, Mess#minichat.id), - User_mess = if Mess#minichat.auteur_id =:= 0 -> inconnu; true -> - {ok, U2} = euphorik_minichat:get_user_by_id(Mess#minichat.auteur_id), - U2 - end, - {message, [{id, erlang:integer_to_list(Mess#minichat.id, 36)}], - [ - {date, [], [format_date(Mess#minichat.date)]}, - {systeme, [], [atom_to_list(Mess#minichat.auteur_id =:= 0)]}, - {proprietaire, [], [atom_to_list(Est_proprietaire)]}, - {repondu, [], [atom_to_list(A_repondu_a_message)]}, - {reponse, [], [atom_to_list(Est_une_reponse_a_user)]}, - {pseudo, [], [Mess#minichat.pseudo]}, - {login, [], [if User_mess =:= inconnu -> Mess#minichat.pseudo; true -> User_mess#user.login end]}, - {contenu, [], [Mess#minichat.contenu]}, - {repondA, [], xml_repond_a(Mess#minichat.id)} - ] - } - end, - get_dernieres_messages(Dernier_id, Nb_message, Page) - ), + end, + % extraction des conversations en [{id, page}, ..] + % Obsolète : obtenu depuis la table 'user' + %~ Conversations = lists:map( + %~ fun(Conv) -> + %~ [#xmlElement{content = [#xmlText{value = Id_racine_str}]}] = xmerl_xpath:string("racine", Conv), + %~ [#xmlElement{content = [#xmlText{value = Page_conv_str}]}] = xmerl_xpath:string("page", Conv), + %~ {erlang:list_to_integer(Id_racine_str, 36), erlang:list_to_integer(Page_conv_str)} + %~ end, + %~ xmerl_xpath:string("conversation", Action) + %~ ), + % accrochez-vous ca va siouxer ;) [{reponse, [{name, "refreshMessages"}], - % la fonction get_nb_page DOIT être évalué après get_dernieres_messages (merci les effets de bord) - [{nbPage, [], [integer_to_list(euphorik_minichat:get_nb_page(Nb_message))]} | Mess] + lists:map( + fun({Conv, Plus}) -> + {conversation, [], + [{autresPages, [], [atom_to_list(Plus)]} | + lists:map( + fun({Mess, Repond_a}) -> + Est_proprietaire = User =/= inconnu andalso User#user.id =:= Mess#minichat.auteur_id, + A_repondu_a_message = User =/= inconnu andalso euphorik_minichat:a_repondu_a_message(User#user.id, Mess#minichat.id), + Est_une_reponse_a_user = User =/= inconnu andalso euphorik_minichat:est_une_reponse_a_user(User#user.id, Mess#minichat.id), + User_mess = + if Mess#minichat.auteur_id =:= 0 -> + inconnu; + true -> + {ok, U2} = euphorik_minichat:user_by_id(Mess#minichat.auteur_id), + U2 + end, + {message, [{id, erlang:integer_to_list(Mess#minichat.id, 36)}], + [ + {date, [], [format_date(Mess#minichat.date)]}, + {systeme, [], [atom_to_list(Mess#minichat.auteur_id =:= 0)]}, + {proprietaire, [], [atom_to_list(Est_proprietaire)]}, + {repondu, [], [atom_to_list(A_repondu_a_message)]}, + {reponse, [], [atom_to_list(Est_une_reponse_a_user)]}, + {pseudo, [], [Mess#minichat.pseudo]}, + {login, [], [if User_mess =:= inconnu -> Mess#minichat.pseudo; true -> User_mess#user.login end]}, + {contenu, [], [Mess#minichat.contenu]}, + {repondA, [], xml_repond_a(Repond_a)} + ] + } + end, + Conv + ) + ] + } + end, + euphorik_minichat_conversation:conversations(User#user.conversations, Nb_message, Dernier_id, User#user.page_principale) + ) }]; _ -> [{reponse, [{name, "refreshMessages"}], [{erreur, [], ["erreur"]}]}] end ). - -% Renvoie les derniers messages, s'il n'y en a pas on effectue une attente. -get_dernieres_messages(Dernier_id, Nb_message, Page) -> - Messages = euphorik_minichat:messages(Dernier_id, Nb_message, Page), - if Messages =:= [] -> - euphorik_minichat:attends_nouveau_messages(), - euphorik_minichat:messages(Dernier_id, Nb_message, Page); - true -> - Messages - end. - + % Prend une liste de xml text node et en resort un string() % xmerl : "test & test" devient deux fragments de texte : "test " et "& test", il faut donc rassembler les morceaux... defragmenter(Text_nodes) -> @@ -212,7 +198,7 @@ message(Action) -> [#xmlElement{content = Pseudo_fragments}], [#xmlElement{content = Contenu_fragments}] } -> - case euphorik_minichat:get_user_by_cookie(Cookie) of + case euphorik_minichat:user_by_cookie(Cookie) of {ok, U} -> Pseudo = defragmenter(Pseudo_fragments), Contenu = defragmenter(Contenu_fragments), @@ -266,7 +252,7 @@ format_date(Date) -> %%%%%%%%% %%%%%%%%% simple_xml_to_string(XML) -> - lists:flatten(xmerl:export_simple(XML, xmerl_xml, [{prolog, ["\n"]}])). + lists:flatten(xmerl:export_simple(XML, xmerl_xml, [{prolog, ["\n"]}])). % Construit une réponse positive à un login @@ -280,8 +266,20 @@ xml_reponse_login_ok(User) -> {pseudo, [User#user.pseudo]}, {login, [User#user.login]}, {email, [User#user.email]}, - {css, [User#user.css]} - ] + {css, [User#user.css]}, + {pagePrincipale, [integer_to_list(User#user.page_principale)]} + ] ++ + lists:map( + fun(C) -> + {conversation, + [ + {racine, [erlang:integer_to_list(element(1, C), 36)]}, + {page, [integer_to_list(element(2, C))]} + ] + } + end, + User#user.conversations + ) }]. @@ -327,13 +325,14 @@ xml_reponse_profile_pas_ok(Message) -> % Renvoie un element XML representant une liste de messages auquel le message M_id repond -xml_repond_a(Mess_id) -> +xml_repond_a(Reponses) -> lists:map( - fun(M) -> - {ok, User} = euphorik_minichat:get_user_by_id(M#minichat.auteur_id), + fun(Id_mess) -> + {ok, M} = euphorik_minichat:message_by_id(Id_mess), + {ok, User} = euphorik_minichat:user_by_mess(Id_mess), {id, [{id, erlang:integer_to_list(M#minichat.id, 36)}, {pseudo, M#minichat.pseudo}, {login, User#user.login}], []} end, - euphorik_minichat:repond_a(Mess_id) + Reponses ). @@ -345,18 +344,5 @@ xml_reponse_message(Ok) -> ] } ]. - -xml_reponse_generation_captcha(Chemin, Captcha) -> - [ - {reponse, [{name, "generationCaptcha"}], - [ - {chemin, [], [Chemin]}, - {captchaCrypt, [], [Captcha]} - ] - } - ]. -%%%%%%%%% %%%%%%%%% - - - +%%%%%%%%% %%%%%%%%%