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 * Object that represents the user.
23 * It carries all the user data like
28 * It can access directly to the DOM if needed. Some examples :
29 * - Changes the menu if the user is logged or not
30 * - Changes the logo if the user is an ek master
33 * @communication See communication.js.
35 euphorik
.Client = function(util
, communication
) {
37 this.communication
= communication
;
40 this.regexCookie
= /cookie=([^;]*)/;
42 this.resetPersonalData();
44 this.setStatus(euphorik
.Client
.statusType
.disconnected
);
46 // If true then each data change is flushed to the server.
47 // Active only for opera which doesn't support the unload event.
48 this.autoflush
= $.browser
.opera
;
51 // The three status of a client.
52 euphorik
.Client
.statusType
= {
53 // authentified and registered : The user can post messages and can modify his profile
55 // authentified but not registered : The user can only post messages
56 auth_not_registered : 1,
57 // disconnected (the initial state) : The user cannot post message
61 euphorik
.Client
.prototype.resetPersonalData = function() {
63 this.nick
= euphorik
.conf
.defaultNick
;
67 this.css
= $("link#mainCss").attr("href");
68 this.chatOrder
= "reverse";
69 this.nickFormat
= "nick";
70 this.viewTimes
= true;
71 this.viewTooltips
= true;
72 this.cookie
= undefined;
74 this.mainConversationPage
= 1;
75 this.ekMaster
= false;
76 this.ostentatiousMaster
= "light";
78 // les conversations, une conversation est un objet possédant les propriétés suivantes :
82 this.conversations
= [];
85 euphorik
.Client
.prototype.setCss = function(css
) {
86 if (this.css
=== css
|| !css
) {
91 $("link#mainCss").attr("href", this.css
);
97 euphorik
.Client
.prototype.pageSuivante = function(numConv
) {
98 if (numConv
< 0 && this.mainConversationPage
> 1) {
99 this.mainConversationPage
-= 1;
100 } else if (this.conversations
[numConv
].page
> 1) {
101 this.conversations
[numConv
].page
-= 1;
105 euphorik
.Client
.prototype.pagePrecedente = function(numConv
) {
107 this.mainConversationPage
+= 1;
109 this.conversations
[numConv
].page
+= 1;
114 * Définit la première page pour la conversation donnée.
115 * @return true si la page a changé sinon false
117 euphorik
.Client
.prototype.goPremierePage = function(numConv
)
120 if (this.mainConversationPage
=== 1) {
123 this.mainConversationPage
= 1;
125 if (this.conversations
[numConv
].page
=== 1) {
128 this.conversations
[numConv
].page
= 1;
134 * Ajoute une conversation à la vue de l'utilisateur.
135 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
136 * @param racines la racine de la conversation (integer)
137 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
139 euphorik
.Client
.prototype.ajouterConversation = function(racine
) {
140 // vérification s'il elle n'existe pas déjà
142 this.conversations
.each(function(i
, conv
) {
143 if (conv
.root
=== racine
) {
151 this.conversations
.push({root : racine
, page : 1, reduit : false});
152 if (this.autoflush
) {
159 euphorik
.Client
.prototype.supprimerConversation = function(num
) {
160 if (num
< 0 || num
>= this.conversations
.length
) {
164 // décalage TODO : supprimer le dernier élément
165 for (var i
= num
; i
< this.conversations
.length
- 1; i
++) {
166 this.conversations
[i
] = this.conversations
[i
+1];
168 this.conversations
.pop();
170 if (this.autoflush
) {
175 euphorik
.Client
.prototype.getJSONConversations = function() {
176 var conversations
= [];
177 this.conversations
.each(function(i
, conv
) {
178 conversations
.push({ "root" : conv
.root
, "minimized" : conv
.reduit
});
180 return conversations
;
183 euphorik
.Client
.prototype.getJSONProfile = function() {
185 "cookie" : this.cookie
,
186 "login" : this.login
,
187 "password" : this.password
,
188 "profile" : this.getJSONProfileInfos()
192 euphorik
.Client
.prototype.getJSONProfileInfos = function() {
195 "email" : this.email
,
197 "chat_order" : this.chatOrder
,
198 "nick_format" : this.nickFormat
,
199 "view_times" : this.viewTimes
,
200 "view_tooltips" : this.viewTooltips
,
201 "conversations" : this.getJSONConversations(),
202 "ostentatious_master" : this.ostentatiousMaster
209 euphorik
.Client
.prototype.getCookie = function() {
210 var cookie
= this.regexCookie
.exec(document
.cookie
);
212 this.cookie
= cookie
[1];
214 this.cookie
= undefined;
218 euphorik
.Client
.prototype.delCookie = function() {
219 document
.cookie
= "cookie=; max-age=0";
220 this.cookie
= undefined;
223 euphorik
.Client
.prototype.setCookie = function() {
228 // doesn't work under IE....
229 /*document.cookie = "cookie=" + this.cookie + "; max-age=" + (60 * 60 * 24 * 365) */
232 "cookie="+this.cookie
+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString();
235 euphorik
.Client
.prototype.authentifie = function() {
236 return this.statut
=== euphorik
.Client
.statusType
.auth_registered
|| this.statut
=== euphorik
.Client
.statusType
.auth_not_registered
;
239 euphorik
.Client
.prototype.setStatus = function(statut
)
241 // conversation en "enum" si en "string"
242 if (typeof(statut
) === "string") {
244 statut
=== "auth_registered" ?
245 euphorik
.Client
.statusType
.auth_registered :
246 (statut
=== "auth_not_registered" ? euphorik
.Client
.statusType
.auth_not_registered : euphorik
.Client
.statusType
.disconnected
);
249 if (statut
=== this.statut
) {
253 this.statut
= statut
;
260 * Try to authentify the client with the cookie information.
261 * Do nothing if there is no cookie.
263 euphorik
.Client
.prototype.connectionCookie = function() {
268 return this.connexion("authentification", { "cookie" : this.cookie
});
271 euphorik
.Client
.prototype.connexionLogin = function(login
, password
) {
272 return this.connexion("authentification", {"login" : login
, "password" : password
});
275 euphorik
.Client
.prototype.enregistrement = function(login
, password
) {
276 if (this.authentifie()) {
278 this.password
= password
;
280 this.setStatus(euphorik
.Client
.statusType
.auth_registered
);
285 return this.connexion("register", this.getJSONEnregistrement(login
, password
));
290 * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible
291 * de s'autentifier avec (login, password).
293 euphorik
.Client
.prototype.getJSONEnregistrement = function(login
, password
) {
296 if (login
&& password
) {
298 mess
.password
= password
;
301 mess
.profile
= this.getJSONProfileInfos();
307 * Connexion. Réalisé de manière synchrone.
309 euphorik
.Client
.prototype.connexion = function(action
, messageJson
) {
310 var thisClient
= this;
312 this.communication
.requete(
316 thisClient
.chargerDonnees(data
);
319 thisClient
.util
.messageDialog(data
.error_message
);
320 thisClient
.delCookie(); // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification
324 return this.authentifie();
327 euphorik
.Client
.prototype.disconnect = function() {
330 this.resetPersonalData();
331 this.setStatus(euphorik
.Client
.statusType
.disconnected
);
334 euphorik
.Client
.prototype.chargerDonnees = function(data
) {
335 // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)
336 // de l'état ekMaster
337 this.ekMaster
= data
.ek_master
? data
.ek_master : false;
339 this.setStatus(data
.status
);
341 if (this.authentifie()) {
342 this.cookie
= data
.cookie
;
346 this.login
= data
.login
;
347 this.nick
= data
.profile
.nick
;
348 this.email
= data
.profile
.email
;
349 this.setCss(data
.profile
.css
);
350 this.chatOrder
= data
.profile
.chat_order
;
351 this.nickFormat
= data
.profile
.nick_format
;
352 this.viewTimes
= data
.profile
.view_times
;
353 this.viewTooltips
= data
.profile
.view_tooltips
;
354 this.ostentatiousMaster
= data
.profile
.ostentatious_master
;
356 // la page de la conversation principale
357 this.mainConversationPage
= 1;
360 this.conversations
= data
.profile
.conversations
;
361 this.conversations
.map(function(conv
) {
362 return { root : conv
.root
, page : 1, reduit : conv
.minimized
};
366 this.majCssSelectionee();
371 * Met à jour les données personne sur serveur.
372 * @param async de manière asynchrone ? défaut = true
373 * @return false si le flush n'a pas pû se faire sinon true
375 euphorik
.Client
.prototype.flush = function(async
) {
376 async
= async
|| false;
378 if (!this.authentifie()) {
382 var thisClient
= this;
385 this.communication
.requete(
387 this.getJSONProfile(),
389 thisClient
.majBulle();
392 thisClient
.util
.messageDialog(data
.error_message
);
401 euphorik
.Client
.prototype.majMenu = function() {
402 var displayType
= "block";
404 $("#menu .admin").css("display", this.ekMaster
? displayType : "none");
406 // met à jour le menu
407 if (this.statut
=== euphorik
.Client
.statusType
.auth_registered
) {
408 $("#menu .profile").css("display", displayType
).text("profile");
409 $("#menu .logout").css("display", displayType
);
410 $("#menu .register").css("display", "none");
411 } else if (this.statut
=== euphorik
.Client
.statusType
.auth_not_registered
) {
412 $("#menu .profile").css("display", "none");
413 $("#menu .logout").css("display", displayType
);
414 $("#menu .register").css("display", displayType
);
416 $("#menu .profile").css("display", displayType
).text("login");
417 $("#menu .logout").css("display", "none");
418 $("#menu .register").css("display", displayType
);
423 * Met à jour l'affichage ou non des infos bulles en fonction du profile.
425 euphorik
.Client
.prototype.majBulle = function() {
426 this.util
.bulleActive
= this.viewTooltips
;
430 * Met à jour la css sélectionnée, lors du chargement des données.
432 euphorik
.Client
.prototype.majCssSelectionee = function() {
433 // extraction du numéro de la css courante
434 var numCssCourante
= this.css
.match(/^.*?\/(\d)\/.*$/);
435 if (numCssCourante
&& numCssCourante
[1]) {
436 $("#menuCss option").removeAttr("selected");
437 $("#menuCss option[value=" + numCssCourante
[1]+ "]").attr("selected", "selected");
442 * Change la "class" du logo en fonction du statut de ekMaster.
444 euphorik
.Client
.prototype.majLogo = function() {
446 $("#logo").addClass("ekMaster");
448 $("#logo").removeClass("ekMaster");
452 euphorik
.Client
.prototype.slap = function(userId
, raison
) {
453 var thisClient
= this;
454 this.communication
.requete("slap", { "cookie" : thisClient
.cookie
, "user_id" : userId
, "reason" : raison
});
457 euphorik
.Client
.prototype.ban = function(userId
, raison
, minutes
) {
458 var thisClient
= this;
460 // par défaut un ban correspond à 3 jours
461 minutes
= minutes
|| euphorik
.conf
.tempsBan
;
462 this.communication
.requete("ban", { "cookie" : thisClient
.cookie
, "duration" : minutes
, "user_id" : userId
, "reason" : raison
});
465 euphorik
.Client
.prototype.kick = function(userId
, raison
) {
466 this.ban(userId
, raison
, euphorik
.conf
.tempsKick
);