MOD begining of #100 (code broken)
[euphorik.git] / js / chat / commandes.js
diff --git a/js/chat/commandes.js b/js/chat/commandes.js
new file mode 100644 (file)
index 0000000..dbcc76a
--- /dev/null
@@ -0,0 +1,120 @@
+// coding: utf-8
+// Copyright 2008 Grégory Burri
+//
+// This file is part of Euphorik.
+//
+// Euphorik is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Euphorik is distributed in the hope that it will be useful,
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Euphorik.  If not, see <http://www.gnu.org/licenses/>.
+ /*jslint laxbreak:true */
+
+/**
+  * Permet d'executer des commandes tapées par l'utilisateur.
+  * les commandes sont entrées directement dans la ligne de saisie du message sous la forme :
+  *  /<commande> <paramètres>
+  * 
+  * Voici les commandes supportées :
+  *  /nick <nouveau nick>
+  *  Modifie le pseudo courant
+  */
+euphorik.Commandes = function(client, pageMinichat, util, formater) {
+   var thisCommandes = this;
+
+   this.client = client;
+   this.pageMinichat = pageMinichat;
+   this.util = util;
+   this.formater = formater;
+   
+   // construction du texte d'aide (liste des commandes) de manière statique
+   this.texteAide = "<div id=\"aideCommandes\"><h1>Commandes</h1><ul>";
+   objectEach(
+      euphorik.Commandes.liste,
+      function(nom, commande) {
+         thisCommandes.texteAide += "<li><span class=\"usage\">" + thisCommandes.formater.traitementComplet(commande.usage) + "</span> : " + thisCommandes.formater.traitementComplet(commande.description) + "</li>";
+      }
+   );
+   this.texteAide += "</ul></div>";
+};
+
+euphorik.Commandes.statut = {ok : 0, pas_une_commande : 1, erreur_commande : 2};
+
+euphorik.Commandes.liste = {
+   "nick" : {
+      description : "Change le pseudo courant",
+      usage : "/nick <nouveau pseudo>",
+      exec : function(args, client) {
+         
+         if (args.length === 0) {
+            return [euphorik.Commandes.statut.erreur_commande, 'Utilisation de la commande : ' + this.usage];
+         }
+   
+         client.pseudo = args[0];
+         $("form#posterMessage input.pseudo").val(client.pseudo);
+   
+         return [euphorik.Commandes.statut.ok, ''];
+      }
+   },
+   "cpf" : {
+      description : "Envoie le message \"C'est pas faux\"",
+      usage : "/cpf",
+      exec : function(args, client, pageMinichat) {
+         pageMinichat.envoyerMessage("C'est pas faux");
+         return [euphorik.Commandes.statut.ok, ''];
+      }
+   },
+   "osef" : {
+      description : "Envoie le message \"On s'en fout !\"",
+      usage : "/osef",
+      exec : function(args, client, pageMinichat) {
+         pageMinichat.envoyerMessage("On s'en fout !");
+         return [euphorik.Commandes.statut.ok, ''];
+      }
+   }
+};
+
+/**
+  * Execute une commande.
+  * @return [statut, message], 'message' n'est utilisé que pour le statut 'erreur_commande'.
+  */
+euphorik.Commandes.prototype.exec = function(chaine) {
+   chaine = chaine.trim();
+   
+   var fragments = chaine.split(/\s+/);
+   if (fragments.length === 0 || fragments[0].charAt(0) != '/') {
+      return [euphorik.Commandes.statut.pas_une_commande, ''];
+   }
+   
+   var nomCommande = fragments[0].slice(1);
+   var args = fragments.slice(1);
+   
+   if (nomCommande === "") {
+      return [euphorik.Commandes.statut.erreur_commande, 'La commande est vide'];
+   }
+   // commandes spéciales pour afficher l'aide : "?", "h", "help", "aide"
+   if (nomCommande === "?" || nomCommande === "h" || nomCommande === "help" || nomCommande === "aide") {
+      this.util.messageDialog(
+         this.texteAide,
+         euphorik.Util.messageType.informatif,
+         {"fermer" : function(){}},
+         false,
+         -1
+      );
+      return [euphorik.Commandes.statut.ok, ''];
+   }
+   
+   if (euphorik.Commandes.liste.hasOwnProperty(nomCommande)) {
+      return euphorik.Commandes.liste[nomCommande].exec(args, this.client, this.pageMinichat);
+   }
+   
+   return [euphorik.Commandes.statut.erreur_commande, 'La commande /' + nomCommande + ' est inconnue'];
+};