Update to the new library 'json2'
[euphorik.git] / modules / erl / euphorik_requests.erl
index 2fb69c0..49cbe02 100755 (executable)
@@ -1,83 +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() ->
-   \r
-   %~ {XML2, _} = xmerl_scan:string(\r
-      %~ "<action name=\"login\">"\r
-      %~ "  <cookie>5DZQ2HCRO7JIX3QCSWRNL</cookie>"\r
-      %~ "</action>"),\r
-   %~ io:format("Login : ~p~n", [euphorik_protocole:login(XML2)]).
-   \r
-   XML =\r
-      "<action name=\"refreshMessages\"><cookie>3FSDCH0FD4ML8WEPN2B5T</cookie>"
-      "<nombreMessage>10</nombreMessage>"
-      "</action>",\r
-   io:format("Messages de la premières page : ~p~n", [traiter_donnees(XML)]).
-   \r
-   %~ traiter_xml("<action name=\"message\">"\r
-      %~ "<cookie>4UDUSY6Z2IZNTQO484S8X</cookie>"\r
-      %~ "<pseudo>Pifou</pseudo>"\r
-      %~ "<contenu>test &amp; plop</contenu>"\r
-      %~ "</action>").\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
-
 \r
-traiter_donnees(Contenu) ->\r
-   case xmerl_scan:string(Contenu) of
-      {XML, _} -> 
-         case XML of
-            #xmlElement{name = json, content = [#xmlText{value = J}|_]} ->
-               case json:decode_string(J) of
-                  {ok, {struct, [{action, Action}| Reste]}} ->
-                     traiter_action(Action, Reste);
-                  _ ->
-                     erreur
-               end;
-            _ ->
-               traiter_action(XML#xmlElement.attributes, XML)
-         end;
-      _ -> erreur
-   end.\r
-   
-   
-traiter_action("authentification", JSON) ->
-   euphorik_protocole:login(JSON);
-% un client s'enregistre (pseudo + password)
-traiter_action([#xmlAttribute{value="register"}], XML) ->
-   euphorik_protocole:nouveau_user_login(XML); \r
-% authentification d'un client\r
-traiter_action([#xmlAttribute{value="login"}], XML) ->\r
-   euphorik_protocole:login(XML); 
-% modification du profile
-traiter_action([#xmlAttribute{value="profile"}], XML) ->
-   euphorik_protocole:profile(XML); \r
-% un utilisateur demande les messages\r
-traiter_action([#xmlAttribute{value="refreshMessages"}], XML) ->\r
- euphorik_protocole:refreshMessage(XML); \r
-% un utilisateur envoie un message\r
-traiter_action([#xmlAttribute{value="message"}], XML) ->\r
-   euphorik_protocole:message(XML).\r
\r
-
+   Ret = traiter_message(Contenu, IP),\r
+   {content, "application/json", Ret}.\r
+\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