2 % Copyright 2008 Grégory Burri
4 % This file is part of Euphorik.
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.
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.
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/>.
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.
25 -module(euphorik_requests
).
27 -include_lib("xmerl/include/xmerl.hrl").
28 -include_lib("yaws/include/yaws_api.hrl").
29 -include("../include/euphorik_defines.hrl").
33 %io:format("~p~n", [A]), % utilisé parfois pendant le debug
34 IP
= case inet:peername(A#arg
.clisock
) of
35 {ok
, {Adresse
, _Port
}} -> Adresse
;
38 % passive -> active, permet de recevoir {tcp_closed, _} lorsque le socket se ferme
39 % keepalive -> true, evite que des firewalls coupe la connexion TCP sans prévenir
40 inet:setopts(A#arg
.clisock
, [{active
, true
}, {keepalive
, true
}]),
41 {value
, {_
, Contenu
}} = lists:keysearch("action", 1, yaws_api:parse_post(A
)),
42 Ret
= traiter_donnees(Contenu
, IP
),
43 {content
, "application/json", Ret
}.
46 % Décode les données JSON.
47 traiter_donnees(Contenu
, IP
) ->
48 {ok
, {struct
, [{header
, {struct
, [{action
, Action
}, {version
, Version_client
}]}} | Reste
]}} = json:decode_string(Contenu
),
50 if Version_client
=:= ?VERSION_PROTOCOLE
->
51 traiter_action(Action
, Reste
, IP
);
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
]
60 % authentification d'un client
61 traiter_action("authentification", JSON
, IP
) ->
62 euphorik_protocole:login(JSON
, IP
);
63 % un client s'enregistre (pseudo + password)
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é 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 traiter_action("ban", JSON
, _
) ->
76 euphorik_protocole:ban(JSON
);
77 traiter_action("slap", JSON
, _
) ->
78 euphorik_protocole:slap(JSON
);
79 traiter_action("put_troll", JSON
, _
) ->
80 euphorik_protocole:put_troll(JSON
);
81 traiter_action("mod_troll", JSON
, _
) ->
82 euphorik_protocole:mod_troll(JSON
);
83 traiter_action("del_troll", JSON
, _
) ->
84 euphorik_protocole:del_troll(JSON
);
85 traiter_action("list_banned_ips", JSON
, _
) ->
86 euphorik_protocole:list_banned_ips(JSON
);
87 traiter_action("unban", JSON
, _
) ->
88 euphorik_protocole:unban_ip(JSON
).