1
0
Fork 0
forked from devs/v5shoutbox

enable message-tags and parse them

This commit is contained in:
Lephenixnoir 2023-06-16 08:26:58 +02:00
parent e1dfaaab4f
commit 79a2619941
Signed by untrusted user: Lephenixnoir
GPG key ID: 1BBA026E13FC0495

View file

@ -124,7 +124,12 @@ let irc = new function() {
}
conn.socket.onmessage = function(net) {
log("[<] " + net.data);
processMessage(parseMessage(net.data));
let msg = new Message(net.data);
if(msg.command === undefined) {
log("[v5shoutbox] invalid message");
return;
}
processMessage(msg);
}
conn.socket.onclose = function() {
log("[v5shoutbox] Disconnected.");
@ -154,7 +159,6 @@ let irc = new function() {
const rMessage = (function(){
const rTags = /(?:@([^ ]+) +)?/;
const rSource = /(?::([^ ]+) +)?/;
// TODO: Check if a-zA-Z is OK for cmd
const rCommand = /(?:([a-zA-Z]+)|(\d{3}))/;
const rArgs = /((?: +[^\r\n :][^\r\n ]*)*)/;
const rTrailing = /(?: +:([^\r\n]*))?/;
@ -162,29 +166,31 @@ let irc = new function() {
.map(r => r.source).join(""));
})();
function parseMessage(text) {
const matches = rMessage.exec(text);
if(matches === undefined)
return undefined;
class Message {
constructor(text) {
const matches = rMessage.exec(text);
if(matches === undefined)
return;
let msg = Object();
msg.tags = matches[1];
msg.source = matches[2];
if(matches[3] !== undefined) {
msg.textCommand = matches[3];
msg.numCommand = undefined;
this.tags = new Map();
(matches[1] || "").split(";").filter(s => s).forEach(s => {
const parts = s.split(/=(.*)/);
const value = (parts[1] || "").replaceAll("\\\\", "\\")
.replaceAll("\\:", ";")
.replaceAll("\\r", "\r")
.replaceAll("\\n", "\n")
.replaceAll(/\\./g, m => m[1]);
this.tags.set(parts[0], value);
});
this.source = matches[2];
this.command = matches[3] || parseInt(matches[4]);
this.args = (matches[5] || "").split(" ").filter(s => s);
if(matches[6] !== undefined)
this.args.push(matches[6]);
}
else {
msg.textCommand = undefined;
msg.numCommand = parseInt(matches[4]);
}
msg.args = [];
if(matches[5] !== undefined)
msg.args = matches[5].trim().split(" ").filter(s => s);
if(matches[6] !== undefined)
msg.args.push(matches[6]);
return msg;
}
this.Message = Message;
function startExchange() {
sendRaw("CAP LS 302");
@ -193,34 +199,39 @@ let irc = new function() {
}
function processMessage(msg) {
if(msg.textCommand === "PING")
if(msg.command === "PING")
send("PONG", msg.args);
let sendAuth = false;
if(msg.textCommand === "CAP" && msg.args[1] === "LS") {
if(msg.command === "CAP" && msg.args[1] === "LS") {
const caps = msg.args[2].split(" ");
if(caps.includes("draft/chathistory"))
sendRaw("CAP REQ draft/chathistory");
const wishlist =
["message-tags", "server-time", "batch", "draft/chathistory"];
const req = wishlist.filter(x => caps.includes(x));
if(req.length)
send("CAP", ["REQ", req.join(" ")]);
else
sendRaw("CAP END");
}
if(msg.textCommand === "CAP" && msg.args[1] === "ACK")
if(msg.command === "CAP" && msg.args[1] === "ACK")
sendRaw("CAP END");
if(msg.textCommand === "NOTICE" && msg.args[1].includes("/AUTH")) {
if(msg.command === "NOTICE" && msg.args[1].includes("/AUTH")) {
log("[v5shoutbox] AUTH command sent (not shown)");
conn.socket.send("AUTH " + conn.username + ":" + conn.password);
}
if(msg.numCommand === 900) {
if(msg.command === 900) {
log("[v5shoutbox] Authenticated.");
setState(State.READY);
conn.password = undefined;
irc.onAuthenticated();
}
if(msg.textCommand == "PRIVMSG" && msg.args.length == 2) {
if(msg.command == "PRIVMSG" && msg.args.length == 2) {
let source = msg.source;
if(source.includes("!"))
source = source.substr(0, source.indexOf("!"));