summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/irc/ircUtils.sys.mjs
blob: 190ed8f83000a4b644727987351776045cdd7834 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* 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 { 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/irc.properties")
);

XPCOMUtils.defineLazyGetter(lazy, "TXTToHTML", function () {
  let cs = Cc["@mozilla.org/txttohtmlconv;1"].getService(Ci.mozITXTToHTMLConv);
  return aTXT => cs.scanTXT(aTXT, cs.kEntities);
});

// The timespan after which we consider LIST roomInfo to be stale.
export var kListRefreshInterval = 12 * 60 * 60 * 1000; // 12 hours.

/*
 * The supported formatting control characters, as described in
 * http://www.invlogic.com/irc/ctcp.html#3.11
 * If a string is given, it will replace the control character; if null is
 * given, the current HTML tag stack will be closed; if a function is given,
 * it expects two parameters:
 *  aStack  The ordered list of open HTML tags.
 *  aInput  The current input string.
 * There are three output values returned in an array:
 *  The new ordered list of open HTML tags.
 *  The new text output to append.
 *  The number of characters (from the start of the input string) that the
 *  function handled.
 */
var CTCP_TAGS = {
  "\x02": "b", // \002, ^B, Bold
  "\x16": "i", // \026, ^V, Reverse or Inverse (Italics)
  "\x1D": "i", // \035, ^], Italics (mIRC)
  "\x1F": "u", // \037, ^_, Underline
  "\x03": mIRCColoring, // \003, ^C, Coloring
  "\x0F": null, // \017, ^O, Clear all formatting
};

// Generate an expression that will search for any of the control characters.
var CTCP_TAGS_EXP = new RegExp("[" + Object.keys(CTCP_TAGS).join("") + "]");

// Remove all CTCP formatting characters.
export function ctcpFormatToText(aString) {
  let next,
    input = aString,
    output = "",
    length;

  while ((next = CTCP_TAGS_EXP.exec(input))) {
    if (next.index > 0) {
      output += input.substr(0, next.index);
    }
    // We assume one character will be stripped.
    length = 1;
    let tag = CTCP_TAGS[input[next.index]];
    // If the tag is a function, calculate how many characters are handled.
    if (typeof tag == "function") {
      [, , length] = tag([], input.substr(next.index));
    }

    // Avoid infinite loops.
    length = Math.max(1, length);
    // Skip to after the last match.
    input = input.substr(next.index + length);
  }
  // Append the unmatched bits before returning the output.
  return output + input;
}

function openStack(aStack) {
  return aStack.map(aTag => "<" + aTag + ">").join("");
}

// Close the tags in the opposite order they were opened.
function closeStack(aStack) {
  return aStack
    .reverse()
    .map(aTag => "</" + aTag.split(" ", 1) + ">")
    .join("");
}

/**
 * Convert a string from CTCP escaped formatting to HTML markup.
 *
 * @param aString the string with CTCP formatting to parse
 * @returns The HTML output string
 */
export function ctcpFormatToHTML(aString) {
  let next,
    stack = [],
    input = lazy.TXTToHTML(aString),
    output = "",
    newOutput,
    length;

  while ((next = CTCP_TAGS_EXP.exec(input))) {
    if (next.index > 0) {
      output += input.substr(0, next.index);
    }
    length = 1;
    let tag = CTCP_TAGS[input[next.index]];
    if (tag === null) {
      // Clear all formatting.
      output += closeStack(stack);
      stack = [];
    } else if (typeof tag == "function") {
      [stack, newOutput, length] = tag(stack, input.substr(next.index));
      output += newOutput;
    } else {
      let offset = stack.indexOf(tag);
      if (offset == -1) {
        // Tag not found; open new tag.
        output += "<" + tag + ">";
        stack.push(tag);
      } else {
        // Tag found; close existing tag (and all tags after it).
        output += closeStack(stack.slice(offset));
        // Reopen the tags that came after it.
        output += openStack(stack.slice(offset + 1));
        // Remove the tag from the stack.
        stack.splice(offset, 1);
      }
    }

    // Avoid infinite loops.
    length = Math.max(1, length);
    // Skip to after the last match.
    input = input.substr(next.index + length);
  }
  // Return unmatched bits and close any open tags at the end.
  return output + input + closeStack(stack);
}

// mIRC colors are defined at http://www.mirc.com/colors.html.
// This expression matches \003<one or two digits>[,<one or two digits>].
// eslint-disable-next-line no-control-regex
var M_IRC_COLORS_EXP = /^\x03(?:(\d\d?)(?:,(\d\d?))?)?/;
var M_IRC_COLOR_MAP = {
  0: "white",
  1: "black",
  2: "navy", // blue (navy)
  3: "green",
  4: "red",
  5: "maroon", // brown (maroon)
  6: "purple",
  7: "orange", // orange (olive)
  8: "yellow",
  9: "lime", // light green (lime)
  10: "teal", // teal (a green/blue cyan)
  11: "aqua", // light cyan (cyan) (aqua)
  12: "blue", // light blue (royal)",
  13: "fuchsia", // pink (light purple) (fuchsia)
  14: "grey",
  15: "silver", // light grey (silver)
  99: "transparent",
};

function mIRCColoring(aStack, aInput) {
  function getColor(aKey) {
    let key = aKey;
    // Single digit numbers can (must?) be prefixed by a zero.
    if (key.length == 2 && key[0] == "0") {
      key = key[1];
    }

    if (M_IRC_COLOR_MAP.hasOwnProperty(key)) {
      return M_IRC_COLOR_MAP[key];
    }

    return null;
  }

  let matches,
    stack = aStack,
    input = aInput,
    output = "",
    length = 1;

  if ((matches = M_IRC_COLORS_EXP.exec(input))) {
    let format = ["font"];

    // Only \003 was found with no formatting digits after it, close the
    // first open font tag.
    if (!matches[1]) {
      // Find the first font tag.
      let offset = stack.map(aTag => aTag.indexOf("font") === 0).indexOf(true);

      // Close all tags from the first font tag on.
      output = closeStack(stack.slice(offset));
      // Remove the font tags from the stack.
      stack = stack.filter(aTag => aTag.indexOf("font"));
      // Reopen the other tags.
      output += openStack(stack.slice(offset));
    } else {
      // Otherwise we have a match and are setting new colors.
      // The foreground color.
      let color = getColor(matches[1]);
      if (color) {
        format.push('color="' + color + '"');
      }

      // The background color.
      if (matches[2]) {
        let color = getColor(matches[2]);
        if (color) {
          format.push('background="' + color + '"');
        }
      }

      if (format.length > 1) {
        let tag = format.join(" ");
        output = "<" + tag + ">";
        stack.push(tag);
        length = matches[0].length;
      }
    }
  }

  return [stack, output, length];
}

// Print an error message into a conversation, optionally mark the conversation
// as not joined and/or not rejoinable.
export function conversationErrorMessage(
  aAccount,
  aMessage,
  aError,
  aJoinFailed = false,
  aRejoinable = true
) {
  let conv = aAccount.getConversation(aMessage.params[1]);
  conv.writeMessage(
    aMessage.origin,
    lazy._(aError, aMessage.params[1], aMessage.params[2] || undefined),
    {
      error: true,
      system: true,
    }
  );
  delete conv._pendingMessage;

  // Channels have a couple extra things that can be done to them.
  if (aAccount.isMUCName(aMessage.params[1])) {
    // If a value for joining is explicitly given, mark it.
    if (aJoinFailed) {
      conv.joining = false;
    }
    // If the conversation cannot be rejoined automatically, delete
    // chatRoomFields.
    if (!aRejoinable) {
      delete conv.chatRoomFields;
    }
  }

  return true;
}

/**
 * Display a PRIVMSG or NOTICE in a conversation.
 *
 * @param {ircAccount} aAccount - The current account.
 * @param {ircMessage} aMessage - The IRC message to display, provides the IRC
 *  tags, conversation name, and sender.
 * @param {object} aExtraParams - (Extra) parameters to pass to ircConversation.writeMessage.
 * @param {string|null} aText - The text to display, defaults to the second parameter
 *  on aMessage.
 * @returns {boolean} True if the message was sent successfully.
 */
export function displayMessage(aAccount, aMessage, aExtraParams, aText) {
  let params = { tags: aMessage.tags, ...aExtraParams };
  // If the the message is from our nick, it is outgoing to the conversation it
  // is targeting. Otherwise, the message is incoming, but could be for a
  // private message or a channel.
  //
  // Note that the only time it is expected to receive a message from us is if
  // the echo-message capability is enabled.
  let convName;
  if (
    aAccount.normalizeNick(aMessage.origin) ==
    aAccount.normalizeNick(aAccount._nickname)
  ) {
    params.outgoing = true;
    // The conversation name is who it is being sent to.
    convName = aMessage.params[0];
  } else {
    params.incoming = true;
    // If the target is a MUC name, use the target as the conversation name.
    // Otherwise, this is a private message: use the sender as the conversation
    // name.
    convName = aAccount.isMUCName(aMessage.params[0])
      ? aMessage.params[0]
      : aMessage.origin;
  }
  aAccount
    .getConversation(convName)
    .writeMessage(aMessage.origin, aText || aMessage.params[1], params);
  return true;
}