a09819b58232c19fa87c02c07f565fdd1038fad5
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 function Client(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 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 Client
.prototype.setCss = function(css
) {
74 if (this.css
== css
|| css
== "") {
79 $("link#cssPrincipale").attr("href", this.css
);
85 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 Client
.prototype.pagePrecedente = function(numConv
) {
95 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 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 Client
.prototype.ajouterConversation = function(racine
) {
129 // vérification s'il elle n'existe pas déjà
130 for (var i
= 0; i
< this.conversations
.length
; i
++)
131 if (this.conversations
[i
].root
== racine
)
134 this.conversations
.push({root : racine
, page : 1, reduit : false})
135 if (this.autoflush
) this.flush(true)
140 Client
.prototype.supprimerConversation = function(num
)
142 if (num
< 0 || num
>= this.conversations
.length
) return
144 // décalage TODO : supprimer le dernier élément
145 for (var i
= num
; i
< this.conversations
.length
- 1; i
++)
146 this.conversations
[i
] = this.conversations
[i
+1]
147 this.conversations
.pop()
149 if (this.autoflush
) this.flush(true)
152 Client
.prototype.getJSONLogin = function(login
, password
)
155 "header" : { "action" : "authentification", "version" : euphorik
.conf
.versionProtocole
},
157 "password" : password
161 Client
.prototype.getJSONLoginCookie = function()
164 "header" : { "action" : "authentification", "version" : euphorik
.conf
.versionProtocole
},
165 "cookie" : this.cookie
170 * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible
171 * de s'autentifier avec (login, password).
173 Client
.prototype.getJSONEnregistrement = function(login
, password
)
175 var mess
= { "header" : { "action" : "register", "version" : euphorik
.conf
.versionProtocole
}}
177 if (login
!= undefined && password
!= undefined)
179 mess
["login"] = login
180 mess
["password"] = password
186 Client
.prototype.getJSONConversations = function()
188 var conversations
= new Array()
189 for (var i
= 0; i
< this.conversations
.length
; i
++)
190 conversations
.push({root : this.conversations
[i
].root
, minimized : this.conversations
[i
].reduit
})
194 Client
.prototype.getJSONProfile = function()
197 "header" : { "action" : "set_profile", "version" : euphorik
.conf
.versionProtocole
},
198 "cookie" : this.cookie
,
199 "login" : this.login
,
200 "password" : this.password
,
201 "nick" : this.pseudo
,
202 "email" : this.email
,
204 "chat_order" : this.chatOrder
,
205 "nick_format" : this.nickFormat
,
206 "view_times" : this.viewTimes
,
207 "view_tooltips" : this.viewTooltips
,
208 "conversations" : this.getJSONConversations(),
209 "ostentatious_master" : this.ostentatiousMaster
214 * Renvoie null si pas définit.
216 Client
.prototype.getCookie = function()
218 var cookie
= this.regexCookie
.exec(document
.cookie
)
219 if (cookie
== null) this.cookie
= null
220 else this.cookie
= cookie
[1]
223 Client
.prototype.delCookie = function()
225 document
.cookie
= "cookie=; max-age=0"
228 Client
.prototype.setCookie = function()
230 if (this.cookie
== null || this.cookie
== undefined)
233 // ne fonctionne pas sous IE....
234 /*document.cookie = "cookie=" + this.cookie + "; max-age=" + (60 * 60 * 24 * 365) */
237 "cookie="+this.cookie
+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString()
240 Client
.prototype.authentifie = function()
242 return this.statut
== euphorik
.Client
.statutType
.auth_registered
|| this.statut
== euphorik
.Client
.statutType
.auth_not_registered
245 Client
.prototype.setStatut = function(statut
)
247 // conversation en "enum" si en "string"
248 if (typeof(statut
) == "string")
251 statut
== "auth_registered" ?
252 euphorik
.Client
.statutType
.auth_registered :
253 (statut
== "auth_not_registered" ? euphorik
.Client
.statutType
.auth_not_registered : euphorik
.Client
.statutType
.deconnected
)
256 if (statut
== this.statut
) return
264 * Effectue la connexion vers le serveur.
265 * Cette fonction est bloquante tant que la connexion n'a pas été établie.
266 * S'il existe un cookie en local on s'authentifie directement avec lui.
267 * Si il n'est pas possible de s'authentifier alors on affiche un captcha anti-bot.
269 Client
.prototype.connexionCookie = function()
272 if (this.cookie
== null) return false;
273 return this.connexion(this.getJSONLoginCookie())
276 Client
.prototype.connexionLogin = function(login
, password
)
278 return this.connexion(this.getJSONLogin(login
, password
))
281 Client
.prototype.enregistrement = function(login
, password
)
283 if (this.authentifie())
286 this.password
= password
289 this.setStatut(euphorik
.Client
.statutType
.auth_registered
)
296 return this.connexion(this.getJSONEnregistrement(login
, password
))
301 * Connexion. Réalisé de manière synchrone.
303 Client
.prototype.connexion = function(messageJson
)
305 var thisClient
= this
312 data: this.util
.jsonVersAction(messageJson
),
316 if (data
["reply"] == "error")
318 thisClient
.util
.messageDialogue(data
["error_message"])
319 // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification
320 thisClient
.delCookie()
323 thisClient
.chargerDonnees(data
)
327 return this.authentifie()
330 Client
.prototype.deconnexion = function()
334 this.resetDonneesPersonnelles()
335 this.setStatut(euphorik
.Client
.statutType
.deconnected
) // deconnexion
338 Client
.prototype.chargerDonnees = function(data
)
340 // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)
341 // de l'état ekMaster
342 this.ekMaster
= data
["ek_master"] != undefined ? data
["ek_master"] : false
344 this.setStatut(data
["status"])
346 if (this.authentifie())
348 this.cookie
= data
["cookie"]
352 this.login
= data
["login"]
353 this.pseudo
= data
["nick"]
354 this.email
= data
["email"]
355 this.setCss(data
["css"])
356 this.chatOrder
= data
["chat_order"]
357 this.nickFormat
= data
["nick_format"]
358 this.viewTimes
= data
["view_times"]
359 this.viewTooltips
= data
["view_tooltips"]
360 this.ostentatiousMaster
= data
["ostentatious_master"]
362 // la page de la conversation principale
363 this.pagePrincipale
= 1
366 this.conversations
= data
["conversations"]
367 for (var i
= 0; i
< this.conversations
.length
; i
++)
368 this.conversations
[i
] = {root : this.conversations
[i
].root
, page : 1, reduit : this.conversations
[i
].minimized
}
371 this.majCssSelectionee()
377 * Met à jour les données personne sur serveur.
378 * @param async de manière asynchrone ? défaut = true
379 * @return false si le flush n'a pas pû se faire sinon true
381 Client
.prototype.flush = function(async
)
383 if (async
== undefined)
386 if (!this.authentifie())
389 var thisClient
= this
397 data: this.util
.jsonVersAction(this.getJSONProfile()),
401 if (data
["reply"] == "error")
403 thisClient
.util
.messageDialogue(data
["error_message"])
408 thisClient
.majBulle()
417 Client
.prototype.majMenu = function()
419 var displayType
= "block"
421 $("#menu .admin").css("display", this.ekMaster
? displayType : "none")
423 // met à jour le menu
424 if (this.statut
== euphorik
.Client
.statutType
.auth_registered
)
426 $("#menu .profile").css("display", displayType
).text("profile")
427 $("#menu .logout").css("display", displayType
)
428 $("#menu .register").css("display", "none")
430 else if (this.statut
== euphorik
.Client
.statutType
.auth_not_registered
)
432 $("#menu .profile").css("display", "none")
433 $("#menu .logout").css("display", displayType
)
434 $("#menu .register").css("display", displayType
)
438 $("#menu .profile").css("display", displayType
).text("login")
439 $("#menu .logout").css("display", "none")
440 $("#menu .register").css("display", displayType
)
445 * Met à jour l'affichage des infos bulles en fonction du profile.
447 Client
.prototype.majBulle = function()
449 this.util
.bulleActive
= this.viewTooltips
453 * Met à jour la css sélectionnée, lors du chargement des données.
455 Client
.prototype.majCssSelectionee = function()
457 // extraction du numéro de la css courante
458 var numCssCourante
= this.css
.match(/^.*?\/(\d)\/.*$/)
459 if (numCssCourante
!= null && numCssCourante
[1] != undefined)
461 $("#menuCss option").removeAttr("selected")
462 $("#menuCss option[value=" + numCssCourante
[1]+ "]").attr("selected", "selected")
467 * Change la "class" du logo en fonction du statut de ekMaster.
469 Client
.prototype.majLogo = function()
472 $("#logo").addClass("ekMaster")
474 $("#logo").removeClass("ekMaster")
478 Client
.prototype.slap = function(userId
, raison
)
480 var thisClient
= this
486 data: this.util
.jsonVersAction(
488 "header" : { "action" : "slap", "version" : euphorik
.conf
.versionProtocole
},
489 "cookie" : thisClient
.cookie
,
496 if (data
["reply"] == "error")
497 thisClient
.util
.messageDialogue(data
["error_message"])
502 Client
.prototype.ban = function(userId
, raison
, minutes
)
504 var thisClient
= this
506 // par défaut un ban correspond à 3 jours
507 if (typeof(minutes
) == "undefined")
508 minutes
= euphorik
.conf
.tempsBan
;
514 data: this.util
.jsonVersAction(
516 "header" : { "action" : "ban", "version" : euphorik
.conf
.versionProtocole
},
517 "cookie" : thisClient
.cookie
,
518 "duration" : minutes
,
525 if (data
["reply"] == "error")
526 thisClient
.util
.messageDialogue(data
["error_message"])
531 Client
.prototype.kick = function(userId
, raison
)
533 this.ban(userId
, raison
, euphorik
.conf
.tempsKick
)