<commit_before>import Bluebird from "bluebird";
import Datastore from "nedb";
import extend from "extend";
import http from "http";
import https from "https";
import { AppServiceRegistration, RoomBridgeStore, UserBridgeStore } from "matrix-appservice-bridge";
import { IrcBridge } from "./bridge/IrcBridge";
import { IrcServer } from "./irc/IrcServer";
import stats from "./config/stats";
import ident from "./irc/Ident";
import * as logging from "./logging";
import { LoggerInstance } from "winston";

const log = logging.get("main");

// We set this to 1000 by default now to set a default, and to ensure we're the first
// one to load the libraries in. Later on in runBridge we actually define the real limit.
http.globalAgent.maxSockets = 1000;
https.globalAgent.maxSockets = 1000;

process.on("unhandledRejection", (reason?: Error) => {
    log.error((reason ? reason.stack : undefined) ||  "No reason given");
});

const _toServer = (domain: string, serverConfig: any, homeserverDomain: string) => {
    // set server config defaults
    if (serverConfig.dynamicChannels.visibility) {
        throw new Error(
            `[DEPRECATED] Use of the config field dynamicChannels.visibility
            is deprecated. Use dynamicChannels.published, dynamicChannels.joinRule
            and dynamicChannels.createAlias instead.`
        );
    }
    return new IrcServer(
        domain, extend(true, IrcServer.DEFAULT_CONFIG, serverConfig), homeserverDomain
    );
};

export async function generateRegistration(reg: AppServiceRegistration, config: any) {
    let asToken;

    if (!reg.getSenderLocalpart()) {
        reg.setSenderLocalpart(IrcBridge.DEFAULT_LOCALPART);
    }
    reg.setId(AppServiceRegistration.generateToken());
    reg.setHomeserverToken(AppServiceRegistration.generateToken());
    reg.setAppServiceToken(asToken || AppServiceRegistration.generateToken());

    // Disable rate limiting to allow large numbers of requests when many IRC users
    // connect, for example on startup.
    reg.setRateLimited(false);

    // Set protocols to IRC, so that the bridge appears in the list of
    // thirdparty protocols
    reg.setProtocols(["irc"]);

    const serverDomains = Object.keys(config.ircService.servers);
    serverDomains.sort().forEach(function(domain) {
        let server = _toServer(domain, config.ircService.servers[domain], config.homeserver.domain);
        server.getHardCodedRoomIds().sort().forEach(function(roomId) {
            reg.addRegexPattern("rooms", roomId, false);
        });
        // add an alias pattern for servers who want aliases exposed.
        if (server.createsDynamicAliases()) {
            reg.addRegexPattern("aliases", server.getAliasRegex(), true);
        }
        reg.addRegexPattern("users", server.getUserRegex(), true);
    });

    return reg;
}

export async function runBridge(port: number, config: any, reg: AppServiceRegistration, isDBInMemory: boolean = false) {
    // configure global stuff for the process
    if (config.ircService.logging) {
        logging.configure(config.ircService.logging);
        logging.setUncaughtExceptionLogger(log as LoggerInstance);
    }
    if (config.ircService.statsd.hostname) {
        stats.setEndpoint(config.ircService.statsd);
    }
    if (config.ircService.ident.enabled) {
        ident.configure(config.ircService.ident);
        ident.run();
    }

    const maxSockets = (config["advanced"] || {})["maxHttpSockets"] || 1000;
    require("http").globalAgent.maxSockets = maxSockets;
    require("https").globalAgent.maxSockets = maxSockets;

    // run the bridge
    const ircBridge = new IrcBridge(config, reg);
    const engine = config.database ? config.database.engine : "nedb";
    // Use in-memory DBs
    if (isDBInMemory) {
        ircBridge.getAppServiceBridge().opts.roomStore = new RoomBridgeStore(new Datastore());
        ircBridge.getAppServiceBridge().opts.userStore = new UserBridgeStore(new Datastore());
    }
    else if (engine === "postgres") {
        // Enforce these not to be created<commit_msg>Add deprecation message for statsd<commit_after>import Bluebird from "bluebird";
import Datastore from "nedb";
import extend from "extend";
import http from "http";
import https from "https";
import { AppServiceRegistration, RoomBridgeStore, UserBridgeStore } from "matrix-appservice-bridge";
import { IrcBridge } from "./bridge/IrcBridge";
import { IrcServer } from "./irc/IrcServer";
import stats from "./config/stats";
import ident from "./irc/Ident";
import * as logging from "./logging";
import { LoggerInstance } from "winston";

const log = logging.get("main");

// We set this to 1000 by default now to set a default, and to ensure we're the first
// one to load the libraries in. Later on in runBridge we actually define the real limit.
http.globalAgent.maxSockets = 1000;
https.globalAgent.maxSockets = 1000;

process.on("unhandledRejection", (reason?: Error) => {
    log.error((reason ? reason.stack : undefined) ||  "No reason given");
});

const _toServer = (domain: string, serverConfig: any, homeserverDomain: string) => {
    // set server config defaults
    if (serverConfig.dynamicChannels.visibility) {
        throw new Error(
            `[DEPRECATED] Use of the config field dynamicChannels.visibility
            is deprecated. Use dynamicChannels.published, dynamicChannels.joinRule
            and dynamicChannels.createAlias instead.`
        );
    }
    return new IrcServer(
        domain, extend(true, IrcServer.DEFAULT_CONFIG, serverConfig), homeserverDomain
    );
};

export async function generateRegistration(reg: AppServiceRegistration, config: any) {
    let asToken;

    if (!reg.getSenderLocalpart()) {
        reg.setSenderLocalpart(IrcBridge.DEFAULT_LOCALPART);
    }
    reg.setId(AppServiceRegistration.generateToken());
    reg.setHomeserverToken(AppServiceRegistration.generateToken());
    reg.setAppServiceToken(asToken || AppServiceRegistration.generateToken());

    // Disable rate limiting to allow large numbers of requests when many IRC users
    // connect, for example on startup.
    reg.setRateLimited(false);

    // Set protocols to IRC, so that the bridge appears in the list of
    // thirdparty protocols
    reg.setProtocols(["irc"]);

    const serverDomains = Object.keys(config.ircService.servers);
    serverDomains.sort().forEach(function(domain) {
        let server = _toServer(domain, config.ircService.servers[domain], config.homeserver.domain);
        server.getHardCodedRoomIds().sort().forEach(function(roomId) {
            reg.addRegexPattern("rooms", roomId, false);
        });
        // add an alias pattern for servers who want aliases exposed.
        if (server.createsDynamicAliases()) {
            reg.addRegexPattern("aliases", server.getAliasRegex(), true);
        }
        reg.addRegexPattern("users", server.getUserRegex(), true);
    });

    return reg;
}

export async function runBridge(port: number, config: any, reg: AppServiceRegistration, isDBInMemory: boolean = false) {
    // configure global stuff for the process
    if (config.ircService.logging) {
        logging.configure(config.ircService.logging);
        logging.setUncaughtExceptionLogger(log as LoggerInstance);
    }
    if (config.ircService.statsd.hostname) {
        log.warn("STATSD WILL BE DEPRECATED SOON")
        log.warn("SEE https://github.com/matrix-org/matrix-appservice-irc/issues/818")
        stats.setEndpoint(config.ircService.statsd);
    }
    if (config.ircService.ident.enabled) {
        ident.configure(config.ircService.ident);
        ident.run();
    }

    const maxSockets = (config["advanced"] || {})["maxHttpSockets"] || 1000;
    require("http").globalAgent.maxSockets = maxSockets;
    require("https").globalAgent.maxSockets = maxSockets;

    // run the bridge
    const ircBridge = new IrcBridge(config, reg);
    const engine = config.database ? config.database.engine : "nedb";
    // Use in-memory DBs
    if (isDBInMemory) {
        ircBridge.getAppServiceBridge().opts.roomStore = new RoomBridgeStore(new Datastore());
        ircBridge.getAppServiceBridge().opts.userStore = new UserBridgeStore(new Datastore());
    }
    else if (engine === "postgres") {
        // Enforce these not to be created
{'fraction_non_alphanumeric': 0.11436524264794869, 'fraction_numerical': 0.004235749727701803, 'mean_word_length': 3.596218020022247, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '286115', 'n_tokens_mistral': 2401, 'n_tokens_neox': 2273, 'n_words': 612}