From 5d9992368bb386d2e606ae037c5478fe10ac70e8 Mon Sep 17 00:00:00 2001 From: Ummon Date: Sat, 13 Apr 2019 21:40:57 +0200 Subject: [PATCH] Update to the new library 'json2' --- modules/erl/euphorik_bd.erl | 26 ++--- modules/erl/euphorik_bd_admin.erl | 2 +- modules/erl/euphorik_protocole.erl | 180 ++++++++++++++--------------- modules/erl/euphorik_requests.erl | 7 +- modules/erl/euphorik_test.erl | 4 +- tools/yaws.conf | 19 +-- 6 files changed, 116 insertions(+), 122 deletions(-) diff --git a/modules/erl/euphorik_bd.erl b/modules/erl/euphorik_bd.erl index 0933645..6b10f4b 100755 --- a/modules/erl/euphorik_bd.erl +++ b/modules/erl/euphorik_bd.erl @@ -96,7 +96,7 @@ get_texte(Id, _Lang = fr) -> nouveau_user(Cookie, Profile) -> F = fun() -> Id = nouvel_id(user), - User = #user{id = Id, cookie = Cookie, date_creation = now(), date_derniere_connexion = now(), profile = Profile}, + User = #user{id = Id, cookie = Cookie, date_creation = erlang:timestamp(), date_derniere_connexion = erlang:timestamp(), profile = Profile}, mnesia:write(User), User end, @@ -107,7 +107,7 @@ nouveau_user(Cookie, Profile) -> nouveau_user(Login, Password, Cookie, Profile) -> F = fun() -> Id = nouvel_id(user), - User = #user{id = Id, cookie = Cookie, login = Login, password = Password, date_creation = now(), date_derniere_connexion = now(), profile = Profile#profile{pseudo = Login}}, + User = #user{id = Id, cookie = Cookie, login = Login, password = Password, date_creation = erlang:timestamp(), date_derniere_connexion = erlang:timestamp(), profile = Profile#profile{pseudo = Login}}, mnesia:write(User), User end, @@ -145,7 +145,7 @@ update_date_derniere_connexion(User_id) -> fun() -> case mnesia:wread({user, User_id}) of [User] -> - mnesia:write(User#user{date_derniere_connexion = now()}); + mnesia:write(User#user{date_derniere_connexion = erlang:timestamp()}); _ -> mnesia:abort("update_date_derniere_connexion: User inconnu") end @@ -298,7 +298,7 @@ nouveau_message(Mess, Auteur_id, Repond_A_ids) -> case mnesia:wread({user, Auteur_id}) of [#user{profile = Profile} = Auteur] -> % comparaison entre la date du dernier poste et maintenant (gestion du flood) - Now = now(), + Now = erlang:timestamp(), Delta = euphorik_common:delta_date_ms(Auteur#user.date_derniere_connexion, Now), Nouvel_indice_flood = Auteur#user.indice_flood + if Delta =< ?DUREE_SPAM -> 2; true -> -1 end, Auteur_maj = Auteur#user{ @@ -348,7 +348,7 @@ nouveau_message_sys(Mess) -> resultat_transaction(mnesia:transaction( fun() -> Id = nouvel_id(minichat), - mnesia:write(#minichat{id = Id, auteur_id = 0, date = now(), pseudo = Profile#profile.pseudo, contenu = Mess, racine_id = Id}), + mnesia:write(#minichat{id = Id, auteur_id = 0, date = erlang:timestamp(), pseudo = Profile#profile.pseudo, contenu = Mess, racine_id = Id}), Id end )). @@ -550,7 +550,7 @@ possede_message(Id_user, Id_mess) -> list_ban() -> resultat_transaction(mnesia:transaction( fun() -> - Now = now(), + Now = erlang:timestamp(), e(qlc:keysort(1, q([ { IP#ip_table.ip, @@ -570,9 +570,9 @@ ban(IP, Duration) -> fun() -> case mnesia:wread({ip_table, IP}) of [IP_tuple] -> - mnesia:write(IP_tuple#ip_table{ban = now(), ban_duration = Duration}); + mnesia:write(IP_tuple#ip_table{ban = erlang:timestamp(), ban_duration = Duration}); _ -> - mnesia:write(#ip_table{ip = IP, ban = now(), ban_duration = Duration}) + mnesia:write(#ip_table{ip = IP, ban = erlang:timestamp(), ban_duration = Duration}) end end ). @@ -597,7 +597,7 @@ est_banni(User_id) -> ]), [{tmpdir, ?KEY_SORT_TEMP_DIR}]) of [{Ban, Ban_duration}] -> Echeance = date_plus_minutes(Ban, Ban_duration), - Now = now(), + Now = erlang:timestamp(), if Echeance < Now -> % l'échéance est passée false; true -> @@ -610,7 +610,7 @@ est_banni(User_id) -> )). -% Ban est une date tel que retourner par now(). +% Ban est une date tel que retourner par erlang:timestamp(). % Ban_duration est un temps en minutes. % retourne une date. date_plus_minutes(Ban, Ban_duration) -> @@ -627,16 +627,16 @@ can_register(IP) -> fun() -> case e(q([I || I <- mnesia:table(ip_table), I#ip_table.ip =:= IP]), [{tmpdir, ?KEY_SORT_TEMP_DIR}]) of [] -> - mnesia:write(#ip_table{ip = IP, date_last_try_register = now()}), + mnesia:write(#ip_table{ip = IP, date_last_try_register = erlang:timestamp()}), true; [T] -> - Delta = euphorik_common:delta_date_ms(T#ip_table.date_last_try_register, now()), + Delta = euphorik_common:delta_date_ms(T#ip_table.date_last_try_register, erlang:timestamp()), if T#ip_table.nb_try_register =:= ?NB_MAX_FLOOD_REGISTER, Delta < ?TEMPS_BAN_FLOOD_REGISTER -> false; true -> mnesia:write(T#ip_table{ ip = IP, - date_last_try_register = now(), + date_last_try_register = erlang:timestamp(), nb_try_register = T#ip_table.nb_try_register + if Delta < ?TEMPS_FLOOD_REGISTER -> 1; T#ip_table.nb_try_register > 0 -> -1; true -> 0 end }), true diff --git a/modules/erl/euphorik_bd_admin.erl b/modules/erl/euphorik_bd_admin.erl index 132b238..e5c20bc 100644 --- a/modules/erl/euphorik_bd_admin.erl +++ b/modules/erl/euphorik_bd_admin.erl @@ -150,7 +150,7 @@ reset() -> % crée l'utilisateur root mnesia:transaction(fun() -> mnesia:write(#proprietes{nom = version, valeur = ?VERSION_BD}), - User = #user{id = 0, profile = #profile{pseudo = "Sys"}, login = "Sys", date_creation = now(), date_derniere_connexion = now(), ek_master = true}, + User = #user{id = 0, profile = #profile{pseudo = "Sys"}, login = "Sys", date_creation = erlang:timestamp(), date_derniere_connexion = erlang:timestamp(), ek_master = true}, mnesia:write(User), User end), 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)} ]}. diff --git a/modules/erl/euphorik_requests.erl b/modules/erl/euphorik_requests.erl index 69a5597..49cbe02 100755 --- a/modules/erl/euphorik_requests.erl +++ b/modules/erl/euphorik_requests.erl @@ -36,8 +36,10 @@ out(A) -> end, % active: to receive {tcp_closed, _} when the socket is closing. % keepalive: avoid firewalls to cut the connection. + inet:setopts(A#arg.clisock, [{active, true}, {keepalive, true}]), {value, {_, Contenu}} = lists:keysearch("action", 1, yaws_api:parse_post(A)), + Ret = traiter_message(Contenu, IP), {content, "application/json", Ret}. @@ -45,8 +47,9 @@ out(A) -> % Décode le message JSON. traiter_message(Contenu, IP) -> % extrait l'entête obligatoire des messages JSON - {ok, {struct, [{header, {struct, [{action, Action}, {version, Version_client}]}} | Reste]}} = json:decode_string(Contenu), - json:encode( + {ok, {struct, [{"header", {struct, [{"action", Action}, {"version", Version_client}]}} | Reste]}} = json2:decode_string(Contenu), + + json2:encode( if Version_client =:= ?VERSION_PROTOCOLE -> traiter_action(Action, Reste, IP); true -> diff --git a/modules/erl/euphorik_test.erl b/modules/erl/euphorik_test.erl index 7b2d9a1..a166465 100644 --- a/modules/erl/euphorik_test.erl +++ b/modules/erl/euphorik_test.erl @@ -45,7 +45,7 @@ start(N, M) -> timer:sleep(100), spawn( fun() -> - {A1, A2, A3} = now(), + {A1, A2, A3} = erlang:timestamp(), random:seed(A1, A2, A3), loop(Id, M) end @@ -197,7 +197,7 @@ bench_write_minichat(N, Temps) -> mnesia:write(#minichat{ id = Id, auteur_id = random:uniform(10000), - date = now(), + date = erlang:timestamp(), pseudo = "Test", contenu = "Blabla blabla bla.", racine_id = random:uniform(10000) diff --git a/tools/yaws.conf b/tools/yaws.conf index 6c0994d..820f310 100644 --- a/tools/yaws.conf +++ b/tools/yaws.conf @@ -1,4 +1,4 @@ -# Configuration pour euphorik.ch en mode de développement. +# Configuration pour euphorik.ch en mode de d�veloppement. logdir = /tmp @@ -15,6 +15,7 @@ include_dir = ../modules/include allowed_scripts = yaws appmods = start_mod = euphorik_daemon + auth_log = true # dir_listings = true @@ -27,7 +28,7 @@ trace = false cache_refresh_secs = 0 -# it is possible to have yaws start additional +# it is possible to have yaws start additional # application specific code at startup # runmod = my_app @@ -48,17 +49,16 @@ log_wrap_size = 1000000 log_resolve_hostname = false -# fail completely or not if yaws fails +# fail completely or not if yaws fails # to bind a listen socket fail_on_bind_err = true # If HTTP auth is used, it is possible to have a specific # auth log. -auth_log = true -# When we're running multiple yaws systems on the same +# When we're running multiple yaws systems on the same # host, we need to give each yaws system an individual # name. Yaws will write a number of runtime files under # /tmp/yaws/${id} @@ -75,12 +75,3 @@ auth_log = true # This is often nice in testing environments but not # acceptable in real live hosting scenarios pick_first_virthost_on_nomatch = true - -# All unices are broken since it's not possible to bind to -# a privileged port (< 1024) unless uid==0 -# There is a contrib in jungerl which makes it possible by means -# of an external setuid root programm called fdsrv to listen to -# to privileged port. -# If we use this feature, it requires fdsrv to be properly installed. -# Doesn't yet work with SSL. -use_fdsrv = false -- 2.45.2