98c0413d03366980eb2a90a1f01d70e4d3f96916
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 /*jslint laxbreak:true */
22 * Représente l'utilisateur du site.
24 euphorik
.Client = function(util
, communication
) {
26 this.communication
= communication
;
29 this.regexCookie
= /cookie=([^;]*)/;
32 this.resetDonneesPersonnelles();
34 this.setStatut(euphorik
.Client
.statutType
.disconnected
);
36 // si true alors chaque modification du client est mémorisé sur le serveur
37 this.autoflush
= $.browser
.opera
;
40 // les statuts possibes du client
41 euphorik
.Client
.statutType
= {
42 // mode enregistré, peut poster des messages et modifier son profile
44 // mode identifié, peut poster des messages mais n'a pas accès au profile
45 auth_not_registered : 1,
46 // mode déconnecté, ne peut pas poster de message
50 euphorik
.Client
.prototype.resetDonneesPersonnelles = function() {
52 this.pseudo
= euphorik
.conf
.pseudoDefaut
;
56 this.css
= $("link#mainCss").attr("href");
57 this.chatOrder
= "reverse";
58 this.nickFormat
= "nick";
59 this.viewTimes
= true;
60 this.viewTooltips
= true;
61 this.cookie
= undefined;
63 this.pagePrincipale
= 1;
64 this.ekMaster
= false;
65 this.ostentatiousMaster
= "light";
67 // les conversations, une conversation est un objet possédant les propriétés suivantes :
71 this.conversations
= [];
74 euphorik
.Client
.prototype.setCss = function(css
) {
75 if (this.css
=== css
|| !css
) {
80 $("link#mainCss").attr("href", this.css
);
86 euphorik
.Client
.prototype.pageSuivante = function(numConv
) {
87 if (numConv
< 0 && this.pagePrincipale
> 1) {
88 this.pagePrincipale
-= 1;
89 } else if (this.conversations
[numConv
].page
> 1) {
90 this.conversations
[numConv
].page
-= 1;
94 euphorik
.Client
.prototype.pagePrecedente = function(numConv
) {
96 this.pagePrincipale
+= 1;
98 this.conversations
[numConv
].page
+= 1;
103 * Définit la première page pour la conversation donnée.
104 * @return true si la page a changé sinon false
106 euphorik
.Client
.prototype.goPremierePage = function(numConv
)
109 if (this.pagePrincipale
=== 1) {
112 this.pagePrincipale
= 1;
114 if (this.conversations
[numConv
].page
=== 1) {
117 this.conversations
[numConv
].page
= 1;
123 * Ajoute une conversation à la vue de l'utilisateur.
124 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
125 * @param racines la racine de la conversation (integer)
126 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
128 euphorik
.Client
.prototype.ajouterConversation = function(racine
) {
129 // vérification s'il elle n'existe pas déjà
131 this.conversations
.each(function(i
, conv
) {
132 if (conv
.root
=== racine
) {
140 this.conversations
.push({root : racine
, page : 1, reduit : false});
141 if (this.autoflush
) {
148 euphorik
.Client
.prototype.supprimerConversation = function(num
) {
149 if (num
< 0 || num
>= this.conversations
.length
) {
153 // décalage TODO : supprimer le dernier élément
154 for (var i
= num
; i
< this.conversations
.length
- 1; i
++) {
155 this.conversations
[i
] = this.conversations
[i
+1];
157 this.conversations
.pop();
159 if (this.autoflush
) {
164 euphorik
.Client
.prototype.getJSONConversations = function() {
165 var conversations
= [];
166 this.conversations
.each(function(i
, conv
) {
167 conversations
.push({ "root" : conv
.root
, "minimized" : conv
.reduit
});
169 return conversations
;
172 euphorik
.Client
.prototype.getJSONProfile = function() {
174 "cookie" : this.cookie
,
175 "login" : this.login
,
176 "password" : this.password
,
177 "profile" : this.getJSONProfileInfos()
181 euphorik
.Client
.prototype.getJSONProfileInfos = function() {
183 "nick" : this.pseudo
,
184 "email" : this.email
,
186 "chat_order" : this.chatOrder
,
187 "nick_format" : this.nickFormat
,
188 "view_times" : this.viewTimes
,
189 "view_tooltips" : this.viewTooltips
,
190 "conversations" : this.getJSONConversations(),
191 "ostentatious_master" : this.ostentatiousMaster
198 euphorik
.Client
.prototype.getCookie = function() {
199 var cookie
= this.regexCookie
.exec(document
.cookie
);
201 this.cookie
= cookie
[1];
203 this.cookie
= undefined;
207 euphorik
.Client
.prototype.delCookie = function() {
208 document
.cookie
= "cookie=; max-age=0";
209 this.cookie
= undefined;
212 euphorik
.Client
.prototype.setCookie = function() {
217 // ne fonctionne pas sous IE....
218 /*document.cookie = "cookie=" + this.cookie + "; max-age=" + (60 * 60 * 24 * 365) */
221 "cookie="+this.cookie
+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString();
224 euphorik
.Client
.prototype.authentifie = function() {
225 return this.statut
=== euphorik
.Client
.statutType
.auth_registered
|| this.statut
=== euphorik
.Client
.statutType
.auth_not_registered
;
228 euphorik
.Client
.prototype.setStatut = function(statut
)
230 // conversation en "enum" si en "string"
231 if (typeof(statut
) === "string") {
233 statut
=== "auth_registered" ?
234 euphorik
.Client
.statutType
.auth_registered :
235 (statut
=== "auth_not_registered" ? euphorik
.Client
.statutType
.auth_not_registered : euphorik
.Client
.statutType
.disconnected
);
238 if (statut
=== this.statut
) {
242 this.statut
= statut
;
249 * Try to authentify the client with the cookie information.
250 * Do nothing if there is no cookie.
252 euphorik
.Client
.prototype.connectionCookie = function() {
257 return this.connexion("authentification", { "cookie" : this.cookie
});
260 euphorik
.Client
.prototype.connexionLogin = function(login
, password
) {
261 return this.connexion("authentification", {"login" : login
, "password" : password
});
264 euphorik
.Client
.prototype.enregistrement = function(login
, password
) {
265 if (this.authentifie()) {
267 this.password
= password
;
269 this.setStatut(euphorik
.Client
.statutType
.auth_registered
);
274 return this.connexion("register", this.getJSONEnregistrement(login
, password
));
279 * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible
280 * de s'autentifier avec (login, password).
282 euphorik
.Client
.prototype.getJSONEnregistrement = function(login
, password
) {
285 if (login
&& password
) {
287 mess
.password
= password
;
290 mess
.profile
= this.getJSONProfileInfos();
296 * Connexion. Réalisé de manière synchrone.
298 euphorik
.Client
.prototype.connexion = function(action
, messageJson
) {
299 var thisClient
= this;
301 this.communication
.requete(
305 thisClient
.chargerDonnees(data
);
308 thisClient
.util
.messageDialog(data
.error_message
);
309 thisClient
.delCookie(); // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification
313 return this.authentifie();
316 euphorik
.Client
.prototype.disconnect = function() {
319 this.resetDonneesPersonnelles();
320 this.setStatut(euphorik
.Client
.statutType
.disconnected
);
323 euphorik
.Client
.prototype.chargerDonnees = function(data
) {
324 // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)
325 // de l'état ekMaster
326 this.ekMaster
= data
.ek_master
? data
.ek_master : false;
328 this.setStatut(data
.status
);
330 if (this.authentifie()) {
331 this.cookie
= data
.cookie
;
335 this.login
= data
.login
;
336 this.pseudo
= data
.profile
.nick
;
337 this.email
= data
.profile
.email
;
338 this.setCss(data
.profile
.css
);
339 this.chatOrder
= data
.profile
.chat_order
;
340 this.nickFormat
= data
.profile
.nick_format
;
341 this.viewTimes
= data
.profile
.view_times
;
342 this.viewTooltips
= data
.profile
.view_tooltips
;
343 this.ostentatiousMaster
= data
.profile
.ostentatious_master
;
345 // la page de la conversation principale
346 this.pagePrincipale
= 1;
349 this.conversations
= data
.profile
.conversations
;
350 this.conversations
.map(function(conv
) {
351 return { root : conv
.root
, page : 1, reduit : conv
.minimized
};
355 this.majCssSelectionee();
360 * Met à jour les données personne sur serveur.
361 * @param async de manière asynchrone ? défaut = true
362 * @return false si le flush n'a pas pû se faire sinon true
364 euphorik
.Client
.prototype.flush = function(async
) {
365 async
= async
|| false;
367 if (!this.authentifie()) {
371 var thisClient
= this;
374 this.communication
.requete(
376 this.getJSONProfile(),
378 thisClient
.majBulle();
381 thisClient
.util
.messageDialog(data
.error_message
);
390 euphorik
.Client
.prototype.majMenu = function() {
391 var displayType
= "block";
393 $("#menu .admin").css("display", this.ekMaster
? displayType : "none");
395 // met à jour le menu
396 if (this.statut
=== euphorik
.Client
.statutType
.auth_registered
) {
397 $("#menu .profile").css("display", displayType
).text("profile");
398 $("#menu .logout").css("display", displayType
);
399 $("#menu .register").css("display", "none");
400 } else if (this.statut
=== euphorik
.Client
.statutType
.auth_not_registered
) {
401 $("#menu .profile").css("display", "none");
402 $("#menu .logout").css("display", displayType
);
403 $("#menu .register").css("display", displayType
);
405 $("#menu .profile").css("display", displayType
).text("login");
406 $("#menu .logout").css("display", "none");
407 $("#menu .register").css("display", displayType
);
412 * Met à jour l'affichage ou non des infos bulles en fonction du profile.
414 euphorik
.Client
.prototype.majBulle = function() {
415 this.util
.bulleActive
= this.viewTooltips
;
419 * Met à jour la css sélectionnée, lors du chargement des données.
421 euphorik
.Client
.prototype.majCssSelectionee = function() {
422 // extraction du numéro de la css courante
423 var numCssCourante
= this.css
.match(/^.*?\/(\d)\/.*$/);
424 if (numCssCourante
&& numCssCourante
[1]) {
425 $("#menuCss option").removeAttr("selected");
426 $("#menuCss option[value=" + numCssCourante
[1]+ "]").attr("selected", "selected");
431 * Change la "class" du logo en fonction du statut de ekMaster.
433 euphorik
.Client
.prototype.majLogo = function() {
435 $("#logo").addClass("ekMaster");
437 $("#logo").removeClass("ekMaster");
441 euphorik
.Client
.prototype.slap = function(userId
, raison
) {
442 var thisClient
= this;
443 this.communication
.requete("slap", { "cookie" : thisClient
.cookie
, "user_id" : userId
, "reason" : raison
});
446 euphorik
.Client
.prototype.ban = function(userId
, raison
, minutes
) {
447 var thisClient
= this;
449 // par défaut un ban correspond à 3 jours
450 minutes
= minutes
|| euphorik
.conf
.tempsBan
;
451 this.communication
.requete("ban", { "cookie" : thisClient
.cookie
, "duration" : minutes
, "user_id" : userId
, "reason" : raison
});
454 euphorik
.Client
.prototype.kick = function(userId
, raison
) {
455 this.ban(userId
, raison
, euphorik
.conf
.tempsKick
);