1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-26 02:25:58 +01:00

Allow usernames with spaces.

This requires a proper parser for commands.
This commit is contained in:
Juliusz Chroboczek 2020-09-16 01:42:39 +02:00
parent 1672f13230
commit 77179c3db9
2 changed files with 42 additions and 35 deletions

View file

@ -992,6 +992,40 @@ function clearChat() {
document.getElementById('box').textContent = ''; document.getElementById('box').textContent = '';
} }
/**
* parseCommand splits a string into two space-separated parts. The first
* part may be quoted and may include backslash escapes.
*
* @param {string} line
* @returns {Array.<string>}
*/
function parseCommand(line) {
let i = 0;
while(i < line.length && line[i] === ' ')
i++;
let start = ' ';
if(i < line.length && line[i] === '"' || line[i] === "'") {
start = line[i];
i++;
}
let first = "";
while(i < line.length) {
if(line[i] === start) {
if(start !== ' ')
i++;
break;
}
if(line[i] === '\\' && i < line.length - 1)
i++;
first = first + line[i];
i++;
}
while(i < line.length && line[i] === ' ')
i++;
return [first, line.slice(i)];
}
function handleInput() { function handleInput() {
let input = document.getElementById('input'); let input = document.getElementById('input');
let data = input.value; let data = input.value;
@ -1002,8 +1036,8 @@ function handleInput() {
if(data === '') if(data === '')
return; return;
if(data.charAt(0) === '/') { if(data[0] === '/') {
if(data.charAt(1) === '/') { if(data.length > 1 && data[1] === '/') {
message = data.substring(1); message = data.substring(1);
me = false; me = false;
} else { } else {
@ -1057,31 +1091,23 @@ function handleInput() {
displayError("You're not an operator"); displayError("You're not an operator");
return; return;
} }
let username, message; let parsed = parseCommand(rest);
let space = rest.indexOf(' ');
if(space < 0) {
username = rest;
message = null;
} else {
username = rest.slice(0, space);
message = rest.slice(space + 1).trim();
}
let id; let id;
if(username in users) { if(parsed[0] in users) {
id = rest; id = parsed[0];
} else { } else {
for(let i in users) { for(let i in users) {
if(users[i] === username) { if(users[i] === parsed[0]) {
id = i; id = i;
break; break;
} }
} }
} }
if(!id) { if(!id) {
displayError('Unknown user ' + username); displayError('Unknown user ' + parsed[0]);
return; return;
} }
serverConnection.userAction(cmd.slice(1), id, message); serverConnection.userAction(cmd.slice(1), id, parsed[1]);
return; return;
} }
default: default:

View file

@ -10,7 +10,6 @@ import (
"errors" "errors"
"log" "log"
"os" "os"
"strings"
"sync" "sync"
"time" "time"
@ -645,24 +644,6 @@ func startClient(conn *websocket.Conn) (err error) {
return return
} }
if strings.ContainsRune(m.Username, ' ') {
// at this point, the writer is not running yet, so format
// the message ourselves
conn.WriteJSON(clientMessage{
Type: "usermessage",
Kind: "error",
Value: "don't put spaces in your username",
})
conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(
websocket.CloseProtocolError,
"don't put spaces in your username",
),
)
conn.Close()
return
}
c := &webClient{ c := &webClient{
id: m.Id, id: m.Id,
credentials: clientCredentials{ credentials: clientCredentials{