MOD avancement dans la Grande Restructuration
[euphorik.git] / js / pageMinichat / commandes.js
diff --git a/js/pageMinichat/commandes.js b/js/pageMinichat/commandes.js
new file mode 100644 (file)
index 0000000..0a2c38e
--- /dev/null
@@ -0,0 +1,68 @@
+// 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) {
+   this.client = client;
+};
+
+euphorik.Commandes.statut = {ok : 0, pas_une_commande : 1, erreur_commande : 2};
+
+/**
+  * 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 commande = fragments[0].slice(1);
+   var args = fragments.slice(1);
+   
+   switch (commande) {
+      case "" : return [euphorik.Commandes.statut.erreur_commande, 'La commande est vide'];
+      case "nick" : return this.commandeNick(args);
+   }
+   
+   return [euphorik.Commandes.statut.erreur_commande, 'La commande \\' + commande + ' est inconnue'];
+};
+
+euphorik.Commandes.prototype.commandeNick = function(args) {
+   if (args.length === 0) {
+      return [euphorik.Commandes.statut.erreur_commande, 'Utilisation de la commande : "\nick <nouveau pseudo>"'];
+   }
+   
+   this.client.pseudo = args[0];
+   $("form#posterMessage input.pseudo").val(this.client.pseudo);
+   
+   return [euphorik.Commandes.statut.ok, ''];
+};