X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;ds=inline;f=modules%2Ferl%2Feuphorik_requests.erl;h=69a5597eade7af2ec44e6b7338bf1eff8a4906dd;hb=4a6c575807a90370c0069b688026b10102e1ce10;hp=81dd2e875094e3ff2cd7cd828849b1d837560d50;hpb=35838780dce22af9f2dc80af9149a30a0433de0a;p=euphorik.git
diff --git a/modules/erl/euphorik_requests.erl b/modules/erl/euphorik_requests.erl
index 81dd2e8..69a5597 100755
--- a/modules/erl/euphorik_requests.erl
+++ b/modules/erl/euphorik_requests.erl
@@ -1,65 +1,86 @@
% coding: utf-8
-% Ce module est fait pour répondre à des requêtes 'AJAX'.
-% Il est définit comme 'appmods' pour l'url "request" dans yaws.
-% Par exemple http://www.euphorik.ch/request abouti sur la fonction out() de ce module.
-% @author G.Burri
-
--module(euphorik_requests).
--export([
- tester/0,
- out/1
-]).
+% Copyright 2008 Grégory Burri
+%
+% This file is part of Euphorik.
+%
+% Euphorik is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Euphorik is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Euphorik. If not, see .
+%
+% This module responds to JSON requests via 'AJAX'.
+% It's defined as an Yaws 'appmods' with the url '/request'.
+% For instance in debug mode 'http://localhost:8090/request' will call the out/1 function.
+
--include_lib("xmerl/include/xmerl.hrl").
--include_lib("yaws/include/yaws_api.hrl").
+-module(euphorik_requests).
+-author("Greg Burri ").
+-export([out/1]).
+-include_lib("yaws_api.hrl").
+-include("../include/euphorik_defines.hrl").
-% Test du module
-tester() ->
- que_dal.
-
-% il faut catcher toutes les exceptions possibles
+% Entry point for all AJAX requests on '/request'.
out(A) ->
- %inet:setopts(A#arg.clisock, inet:getopts(A#arg.clisock, [active])),
+ IP = case inet:peername(A#arg.clisock) of
+ {ok, {Address, _Port}} -> Address;
+ _ -> unknown
+ 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_donnees(Contenu),
- %{content, "text/xml", Ret}.
+ Ret = traiter_message(Contenu, IP),
{content, "application/json", Ret}.
-
-traiter_donnees(Contenu) ->
- % FIXME : ne plus encapsuler le JSON dans de l'xml... apparement yaws veux absolument de l'xml (voir post mi-avril 2008 sur la mailing list)
- Reponse = case xmerl_scan:string(Contenu) of
- {#xmlElement{name = json, content = [#xmlText{value = J}|_]}, _} ->
- case json:decode_string(J) of
- {ok, {struct, [{action, Action}| Reste]}} ->
- %io:format("~p~n", [euphorik_protocole:login(JSON)]),
- json:encode(traiter_action(Action, Reste));
- _ ->
- error
- end;
- _ -> error
- end,
- if Reponse =:= error ->
- euphorik_protocole:erreur("Format XML/JSON incorrect");
- true ->
- Reponse
- end.
-
-
-% authentification d'un client
-traiter_action("authentification", JSON) ->
- euphorik_protocole:login(JSON);
-% un client s'enregistre (pseudo + password)
-traiter_action("register", JSON) ->
- euphorik_protocole:register(JSON);
-% modification du profile
-traiter_action("set_profile", JSON) ->
- euphorik_protocole:profile(JSON);
-% un utilisateur attend un événement (par exemple l'arrivé d'un nouveau message)
-traiter_action("wait_event", JSON) ->
- euphorik_protocole:wait_event(JSON);
-% un utilisateur envoie un message
-traiter_action("put_message", JSON) ->
- euphorik_protocole:put_message(JSON).
-
\ No newline at end of file
+
+% 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(
+ if Version_client =:= ?VERSION_PROTOCOLE ->
+ traiter_action(Action, Reste, IP);
+ true ->
+ euphorik_protocole:erreur(lists:flatten(io_lib:format(
+ "La version du protocole du client (~w) ne correspond à celle du serveur (~w)", [Version_client, ?VERSION_PROTOCOLE]
+ )))
+ end
+ ).
+
+
+% Authentification d'un client.
+traiter_action("authentification", JSON, IP) ->
+ euphorik_protocole:login(JSON, IP);
+% Un client s'enregistre : (pseudo + password) ou de manière anonyme.
+traiter_action("register", JSON, IP) ->
+ euphorik_protocole:register(JSON, IP);
+% Modification du profile.
+traiter_action("set_profile", JSON, _) ->
+ euphorik_protocole:profile(JSON);
+% Un utilisateur attend un événement (par exemple l'arrivée d'un nouveau message).
+traiter_action("wait_event", JSON, _) ->
+ euphorik_protocole:wait_event(JSON);
+% Un utilisateur envoie un message.
+traiter_action("put_message", JSON, _) ->
+ euphorik_protocole:put_message(JSON);
+% Un ekMaster bannie un utilisateur (ip).
+traiter_action("ban", JSON, _) ->
+ euphorik_protocole:ban(JSON);
+% Un ekMaster slap un utilisateur.
+traiter_action("slap", JSON, _) ->
+ euphorik_protocole:slap(JSON);
+% Un ekMaster demande la liste des ips bannies.
+traiter_action("list_banned_ips", JSON, _) ->
+ euphorik_protocole:list_banned_ips(JSON);
+% Un ekMaster débannie une ip.
+traiter_action("unban", JSON, _) ->
+ euphorik_protocole:unban_ip(JSON).