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.
62 * Reset all the local personal data. Does not erase the remote data.
63 * This function is used during disconnecting.
65 euphorik
.Client
.prototype.resetPersonalData = function() {
67 this.nick
= euphorik
.conf
.defaultNick
;
71 this.css
= $("link#mainCss").attr("href");
72 this.chatOrder
= "reverse";
73 this.nickFormat
= "nick";
74 this.viewTimes
= true;
75 this.viewTooltips
= true;
76 this.cookie
= undefined;
78 this.mainConversationPage
= 1;
79 this.ekMaster
= false;
80 this.ostentatiousMaster
= "light";
82 // The user opened conversations.
83 // Each conversation object owns theses properties :
86 // - isCollapsed (bool)
87 this.conversations
= [];
91 * Set the CSS. Dynamically change the href to the CSS in the DOM.
92 * @css The relative path to the CSS file, for example :
93 * "styles/1/euphorik.css".
95 euphorik
.Client
.prototype.setCss = function(css
) {
96 if (this.css
=== css
|| !css
) {
101 $("link#mainCss").attr("href", this.css
);
103 if (this.autoflush
) {
109 * Change the current page to the next one for the given conversation.
110 * @numConv The number of the conversation.
112 euphorik
.Client
.prototype.nextPage = function(numConv
) {
113 if (numConv
< 0 && this.mainConversationPage
> 1) {
114 this.mainConversationPage
-= 1;
115 } else if (this.conversations
[numConv
].page
> 1) {
116 this.conversations
[numConv
].page
-= 1;
121 * Change the current page to the previous one for the given conversation.
122 * @numConv The number of the conversation.
124 euphorik
.Client
.prototype.previousPage = function(numConv
) {
126 this.mainConversationPage
+= 1;
128 this.conversations
[numConv
].page
+= 1;
133 * Définit la première page pour la conversation donnée.
134 * @return true si la page a changé sinon false
136 euphorik
.Client
.prototype.goFirstPage = function(numConv
) {
138 if (this.mainConversationPage
=== 1) {
141 this.mainConversationPage
= 1;
143 if (this.conversations
[numConv
].page
=== 1) {
146 this.conversations
[numConv
].page
= 1;
152 * Ajoute une conversation à la vue de l'utilisateur.
153 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
154 * @param racines la racine de la conversation (integer)
155 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
157 euphorik
.Client
.prototype.ajouterConversation = function(racine
) {
158 // vérification s'il elle n'existe pas déjà
160 this.conversations
.each(function(i
, conv
) {
161 if (conv
.root
=== racine
) {
169 this.conversations
.push({root : racine
, page : 1, isCollapsed : false});
170 if (this.autoflush
) {
177 euphorik
.Client
.prototype.supprimerConversation = function(num
) {
178 if (num
< 0 || num
>= this.conversations
.length
) {
182 // décalage TODO : supprimer le dernier élément
183 for (var i
= num
; i
< this.conversations
.length
- 1; i
++) {
184 this.conversations
[i
] = this.conversations
[i
+1];
186 this.conversations
.pop();
188 if (this.autoflush
) {
193 euphorik
.Client
.prototype.getJSONConversations = function() {
194 var conversations
= [];
195 this.conversations
.each(function(i
, conv
) {
196 conversations
.push({ "root" : conv
.root
, "minimized" : conv
.isCollapsed
});
198 return conversations
;
201 euphorik
.Client
.prototype.getJSONProfile = function() {
203 "cookie" : this.cookie
,
204 "login" : this.login
,
205 "password" : this.password
,
206 "profile" : this.getJSONProfileInfos()
210 euphorik
.Client
.prototype.getJSONProfileInfos = function() {
213 "email" : this.email
,
215 "chat_order" : this.chatOrder
,
216 "nick_format" : this.nickFormat
,
217 "view_times" : this.viewTimes
,
218 "view_tooltips" : this.viewTooltips
,
219 "conversations" : this.getJSONConversations(),
220 "ostentatious_master" : this.ostentatiousMaster
227 euphorik
.Client
.prototype.getCookie = function() {
228 var cookie
= this.regexCookie
.exec(document
.cookie
);
230 this.cookie
= cookie
[1];
232 this.cookie
= undefined;
236 euphorik
.Client
.prototype.delCookie = function() {
237 document
.cookie
= "cookie=; max-age=0";
238 this.cookie
= undefined;
241 euphorik
.Client
.prototype.setCookie = function() {
246 // doesn't work under IE....
247 /*document.cookie = "cookie=" + this.cookie + "; max-age=" + (60 * 60 * 24 * 365) */
250 "cookie="+this.cookie
+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString();
253 euphorik
.Client
.prototype.authentifie = function() {
254 return this.statut
=== euphorik
.Client
.statusType
.auth_registered
|| this.statut
=== euphorik
.Client
.statusType
.auth_not_registered
;
257 euphorik
.Client
.prototype.setStatus = function(statut
)
259 // conversation en "enum" si en "string"
260 if (typeof(statut
) === "string") {
262 statut
=== "auth_registered" ?
263 euphorik
.Client
.statusType
.auth_registered :
264 (statut
=== "auth_not_registered" ? euphorik
.Client
.statusType
.auth_not_registered : euphorik
.Client
.statusType
.disconnected
);
267 if (statut
=== this.statut
) {
271 this.statut
= statut
;
278 * Try to authentify the client with the cookie information.
279 * Do nothing if there is no cookie.
281 euphorik
.Client
.prototype.connectionCookie = function() {
286 return this.connexion("authentification", { "cookie" : this.cookie
});
289 euphorik
.Client
.prototype.connexionLogin = function(login
, password
) {
290 return this.connexion("authentification", {"login" : login
, "password" : password
});
293 euphorik
.Client
.prototype.enregistrement = function(login
, password
) {
294 if (this.authentifie()) {
296 this.password
= password
;
298 this.setStatus(euphorik
.Client
.statusType
.auth_registered
);
303 return this.connexion("register", this.getJSONEnregistrement(login
, password
));
308 * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible
309 * de s'autentifier avec (login, password).
311 euphorik
.Client
.prototype.getJSONEnregistrement = function(login
, password
) {
314 if (login
&& password
) {
316 mess
.password
= password
;
319 mess
.profile
= this.getJSONProfileInfos();
325 * Connexion. Réalisé de manière synchrone.
327 euphorik
.Client
.prototype.connexion = function(action
, messageJson
) {
328 var thisClient
= this;
330 this.communication
.requete(
334 thisClient
.chargerDonnees(data
);
337 thisClient
.util
.messageDialog(data
.error_message
);
338 thisClient
.delCookie(); // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification
342 return this.authentifie();
345 euphorik
.Client
.prototype.disconnect = function() {
348 this.resetPersonalData();
349 this.setStatus(euphorik
.Client
.statusType
.disconnected
);
352 euphorik
.Client
.prototype.chargerDonnees = function(data
) {
353 // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)
354 // de l'état ekMaster
355 this.ekMaster
= data
.ek_master
? data
.ek_master : false;
357 this.setStatus(data
.status
);
359 if (this.authentifie()) {
360 this.cookie
= data
.cookie
;
364 this.login
= data
.login
;
365 this.nick
= data
.profile
.nick
;
366 this.email
= data
.profile
.email
;
367 this.setCss(data
.profile
.css
);
368 this.chatOrder
= data
.profile
.chat_order
;
369 this.nickFormat
= data
.profile
.nick_format
;
370 this.viewTimes
= data
.profile
.view_times
;
371 this.viewTooltips
= data
.profile
.view_tooltips
;
372 this.ostentatiousMaster
= data
.profile
.ostentatious_master
;
374 // la page de la conversation principale
375 this.mainConversationPage
= 1;
378 this.conversations
= data
.profile
.conversations
;
379 this.conversations
.map(function(conv
) {
380 return { root : conv
.root
, page : 1, isCollapsed : conv
.minimized
};
384 this.majCssSelectionee();
389 * Met à jour les données personne sur serveur.
390 * @param async de manière asynchrone ? défaut = true
391 * @return false si le flush n'a pas pû se faire sinon true
393 euphorik
.Client
.prototype.flush = function(async
) {
394 async
= async
|| false;
396 if (!this.authentifie()) {
400 var thisClient
= this;
403 this.communication
.requete(
405 this.getJSONProfile(),
407 thisClient
.majBulle();
410 thisClient
.util
.messageDialog(data
.error_message
);
419 euphorik
.Client
.prototype.majMenu = function() {
420 var displayType
= "block";
422 $("#menu .admin").css("display", this.ekMaster
? displayType : "none");
424 // met à jour le menu
425 if (this.statut
=== euphorik
.Client
.statusType
.auth_registered
) {
426 $("#menu .profile").css("display", displayType
).text("profile");
427 $("#menu .logout").css("display", displayType
);
428 $("#menu .register").css("display", "none");
429 } else if (this.statut
=== euphorik
.Client
.statusType
.auth_not_registered
) {
430 $("#menu .profile").css("display", "none");
431 $("#menu .logout").css("display", displayType
);
432 $("#menu .register").css("display", displayType
);
434 $("#menu .profile").css("display", displayType
).text("login");
435 $("#menu .logout").css("display", "none");
436 $("#menu .register").css("display", displayType
);
441 * Met à jour l'affichage ou non des infos bulles en fonction du profile.
443 euphorik
.Client
.prototype.majBulle = function() {
444 this.util
.bulleActive
= this.viewTooltips
;
448 * Met à jour la css sélectionnée, lors du chargement des données.
450 euphorik
.Client
.prototype.majCssSelectionee = function() {
451 // extraction du numéro de la css courante
452 var numCssCourante
= this.css
.match(/^.*?\/(\d)\/.*$/);
453 if (numCssCourante
&& numCssCourante
[1]) {
454 $("#menuCss option").removeAttr("selected");
455 $("#menuCss option[value=" + numCssCourante
[1]+ "]").attr("selected", "selected");
460 * Change la "class" du logo en fonction du statut de ekMaster.
462 euphorik
.Client
.prototype.majLogo = function() {
464 $("#logo").addClass("ekMaster");
466 $("#logo").removeClass("ekMaster");
470 euphorik
.Client
.prototype.slap = function(userId
, raison
) {
471 var thisClient
= this;
472 this.communication
.requete("slap", { "cookie" : thisClient
.cookie
, "user_id" : userId
, "reason" : raison
});
475 euphorik
.Client
.prototype.ban = function(userId
, raison
, minutes
) {
476 var thisClient
= this;
478 // par défaut un ban correspond à 3 jours
479 minutes
= minutes
|| euphorik
.conf
.tempsBan
;
480 this.communication
.requete("ban", { "cookie" : thisClient
.cookie
, "duration" : minutes
, "user_id" : userId
, "reason" : raison
});
483 euphorik
.Client
.prototype.kick = function(userId
, raison
) {
484 this.ban(userId
, raison
, euphorik
.conf
.tempsKick
);