ADD licences sur les fichier .erl
[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 % Ce module est fait pour répondre à des requêtes 'AJAX'.
20 % Il est définit comme 'appmods' pour l'url "request" dans yaws.
21 % Par exemple http://www.euphorik.ch/request abouti sur la fonction out() de ce module.
22 % @author G.Burri
23
24
25 -module(euphorik_requests).
26 -export([
27 tester/0,
28 out/1
29 ]).
30 -include_lib("xmerl/include/xmerl.hrl").
31 -include_lib("yaws/include/yaws_api.hrl").
32
33
34 % Test du module (TODO)
35 tester() ->
36 que_dal.
37
38
39 out(A) ->
40 %io:format("~p~n", [A]), % utilisé parfois pendant le debug
41 IP = case inet:peername(A#arg.clisock) of
42 {ok, {Adresse, _Port}} -> Adresse;
43 _ -> inconnue
44 end,
45 % passive -> active, permet de recevoir {tcp_closed, _} lorsque le socket se ferme
46 inet:setopts(A#arg.clisock, [{active, true}]),
47 {value, {_, Contenu}} = lists:keysearch("action", 1, yaws_api:parse_post(A)),
48 Ret = traiter_donnees(Contenu, IP),
49 {content, "application/json", Ret}.
50
51
52 traiter_donnees(Contenu, IP) ->
53 case json:decode_string(Contenu) of
54 {ok, {struct, [{action, Action}| Reste]}} ->
55 json:encode(traiter_action(Action, Reste, IP));
56 _ ->
57 error
58 end.
59
60
61 % authentification d'un client
62 traiter_action("authentification", JSON, IP) ->
63 euphorik_protocole:login(JSON, IP);
64 % un client s'enregistre (pseudo + password)
65 traiter_action("register", JSON, IP) ->
66 euphorik_protocole:register(JSON, IP);
67 % modification du profile
68 traiter_action("set_profile", JSON, _) ->
69 euphorik_protocole:profile(JSON);
70 % un utilisateur attend un événement (par exemple l'arrivé d'un nouveau message)
71 traiter_action("wait_event", JSON, _) ->
72 euphorik_protocole:wait_event(JSON);
73 % un utilisateur envoie un message
74 traiter_action("put_message", JSON, _) ->
75 euphorik_protocole:put_message(JSON);
76 traiter_action("ban", JSON, _) ->
77 euphorik_protocole:ban(JSON);
78 traiter_action("slap", JSON, _) ->
79 euphorik_protocole:slap(JSON);
80 traiter_action("put_troll", JSON, _) ->
81 euphorik_protocole:put_troll(JSON);
82 traiter_action("mod_troll", JSON, _) ->
83 euphorik_protocole:mod_troll(JSON);
84 traiter_action("del_troll", JSON, _) ->
85 euphorik_protocole:del_troll(JSON);
86 traiter_action("list_banned_ips", JSON, _) ->
87 euphorik_protocole:list_banned_ips(JSON);
88 traiter_action("unban", JSON, _) ->
89 euphorik_protocole:unban_ip(JSON).
90