<commit_before>/*
Copyright 2015, 2016 OpenMarket Ltd
Copyright 2018 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/


import React from 'react';
import MatrixClientPeg from './MatrixClientPeg';
import dis from './dispatcher';
import Tinter from './Tinter';
import sdk from './index';
import {_t, _td} from './languageHandler';
import Modal from './Modal';
import SettingsStore, {SettingLevel} from './settings/SettingsStore';
import {MATRIXTO_URL_PATTERN} from "./linkify-matrix";
import * as querystring from "querystring";


class Command {
    constructor({name, args='', description, runFn, hideCompletionAfterSpace=false}) {
        this.command = '/' + name;
        this.args = args;
        this.description = description;
        this.runFn = runFn;
        this.hideCompletionAfterSpace = hideCompletionAfterSpace;
    }

    getCommand() {
        return this.command;
    }

    getCommandWithArgs() {
        return this.getCommand() + " " + this.args;
    }

    run(roomId, args) {
        return this.runFn.bind(this)(roomId, args);
    }

    getUsage() {
        return _t('Usage') + ': ' + this.getCommandWithArgs();
    }
}

function reject(error) {
    return {error};
}

function success(promise) {
    return {promise};
}

/* Disable the "unexpected this" error for these commands - all of the run
 * functions are called with `this` bound to the Command instance.
 */

/* eslint-disable babel/no-invalid-this */

export const CommandMap = {
    ddg: new Command({
        name: 'ddg',
        args: '<query>',
        description: _td('Searches DuckDuckGo for results'),
        runFn: function(roomId, args) {
            const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
            // TODO Don't explain this away, actually show a search UI here.
            Modal.createTrackedDialog('Slash Commands', '/ddg is not a command', ErrorDialog, {
                title: _t('/ddg is not a command'),
                description: _t('To use it, just wait for autocomplete results to load and tab through them.'),
            });
            return success();
        },
        hideCompletionAfterSpace: true,
    }),

    nick: new Command({
        name: 'nick',
        args: '<display_name>',
        description: _td('Changes your display nickname'),
        runFn: function(roomId, args) {
            if (args) {
                return success(MatrixClientPeg.get().setDisplayName(args));
            }
            return reject(this.getUsage());
        },
    }),

    tint: new Command({
        name: 'tint',
        args: '<color1> [<color2>]',
        description: _td('Changes colour scheme of current room'),
        runFn: function(roomId, args) {
            if (args) {
                const matches = args.match(/^(#([\da-fA-F]{3}|[\da-fA-F]{6}))( +(#([\da-fA-F]{3}|[\da-fA-F]{6})))?$/);
                if (matches) {
                    Tinter.tint(matches[1], matches[4]);
                    const colorScheme = {};
                    colorScheme.primary_color = matches[1];
                    if (matches[4]) {
                        colorScheme.secondary_color = matches[4];
                    } else {
                        colorScheme.secondary_color = colorScheme.primary_color;
                    }
                    return success(
                        SettingsStore.setValue('roomColor', roomId, SettingLevel.ROOM_ACCOUNT, colorScheme),
                    );
                }
            }
            return reject(this.getUsage());
        },
    }),

    topic: new Command({
        name: 'topic',
        args: '<topic>',
        description: _td('Sets the room topic'),
        runFn: function(roomId, args) {
            if (args) {
                return success(MatrixClientPeg.get().setRoomTopic(roomId, args));
            }
            return reject(this.getUsage());
        },
    }),

    invite: new Command({
        name: 'invite',
        args: '<user-id>',
        description: _td('Invites user with given id to current room'),
        runFn: function(roomId, args) {
            if (args) {
                const matches = args.match(/^(\S+)$/);
                if (matches) {
                    return success(MatrixClientPeg.get().invite(roomId, matches[1]));
                }
            }
            return reject(this.getUsage());
        },
    }),

    join: new Command({
        name: 'join',
        args: '<room-alias>',
        description: _td('Joins room with given alias'),
        runFn: function(roomId, args) {
            console.log(args);
            if (args) {
                // Note: we support 2 versions of this command. The first is
                // the public-facing one for most users and the other is a
                // power-user edition where someone may join via permalink or
                // room ID with optional servers. Practically, this results
                // in the following variations:
                //   /join #example:example.org
                //   /join !example:example.org
                //   /join !example:example.org altserver.com elsewhere.ca
                //   /join https://matrix.to/#/!example:example.org?via=altserver.com
                // The command also supports event permalinks transparently:
                //   /join https://matrix.to/#/!example:example.org/$something:example.org
                //   /join https://matrix.to/#/!example:example.org/$something:example.org?via=altserver.com
                const params = args.split(' ');
                if (params.length < 1) return reject(this.getUsage());

                const matrixToMatches = params[0].match(MATRIXTO_URL_PATTERN);
                if (params[0][0] === '#') {
                    let roomAlias = params[0];
                    if (!roomAlias.includes(':')) {
                        roomAlias += ':' + MatrixClientPeg.get().getDomain();
                    }
<commit_msg>debugging--;<commit_after>/*
Copyright 2015, 2016 OpenMarket Ltd
Copyright 2018 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/


import React from 'react';
import MatrixClientPeg from './MatrixClientPeg';
import dis from './dispatcher';
import Tinter from './Tinter';
import sdk from './index';
import {_t, _td} from './languageHandler';
import Modal from './Modal';
import SettingsStore, {SettingLevel} from './settings/SettingsStore';
import {MATRIXTO_URL_PATTERN} from "./linkify-matrix";
import * as querystring from "querystring";


class Command {
    constructor({name, args='', description, runFn, hideCompletionAfterSpace=false}) {
        this.command = '/' + name;
        this.args = args;
        this.description = description;
        this.runFn = runFn;
        this.hideCompletionAfterSpace = hideCompletionAfterSpace;
    }

    getCommand() {
        return this.command;
    }

    getCommandWithArgs() {
        return this.getCommand() + " " + this.args;
    }

    run(roomId, args) {
        return this.runFn.bind(this)(roomId, args);
    }

    getUsage() {
        return _t('Usage') + ': ' + this.getCommandWithArgs();
    }
}

function reject(error) {
    return {error};
}

function success(promise) {
    return {promise};
}

/* Disable the "unexpected this" error for these commands - all of the run
 * functions are called with `this` bound to the Command instance.
 */

/* eslint-disable babel/no-invalid-this */

export const CommandMap = {
    ddg: new Command({
        name: 'ddg',
        args: '<query>',
        description: _td('Searches DuckDuckGo for results'),
        runFn: function(roomId, args) {
            const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
            // TODO Don't explain this away, actually show a search UI here.
            Modal.createTrackedDialog('Slash Commands', '/ddg is not a command', ErrorDialog, {
                title: _t('/ddg is not a command'),
                description: _t('To use it, just wait for autocomplete results to load and tab through them.'),
            });
            return success();
        },
        hideCompletionAfterSpace: true,
    }),

    nick: new Command({
        name: 'nick',
        args: '<display_name>',
        description: _td('Changes your display nickname'),
        runFn: function(roomId, args) {
            if (args) {
                return success(MatrixClientPeg.get().setDisplayName(args));
            }
            return reject(this.getUsage());
        },
    }),

    tint: new Command({
        name: 'tint',
        args: '<color1> [<color2>]',
        description: _td('Changes colour scheme of current room'),
        runFn: function(roomId, args) {
            if (args) {
                const matches = args.match(/^(#([\da-fA-F]{3}|[\da-fA-F]{6}))( +(#([\da-fA-F]{3}|[\da-fA-F]{6})))?$/);
                if (matches) {
                    Tinter.tint(matches[1], matches[4]);
                    const colorScheme = {};
                    colorScheme.primary_color = matches[1];
                    if (matches[4]) {
                        colorScheme.secondary_color = matches[4];
                    } else {
                        colorScheme.secondary_color = colorScheme.primary_color;
                    }
                    return success(
                        SettingsStore.setValue('roomColor', roomId, SettingLevel.ROOM_ACCOUNT, colorScheme),
                    );
                }
            }
            return reject(this.getUsage());
        },
    }),

    topic: new Command({
        name: 'topic',
        args: '<topic>',
        description: _td('Sets the room topic'),
        runFn: function(roomId, args) {
            if (args) {
                return success(MatrixClientPeg.get().setRoomTopic(roomId, args));
            }
            return reject(this.getUsage());
        },
    }),

    invite: new Command({
        name: 'invite',
        args: '<user-id>',
        description: _td('Invites user with given id to current room'),
        runFn: function(roomId, args) {
            if (args) {
                const matches = args.match(/^(\S+)$/);
                if (matches) {
                    return success(MatrixClientPeg.get().invite(roomId, matches[1]));
                }
            }
            return reject(this.getUsage());
        },
    }),

    join: new Command({
        name: 'join',
        args: '<room-alias>',
        description: _td('Joins room with given alias'),
        runFn: function(roomId, args) {
            if (args) {
                // Note: we support 2 versions of this command. The first is
                // the public-facing one for most users and the other is a
                // power-user edition where someone may join via permalink or
                // room ID with optional servers. Practically, this results
                // in the following variations:
                //   /join #example:example.org
                //   /join !example:example.org
                //   /join !example:example.org altserver.com elsewhere.ca
                //   /join https://matrix.to/#/!example:example.org?via=altserver.com
                // The command also supports event permalinks transparently:
                //   /join https://matrix.to/#/!example:example.org/$something:example.org
                //   /join https://matrix.to/#/!example:example.org/$something:example.org?via=altserver.com
                const params = args.split(' ');
                if (params.length < 1) return reject(this.getUsage());

                const matrixToMatches = params[0].match(MATRIXTO_URL_PATTERN);
                if (params[0][0] === '#') {
                    let roomAlias = params[0];
                    if (!roomAlias.includes(':')) {
                        roomAlias += ':' + MatrixClientPeg.get().getDomain();
                    }

{'fraction_non_alphanumeric': 0.12289325842696629, 'fraction_numerical': 0.005305867665418227, 'mean_word_length': 1.9296, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, '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': '1835785', 'n_tokens_mistral': 3543, 'n_tokens_neox': 3376, 'n_words': 971}