2 % Ce module gére les différents message envoyé par le client (javascript) via AJAX.
3 % Par exemple le client peut demander les derniers messages du minichat.
4 % Les messages sont au format XML, la plus part des fonctions accepte un xmlDocument() et renvoie un string()
5 % qui est la réponse XML.
6 % Example XML : http://www.erlang.org/doc/apps/xmerl/xmerl_ug.html.
9 -module(euphorik_protocole
).
11 %generation_captcha/1, Obsolète
12 %nouveau_user_captcha/1, Obsolète
21 -include_lib("xmerl/include/xmerl.hrl").
22 -include("../include/euphorik_bd.hrl").
23 -include("../include/euphorik_defines.hrl").
26 % Génère un nouveau captchat dans ?DOSSIER_CAPTCHA
28 %~ generation_captcha(_) ->
29 %~ {Mot_crypt, Nom_fichier} = captcha:create(5, ?DOSSIER_CAPTCHA),
30 %~ simple_xml_to_string(xml_reponse_generation_captcha(?DOSSIER_CAPTCHA_RELATIF "/" ++ Nom_fichier, Mot_crypt)).
33 % Un nouvel utilisateur doit être créé.
34 % Obolète : le captcha n'existe plus
35 % Action est un xmlElement().
36 %~ nouveau_user_captcha(Action) ->
37 %~ simple_xml_to_string(
38 %~ case {xmerl_xpath:string("captchaCrypt", Action), xmerl_xpath:string("captchaInput", Action)} of
39 %~ {[#xmlElement{content = [#xmlText{value = C1}]}], [#xmlElement{content = [#xmlText{value = C2}]}]} ->
40 %~ C2_crypt = common:crypt(C2),
41 %~ if C1 =:= C2_crypt ->
42 %~ Cookie = generer_cookie(),
43 %~ User = euphorik_minichat:nouveau_user("Paul", Cookie),
44 %~ xml_reponse_login_ok(User);
46 %~ xml_reponse_login_pas_ok("Captcha incorrect")
49 %~ xml_reponse_login_pas_ok("XML malformé")
54 % Une utilisateur s'enregistre avec un tuple {Login, Password}.
55 % @spec nouveau_user_login(xmerl:xmlElement()) -> string()
56 nouveau_user_login(Action
) ->
57 {Login
, Password
, Login_deja_pris
} = case {xmerl_xpath:string("login", Action
), xmerl_xpath:string("password", Action
)} of
58 {[#xmlElement
{content
= [#xmlText
{value
= L
}]}], [#xmlElement
{content
= [#xmlText
{value
= P
}]}]} ->
59 {L
, P
, case euphorik_minichat:user_by_login(L
) of {ok
, _
} -> true
; _
-> false
end};
64 xml_reponse_login_pas_ok("Login déjà pris");
66 Cookie
= generer_cookie(),
67 User
= euphorik_minichat:nouveau_user(Login
, Password
, Cookie
),
68 xml_reponse_login_ok(User
)
73 % Un utilisateur se logge.
75 case xmerl_xpath:string("cookie", Action
) of
76 [#xmlElement
{content
= [#xmlText
{value
= Cookie
}]}] ->
77 loginUser(euphorik_minichat:user_by_cookie(Cookie
));
79 case {xmerl_xpath:string("login", Action
), xmerl_xpath:string("password", Action
)} of
80 {[#xmlElement
{content
= [#xmlText
{value
= Login
}]}], [#xmlElement
{content
= [#xmlText
{value
= Password
}]}]} ->
81 loginUser(euphorik_minichat:user_by_login_password(Login
, Password
));
83 simple_xml_to_string(xml_reponse_login_pas_ok("XML malformé"))
86 loginUser({ok
, User
}) ->
87 euphorik_minichat:update_date_derniere_connexion(User#user
.id
),
88 simple_xml_to_string(xml_reponse_login_ok(User
));
90 simple_xml_to_string(xml_reponse_login_pas_ok("Erreur de login")).
93 % Renvoie un string() représentant un cookie en base 36. Il y a 10^32 possibillités.
96 random:seed(A1
, A2
, A3
),
97 erlang:integer_to_list(random:uniform(math:pow(10, 32)), 36).
100 % Un utilisateur se délogge.
105 % Modification du profile.
107 simple_xml_to_string(
108 case xmerl_xpath:string("cookie", Action
) of
109 [#xmlElement
{content
= [#xmlText
{value
= Cookie
}]}] ->
110 Login
= case xmerl_xpath:string("login", Action
) of [#xmlElement
{content
= [#xmlText
{value
= L
}]}] -> L
; _
-> undefined
end,
111 Password
= case xmerl_xpath:string("password", Action
) of [#xmlElement
{content
= [#xmlText
{value
= P
}]}] -> P
; _
-> undefined
end,
112 Pseudo
= case xmerl_xpath:string("pseudo", Action
) of [#xmlElement
{content
= [#xmlText
{value
= P2
}]}] -> P2
; _
-> Login
end,
113 Email
= case xmerl_xpath:string("email", Action
) of [#xmlElement
{content
= [#xmlText
{value
= E
}]}] -> E
; _
-> undefined
end,
114 Css
= case xmerl_xpath:string("css", Action
) of [#xmlElement
{content
= [#xmlText
{value
= C
}]}] -> C
; _
-> undefined
end,
115 case euphorik_minichat:set_profile(Cookie
, Login
, Password
, Pseudo
, Email
, Css
) of
117 xml_reponse_profile_ok();
119 xml_reponse_profile_pas_ok("Login déjà pris");
121 xml_reponse_profile_pas_ok("Impossible de mettre à jour le profile")
124 xml_reponse_profile_pas_ok("XML malformé")
129 % Renvoie les messages appropriés.
130 refreshMessage(Action
) ->
131 simple_xml_to_string(
133 xmerl_xpath:string("nombreMessage", Action
), % le nombre de message qu'affiche le client
134 xmerl_xpath:string("page", Action
) % la page désiré, la première (les derniers messages) étant la 1
137 [#xmlElement
{content
= [#xmlText
{value
= Nb_message_str
}]}],
138 [#xmlElement
{content
= [#xmlText
{value
= Page_str
}]}]
140 Nb_message
= list_to_integer(Nb_message_str
),
141 Page
= list_to_integer(Page_str
),
142 Dernier_id
= case xmerl_xpath:string("dernierMessageId", Action
) of % l'id du dernier message que connait le client
143 [#xmlElement
{content
= [#xmlText
{value
= D
}]}] -> erlang:list_to_integer(D
, 36);
146 User
= case xmerl_xpath:string("cookie", Action
) of
147 [#xmlElement
{content
= [#xmlText
{value
= Cookie
}]}] ->
148 case euphorik_minichat:user_by_cookie(Cookie
) of
154 % extraction des conversations en [{id, page}, ..]
155 Conversations
= lists:map(
157 [#xmlElement
{content
= [#xmlText
{value
= Id_racine_str
}]}] = xmerl_xpath:string("racine", Conv
),
158 [#xmlElement
{content
= [#xmlText
{value
= Page_conv_str
}]}] = xmerl_xpath:string("page", Conv
),
159 {erlang:list_to_integer(Id_racine_str
, 36), erlang:list_to_integer(Page_conv_str
)}
161 xmerl_xpath:string("conversation", Action
)
163 % accrochez-vous ca va siouxer ;)
164 [{reponse
, [{name
, "refreshMessages"}],
168 [{autresPages
, [], [atom_to_list(Plus
)]} |
170 fun({Mess
, Repond_a
}) ->
171 Est_proprietaire
= User
=/= inconnu andalso User#user
.id
=:= Mess#minichat
.auteur_id
,
172 A_repondu_a_message
= User
=/= inconnu andalso
euphorik_minichat:a_repondu_a_message(User#user
.id
, Mess#minichat
.id
),
173 Est_une_reponse_a_user
= User
=/= inconnu andalso
euphorik_minichat:est_une_reponse_a_user(User#user
.id
, Mess#minichat
.id
),
175 if Mess#minichat
.auteur_id
=:= 0 ->
178 {ok
, U2
} = euphorik_minichat:user_by_id(Mess#minichat
.auteur_id
),
181 {message
, [{id
, erlang:integer_to_list(Mess#minichat
.id
, 36)}],
183 {date, [], [format_date(Mess#minichat
.date)]},
184 {systeme
, [], [atom_to_list(Mess#minichat
.auteur_id
=:= 0)]},
185 {proprietaire
, [], [atom_to_list(Est_proprietaire
)]},
186 {repondu
, [], [atom_to_list(A_repondu_a_message
)]},
187 {reponse
, [], [atom_to_list(Est_une_reponse_a_user
)]},
188 {pseudo
, [], [Mess#minichat
.pseudo
]},
189 {login
, [], [if User_mess
=:= inconnu
-> Mess#minichat
.pseudo
; true
-> User_mess#user
.login
end]},
190 {contenu
, [], [Mess#minichat
.contenu
]},
191 {repondA
, [], xml_repond_a(Repond_a
)}
200 euphorik_minichat_conversation:conversations(Conversations
, Nb_message
, Dernier_id
, Page
)
204 [{reponse
, [{name
, "refreshMessages"}], [{erreur
, [], ["erreur"]}]}]
209 % Prend une liste de xml text node et en resort un string()
210 % xmerl : "test & test" devient deux fragments de texte : "test " et "& test", il faut donc rassembler les morceaux...
211 defragmenter(Text_nodes
) ->
212 lists:foldl(fun(Node
, Acc
) -> #xmlText
{value
= V
} = Node
, Acc
++ V
end, [], Text_nodes
).
215 % Un utilisateur envoie un message
217 simple_xml_to_string(
219 xmerl_xpath:string("cookie", Action
),
220 xmerl_xpath:string("pseudo", Action
),
221 xmerl_xpath:string("contenu", Action
)
224 [#xmlElement
{content
= [#xmlText
{value
= Cookie
}]}],
225 [#xmlElement
{content
= Pseudo_fragments
}],
226 [#xmlElement
{content
= Contenu_fragments
}]
228 case euphorik_minichat:user_by_cookie(Cookie
) of
230 Pseudo
= defragmenter(Pseudo_fragments
),
231 Contenu
= defragmenter(Contenu_fragments
),
232 % met à jour le pseudo du user
233 euphorik_minichat:update_pseudo_user(U#user
.id
, Pseudo
),
234 Reponses
= case xmerl_xpath:string("reponses", Action
) of
235 [#xmlElement
{content
= C
}] ->
238 #xmlElement
{attributes
= [#xmlAttribute
{name
= id
, value
= Id_reponse
}]} = Reponse
,
239 erlang:list_to_integer(Id_reponse
, 36)
244 Contenu_strip
= string:strip(Contenu
),
245 if Contenu_strip
=:= [] -> xml_reponse_message(pas_ok
);
247 case euphorik_minichat:nouveau_message(Contenu
, U#user
.id
, Reponses
) of
248 erreur
-> xml_reponse_message(pas_ok
);
249 _
-> xml_reponse_message(ok
)
252 _
-> xml_reponse_message(pas_ok
)
255 xml_reponse_message(pas_ok
)
260 % Formatage d'une heure
261 % local_time() -> string
263 DateLocal
= calendar:now_to_local_time(Date
),
264 DateNowLocal
= calendar:local_time(),
265 {{Annee
, Mois
, Jour
}, {Heure
, Minute
, Seconde
}} = DateLocal
,
266 {{AnneeNow
, _
, _
}, {_
, _
, _
}} = DateNowLocal
,
267 Hier
= calendar:date_to_gregorian_days(element(1, DateLocal
)) =:= calendar:date_to_gregorian_days(element(1, DateNowLocal
)) - 1,
268 if element(1, DateLocal
) =:= element(1, DateNowLocal
) ->
272 Annee
=:= AnneeNow
->
273 io_lib:format("~2.10.0B/~2.10.0B ", [Jour
, Mois
]);
275 io_lib:format("~2.10.0B/~2.10.0B/~B ", [Jour
, Mois
, Annee
])
277 io_lib:format("~2.10.0B:~2.10.0B:~2.10.0B", [Heure
, Minute
, Seconde
]).
280 %%%%%%%%% <Réponses XML> %%%%%%%%%
281 simple_xml_to_string(XML
) ->
282 lists:flatten(xmerl:export_simple(XML
, xmerl_xml
, [{prolog
, ["<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"]}])).
285 % Construit une réponse positive à un login
286 % si Enregistre vaut true alors cela veut dire que la personne s'est enregistré (elle possède au moins un login et un password)
287 xml_reponse_login_ok(User
) ->
288 [{reponse
, [{name
, "login"}],
290 {statut
, [if (User#user
.password
=/= []) and (User#user
.login
=/= []) -> "enregistre"; true
-> "identifie" end]},
291 {cookie
, [User#user
.cookie
]},
292 {id
, [erlang:integer_to_list(User#user
.id
, 36)]},
293 {pseudo
, [User#user
.pseudo
]},
294 {login
, [User#user
.login
]},
295 {email
, [User#user
.email
]},
296 {css
, [User#user
.css
]}
301 % Construit un réponse négative à un login
302 xml_reponse_login_pas_ok(Message
) ->
303 [{reponse
, [{name
, "login"}],
305 {statut
, ["erreur"]},
306 {information
, [Message
]}
311 xml_reponse_profile_ok() ->
312 [{reponse
, [{name
, "profile"}],
319 xml_reponse_profile_pas_ok(Message
) ->
320 [{reponse
, [{name
, "profile"}],
322 {statut
, ["pas ok"]},
323 {information
, [Message
]}
329 %~ xml_conversation(Mess_id, Nb) ->
330 %~ {Mess_id, Conversation} = minichat:conversation(Mess_id, Nb),
331 %~ xml_conversation(Conversation).
332 %~ xml_conversation([]) -> [];
333 %~ xml_conversation(Liste_id) ->
335 %~ fun({Id, Sous_liste}) ->
336 %~ {id, [{id, erlang:integer_to_list(Id, 36)}], xml_conversation(Sous_liste)}
342 % Renvoie un element XML representant une liste de messages auquel le message M_id repond
343 xml_repond_a(Reponses
) ->
346 {ok
, M
} = euphorik_minichat:message_by_id(Id_mess
),
347 {ok
, User
} = euphorik_minichat:user_by_mess(Id_mess
),
348 {id
, [{id
, erlang:integer_to_list(M#minichat
.id
, 36)}, {pseudo
, M#minichat
.pseudo
}, {login
, User#user
.login
}], []}
354 xml_reponse_message(Ok
) ->
356 {reponse
, [{name
, "message"}],
358 {statut
, [], [case Ok
of ok
-> "ok"; pas_ok
-> "pas ok" end]}
365 %~ xml_reponse_generation_captcha(Chemin, Captcha) ->
367 %~ {reponse, [{name, "generationCaptcha"}],
369 %~ {chemin, [], [Chemin]},
370 %~ {captchaCrypt, [], [Captcha]}
374 %%%%%%%%% </réponses XML> %%%%%%%%%