Remove the weekly troll.
[euphorik.git] / js / chat / 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 nick courant
29 */
30 euphorik.Commandes = function(client, pageMinichat, util, formater) {
31 var thisCommandes = this;
32
33 this.client = client;
34 this.pageMinichat = pageMinichat;
35 this.util = util;
36 this.formater = formater;
37
38 // construction du texte d'aide (liste des commandes) de manière statique
39 this.texteAide = "<div id=\"aideCommandes\"><h1>Commandes</h1><ul>";
40 objectEach(
41 euphorik.Commandes.liste,
42 function(name, commande) {
43 thisCommandes.texteAide += "<li><span class=\"usage\">" + thisCommandes.formater.completeProcessing(commande.usage) + "</span> : " + thisCommandes.formater.completeProcessing(commande.description) + "</li>";
44 }
45 );
46 this.texteAide += "</ul></div>";
47 };
48
49 euphorik.Commandes.statut = {ok : 0, pas_une_commande : 1, erreur_commande : 2};
50
51 euphorik.Commandes.liste = {
52 "nick" : {
53 description : "Change le nick courant",
54 usage : "/nick <nouveau nick>",
55 exec : function(args, client) {
56
57 if (args.length === 0) {
58 return [euphorik.Commandes.statut.erreur_commande, 'Utilisation de la commande : ' + this.usage];
59 }
60
61 client.nick = args[0];
62 $("form#posterMessage input.nick").val(client.nick);
63
64 return [euphorik.Commandes.statut.ok, ''];
65 }
66 },
67 "cpf" : {
68 description : "Envoie le message \"C'est pas faux\"",
69 usage : "/cpf",
70 exec : function(args, client, pageMinichat) {
71 pageMinichat.envoyerMessage("C'est pas faux");
72 return [euphorik.Commandes.statut.ok, ''];
73 }
74 },
75 "osef" : {
76 description : "Envoie le message \"On s'en fout !\"",
77 usage : "/osef",
78 exec : function(args, client, pageMinichat) {
79 pageMinichat.envoyerMessage("On s'en fout !");
80 return [euphorik.Commandes.statut.ok, ''];
81 }
82 }
83 };
84
85 /**
86 * Execute une commande.
87 * @return [statut, message], 'message' n'est utilisé que pour le statut 'erreur_commande'.
88 */
89 euphorik.Commandes.prototype.exec = function(chaine) {
90 chaine = chaine.trim();
91
92 var fragments = chaine.split(/\s+/);
93 if (fragments.length === 0 || fragments[0].charAt(0) != '/') {
94 return [euphorik.Commandes.statut.pas_une_commande, ''];
95 }
96
97 var commandName = fragments[0].slice(1);
98 var args = fragments.slice(1);
99
100 if (commandName === "") {
101 return [euphorik.Commandes.statut.erreur_commande, 'La commande est vide'];
102 }
103 // commandes spéciales pour afficher l'aide : "?", "h", "help", "aide"
104 if (commandName === "?" || commandName === "h" || commandName === "help" || commandName === "aide") {
105 this.util.messageDialog(
106 this.texteAide,
107 euphorik.Util.messageType.informatif,
108 {"close" : function(){}},
109 false,
110 -1
111 );
112 return [euphorik.Commandes.statut.ok, ''];
113 }
114
115 if (euphorik.Commandes.liste.hasOwnProperty(commandName)) {
116 return euphorik.Commandes.liste[commandName].exec(args, this.client, this.pageMinichat);
117 }
118
119 return [euphorik.Commandes.statut.erreur_commande, 'La commande /' + commandName + ' est inconnue'];
120 };