Update to the new library 'json2'
[euphorik.git] / modules / erl / euphorik_requests.erl
index 13a86cb..49cbe02 100755 (executable)
@@ -1,55 +1,89 @@
 % coding: utf-8\r
-% Ce module est fait pour répondre à des requêtes 'AJAX'.\r
-% Il est définit comme 'appmods' pour l'url "request" dans yaws.\r
-% Par exemple http://www.euphorik.ch/request abouti sur la fonction out() de ce module.
-% @author G.Burri
-
--module(euphorik_requests).\r
--export([\r
-   tester/0,\r
-   out/1\r
-]).\r
+% Copyright 2008 Grégory Burri\r
+%\r
+% This file is part of Euphorik.\r
+%\r
+% Euphorik is free software: you can redistribute it and/or modify\r
+% it under the terms of the GNU General Public License as published by\r
+% the Free Software Foundation, either version 3 of the License, or\r
+% (at your option) any later version.\r
+%\r
+% Euphorik is distributed in the hope that it will be useful,\r
+% but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+% GNU General Public License for more details.\r
+%\r
+% You should have received a copy of the GNU General Public License\r
+% along with Euphorik.  If not, see <http://www.gnu.org/licenses/>.\r
+%\r
+% This module responds to JSON requests via 'AJAX'.\r
+% It's defined as an Yaws 'appmods' with the url '/request'.\r
+% For instance in debug mode 'http://localhost:8090/request' will call the out/1 function.\r
+\r
 \r
--include_lib("xmerl/include/xmerl.hrl").\r
--include_lib("yaws/include/yaws_api.hrl").
+-module(euphorik_requests).\r
+-author("Greg Burri <greg.burri@gmail.com>").\r
+-export([out/1]).\r
+-include_lib("yaws_api.hrl").\r
+-include("../include/euphorik_defines.hrl").\r
 \r
-% Test du module\r
-tester() ->
-   que_dal.\r
-
 \r
-% il faut catcher toutes les exceptions possibles\r
+% Entry point for all AJAX requests on '/request'.\r
 out(A) ->\r
-   %inet:setopts(A#arg.clisock, inet:getopts(A#arg.clisock, [active])),\r
+   IP = case inet:peername(A#arg.clisock) of\r
+      {ok, {Address, _Port}} -> Address;\r
+      _ -> unknown\r
+   end,\r
+   % active: to receive {tcp_closed, _} when the socket is closing.\r
+   % keepalive: avoid firewalls to cut the connection.\r
+\r
+   inet:setopts(A#arg.clisock, [{active, true}, {keepalive, true}]),\r
    {value, {_, Contenu}} = lists:keysearch("action", 1, yaws_api:parse_post(A)),\r
-   Ret = traiter_donnees(Contenu),\r
-   %{content, "text/xml", Ret}.
+\r
+   Ret = traiter_message(Contenu, IP),\r
    {content, "application/json", Ret}.\r
-
-\r
-traiter_donnees(Contenu) ->
-   case json:decode_string(Contenu) of
-      {ok, {struct, [{action, Action}| Reste]}} ->
-         %io:format("~p~n", [euphorik_protocole:login(JSON)]),
-         json:encode(traiter_action(Action, Reste));
-      _ ->
-         error
-   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
+\r
+\r
+% Décode le message JSON.\r
+traiter_message(Contenu, IP) ->\r
+   % extrait l'entête obligatoire des messages JSON\r
+   {ok, {struct, [{"header", {struct, [{"action", Action}, {"version", Version_client}]}} | Reste]}} = json2:decode_string(Contenu),\r
+\r
+   json2:encode(\r
+      if Version_client =:= ?VERSION_PROTOCOLE ->\r
+            traiter_action(Action, Reste, IP);\r
+         true ->\r
+            euphorik_protocole:erreur(lists:flatten(io_lib:format(\r
+               "La version du protocole du client (~w) ne correspond à celle du serveur (~w)", [Version_client, ?VERSION_PROTOCOLE]\r
+            )))\r
+      end\r
+   ).\r
+\r
+\r
+% Authentification d'un client.\r
+traiter_action("authentification", JSON, IP) ->\r
+   euphorik_protocole:login(JSON, IP);\r
+% Un client s'enregistre : (pseudo + password) ou de manière anonyme.\r
+traiter_action("register", JSON, IP) ->\r
+   euphorik_protocole:register(JSON, IP);\r
+% Modification du profile.\r
+traiter_action("set_profile", JSON, _) ->\r
+   euphorik_protocole:profile(JSON);\r
+% Un utilisateur attend un événement (par exemple l'arrivée d'un nouveau message).\r
+traiter_action("wait_event", JSON, _) ->\r
+   euphorik_protocole:wait_event(JSON);\r
+% Un utilisateur envoie un message.\r
+traiter_action("put_message", JSON, _) ->\r
+   euphorik_protocole:put_message(JSON);\r
+% Un ekMaster bannie un utilisateur (ip).\r
+traiter_action("ban", JSON, _) ->\r
+   euphorik_protocole:ban(JSON);\r
+% Un ekMaster slap un utilisateur.\r
+traiter_action("slap", JSON, _) ->\r
+   euphorik_protocole:slap(JSON);\r
+% Un ekMaster demande la liste des ips bannies.\r
+traiter_action("list_banned_ips", JSON, _) ->\r
+   euphorik_protocole:list_banned_ips(JSON);\r
+% Un ekMaster débannie une ip.\r
+traiter_action("unban", JSON, _) ->\r
+   euphorik_protocole:unban_ip(JSON).\r