summaryrefslogtreecommitdiffstats
path: root/comm/chat/components/src/imCommands.sys.mjs
blob: d28bd9a59261e395dab2f0decc81562420f21db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { IMServices } from "resource:///modules/IMServices.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import { l10nHelper } from "resource:///modules/imXPCOMUtils.sys.mjs";

const lazy = {};

XPCOMUtils.defineLazyGetter(lazy, "_", () =>
  l10nHelper("chrome://chat/locale/commands.properties")
);

export function CommandsService() {}
CommandsService.prototype = {
  initCommands() {
    this._commands = {};
    // The say command is directly implemented in the UI layer, but has a
    // dummy command registered here so it shows up as a command (e.g. when
    // using the /help command).
    this.registerCommand({
      name: "say",
      get helpString() {
        return lazy._("sayHelpString");
      },
      usageContext: Ci.imICommand.CMD_CONTEXT_ALL,
      priority: Ci.imICommand.CMD_PRIORITY_HIGH,
      run(aMsg, aConv) {
        throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
      },
    });

    this.registerCommand({
      name: "raw",
      get helpString() {
        return lazy._("rawHelpString");
      },
      usageContext: Ci.imICommand.CMD_CONTEXT_ALL,
      priority: Ci.imICommand.CMD_PRIORITY_DEFAULT,
      run(aMsg, aConv) {
        let conv = IMServices.conversations.getUIConversation(aConv);
        if (!conv) {
          return false;
        }
        conv.sendMsg(aMsg);
        return true;
      },
    });

    this.registerCommand({
      // Reference the command service so we can use the internal properties
      // directly.
      cmdSrv: this,

      name: "help",
      get helpString() {
        return lazy._("helpHelpString");
      },
      usageContext: Ci.imICommand.CMD_CONTEXT_ALL,
      priority: Ci.imICommand.CMD_PRIORITY_DEFAULT,
      run(aMsg, aConv) {
        aMsg = aMsg.trim();
        let conv = IMServices.conversations.getUIConversation(aConv);
        if (!conv) {
          return false;
        }

        // Handle when no command is given, list all possible commands that are
        // available for this conversation (alphabetically).
        if (!aMsg) {
          let commands = this.cmdSrv.listCommandsForConversation(aConv);
          if (!commands.length) {
            return false;
          }

          // Concatenate the command names (separated by a comma and space).
          let cmds = commands
            .map(aCmd => aCmd.name)
            .sort()
            .join(", ");
          let message = lazy._("commands", cmds);

          // Display the message
          conv.systemMessage(message);
          return true;
        }

        // A command name was given, find the commands that match.
        let cmdArray = this.cmdSrv._findCommands(aConv, aMsg);

        if (!cmdArray.length) {
          // No command that matches.
          let message = lazy._("noCommand", aMsg);
          conv.systemMessage(message);
          return true;
        }

        // Only show the help for the one of the highest priority.
        let cmd = cmdArray[0];

        let text = cmd.helpString;
        if (!text) {
          text = lazy._("noHelp", cmd.name);
        }

        // Display the message.
        conv.systemMessage(text);
        return true;
      },
    });

    // Status commands
    let status = {
      back: "AVAILABLE",
      away: "AWAY",
      busy: "UNAVAILABLE",
      dnd: "UNAVAILABLE",
      offline: "OFFLINE",
    };
    for (let cmd in status) {
      let statusValue = Ci.imIStatusInfo["STATUS_" + status[cmd]];
      this.registerCommand({
        name: cmd,
        get helpString() {
          return lazy._("statusCommand", this.name, lazy._(this.name));
        },
        usageContext: Ci.imICommand.CMD_CONTEXT_ALL,
        priority: Ci.imICommand.CMD_PRIORITY_HIGH,
        run(aMsg) {
          IMServices.core.globalUserStatus.setStatus(statusValue, aMsg);
          return true;
        },
      });
    }
  },
  unInitCommands() {
    delete this._commands;
  },

  registerCommand(aCommand, aPrplId) {
    let name = aCommand.name;
    if (!name) {
      throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
    }

    if (!this._commands.hasOwnProperty(name)) {
      this._commands[name] = {};
    }
    this._commands[name][aPrplId || ""] = aCommand;
  },
  unregisterCommand(aCommandName, aPrplId) {
    if (this._commands.hasOwnProperty(aCommandName)) {
      let prplId = aPrplId || "";
      let commands = this._commands[aCommandName];
      if (commands.hasOwnProperty(prplId)) {
        delete commands[prplId];
      }
      if (!Object.keys(commands).length) {
        delete this._commands[aCommandName];
      }
    }
  },
  listCommandsForConversation(aConversation) {
    let result = [];
    let prplId = aConversation && aConversation.account.protocol.id;
    for (let name in this._commands) {
      let commands = this._commands[name];
      if (commands.hasOwnProperty("")) {
        result.push(commands[""]);
      }
      if (prplId && commands.hasOwnProperty(prplId)) {
        result.push(commands[prplId]);
      }
    }
    if (aConversation) {
      result = result.filter(this._usageContextFilter(aConversation));
    }
    return result;
  },
  // List only the commands for a protocol (excluding the global commands).
  listCommandsForProtocol(aPrplId) {
    if (!aPrplId) {
      throw new Error("You must provide a prpl ID.");
    }

    let result = [];
    for (let name in this._commands) {
      let commands = this._commands[name];
      if (commands.hasOwnProperty(aPrplId)) {
        result.push(commands[aPrplId]);
      }
    }
    return result;
  },
  _usageContextFilter(aConversation) {
    let usageContext =
      Ci.imICommand["CMD_CONTEXT_" + (aConversation.isChat ? "CHAT" : "IM")];
    return c => c.usageContext & usageContext;
  },
  _findCommands(aConversation, aName) {
    let prplId = null;
    if (aConversation) {
      let account = aConversation.account;
      if (account.connected) {
        prplId = account.protocol.id;
      }
    }

    let commandNames;
    // If there is an exact match for the given command name,
    // don't look at any other commands.
    if (this._commands.hasOwnProperty(aName)) {
      commandNames = [aName];
    } else {
      // Otherwise, check if there is a partial match.
      commandNames = Object.keys(this._commands).filter(command =>
        command.startsWith(aName)
      );
    }

    // If a single full command name matches the given (partial)
    // command name, return the results for that command name. Otherwise,
    // return an empty array (don't assume a certain command).
    let cmdArray = [];
    for (let commandName of commandNames) {
      let matches = [];

      // Get the 2 possible commands (the global and the proto specific).
      let commands = this._commands[commandName];
      if (commands.hasOwnProperty("")) {
        matches.push(commands[""]);
      }
      if (prplId && commands.hasOwnProperty(prplId)) {
        matches.push(commands[prplId]);
      }

      // Remove the commands that can't apply in this context.
      if (aConversation) {
        matches = matches.filter(this._usageContextFilter(aConversation));
      }

      if (!matches.length) {
        continue;
      }

      // If we have found a second matching command name, return the empty array.
      if (cmdArray.length) {
        return [];
      }

      cmdArray = matches;
    }

    // Sort the matching commands by priority before returning the array.
    return cmdArray.sort((a, b) => b.priority - a.priority);
  },
  executeCommand(aMessage, aConversation, aReturnedConv) {
    if (!aMessage) {
      throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
    }

    let matchResult;
    if (
      aMessage[0] != "/" ||
      !(matchResult = /^\/([a-z0-9]+)(?: |$)([\s\S]*)/.exec(aMessage))
    ) {
      return false;
    }

    let [, name, args] = matchResult;

    let cmdArray = this._findCommands(aConversation, name);
    if (!cmdArray.length) {
      return false;
    }

    // cmdArray contains commands sorted by priority, attempt to apply
    // them in order until one succeeds.
    if (!cmdArray.some(aCmd => aCmd.run(args, aConversation, aReturnedConv))) {
      // If they all failed, print help message.
      this.executeCommand("/help " + name, aConversation);
    }
    return true;
  },

  QueryInterface: ChromeUtils.generateQI(["imICommandsService"]),
  classDescription: "Commands",
};