X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;ds=inline;f=modules%2Ferl%2Feuphorik_requests.erl;h=efe2e064344339750d3953f263b43a1d366f3020;hb=e5e4964417795d52e0be91d15348e5ea850a1085;hp=a289f0dcaae40b792a536c36515946d809210e90;hpb=6feaef2a51f7226c41d69f9e86f605718b78aa02;p=euphorik.git
diff --git a/modules/erl/euphorik_requests.erl b/modules/erl/euphorik_requests.erl
index a289f0d..efe2e06 100755
--- a/modules/erl/euphorik_requests.erl
+++ b/modules/erl/euphorik_requests.erl
@@ -1,82 +1,90 @@
% coding: utf-8
-% Ce module est fait pour répondre à des requêtes 'AJAX'.
-% Auteur : G.Burri
-% Date : 22.10.2007
-
--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 .
+%
+% 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([out/1]).
-include_lib("xmerl/include/xmerl.hrl").
--include_lib("yaws/include/yaws_api.hrl").
+-include_lib("yaws/include/yaws_api.hrl").
+-include("../include/euphorik_defines.hrl").
-% Test du module
-tester() ->
- %~ {XML, _} = xmerl_scan:string(
- %~ ""
- %~ " b1b1b4e72e6f3d00e477cf37cced5851"
- %~ " LKJDLA"
- %~ ""),
- %~ io:format("Nouvel user : ~p~n", [nouveau_user(XML)]).
-
- {XML2, _} = xmerl_scan:string(
- ""
- " 5DZQ2HCRO7JIX3QCSWRNL"
- ""),
- io:format("Login : ~p~n", [euphorik_protocole:login(XML2)]).
-
- %~ {XML, _} = xmerl_scan:string(
- %~ ""
- %~ "5"
- %~ "1"
- %~ ""),
- %~ io:format("Messages de la premières page : ~p~n", [euphorik_protocole:refreshMessage(XML)]).
-
- %~ traiter_xml(""
- %~ "4UDUSY6Z2IZNTQO484S8X"
- %~ "Pifou"
- %~ "test & plop"
- %~ "").
- %~ traiter_xml(
- %~ ""
- %~ "").
-% il faut catcher toutes les exceptions possibles
out(A) ->
- %inet:setopts(A#arg.clisock, inet:getopts(A#arg.clisock, [active])),
+ %io:format("~p~n", [A]), % utilisé parfois pendant le debug
+ IP = case inet:peername(A#arg.clisock) of
+ {ok, {Adresse, _Port}} -> Adresse;
+ _ -> inconnue
+ end,
+ % passive -> active, permet de recevoir {tcp_closed, _} lorsque le socket se ferme
+ % keepalive -> true, evite que des firewalls coupe la connexion TCP sans prévenir
+ inet:setopts(A#arg.clisock, [{active, true}, {keepalive, true}]),
{value, {_, Contenu}} = lists:keysearch("action", 1, yaws_api:parse_post(A)),
- Ret = traiter_xml(Contenu),
- {content, "text/xml", Ret}.
-
+ Ret = traiter_donnees(Contenu, IP),
+ %io:format("~p~n", [Ret]),
+ {content, "application/json", Ret}.
-traiter_xml(Contenu) ->
- {XML, _} = xmerl_scan:string(Contenu),
- traiter_action(XML#xmlElement.attributes, XML).
-
-
-% un client demande la génération d'un captcha
-traiter_action([#xmlAttribute{value="generationCaptcha"}], XML) ->
- euphorik_protocole:generation_captcha(XML);
-% un client se log pour la première fois
-traiter_action([#xmlAttribute{value="loginCaptcha"}], XML) ->
- euphorik_protocole:nouveau_user_captcha(XML);
-% un client s'enregistre (pseudo + password)
-traiter_action([#xmlAttribute{value="register"}], XML) ->
- euphorik_protocole:nouveau_user_login(XML);
-% authentification d'un client
-traiter_action([#xmlAttribute{value="login"}], XML) ->
- euphorik_protocole:login(XML);
-% modification du profile
-traiter_action([#xmlAttribute{value="profile"}], XML) ->
- euphorik_protocole:profile(XML);
-% le client désire les messages
-traiter_action([#xmlAttribute{value="refreshMessages"}], XML) ->
- euphorik_protocole:refreshMessage(XML);
-% envoie d'un message
-traiter_action([#xmlAttribute{value="message"}], XML) ->
- euphorik_protocole:message(XML).
-
-
+% Décode les données JSON.
+traiter_donnees(Contenu, IP) ->
+ {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)
+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é 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);
+traiter_action("ban", JSON, _) ->
+ euphorik_protocole:ban(JSON);
+traiter_action("slap", JSON, _) ->
+ euphorik_protocole:slap(JSON);
+traiter_action("put_troll", JSON, _) ->
+ euphorik_protocole:put_troll(JSON);
+traiter_action("mod_troll", JSON, _) ->
+ euphorik_protocole:mod_troll(JSON);
+traiter_action("del_troll", JSON, _) ->
+ euphorik_protocole:del_troll(JSON);
+traiter_action("list_banned_ips", JSON, _) ->
+ euphorik_protocole:list_banned_ips(JSON);
+traiter_action("unban", JSON, _) ->
+ euphorik_protocole:unban_ip(JSON).
+
\ No newline at end of file