<commit_before>import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Session } from 'meteor/session';
import { Template } from 'meteor/templating';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import toastr from 'toastr';

import { AdminChatRoom } from './adminRooms';
import { t, handleError } from '../../../utils';
import { call, modal } from '../../../ui-utils';
import { hasAllPermission, hasAtLeastOnePermission } from '../../../authorization';
import { ChannelSettings } from '../../../channel-settings';
import { settings } from '../../../settings';
import { callbacks } from '../../../callbacks';

Template.adminRoomInfo.helpers({
	selectedRoom() {
		return Session.get('adminRoomsSelected');
	},
	canEdit() {
		return hasAllPermission('edit-room', this.rid);
	},
	editing(field) {
		return Template.instance().editing.get() === field;
	},
	notDirect() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { t: 1 } });
		return room && room.t !== 'd';
	},
	roomType() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { t: 1 } });
		return room && room.t;
	},
	channelSettings() {
		return ChannelSettings.getOptions(undefined, 'admin-room');
	},
	roomTypeDescription() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { t: 1 } });
		const roomType = room && room.t;
		if (roomType === 'c') {
			return t('Channel');
		} if (roomType === 'p') {
			return t('Private_Group');
		}
	},
	roomName() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { name: 1 } });
		return room && room.name;
	},
	roomOwner() {
		const roomOwner = Template.instance().roomOwner.get();
		return roomOwner && (roomOwner.name || roomOwner.username);
	},
	roomTopic() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { topic: 1 } });
		return room && room.topic;
	},
	archivationState() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { archived: 1 } });
		return room && room.archived;
	},
	archivationStateDescription() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { archived: 1 } });
		const archivationState = room && room.archived;
		if (archivationState === true) {
			return t('Room_archivation_state_true');
		}
		return t('Room_archivation_state_false');
	},
	canDeleteRoom() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { t: 1 } });
		const roomType = room && room.t;
		return (roomType != null) && hasAtLeastOnePermission(`delete-${ roomType }`);
	},
	readOnly() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { ro: 1 } });
		return room && room.ro;
	},
	readOnlyDescription() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { ro: 1 } });
		const readOnly = room && room.ro;

		if (readOnly === true) {
			return t('True');
		}
		return t('False');
	},
});

Template.adminRoomInfo.events({
	'click .delete'() {
		modal.open({
			title: t('Are_you_sure'),
			text: t('Delete_Room_Warning'),
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#DD6B55',
			confirmButtonText: t('Yes_delete_it'),
			cancelButtonText: t('Cancel'),
			closeOnConfirm: false,
			html: false,
		}, () => {
			Meteor.call('eraseRoom', this.rid, function(error) {
				if (error) {
					handleError(error);
				} else {
					modal.open({
						title: t('Deleted'),
						text: t('Room_has_been_deleted'),
						type: 'success',
						timer: 2000,
						showConfirmButton: false,
					});
				}
			});
		});
	},
	'keydown input[type=text]'(e, t) {
		if (e.keyCode === 13) {
			e.preventDefault();
			t.saveSetting(this.rid);
		}
	},
	'click [data-edit]'(e, t) {
		e.preventDefault();
		t.editing.set($(e.currentTarget).data('edit'));
		return setTimeout(function() {
			t.$('input.editing').focus().select();
		}, 100);
	},
	'click .cancel'(e, t) {
		e.preventDefault();
		t.editing.set();
	},
	'click .save'(e, t) {
		e.preventDefault();
		t.saveSetting(this.rid);
	},
});

Template.adminRoomInfo.onCreated(function() {
	this.editing = new ReactiveVar();
	this.roomOwner = new ReactiveVar();
	this.validateRoomType = () => {
		const type = this.$('input[name=roomType]:checked').val();
		if (type !== 'c' && type !== 'p') {
			toastr.error(t('error-invalid-room-type', { type }));
		}
		return true;
	};
	this.validateRoomName = (rid) => {
		const room = AdminChatRoom.findOne(rid);
		let nameValidation;
		if (!hasAllPermission('edit-room', rid) || (room.t !== 'c' && room.t !== 'p')) {
			toastr.error(t('error-not-allowed'));
			return false;
		}
		name = $('input[name=roomName]').val();
		try {
			nameValidation = new RegExp(`^${ settings.get('UTF8_Names_Validation') }$`);
		} catch (_error) {
			nameValidation = new RegExp('^[0-9a-zA-Z-_.]+$');
		}
		if (!nameValidation.test(name)) {
			toastr.error(t('error-invalid-room-name', {
				room_name: name,
			}));
			return false;
		}
		return true;<commit_msg> [FIX] Self-XSS in validation functionality (#15564)<commit_after>import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Session } from 'meteor/session';
import { Template } from 'meteor/templating';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import toastr from 'toastr';
import s from 'underscore.string';

import { AdminChatRoom } from './adminRooms';
import { t, handleError } from '../../../utils';
import { call, modal } from '../../../ui-utils';
import { hasAllPermission, hasAtLeastOnePermission } from '../../../authorization';
import { ChannelSettings } from '../../../channel-settings';
import { settings } from '../../../settings';
import { callbacks } from '../../../callbacks';

Template.adminRoomInfo.helpers({
	selectedRoom() {
		return Session.get('adminRoomsSelected');
	},
	canEdit() {
		return hasAllPermission('edit-room', this.rid);
	},
	editing(field) {
		return Template.instance().editing.get() === field;
	},
	notDirect() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { t: 1 } });
		return room && room.t !== 'd';
	},
	roomType() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { t: 1 } });
		return room && room.t;
	},
	channelSettings() {
		return ChannelSettings.getOptions(undefined, 'admin-room');
	},
	roomTypeDescription() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { t: 1 } });
		const roomType = room && room.t;
		if (roomType === 'c') {
			return t('Channel');
		} if (roomType === 'p') {
			return t('Private_Group');
		}
	},
	roomName() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { name: 1 } });
		return room && room.name;
	},
	roomOwner() {
		const roomOwner = Template.instance().roomOwner.get();
		return roomOwner && (roomOwner.name || roomOwner.username);
	},
	roomTopic() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { topic: 1 } });
		return room && room.topic;
	},
	archivationState() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { archived: 1 } });
		return room && room.archived;
	},
	archivationStateDescription() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { archived: 1 } });
		const archivationState = room && room.archived;
		if (archivationState === true) {
			return t('Room_archivation_state_true');
		}
		return t('Room_archivation_state_false');
	},
	canDeleteRoom() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { t: 1 } });
		const roomType = room && room.t;
		return (roomType != null) && hasAtLeastOnePermission(`delete-${ roomType }`);
	},
	readOnly() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { ro: 1 } });
		return room && room.ro;
	},
	readOnlyDescription() {
		const room = AdminChatRoom.findOne(this.rid, { fields: { ro: 1 } });
		const readOnly = room && room.ro;

		if (readOnly === true) {
			return t('True');
		}
		return t('False');
	},
});

Template.adminRoomInfo.events({
	'click .delete'() {
		modal.open({
			title: t('Are_you_sure'),
			text: t('Delete_Room_Warning'),
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#DD6B55',
			confirmButtonText: t('Yes_delete_it'),
			cancelButtonText: t('Cancel'),
			closeOnConfirm: false,
			html: false,
		}, () => {
			Meteor.call('eraseRoom', this.rid, function(error) {
				if (error) {
					handleError(error);
				} else {
					modal.open({
						title: t('Deleted'),
						text: t('Room_has_been_deleted'),
						type: 'success',
						timer: 2000,
						showConfirmButton: false,
					});
				}
			});
		});
	},
	'keydown input[type=text]'(e, t) {
		if (e.keyCode === 13) {
			e.preventDefault();
			t.saveSetting(this.rid);
		}
	},
	'click [data-edit]'(e, t) {
		e.preventDefault();
		t.editing.set($(e.currentTarget).data('edit'));
		return setTimeout(function() {
			t.$('input.editing').focus().select();
		}, 100);
	},
	'click .cancel'(e, t) {
		e.preventDefault();
		t.editing.set();
	},
	'click .save'(e, t) {
		e.preventDefault();
		t.saveSetting(this.rid);
	},
});

Template.adminRoomInfo.onCreated(function() {
	this.editing = new ReactiveVar();
	this.roomOwner = new ReactiveVar();
	this.validateRoomType = () => {
		const type = this.$('input[name=roomType]:checked').val();
		if (type !== 'c' && type !== 'p') {
			toastr.error(t('error-invalid-room-type', { type }));
		}
		return true;
	};
	this.validateRoomName = (rid) => {
		const room = AdminChatRoom.findOne(rid);
		let nameValidation;
		if (!hasAllPermission('edit-room', rid) || (room.t !== 'c' && room.t !== 'p')) {
			toastr.error(t('error-not-allowed'));
			return false;
		}
		name = $('input[name=roomName]').val();
		try {
			nameValidation = new RegExp(`^${ settings.get('UTF8_Names_Validation') }$`);
		} catch (_error) {
			nameValidation = new RegExp('^[0-9a-zA-Z-_.]+$');
		}
		if (!nameValidation.test(name)) {
			toastr.error(t('error-invalid-room-name', {
				room_name: s.escapeHTML(name),
			}));
			return false;
		}
		return true;
{'fraction_non_alphanumeric': 0.1957849725106903, 'fraction_numerical': 0.006414172266340868, 'mean_word_length': 3.73625843780135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2222970', 'n_tokens_mistral': 3802, 'n_tokens_neox': 3420, 'n_words': 685}