Update to the new library 'json2'
[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
23
24 -module(euphorik_requests).
25 -author("Greg Burri <greg.burri@gmail.com>").
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
40 inet:setopts(A#arg.clisock, [{active, true}, {keepalive, true}]),
41 {value, {_, Contenu}} = lists:keysearch("action", 1, yaws_api:parse_post(A)),
42
43 Ret = traiter_message(Contenu, IP),
44 {content, "application/json", Ret}.
45
46
47 % Décode le message JSON.
48 traiter_message(Contenu, IP) ->
49 % extrait l'entête obligatoire des messages JSON
50 {ok, {struct, [{"header", {struct, [{"action", Action}, {"version", Version_client}]}} | Reste]}} = json2:decode_string(Contenu),
51
52 json2:encode(
53 if Version_client =:= ?VERSION_PROTOCOLE ->
54 traiter_action(Action, Reste, IP);
55 true ->
56 euphorik_protocole:erreur(lists:flatten(io_lib:format(
57 "La version du protocole du client (~w) ne correspond à celle du serveur (~w)", [Version_client, ?VERSION_PROTOCOLE]
58 )))
59 end
60 ).
61
62
63 % Authentification d'un client.
64 traiter_action("authentification", JSON, IP) ->
65 euphorik_protocole:login(JSON, IP);
66 % Un client s'enregistre : (pseudo + password) ou de manière anonyme.
67 traiter_action("register", JSON, IP) ->
68 euphorik_protocole:register(JSON, IP);
69 % Modification du profile.
70 traiter_action("set_profile", JSON, _) ->
71 euphorik_protocole:profile(JSON);
72 % Un utilisateur attend un événement (par exemple l'arrivée d'un nouveau message).
73 traiter_action("wait_event", JSON, _) ->
74 euphorik_protocole:wait_event(JSON);
75 % Un utilisateur envoie un message.
76 traiter_action("put_message", JSON, _) ->
77 euphorik_protocole:put_message(JSON);
78 % Un ekMaster bannie un utilisateur (ip).
79 traiter_action("ban", JSON, _) ->
80 euphorik_protocole:ban(JSON);
81 % Un ekMaster slap un utilisateur.
82 traiter_action("slap", JSON, _) ->
83 euphorik_protocole:slap(JSON);
84 % Un ekMaster demande la liste des ips bannies.
85 traiter_action("list_banned_ips", JSON, _) ->
86 euphorik_protocole:list_banned_ips(JSON);
87 % Un ekMaster débannie une ip.
88 traiter_action("unban", JSON, _) ->
89 euphorik_protocole:unban_ip(JSON).