MOD Avancement sur le passage à JSON
[euphorik.git] / modules / erl / euphorik_protocole.erl
index 19932f2..7e80308 100755 (executable)
@@ -8,8 +8,6 @@
 
 -module(euphorik_protocole).
 -export([
-   %generation_captcha/1, Obsolète
-   %nouveau_user_captcha/1, Obsolète
    nouveau_user_login/1,
    login/1,
    logout/1,
 -include("../include/euphorik_bd.hrl").\r
 -include("../include/euphorik_defines.hrl").\r
 
-
-% Génère un nouveau captchat dans ?DOSSIER_CAPTCHA\r
-% Obsolète
-%~ 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)).
-   
-\r
-% Un nouvel utilisateur doit être créé.\r
-% Obolète : le captcha n'existe plus
-% Action est un xmlElement().
-%~ nouveau_user_captcha(Action) ->
-   %~ simple_xml_to_string(\r
-      %~ case {xmerl_xpath:string("captchaCrypt", Action), xmerl_xpath:string("captchaInput", Action)} of\r
-         %~ {[#xmlElement{content = [#xmlText{value = C1}]}], [#xmlElement{content = [#xmlText{value = C2}]}]} ->\r
-            %~ C2_crypt = common:crypt(C2),\r
-            %~ if C1 =:= C2_crypt ->\r
-                  %~ Cookie = generer_cookie(),\r
-                  %~ User = euphorik_minichat:nouveau_user("Paul", Cookie),\r
-                  %~ xml_reponse_login_ok(User);\r
-               %~ true ->
-                  %~ xml_reponse_login_pas_ok("Captcha incorrect")\r
-            %~ end;
-         %~ _ ->
-            %~ xml_reponse_login_pas_ok("XML malformé")\r
-      %~ end
-   %~ ).
-   
    
 % Une utilisateur s'enregistre avec un tuple {Login, Password}.\r
 % @spec nouveau_user_login(xmerl:xmlElement()) -> string()
@@ -71,6 +41,10 @@ nouveau_user_login(Action) ->
    
 \r
 % Un utilisateur se logge.
+login([{login, Login}, {password, Password}]) ->
+   {ok, User} = euphorik_minichat:user_by_login_password(Login, Password),
+   
+   
 login(Action) ->
    case xmerl_xpath:string("cookie", Action) of
       [#xmlElement{content = [#xmlText{value = Cookie}]}] ->
@@ -113,12 +87,20 @@ profile(Action) ->
             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,
             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) ->
+            Conversations = lists:foldr(
+               fun(Conv, Acc) ->
                   [#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)}
+                  Message_id = erlang:list_to_integer(Id_racine_str, 36),
+                  % vérification de la validité de l'id
+                  Message_existe = euphorik_minichat:message_existe(Message_id),
+                  if  Message_existe ->
+                        [{Message_id, list_to_integer(Page_conv_str)} | Acc];
+                     true ->
+                        Acc
+                  end
                end,
+               [],
                xmerl_xpath:string("conversation", Action)
             ),
             case euphorik_minichat:set_profile(Cookie, Login, Password, Pseudo, Email, Css, Page_principale, Conversations) of
@@ -152,17 +134,7 @@ refreshMessage(Action) ->
                      _ -> inconnu
                   end;
                _ -> inconnu
-            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)
-            %~ ),\r
+            end,\r
             % accrochez-vous ca va siouxer ;)
             [{reponse, [{name, "refreshMessages"}],\r
                lists:map(\r
@@ -199,8 +171,13 @@ refreshMessage(Action) ->
                            )
                         ]
                      }\r
-                  end,            
-                  euphorik_minichat_conversation:conversations(User#user.conversations, Nb_message, Dernier_id, User#user.page_principale)\r
+                  end,
+                  euphorik_minichat_conversation:conversations(
+                     if User =/= inconnu -> User#user.conversations; true -> [] end,
+                     Nb_message,
+                     Dernier_id,
+                     if User =/= inconnu -> User#user.page_principale; true -> 1 end
+                  )\r
                )
             }];
          _ ->
@@ -374,16 +351,5 @@ xml_reponse_message(Ok) ->
          ]\r
       }\r
    ].
-\r
-\r
-% Obsolète
-%~ xml_reponse_generation_captcha(Chemin, Captcha) ->
-   %~ [
-      %~ {reponse, [{name, "generationCaptcha"}],
-         %~ [
-            %~ {chemin, [], [Chemin]},
-            %~ {captchaCrypt, [], [Captcha]}
-         %~ ]
-      %~ }
-   %~ ].
+
 %%%%%%%%% </réponses XML> %%%%%%%%%