4d25a4317de20775621590a00f23b9f3cf7715f2
[euphorik.git] / js / pageMinichat / commandes.js
1 // coding: utf-8
2 // Copyright 2008 Grégory Burri
3 //
4 // This file is part of Euphorik.
5 //
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.
10 //
11 // Euphorik is distributed in the hope that it will be useful,
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // GNU General Public License for more details.
15 //
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/>.
18
19 /*jslint laxbreak:true */
20
21 /**
22 * Permet d'executer des commandes tapées par l'utilisateur.
23 * les commandes sont entrées directement dans la ligne de saisie du message sous la forme :
24 * /<commande> <paramètres>
25 *
26 * Voici les commandes supportées :
27 * /nick <nouveau nick>
28 * Modifie le pseudo courant
29 */
30 euphorik.Commandes = function(client) {
31 this.client = client;
32 };
33
34 euphorik.Commandes.statut = {ok : 0, pas_une_commande : 1, erreur_commande : 2};
35
36 euphorik.Commandes.liste = {
37 "nick" : {
38 usage : "\\nick <nouveau pseudo",
39 exec : function(args, client) {
40
41 if (args.length === 0) {
42 return [euphorik.Commandes.statut.erreur_commande, 'Utilisation de la commande : ' + this.usage];
43 }
44
45 client.pseudo = args[0];
46 $("form#posterMessage input.pseudo").val(client.pseudo);
47
48 return [euphorik.Commandes.statut.ok, ''];
49 }
50 }
51 };
52
53 /**
54 * Execute une commande.
55 * @return [statut, message], 'message' n'est utilisé que pour le statut 'erreur_commande'.
56 */
57 euphorik.Commandes.prototype.exec = function(chaine) {
58 chaine = chaine.trim();
59
60 var fragments = chaine.split(/\s+/);
61 if (fragments.length === 0 || fragments[0].charAt(0) != '/') {
62 return [euphorik.Commandes.statut.pas_une_commande, ''];
63 }
64 console.log(fragments)
65
66 var nomCommande = fragments[0].slice(1);
67 var args = fragments.slice(1);
68
69 if (nomCommande === "") {
70 return [euphorik.Commandes.statut.erreur_commande, 'La commande est vide'];
71 }
72
73 if (euphorik.Commandes.liste.hasOwnProperty(nomCommande)) {
74 return euphorik.Commandes.liste[nomCommande].exec(args, this.client);
75 }
76
77 return [euphorik.Commandes.statut.erreur_commande, 'La commande \\' + nomCommande + ' est inconnue'];
78 };