X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=modules%2Ferl%2Feuphorik_protocole.erl;h=ef37ace3991fe3194607f60080b29b120b181574;hp=518d22d19ad55ee39418dbcbfaeb6cb3f12b4c1e;hb=5d9992368bb386d2e606ae037c5478fe10ac70e8;hpb=4a6c575807a90370c0069b688026b10102e1ce10 diff --git a/modules/erl/euphorik_protocole.erl b/modules/erl/euphorik_protocole.erl index 518d22d..ef37ace 100755 --- a/modules/erl/euphorik_protocole.erl +++ b/modules/erl/euphorik_protocole.erl @@ -39,7 +39,7 @@ % Une utilisateur s'enregistre avec un tuple {Login, Password}. -register([{login, Login}, {password, Password}, {profile, Profile_json}], IP) -> +register([{"login", Login}, {"password", Password}, {"profile", Profile_json}], IP) -> Can_register = euphorik_bd:can_register(IP), if Can_register -> case euphorik_bd:user_by_login(Login) of @@ -54,7 +54,7 @@ register([{login, Login}, {password, Password}, {profile, Profile_json}], IP) -> erreur_register_flood() end; % Enregistrement sans {Login, Password} -register([{profile, Profile_json}], IP) -> +register([{"profile", Profile_json}], IP) -> Can_register = euphorik_bd:can_register(IP), if Can_register -> Profile = profile_from_json(Profile_json), @@ -70,7 +70,7 @@ erreur_register_flood() -> % Un utilisateur se logge (avec un couple {login, mot de passe}) -login([{login, Login}, {password, Password}], IP) -> +login([{"login", Login}, {"password", Password}], IP) -> case euphorik_bd:user_by_login_password(Login, Password) of {ok, User} -> loginUser(User, IP); @@ -79,7 +79,7 @@ login([{login, Login}, {password, Password}], IP) -> erreur(30) end; % Un utilisateur se logge (avec un cookie) -login([{cookie, Cookie}], IP) -> +login([{"cookie", Cookie}], IP) -> case euphorik_bd:user_by_cookie(Cookie) of {ok, User} -> loginUser(User, IP); @@ -98,7 +98,7 @@ loginUser(User, IP) -> % Renvoie un string() représentant un cookie en base 36. Il y a 10^32 possibillités. generer_cookie() -> - {A1, A2, A3} = now(), + {A1, A2, A3} = erlang:timestamp(), random:seed(A1, A2, A3), erlang:integer_to_list(random:uniform(trunc(math:pow(10, 32))), 36). @@ -106,10 +106,10 @@ generer_cookie() -> % Modification du profile. profile( [ - {cookie, Cookie}, - {login, Login}, - {password, Password}, - {profile, Profile_json} + {"cookie", Cookie}, + {"login", Login}, + {"password", Password}, + {"profile", Profile_json} ] ) -> case profile_from_json(Profile_json) of @@ -130,21 +130,21 @@ profile( profile_from_json( {struct, [ - {nick, Pseudo}, - {email, Email}, - {css, Css}, - {chat_order, Chat_order_str}, - {nick_format, Nick_format_str}, - {view_times, View_times}, - {view_tooltips, View_tooltips}, - {conversations, {array, Conversations_json}}, - {ostentatious_master, Ostentatious_master_str} + {"nick", Pseudo}, + {"email", Email}, + {"css", Css}, + {"chat_order", Chat_order_str}, + {"nick_format", Nick_format_str}, + {"view_times", View_times}, + {"view_tooltips", View_tooltips}, + {"conversations", {array, Conversations_json}}, + {"ostentatious_master", Ostentatious_master_str} ] } ) -> % décomposition de la strucure JSON Conversations = lists:foldr( - fun({struct, [{root, Racine}, {minimized, Reduit}]}, A) -> + fun({struct, [{"root", Racine}, {"minimized", Reduit}]}, A) -> % virage des messages qui n'existent pas Message_exite = euphorik_bd:message_existe(Racine), if Message_exite -> @@ -190,16 +190,16 @@ profile_from_json( % Attend un événement pour la page "Chat" % last_message id et cookie sont facultatifs -wait_event([{page, "chat"} | Data]) -> +wait_event([{"page", "chat"} | Data]) -> % traitement des inputs - Cookie = case lists:keysearch(cookie, 1, Data) of {value, {_, C}} -> C; _ -> inconnu end, - Last_message_id = case lists:keysearch(last_message_id, 1, Data) of {value, {_, Id}} -> Id; _ -> 0 end, - {value, {_, Message_count}} = lists:keysearch(message_count, 1, Data), - Main_page = case lists:keysearch(main_page, 1, Data) of {value, {_, P}} -> P; _ -> 1 end, - {value, {_, {array, Conversations_json}}} = lists:keysearch(conversations, 1, Data), + Cookie = case lists:keysearch("cookie", 1, Data) of {value, {_, C}} -> C; _ -> inconnu end, + Last_message_id = case lists:keysearch("last_message_id", 1, Data) of {value, {_, Id}} -> Id; _ -> 0 end, + {value, {_, Message_count}} = lists:keysearch("message_count", 1, Data), + Main_page = case lists:keysearch("main_page", 1, Data) of {value, {_, P}} -> P; _ -> 1 end, + {value, {_, {array, Conversations_json}}} = lists:keysearch("conversations", 1, Data), Racines_conversations = lists:map( - fun({struct, [{root, Racine}, {page, Page} | Reste]}) -> - Last_mess_conv = case Reste of [{last_message_id, L}] -> L; _ -> 0 end, + fun({struct, [{"root", Racine}, {"page", Page} | Reste]}) -> + Last_mess_conv = case Reste of [{"last_message_id", L}] -> L; _ -> 0 end, {Racine, Page, Last_mess_conv} end, Conversations_json @@ -217,12 +217,12 @@ wait_event([{page, "chat"} | Data]) -> R end; % Attend un événement pour la page "Admin" -wait_event([{page, "admin"}]) -> +wait_event([{"page", "admin"}]) -> case wait_event_page_admin() of banned_ips_refresh -> {struct, [ - {reply, "banned_ips_refresh"} + {"reply", "banned_ips_refresh"} ] }; _ -> @@ -242,13 +242,13 @@ wait_event_page_chat(User, Racines_conversations, Message_count, Last_message_id Conversations -> % Accrochez-vous ca va siouxer ;). {struct, [ - {reply, "new_messages"}, - {conversations, {array, + {"reply", "new_messages"}, + {"conversations", {array, lists:map( fun({Racine, {Conv, Plus}}) -> {struct, [ - {last_page, not Plus}, - {first, % le premier message de la conversation + {"last_page", not Plus}, + {"first", % le premier message de la conversation if Racine =:= undefined orelse Conv =:= [] -> null; true -> @@ -333,10 +333,10 @@ wait_event_page_admin() -> % Answer_to est une liste d'id (int) put_message( [ - {cookie, Cookie}, - {nick, Nick}, - {content, Content}, - {answer_to, {array, Answer_to}} + {"cookie", Cookie}, + {"nick", Nick}, + {"content", Content}, + {"answer_to", {array, Answer_to}} ] ) -> case euphorik_bd:user_by_cookie(Cookie) of @@ -366,10 +366,10 @@ put_message( % bannissement d'un utilisateur (son ip est bannie) ban( [ - {cookie, Cookie}, - {duration, Duration}, - {user_id, User_id}, - {reason, Reason} + {"cookie", Cookie}, + {"duration", Duration}, + {"user_id", User_id}, + {"reason", Reason} ]) -> % controle que l'utilisateur est un admin case euphorik_bd:user_by_cookie(Cookie) of @@ -402,9 +402,9 @@ ban( % slapage d'un user (avertissement) slap( [ - {cookie, Cookie}, - {user_id, User_id}, - {reason, Reason} + {"cookie", Cookie}, + {"user_id", User_id}, + {"reason", Reason} ]) -> % controle que l'utilisateur est un admin case euphorik_bd:user_by_cookie(Cookie) of @@ -439,8 +439,8 @@ slap( unban_ip( [ - {cookie, Cookie}, - {ip, IP} + {"cookie", Cookie}, + {"ip", IP} ] ) -> case euphorik_bd:user_by_cookie(Cookie) of @@ -454,7 +454,7 @@ unban_ip( list_banned_ips( [ - {cookie, Cookie} + {"cookie", Cookie} ] ) -> case euphorik_bd:user_by_cookie(Cookie) of @@ -462,19 +462,19 @@ list_banned_ips( { struct, [ - {reply, "list_banned_ips"}, - {list, {array, lists:map( + {"reply", "list_banned_ips"}, + {"list", {array, lists:map( fun({IP, T, Users}) -> {struct, [ - {ip, euphorik_common:serialize_ip(IP)}, - {remaining_time, format_minutes(T)}, - {users, {array, lists:map( + {"ip", euphorik_common:serialize_ip(IP)}, + {"remaining_time", format_minutes(T)}, + {"users", {array, lists:map( fun({Pseudo, Login}) -> {struct, [ - {nick, Pseudo}, - {login, Login} + {"nick", Pseudo}, + {"login", Login} ] } end, @@ -504,9 +504,9 @@ erreur(Num) -> erreur_json(Num, Mess) -> { struct, [ - {reply, "error"}, - {no, Num}, - {error_message, Mess} + {"reply", "error"}, + {"no", Num}, + {"error_message", Mess} ] }. @@ -549,34 +549,34 @@ format_date(Date) -> json_reponse_ok() -> - {struct, [{reply, "ok"}]}. + {struct, [{"reply", "ok"}]}. json_reponse_login_ok(#user{profile = Profile} = User) -> { struct, [ - {reply, "login"}, - {status, if (User#user.password =/= []) and (User#user.login =/= []) -> "auth_registered"; true -> "auth_not_registered" end}, - {cookie, User#user.cookie}, - {id, User#user.id}, - {login, User#user.login}, - {ek_master, User#user.ek_master}, - {profile, {struct, + {"reply", "login"}, + {"status", if (User#user.password =/= []) and (User#user.login =/= []) -> "auth_registered"; true -> "auth_not_registered" end}, + {"cookie", User#user.cookie}, + {"id", User#user.id}, + {"login", User#user.login}, + {"ek_master", User#user.ek_master}, + {"profile", {struct, [ - {nick, Profile#profile.pseudo}, - {email, Profile#profile.email}, - {css, Profile#profile.css}, - {chat_order, atom_to_list(Profile#profile.chat_order)}, - {nick_format, atom_to_list(Profile#profile.nick_format)}, - {view_times, Profile#profile.view_times}, - {view_tooltips, Profile#profile.view_tooltips}, - {conversations, {array, lists:map( + {"nick", Profile#profile.pseudo}, + {"email", Profile#profile.email}, + {"css", Profile#profile.css}, + {"chat_order", atom_to_list(Profile#profile.chat_order)}, + {"nick_format", atom_to_list(Profile#profile.nick_format)}, + {"view_times", Profile#profile.view_times}, + {"view_tooltips", Profile#profile.view_tooltips}, + {"conversations", {array, lists:map( fun({Racine, Reduit}) -> - {struct, [{root, Racine}, {minimized, Reduit}]} + {struct, [{"root", Racine}, {"minimized", Reduit}]} end, Profile#profile.conversations )}}, - {ostentatious_master, atom_to_list(Profile#profile.ostentatious_master)} + {"ostentatious_master", atom_to_list(Profile#profile.ostentatious_master)} ] }} ] @@ -592,25 +592,25 @@ json_message(Mess, Repond_a, User) -> Est_une_reponse_a_user = User =/= inconnu andalso euphorik_bd:est_une_reponse_a_user(User#user.id, Mess#minichat.id), {ok, #user{profile = Profile_mess} = User_mess } = euphorik_bd:user_by_id(Mess#minichat.auteur_id), {struct, [ - {id, Mess#minichat.id}, - {user_id, User_mess#user.id}, - {date, case Mess#minichat.date of undefined -> "?"; _ -> format_date(Mess#minichat.date) end}, - {system, Mess#minichat.auteur_id =:= 0}, - {owner, Est_proprietaire}, - {answered, A_repondu_a_message}, - {is_a_reply, Est_une_reponse_a_user}, - {nick, Mess#minichat.pseudo}, - {login, User_mess#user.login}, - {content, Mess#minichat.contenu}, - {root, Mess#minichat.racine_id}, - {answer_to, {array, lists:map( + {"id", Mess#minichat.id}, + {"user_id", User_mess#user.id}, + {"date", case Mess#minichat.date of undefined -> "?"; _ -> format_date(Mess#minichat.date) end}, + {"system", Mess#minichat.auteur_id =:= 0}, + {"owner", Est_proprietaire}, + {"answered", A_repondu_a_message}, + {"is_a_reply", Est_une_reponse_a_user}, + {"nick", Mess#minichat.pseudo}, + {"login", User_mess#user.login}, + {"content", Mess#minichat.contenu}, + {"root", Mess#minichat.racine_id}, + {"answer_to", {array, lists:map( fun(Id_mess) -> {ok, M} = euphorik_bd:message_by_id(Id_mess), {ok, User_reponse} = euphorik_bd:user_by_mess(M#minichat.id), - {struct, [{id, M#minichat.id}, {nick, M#minichat.pseudo}, {login, User_reponse#user.login}]} + {struct, [{"id", M#minichat.id}, {"nick", M#minichat.pseudo}, {"login", User_reponse#user.login}]} end, Repond_a )}}, - {ek_master, User_mess#user.ek_master}, - {ostentatious_master, atom_to_list(Profile_mess#profile.ostentatious_master)} + {"ek_master", User_mess#user.ek_master}, + {"ostentatious_master", atom_to_list(Profile_mess#profile.ostentatious_master)} ]}.