Files
bare-operating-system/packages/bare-os-coreutils/lib/irc-dial.js
T
Raven Scott 15afc148d7
Release rolling / release (push) Successful in 9m30s
Updates
2026-08-13 00:13:03 -04:00

103 lines
2.8 KiB
JavaScript

/** Resolve a TCP/TLS socket for /bin/irc without requiring ctx.bareOsTlsConnect. */
function bareIrcTryHostTls(ctx) {
var req = null
try {
if (typeof globalThis.require === 'function') req = globalThis.require
} catch (e) {
req = null
}
if (
!req &&
typeof Bare !== 'undefined' &&
Bare &&
typeof Bare.require === 'function'
) {
req = Bare.require
}
if (!req) return null
var names = ['bare-tls', 'tls', 'node:tls']
var i
for (i = 0; i < names.length; i++) {
try {
var m = req(names[i])
if (m) return m
} catch (e2) {
/* next */
}
}
return null
}
function bareIrcTlsLib(ctx) {
if (ctx && ctx.bare) {
if (ctx.bare.bareTls) return ctx.bare.bareTls
if (ctx.bare.tls) return ctx.bare.tls
}
return bareIrcTryHostTls(ctx)
}
function bareIrcOpenPlain(ctx, spec) {
if (typeof ctx.bareOsTelnetConnect === 'function') {
return ctx.bareOsTelnetConnect(spec.host, spec.port, {})
}
var net =
ctx && ctx.bare && (ctx.bare.bareTcp || ctx.bare.bareNet || ctx.bare.net)
if (net && typeof net.createConnection === 'function') {
return net.createConnection({ host: spec.host, port: spec.port })
}
throw new Error('irc: no plaintext connector (ctx.bareOsTelnetConnect)')
}
function bareIrcOpenTls(ctx, spec) {
var opts = {
host: spec.host,
port: spec.port,
servername: spec.host,
rejectUnauthorized: true
}
if (typeof ctx.bareOsTlsConnect === 'function') {
return ctx.bareOsTlsConnect(spec.host, spec.port, opts)
}
var tls = bareIrcTlsLib(ctx)
if (tls && typeof tls.connect === 'function') {
return tls.connect(opts)
}
if (tls && typeof tls.createConnection === 'function') {
return tls.createConnection(opts)
}
var Sock = tls && (tls.Socket || tls.TLSSocket)
if (typeof Sock === 'function') {
var tcp = bareIrcOpenPlain(ctx, spec)
return new Sock(tcp, { host: spec.host })
}
throw new Error(
'irc: no TLS connector (need ctx.bareOsTlsConnect or ctx.bare.bareTls)'
)
}
function bareIrcDial(ctx, spec) {
if (typeof ctx.bareOsIrcHostAllowed === 'function') {
var gate = ctx.bareOsIrcHostAllowed(spec.host)
if (gate && gate.ok === false) {
throw new Error(
'irc: host blocked (' + (gate.reason || 'policy') + '): ' + spec.host
)
}
}
if (spec.tls === false) {
var plainOk =
typeof ctx.bareOsIrcPlaintextAllowed === 'function'
? ctx.bareOsIrcPlaintextAllowed(spec.host, {
tls: false,
insecurePlain: spec.insecurePlain
})
: { ok: !bareIrcHostLooksLibera(spec.host) || spec.insecurePlain }
if (plainOk && plainOk.ok === false) {
throw new Error('irc: ' + (plainOk.reason || 'plaintext blocked'))
}
return bareIrcOpenPlain(ctx, spec)
}
return bareIrcOpenTls(ctx, spec)
}