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 */
21 // les statuts possibes du client
22 euphorik
.Client
.statutType
= {
23 // mode enregistré, peut poster des messages et modifier son profile
25 // mode identifié, peut poster des messages mais n'a pas accès au profile
26 auth_not_registered : 1,
27 // mode déconnecté, ne peut pas poster de message
32 * Représente l'utilisateur du site.
34 euphorik
.Client = function(util
) {
38 this.regexCookie
= /^cookie=([^;]*)/;
41 this.resetDonneesPersonnelles();
43 this.setStatut(euphorik
.Client
.statutType
.deconnected
);
45 // si true alors chaque modification du client est mémorisé sur le serveur
46 this.autoflush
= $.browser
.opera
;
49 euphorik
.Client
.prototype.resetDonneesPersonnelles = function() {
51 this.pseudo
= euphorik
.conf
.pseudoDefaut
;
55 this.css
= $("link#cssPrincipale").attr("href");
56 this.chatOrder
= "reverse";
57 this.nickFormat
= "nick";
58 this.viewTimes
= true;
59 this.viewTooltips
= true;
60 this.cookie
= undefined;
62 this.pagePrincipale
= 1;
63 this.ekMaster
= false;
64 this.ostentatiousMaster
= "light";
66 // les conversations, une conversation est un objet possédant les propriétés suivantes :
70 this.conversations
= [];
73 euphorik
.Client
.prototype.setCss = function(css
) {
74 if (this.css
=== css
|| !css
) {
79 $("link#cssPrincipale").attr("href", this.css
);
85 euphorik
.Client
.prototype.pageSuivante = function(numConv
) {
86 if (numConv
< 0 && this.pagePrincipale
> 1) {
87 this.pagePrincipale
-= 1;
88 } else if (this.conversations
[numConv
].page
> 1) {
89 this.conversations
[numConv
].page
-= 1;
93 euphorik
.Client
.prototype.pagePrecedente = function(numConv
) {
95 this.pagePrincipale
+= 1;
97 this.conversations
[numConv
].page
+= 1;
102 * Définit la première page pour la conversation donnée.
103 * @return true si la page a changé sinon false
105 euphorik
.Client
.prototype.goPremierePage = function(numConv
)
108 if (this.pagePrincipale
== 1) {
111 this.pagePrincipale
= 1;
113 if (this.conversations
[numConv
].page
== 1) {
116 this.conversations
[numConv
].page
= 1;
122 * Ajoute une conversation à la vue de l'utilisateur.
123 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
124 * @param racines la racine de la conversation (integer)
125 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
127 euphorik
.Client
.prototype.ajouterConversation = function(racine
) {
128 // vérification s'il elle n'existe pas déjà
129 this.conversations
.each(function(i
, conv
) {
130 if (conv
.root
=== racine
) {
135 this.conversations
.push({root : racine
, page : 1, reduit : false});
136 if (this.autoflush
) {
143 euphorik
.Client
.prototype.supprimerConversation = function(num
) {
144 if (num
< 0 || num
>= this.conversations
.length
) {
148 // décalage TODO : supprimer le dernier élément
149 for (var i
= num
; i
< this.conversations
.length
- 1; i
++) {
150 this.conversations
[i
] = this.conversations
[i
+1];
152 this.conversations
.pop();
154 if (this.autoflush
) {
159 euphorik
.Client
.prototype.getJSONLogin = function(login
, password
) {
161 "header" : { "action" : "authentification", "version" : euphorik
.conf
.versionProtocole
},
163 "password" : password
167 euphorik
.Client
.prototype.getJSONLoginCookie = function() {
169 "header" : { "action" : "authentification", "version" : euphorik
.conf
.versionProtocole
},
170 "cookie" : this.cookie
175 * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible
176 * de s'autentifier avec (login, password).
178 euphorik
.Client
.prototype.getJSONEnregistrement = function(login
, password
) {
179 var mess
= { "header" : { "action" : "register", "version" : euphorik
.conf
.versionProtocole
} };
181 if (login
&& password
) {
183 mess
.password
= password
;
189 euphorik
.Client
.prototype.getJSONConversations = function() {
190 var conversations
= [];
191 this.conversations
.each(function(i
, conv
) {
192 conversations
.push({ "root" : conv
.root
, "minimized" : conv
.reduit
});
194 return conversations
;
197 euphorik
.Client
.prototype.getJSONProfile = function() {
199 "header" : { "action" : "set_profile", "version" : euphorik
.conf
.versionProtocole
},
200 "cookie" : this.cookie
,
201 "login" : this.login
,
202 "password" : this.password
,
203 "nick" : this.pseudo
,
204 "email" : this.email
,
206 "chat_order" : this.chatOrder
,
207 "nick_format" : this.nickFormat
,
208 "view_times" : this.viewTimes
,
209 "view_tooltips" : this.viewTooltips
,
210 "conversations" : this.getJSONConversations(),
211 "ostentatious_master" : this.ostentatiousMaster
218 euphorik
.Client
.prototype.getCookie = function() {
219 var cookie
= this.regexCookie
.exec(document
.cookie
);
221 this.cookie
= cookie
[1];
223 this.cookie
= undefined;
227 euphorik
.Client
.prototype.delCookie = function() {
228 document
.cookie
= "cookie=; max-age=0";
229 this.cookie
= undefined;
232 euphorik
.Client
.prototype.setCookie = function() {
237 // ne fonctionne pas sous IE....
238 /*document.cookie = "cookie=" + this.cookie + "; max-age=" + (60 * 60 * 24 * 365) */
241 "cookie="+this.cookie
+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString();
244 euphorik
.Client
.prototype.authentifie = function() {
245 return this.statut
=== euphorik
.Client
.statutType
.auth_registered
|| this.statut
=== euphorik
.Client
.statutType
.auth_not_registered
;
248 euphorik
.Client
.prototype.setStatut = function(statut
)
250 // conversation en "enum" si en "string"
251 if (typeof(statut
) === "string") {
253 statut
=== "auth_registered" ?
254 euphorik
.Client
.statutType
.auth_registered :
255 (statut
=== "auth_not_registered" ? euphorik
.Client
.statutType
.auth_not_registered : euphorik
.Client
.statutType
.deconnected
);
258 if (statut
=== this.statut
) {
262 this.statut
= statut
;
268 * Effectue la connexion vers le serveur.
269 * Cette fonction est bloquante tant que la connexion n'a pas été établie.
270 * S'il existe un cookie en local on s'authentifie directement avec lui.
271 * Si il n'est pas possible de s'authentifier alors on affiche un captcha anti-bot.
273 euphorik
.Client
.prototype.connexionCookie = function() {
278 return this.connexion(this.getJSONLoginCookie());
281 euphorik
.Client
.prototype.connexionLogin = function(login
, password
) {
282 return this.connexion(this.getJSONLogin(login
, password
));
285 euphorik
.Client
.prototype.enregistrement = function(login
, password
) {
286 if (this.authentifie()) {
288 this.password
= password
;
290 this.setStatut(euphorik
.Client
.statutType
.auth_registered
);
295 return this.connexion(this.getJSONEnregistrement(login
, password
));
300 * Connexion. Réalisé de manière synchrone.
302 euphorik
.Client
.prototype.connexion = function(messageJson
) {
303 var thisClient
= this;
309 data: this.util
.jsonVersAction(messageJson
),
312 if (data
.reply
=== "error") {
313 thisClient
.util
.messageDialogue(data
.error_message
);
314 // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification
315 thisClient
.delCookie();
317 thisClient
.chargerDonnees(data
);
321 return this.authentifie();
324 euphorik
.Client
.prototype.deconnexion = function() {
327 this.resetDonneesPersonnelles();
328 this.setStatut(euphorik
.Client
.statutType
.deconnected
); // deconnexion
331 euphorik
.Client
.prototype.chargerDonnees = function(data
) {
332 // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)
333 // de l'état ekMaster
334 this.ekMaster
= data
.ek_master
? data
.ek_master : false;
336 this.setStatut(data
.status
);
338 if (this.authentifie()) {
339 this.cookie
= data
.cookie
;
343 this.login
= data
.login
;
344 this.pseudo
= data
.nick
;
345 this.email
= data
.email
;
346 this.setCss(data
.css
);
347 this.chatOrder
= data
.chat_order
;
348 this.nickFormat
= data
.nick_format
;
349 this.viewTimes
= data
.view_times
;
350 this.viewTooltips
= data
.view_tooltips
;
351 this.ostentatiousMaster
= data
.ostentatious_master
;
353 // la page de la conversation principale
354 this.pagePrincipale
= 1;
357 this.conversations
= data
.conversations
;
358 this.conversations
.map(function(conv
) {
359 return { root : conv
.root
, page : 1, reduit : conv
.minimized
};
363 this.majCssSelectionee();
368 * Met à jour les données personne sur serveur.
369 * @param async de manière asynchrone ? défaut = true
370 * @return false si le flush n'a pas pû se faire sinon true
372 euphorik
.Client
.prototype.flush = function(async
) {
373 async
= async
|| false;
375 if (!this.authentifie()) {
379 var thisClient
= this;
386 data: this.util
.jsonVersAction(this.getJSONProfile()),
389 if (data
.reply
=== "error") {
390 thisClient
.util
.messageDialogue(data
.error_message
);
393 thisClient
.majBulle();
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
.statutType
.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
.statutType
.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;
459 data: this.util
.jsonVersAction({
460 "header" : { "action" : "slap", "version" : euphorik
.conf
.versionProtocole
},
461 "cookie" : thisClient
.cookie
,
467 if (data
.reply
=== "error") {
468 thisClient
.util
.messageDialogue(data
.error_message
);
474 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
;
485 data: this.util
.jsonVersAction({
486 "header" : { "action" : "ban", "version" : euphorik
.conf
.versionProtocole
},
487 "cookie" : thisClient
.cookie
,
488 "duration" : minutes
,
494 if (data
.reply
=== "error") {
495 thisClient
.util
.messageDialogue(data
.error_message
);
501 euphorik
.Client
.prototype.kick = function(userId
, raison
) {
502 this.ban(userId
, raison
, euphorik
.conf
.tempsKick
);