MOD avancement dans la Grande Restructuration
[euphorik.git] / js / client.js
diff --git a/js/client.js b/js/client.js
new file mode 100644 (file)
index 0000000..a09819b
--- /dev/null
@@ -0,0 +1,534 @@
+// coding: utf-8\r
+// Copyright 2008 Grégory Burri\r
+//\r
+// This file is part of Euphorik.\r
+//\r
+// Euphorik is free software: you can redistribute it and/or modify\r
+// it under the terms of the GNU General Public License as published by\r
+// the Free Software Foundation, either version 3 of the License, or\r
+// (at your option) any later version.\r
+//\r
+// Euphorik is distributed in the hope that it will be useful,\r
+// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+// GNU General Public License for more details.\r
+//\r
+// You should have received a copy of the GNU General Public License\r
+// along with Euphorik.  If not, see <http://www.gnu.org/licenses/>.\r
+\r
+/*jslint laxbreak:true */\r
+\r
+// les statuts possibes du client\r
+euphorik.Client.statutType = {\r
+   // mode enregistré, peut poster des messages et modifier son profile\r
+   auth_registered : 0,\r
+   // mode identifié, peut poster des messages mais n'a pas accès au profile\r
+   auth_not_registered : 1,\r
+   // mode déconnecté, ne peut pas poster de message\r
+   deconnected : 2\r
+}\r
+\r
+/**\r
+  * Représente l'utilisateur du site.\r
+  */\r
+function Client(util) {\r
+   this.util = util;\r
+   \r
+   this.cookie = null;\r
+   this.regexCookie = /^cookie=([^;]*)/;\r
+   \r
+   // données personnels\r
+   this.resetDonneesPersonnelles();\r
+   \r
+   this.setStatut(euphorik.Client.statutType.deconnected);\r
+   \r
+   // si true alors chaque modification du client est mémorisé sur le serveur\r
+   this.autoflush = $.browser["opera"];\r
+}\r
+\r
+Client.prototype.resetDonneesPersonnelles = function() {\r
+   this.id = 0;\r
+   this.pseudo = euphorik.conf.pseudoDefaut;\r
+   this.login = "";\r
+   this.password = "";\r
+   this.email = "";\r
+   this.css = $("link#cssPrincipale").attr("href");\r
+   this.chatOrder = "reverse";\r
+   this.nickFormat = "nick";\r
+   this.viewTimes = true;\r
+   this.viewTooltips = true;\r
+   this.cookie = undefined;\r
+   \r
+   this.pagePrincipale = 1;\r
+   this.ekMaster = false;\r
+   this.ostentatiousMaster = "light";\r
+   \r
+   // les conversations, une conversation est un objet possédant les propriétés suivantes :\r
+   // - root (entier)\r
+   // - page (entier)\r
+   // - reduit (bool)\r
+   this.conversations = [];\r
+}\r
+\r
+Client.prototype.setCss = function(css) {\r
+   if (this.css == css || css == "") {\r
+      return;\r
+   }\r
+\r
+   this.css = css;\r
+   $("link#cssPrincipale").attr("href", this.css);\r
+   if (this.autoflush) {\r
+      this.flush(true);\r
+   }\r
+}\r
+\r
+Client.prototype.pageSuivante = function(numConv) {\r
+   if (numConv < 0 && this.pagePrincipale > 1) {\r
+      this.pagePrincipale -= 1;\r
+   } else if (this.conversations[numConv].page > 1) {\r
+      this.conversations[numConv].page -= 1;\r
+   }\r
+}\r
+\r
+Client.prototype.pagePrecedente = function(numConv) {\r
+   if (numConv < 0) {\r
+      this.pagePrincipale += 1;\r
+   }\r
+   else {\r
+      this.conversations[numConv].page += 1;\r
+   }\r
+}\r
+\r
+/**\r
+  * Définit la première page pour la conversation donnée.\r
+  * @return true si la page a changé sinon false\r
+  */\r
+Client.prototype.goPremierePage = function(numConv)\r
+{\r
+   if (numConv < 0) {\r
+      if (this.pagePrincipale == 1) {\r
+         return false;\r
+      }\r
+      this.pagePrincipale = 1;\r
+   } else {\r
+      if (this.conversations[numConv].page == 1) {\r
+         return false;\r
+      }\r
+      this.conversations[numConv].page = 1;\r
+   }\r
+   return true;\r
+}\r
+\r
+/**\r
+  * Ajoute une conversation à la vue de l'utilisateur.\r
+  * Le profile de l'utilisateur est directement sauvegardé sur le serveur.\r
+  * @param racines la racine de la conversation (integer)\r
+  * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)\r
+  */\r
+Client.prototype.ajouterConversation = function(racine) {\r
+   // vérification s'il elle n'existe pas déjà\r
+   for (var i = 0; i < this.conversations.length; i++)\r
+      if (this.conversations[i].root == racine)\r
+         return false\r
+   \r
+   this.conversations.push({root : racine, page : 1, reduit : false})\r
+   if (this.autoflush) this.flush(true)\r
+   \r
+   return true\r
+}\r
+\r
+Client.prototype.supprimerConversation = function(num)\r
+{\r
+   if (num < 0 || num >= this.conversations.length) return\r
+   \r
+   // décalage TODO : supprimer le dernier élément \r
+   for (var i = num; i < this.conversations.length - 1; i++)\r
+      this.conversations[i] = this.conversations[i+1]\r
+   this.conversations.pop()\r
+   \r
+   if (this.autoflush) this.flush(true)\r
+}\r
+\r
+Client.prototype.getJSONLogin = function(login, password)\r
+{\r
+   return {\r
+      "header" : { "action" : "authentification", "version" : euphorik.conf.versionProtocole },\r
+      "login" : login,\r
+      "password" : password\r
+   }\r
+}\r
+\r
+Client.prototype.getJSONLoginCookie = function()\r
+{\r
+   return {\r
+      "header" : { "action" : "authentification", "version" : euphorik.conf.versionProtocole },\r
+      "cookie" : this.cookie\r
+   }\r
+}  \r
+\r
+/**\r
+  * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible\r
+  * de s'autentifier avec (login, password).\r
+  */\r
+Client.prototype.getJSONEnregistrement = function(login, password)\r
+{\r
+   var mess = { "header" : { "action" : "register", "version" : euphorik.conf.versionProtocole }}\r
+   \r
+   if (login != undefined && password != undefined)\r
+   {\r
+      mess["login"] = login\r
+      mess["password"] = password\r
+   }\r
+   \r
+   return mess;\r
+}\r
+\r
+Client.prototype.getJSONConversations = function()\r
+{\r
+   var conversations = new Array()\r
+   for (var i = 0; i < this.conversations.length; i++)\r
+      conversations.push({root : this.conversations[i].root, minimized : this.conversations[i].reduit})\r
+   return conversations\r
+}\r
+\r
+Client.prototype.getJSONProfile = function()\r
+{\r
+   return {\r
+      "header" : { "action" : "set_profile", "version" : euphorik.conf.versionProtocole },\r
+      "cookie" : this.cookie,\r
+      "login" : this.login,\r
+      "password" : this.password,\r
+      "nick" : this.pseudo,\r
+      "email" : this.email,\r
+      "css" : this.css,\r
+      "chat_order" : this.chatOrder,\r
+      "nick_format" : this.nickFormat,\r
+      "view_times" : this.viewTimes,\r
+      "view_tooltips" : this.viewTooltips,\r
+      "conversations" : this.getJSONConversations(),\r
+      "ostentatious_master" : this.ostentatiousMaster\r
+   }\r
+}\r
+\r
+/**\r
+  * Renvoie null si pas définit.\r
+  */\r
+Client.prototype.getCookie = function()\r
+{\r
+   var cookie = this.regexCookie.exec(document.cookie)\r
+   if (cookie == null) this.cookie = null\r
+   else this.cookie = cookie[1]\r
+}\r
+\r
+Client.prototype.delCookie = function()\r
+{\r
+   document.cookie = "cookie=; max-age=0"\r
+}\r
+\r
+Client.prototype.setCookie = function()\r
+{\r
+   if (this.cookie == null || this.cookie == undefined)\r
+      return\r
+      \r
+   // ne fonctionne pas sous IE....\r
+   /*document.cookie = "cookie=" + this.cookie + "; max-age="  + (60 * 60 * 24 * 365) */\r
+   \r
+   document.cookie = \r
+      "cookie="+this.cookie+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString()\r
+}\r
+\r
+Client.prototype.authentifie = function()\r
+{\r
+   return this.statut == euphorik.Client.statutType.auth_registered || this.statut == euphorik.Client.statutType.auth_not_registered\r
+}\r
+\r
+Client.prototype.setStatut = function(statut)\r
+{  \r
+   // conversation en "enum" si en "string"\r
+   if (typeof(statut) == "string")\r
+   {\r
+      statut =\r
+         statut == "auth_registered" ?\r
+            euphorik.Client.statutType.auth_registered :\r
+         (statut == "auth_not_registered" ? euphorik.Client.statutType.auth_not_registered : euphorik.Client.statutType.deconnected)\r
+   }   \r
+   \r
+   if (statut == this.statut) return\r
+   \r
+   this.statut = statut   \r
+   this.majMenu()\r
+   this.majLogo()\r
+}\r
+\r
+/**\r
+  * Effectue la connexion vers le serveur.\r
+  * Cette fonction est bloquante tant que la connexion n'a pas été établie.\r
+  * S'il existe un cookie en local on s'authentifie directement avec lui.\r
+  * Si il n'est pas possible de s'authentifier alors on affiche un captcha anti-bot.\r
+  */\r
+Client.prototype.connexionCookie = function()\r
+{\r
+   this.getCookie()\r
+   if (this.cookie == null) return false;\r
+   return this.connexion(this.getJSONLoginCookie())\r
+}\r
+\r
+Client.prototype.connexionLogin = function(login, password)\r
+{\r
+   return this.connexion(this.getJSONLogin(login, password))\r
+}\r
+\r
+Client.prototype.enregistrement = function(login, password)\r
+{ \r
+   if (this.authentifie())\r
+   {\r
+      this.login = login\r
+      this.password = password\r
+      if(this.flush())\r
+      {\r
+         this.setStatut(euphorik.Client.statutType.auth_registered)\r
+         return true\r
+      }\r
+      return false\r
+   }\r
+   else\r
+   {\r
+      return this.connexion(this.getJSONEnregistrement(login, password))\r
+   }\r
+}\r
+\r
+/**\r
+  * Connexion. Réalisé de manière synchrone.\r
+  */\r
+Client.prototype.connexion = function(messageJson)\r
+{\r
+   var thisClient = this\r
+   jQuery.ajax(\r
+      {\r
+         async: false,\r
+         type: "POST",\r
+         url: "request",\r
+         dataType: "json",\r
+         data: this.util.jsonVersAction(messageJson),\r
+         success:\r
+            function(data)\r
+            {\r
+               if (data["reply"] == "error")\r
+               {\r
+                  thisClient.util.messageDialogue(data["error_message"])\r
+                  // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification\r
+                  thisClient.delCookie()\r
+               }\r
+               else\r
+                  thisClient.chargerDonnees(data)\r
+            }\r
+      }\r
+   )\r
+   return this.authentifie()\r
+}\r
+\r
+Client.prototype.deconnexion = function()\r
+{\r
+   this.flush(true)\r
+   this.delCookie()\r
+   this.resetDonneesPersonnelles()\r
+   this.setStatut(euphorik.Client.statutType.deconnected) // deconnexion\r
+}\r
+\r
+Client.prototype.chargerDonnees = function(data)\r
+{\r
+   // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)\r
+   // de l'état ekMaster\r
+   this.ekMaster = data["ek_master"] != undefined ? data["ek_master"] : false\r
+   \r
+   this.setStatut(data["status"]) \r
+   \r
+   if (this.authentifie())\r
+   {\r
+      this.cookie = data["cookie"]\r
+      this.setCookie()\r
+      \r
+      this.id = data["id"]\r
+      this.login = data["login"]\r
+      this.pseudo = data["nick"]\r
+      this.email = data["email"]\r
+      this.setCss(data["css"])\r
+      this.chatOrder = data["chat_order"]\r
+      this.nickFormat = data["nick_format"]\r
+      this.viewTimes = data["view_times"]\r
+      this.viewTooltips = data["view_tooltips"]\r
+      this.ostentatiousMaster = data["ostentatious_master"]\r
+      \r
+      // la page de la conversation principale\r
+      this.pagePrincipale = 1\r
+      \r
+      // les conversations\r
+      this.conversations = data["conversations"]\r
+      for (var i = 0; i < this.conversations.length; i++)\r
+         this.conversations[i] = {root : this.conversations[i].root, page : 1, reduit : this.conversations[i].minimized}\r
+      \r
+      this.majBulle()\r
+      this.majCssSelectionee()\r
+      //this.majLogo()\r
+   }\r
+}\r
+\r
+/**\r
+  * Met à jour les données personne sur serveur.\r
+  * @param async de manière asynchrone ? défaut = true\r
+  * @return false si le flush n'a pas pû se faire sinon true\r
+  */\r
+Client.prototype.flush = function(async)\r
+{\r
+   if (async == undefined)\r
+      async = false\r
+      \r
+   if (!this.authentifie())\r
+      return false\r
+\r
+   var thisClient = this\r
+   var ok = true\r
+   jQuery.ajax(\r
+      {\r
+         async: async,\r
+         type: "POST",\r
+         url: "request",\r
+         dataType: "json",\r
+         data: this.util.jsonVersAction(this.getJSONProfile()),\r
+         success:\r
+            function(data)\r
+            {\r
+               if (data["reply"] == "error")\r
+               {\r
+                  thisClient.util.messageDialogue(data["error_message"])\r
+                  ok = false\r
+               }\r
+               else\r
+               {\r
+                  thisClient.majBulle()\r
+               }\r
+            }\r
+      }\r
+   )\r
+   \r
+   return ok\r
+}\r
+\r
+Client.prototype.majMenu = function()\r
+{\r
+   var displayType = "block"\r
+\r
+   $("#menu .admin").css("display", this.ekMaster ? displayType : "none")\r
+\r
+   // met à jour le menu   \r
+   if (this.statut == euphorik.Client.statutType.auth_registered)\r
+   {\r
+      $("#menu .profile").css("display", displayType).text("profile")\r
+      $("#menu .logout").css("display", displayType)\r
+      $("#menu .register").css("display", "none")\r
+   }\r
+   else if (this.statut == euphorik.Client.statutType.auth_not_registered)\r
+   {\r
+      $("#menu .profile").css("display", "none")\r
+      $("#menu .logout").css("display", displayType)\r
+      $("#menu .register").css("display", displayType)\r
+   }\r
+   else\r
+   {\r
+      $("#menu .profile").css("display", displayType).text("login")\r
+      $("#menu .logout").css("display", "none")\r
+      $("#menu .register").css("display", displayType)\r
+   }\r
+}\r
+\r
+/**\r
+  * Met à jour l'affichage des infos bulles en fonction du profile.\r
+  */\r
+Client.prototype.majBulle = function()\r
+{\r
+   this.util.bulleActive = this.viewTooltips\r
+}\r
+\r
+/**\r
+  * Met à jour la css sélectionnée, lors du chargement des données.\r
+  */\r
+Client.prototype.majCssSelectionee = function()\r
+{\r
+   // extraction du numéro de la css courante\r
+   var numCssCourante = this.css.match(/^.*?\/(\d)\/.*$/)\r
+   if (numCssCourante != null && numCssCourante[1] != undefined)\r
+   {\r
+      $("#menuCss option").removeAttr("selected")\r
+      $("#menuCss option[value=" + numCssCourante[1]+ "]").attr("selected", "selected")\r
+   }\r
+}\r
+\r
+/**\r
+  * Change la "class" du logo en fonction du statut de ekMaster.\r
+  */\r
+Client.prototype.majLogo = function()\r
+{\r
+   if (this.ekMaster)\r
+      $("#logo").addClass("ekMaster")\r
+   else\r
+      $("#logo").removeClass("ekMaster")   \r
+}\r
+\r
+\r
+Client.prototype.slap = function(userId, raison)\r
+{\r
+   var thisClient = this\r
+   \r
+   jQuery.ajax({\r
+      type: "POST",\r
+      url: "request",\r
+      dataType: "json",\r
+      data: this.util.jsonVersAction(\r
+         {\r
+            "header" : { "action" : "slap", "version" : euphorik.conf.versionProtocole },\r
+            "cookie" : thisClient.cookie,\r
+            "user_id" : userId,\r
+            "reason" : raison\r
+         }),\r
+      success: \r
+         function(data)\r
+         {\r
+            if (data["reply"] == "error")\r
+               thisClient.util.messageDialogue(data["error_message"])\r
+         }\r
+   })\r
+}\r
+\r
+Client.prototype.ban = function(userId, raison, minutes)\r
+{\r
+   var thisClient = this\r
+\r
+   // par défaut un ban correspond à 3 jours\r
+   if (typeof(minutes) == "undefined")\r
+      minutes = euphorik.conf.tempsBan;\r
+      \r
+   jQuery.ajax({\r
+      type: "POST",\r
+      url: "request",\r
+      dataType: "json",\r
+      data: this.util.jsonVersAction(\r
+         {\r
+            "header" : { "action" : "ban", "version" : euphorik.conf.versionProtocole },\r
+            "cookie" : thisClient.cookie,\r
+            "duration" : minutes,\r
+            "user_id" : userId,\r
+            "reason" : raison\r
+         }),\r
+      success: \r
+         function(data)\r
+         {\r
+            if (data["reply"] == "error")\r
+               thisClient.util.messageDialogue(data["error_message"])\r
+         }\r
+   })\r
+}\r
+\r
+Client.prototype.kick = function(userId, raison)\r
+{\r
+   this.ban(userId, raison, euphorik.conf.tempsKick)\r
+}\r