8ee67a542733af9a1b54ebed6c68109613aba922
[euphorik.git] / modules / erl / euphorik_requests.erl
1 % coding: utf-8
2 % Copyright 2008 Grégory Burri
3 %
4 % This file is part of Euphorik.
5 %
6 % Euphorik is free software: you can redistribute it and/or modify
7 % it under the terms of the GNU General Public License as published by
8 % the Free Software Foundation, either version 3 of the License, or
9 % (at your option) any later version.
10 %
11 % Euphorik is distributed in the hope that it will be useful,
12 % but WITHOUT ANY WARRANTY; without even the implied warranty of
13 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 % GNU General Public License for more details.
15 %
16 % You should have received a copy of the GNU General Public License
17 % along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18 %
19 % This module responds to JSON requests via 'AJAX'.
20 % It's defined as an Yaws 'appmods' with the url '/request'.
21 % For instance in debug mode 'http://localhost:8090/request' will call the out/1 function.
22 % @author G.Burri
23
24
25 -module(euphorik_requests).
26 -export([out/1]).
27 -include_lib("yaws_api.hrl").
28 -include("../include/euphorik_defines.hrl").
29
30
31 % Entry point for all AJAX requests on '/request'.
32 out(A) ->
33 IP = case inet:peername(A#arg.clisock) of
34 {ok, {Address, _Port}} -> Address;
35 _ -> unknown
36 end,
37 % active: to receive {tcp_closed, _} when the socket is closing.
38 % keepalive: avoid firewalls to cut the connection.
39 inet:setopts(A#arg.clisock, [{active, true}, {keepalive, true}]),
40 {value, {_, Contenu}} = lists:keysearch("action", 1, yaws_api:parse_post(A)),
41 Ret = traiter_message(Contenu, IP),
42 {content, "application/json", Ret}.
43
44
45 % Décode le message JSON.
46 traiter_message(Contenu, IP) ->
47 % extrait l'entête obligatoire des messages JSON
48 {ok, {struct, [{header, {struct, [{action, Action}, {version, Version_client}]}} | Reste]}} = json:decode_string(Contenu),
49 json:encode(
50 if Version_client =:= ?VERSION_PROTOCOLE ->
51 traiter_action(Action, Reste, IP);
52 true ->
53 euphorik_protocole:erreur(lists:flatten(io_lib:format(
54 "La version du protocole du client (~w) ne correspond à celle du serveur (~w)", [Version_client, ?VERSION_PROTOCOLE]
55 )))
56 end
57 ).
58
59
60 % Authentification d'un client.
61 traiter_action("authentification", JSON, IP) ->
62 euphorik_protocole:login(JSON, IP);
63 % Un client s'enregistre : (pseudo + password) ou de manière anonyme.
64 traiter_action("register", JSON, IP) ->
65 euphorik_protocole:register(JSON, IP);
66 % Modification du profile.
67 traiter_action("set_profile", JSON, _) ->
68 euphorik_protocole:profile(JSON);
69 % Un utilisateur attend un événement (par exemple l'arrivée d'un nouveau message).
70 traiter_action("wait_event", JSON, _) ->
71 euphorik_protocole:wait_event(JSON);
72 % Un utilisateur envoie un message.
73 traiter_action("put_message", JSON, _) ->
74 euphorik_protocole:put_message(JSON);
75 % Un ekMaster bannie un utilisateur (ip).
76 traiter_action("ban", JSON, _) ->
77 euphorik_protocole:ban(JSON);
78 % Un ekMaster slap un utilisateur.
79 traiter_action("slap", JSON, _) ->
80 euphorik_protocole:slap(JSON);
81 % Un ekMaster demande la liste des ips bannies.
82 traiter_action("list_banned_ips", JSON, _) ->
83 euphorik_protocole:list_banned_ips(JSON);
84 % Un ekMaster débannie une ip.
85 traiter_action("unban", JSON, _) ->
86 euphorik_protocole:unban_ip(JSON).