0a2c38ef5f86a2b51f5137c2161946f03b2bf7da
[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 /**
37 * Execute une commande.
38 * @return [statut, message], 'message' n'est utilisé que pour le statut 'erreur_commande'.
39 */
40 euphorik.Commandes.prototype.exec = function(chaine) {
41 chaine = chaine.trim();
42
43 var fragments = chaine.split(/\s*/);
44 if (fragments.length === 0 || fragments[0].charAt(0) != '/') {
45 return [euphorik.Commandes.statut.pas_une_commande, ''];
46 }
47
48 var commande = fragments[0].slice(1);
49 var args = fragments.slice(1);
50
51 switch (commande) {
52 case "" : return [euphorik.Commandes.statut.erreur_commande, 'La commande est vide'];
53 case "nick" : return this.commandeNick(args);
54 }
55
56 return [euphorik.Commandes.statut.erreur_commande, 'La commande \\' + commande + ' est inconnue'];
57 };
58
59 euphorik.Commandes.prototype.commandeNick = function(args) {
60 if (args.length === 0) {
61 return [euphorik.Commandes.statut.erreur_commande, 'Utilisation de la commande : "\nick <nouveau pseudo>"'];
62 }
63
64 this.client.pseudo = args[0];
65 $("form#posterMessage input.pseudo").val(this.client.pseudo);
66
67 return [euphorik.Commandes.statut.ok, ''];
68 };