summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/irc/ircServerTime.sys.mjs
blob: 14ce2436f3a2c3589dcd87d9564e270b77faae68 (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
/* 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/. */

/*
 * This implements server-time for IRC.
 *   https://ircv3.net/specs/extensions/server-time-3.2
 */

import { ircHandlerPriorities } from "resource:///modules/ircHandlerPriorities.sys.mjs";

function handleServerTimeTag(aMsg) {
  if (aMsg.tagValue) {
    // Normalize leap seconds to the next second before it.
    const time = aMsg.tagValue.replace(/60.\d{3}(?=Z$)/, "59.999");
    aMsg.message.time = Math.floor(Date.parse(time) / 1000);
    aMsg.message.delayed = true;
  }
}

export var tagServerTime = {
  name: "server-time Tags",
  priority: ircHandlerPriorities.DEFAULT_PRIORITY,
  isEnabled() {
    return (
      this._activeCAPs.has("server-time") ||
      this._activeCAPs.has("znc.in/server-time-iso")
    );
  },

  commands: {
    time: handleServerTimeTag,
    "znc.in/server-time-iso": handleServerTimeTag,
  },
};

export var capServerTime = {
  name: "server-time CAP",
  priority: ircHandlerPriorities.DEFAULT_PRIORITY,
  isEnabled: () => true,

  commands: {
    "server-time": function (aMessage) {
      if (
        aMessage.cap.subcommand === "LS" ||
        aMessage.cap.subcommand === "NEW"
      ) {
        this.addCAP("server-time");
        this.sendMessage("CAP", ["REQ", "server-time"]);
      } else if (
        aMessage.cap.subcommand === "ACK" ||
        aMessage.cap.subcommand === "NAK"
      ) {
        this.removeCAP("server-time");
      } else {
        return false;
      }
      return true;
    },
    "znc.in/server-time-iso": function (aMessage) {
      // Only request legacy server time CAP if the standard one is not available.
      if (
        (aMessage.cap.subcommand === "LS" ||
          aMessage.cap.subcommand === "NEW") &&
        !this._availableCAPs.has("server-time")
      ) {
        this.addCAP("znc.in/server-time-iso");
        this.sendMessage("CAP", ["REQ", "znc.in/server-time-iso"]);
      } else if (
        aMessage.cap.subcommand === "ACK" ||
        aMessage.cap.subcommand === "NAK"
      ) {
        this.removeCAP("znc.in/server-time-iso");
      } else {
        return false;
      }
      return true;
    },
  },
};