% 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/1 de ce module. % @author G.Burri -module(euphorik_requests). -export([out/1, traiter_donnees/2]). -include_lib("yaws/include/yaws_api.hrl"). -include("../include/euphorik_defines.hrl"). % Point d'entrée pour les requêtes AJAX sur http://www.euphorik.ch/request. out(A) -> 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, IP), {content, "application/json", Ret}. % 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) 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é 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 envoie un nouveau troll traiter_action("put_troll", JSON, _) -> euphorik_protocole:put_troll(JSON); % un ekMaster modifie un troll traiter_action("mod_troll", JSON, _) -> euphorik_protocole:mod_troll(JSON); % un ekMaster supprime un troll traiter_action("del_troll", JSON, _) -> euphorik_protocole:del_troll(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).