X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=modules%2Ferl%2Feuphorik_requests.erl;h=28019da95d8be378766e1fd1e5641e45d9ef7656;hp=42348cfcc857271f5b80968a5303d4f04489862a;hb=650c44a784cabc8a1f2bd1daa7e5e61ccf74ca6f;hpb=ccb7fdfe9914f47cc38f96e5973851f2c6f0c4b6 diff --git a/modules/erl/euphorik_requests.erl b/modules/erl/euphorik_requests.erl index 42348cf..28019da 100755 --- a/modules/erl/euphorik_requests.erl +++ b/modules/erl/euphorik_requests.erl @@ -1,80 +1,83 @@ % coding: utf-8 +% 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([ - tester/0, - out/1 -]). - +-export([out/1]). -include_lib("xmerl/include/xmerl.hrl"). -include_lib("yaws/include/yaws_api.hrl"). - -% Test du module -tester() -> - que_dal. -% il faut catcher toutes les exceptions possibles -out(A) -> - %inet:setopts(A#arg.clisock, inet:getopts(A#arg.clisock, [active])), +out(A) -> + %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_donnees(Contenu), - %{content, "text/xml", Ret}. + Ret = traiter_donnees(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 +traiter_donnees(Contenu, IP) -> + case json:decode_string(Contenu) of + {ok, {struct, [{action, Action}| Reste]}} -> + json:encode(traiter_action(Action, Reste, IP)); + _ -> + error end. % authentification d'un client -traiter_action("authentification", JSON) -> - euphorik_protocole:login(JSON)); +traiter_action("authentification", JSON, IP) -> + euphorik_protocole:login(JSON, IP); % un client s'enregistre (pseudo + password) -traiter_action("register", JSON) -> - euphorik_protocole:register(JSON); +traiter_action("register", JSON, IP) -> + euphorik_protocole:register(JSON, IP); % modification du profile -traiter_action("set_profile", JSON) -> +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) -> +traiter_action("wait_event", JSON, _) -> euphorik_protocole:wait_event(JSON); % un utilisateur envoie un message -traiter_action("put_message", JSON) -> - euphorik_protocole:message(JSON). - - - - -traiter_action([#xmlAttribute{value="register"}], XML) -> - euphorik_protocole:nouveau_user_login(XML); -traiter_action([#xmlAttribute{value="login"}], XML) -> - euphorik_protocole:login(XML); -traiter_action([#xmlAttribute{value="profile"}], XML) -> - euphorik_protocole:profile(XML); -traiter_action([#xmlAttribute{value="refreshMessages"}], XML) -> - euphorik_protocole:refreshMessage(XML); -% un utilisateur envoie un message -traiter_action([#xmlAttribute{value="message"}], XML) -> - euphorik_protocole:message(XML). +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