TeaWeb/shared/js/connection/CommandHandler.ts

1160 lines
52 KiB
TypeScript
Raw Normal View History

/// <reference path="ConnectionBase.ts" />
2019-03-17 11:15:39 +00:00
namespace connection {
import Conversation = chat.channel.Conversation;
import MusicInfo = chat.MusicInfo;
2019-08-21 08:00:01 +00:00
export class ServerConnectionCommandBoss extends AbstractCommandHandlerBoss {
constructor(connection: AbstractServerConnection) {
super(connection);
2019-02-23 13:15:22 +00:00
}
}
export class ConnectionCommandHandler extends AbstractCommandHandler {
readonly connection: AbstractServerConnection;
readonly connection_handler: ConnectionHandler;
constructor(connection: AbstractServerConnection) {
super(connection);
this.connection_handler = connection.client;
this["error"] = this.handleCommandResult;
this["channellist"] = this.handleCommandChannelList;
this["channellistfinished"] = this.handleCommandChannelListFinished;
this["notifychannelcreated"] = this.handleCommandChannelCreate;
this["notifychanneldeleted"] = this.handleCommandChannelDelete;
this["notifychannelhide"] = this.handleCommandChannelHide;
this["notifychannelshow"] = this.handleCommandChannelShow;
this["notifyserverconnectioninfo"] = this.handleNotifyServerConnectionInfo;
this["notifyconnectioninfo"] = this.handleNotifyConnectionInfo;
this["notifycliententerview"] = this.handleCommandClientEnterView;
this["notifyclientleftview"] = this.handleCommandClientLeftView;
this["notifyclientmoved"] = this.handleNotifyClientMoved;
this["initserver"] = this.handleCommandServerInit;
this["notifychannelmoved"] = this.handleNotifyChannelMoved;
this["notifychanneledited"] = this.handleNotifyChannelEdited;
this["notifytextmessage"] = this.handleNotifyTextMessage;
this["notifyclientchatcomposing"] = this.notifyClientChatComposing;
this["notifyclientchatclosed"] = this.handleNotifyClientChatClosed;
this["notifyclientupdated"] = this.handleNotifyClientUpdated;
this["notifyserveredited"] = this.handleNotifyServerEdited;
this["notifyserverupdated"] = this.handleNotifyServerUpdated;
this["notifyclientpoke"] = this.handleNotifyClientPoke;
this["notifymusicplayerinfo"] = this.handleNotifyMusicPlayerInfo;
this["notifyservergroupclientadded"] = this.handleNotifyServerGroupClientAdd;
this["notifyservergroupclientdeleted"] = this.handleNotifyServerGroupClientRemove;
this["notifyclientchannelgroupchanged"] = this.handleNotifyClientChannelGroupChanged;
this["notifychannelsubscribed"] = this.handleNotifyChannelSubscribed;
this["notifychannelunsubscribed"] = this.handleNotifyChannelUnsubscribed;
this["notifyconversationhistory"] = this.handleNotifyConversationHistory;
this["notifyconversationmessagedelete"] = this.handleNotifyConversationMessageDelete;
this["notifymusicstatusupdate"] = this.handleNotifyMusicStatusUpdate;
this["notifymusicplayersongchange"] = this.handleMusicPlayerSongChange;
this["notifyplaylistsongadd"] = this.handleNotifyPlaylistSongAdd;
this["notifyplaylistsongremove"] = this.handleNotifyPlaylistSongRemove;
this["notifyplaylistsongreorder"] = this.handleNotifyPlaylistSongReorder;
this["notifyplaylistsongloaded"] = this.handleNotifyPlaylistSongLoaded;
2019-02-23 13:15:22 +00:00
}
private loggable_invoker(unique_id, client_id, name) : log.server.base.Client | undefined {
const id = parseInt(client_id);
if(typeof(client_id) === "undefined" || Number.isNaN(id))
return undefined;
if(id == 0)
return {
client_id: 0,
client_unique_id: this.connection_handler.channelTree.server.properties.virtualserver_unique_identifier,
client_name: this.connection_handler.channelTree.server.properties.virtualserver_name,
};
return {
client_unique_id: unique_id,
client_name: name,
client_id: client_id
};
}
proxy_command_promise(promise: Promise<CommandResult>, options: connection.CommandOptions) {
if(!options.process_result)
return promise;
return promise.catch(ex => {
if(options.process_result) {
if(ex instanceof CommandResult) {
let res = ex;
if(!res.success) {
if(res.id == ErrorID.PERMISSION_ERROR) { //Permission error
const permission = this.connection_handler.permissions.resolveInfo(res.json["failed_permid"] as number);
res.message = tr("Insufficient client permissions. Failed on permission ") + (permission ? permission.name : "unknown");
this.connection_handler.log.log(log.server.Type.ERROR_PERMISSION, {
permission: this.connection_handler.permissions.resolveInfo(res.json["failed_permid"] as number)
});
this.connection_handler.sound.play(Sound.ERROR_INSUFFICIENT_PERMISSIONS);
} else if(res.id != ErrorID.EMPTY_RESULT) {
this.connection_handler.log.log(log.server.Type.ERROR_CUSTOM, {
message: res.extra_message.length == 0 ? res.message : res.extra_message
});
}
}
} else if(typeof(ex) === "string") {
this.connection_handler.log.log(log.server.Type.CONNECTION_COMMAND_ERROR, {error: ex});
} else {
log.error(LogCategory.NETWORKING, tr("Invalid promise result type: %s. Result: %o"), typeof (ex), ex);
}
}
return Promise.reject(ex);
});
}
handle_command(command: ServerCommand) : boolean {
if(this[command.command]) {
this[command.command](command.arguments);
return true;
}
2020-03-27 22:36:57 +00:00
return false;
2019-02-23 13:15:22 +00:00
}
set_handler(command: string, handler: any) {
this[command] = handler;
2019-02-23 13:15:22 +00:00
}
unset_handler(command: string, handler?: any) {
if(handler && this[command] != handler) return;
this[command] = undefined;
}
2020-03-27 22:36:57 +00:00
handleCommandResult(json) {
json = json[0]; //Only one bulk
2019-02-23 13:15:22 +00:00
let code : string = json["return_code"];
if(!code || code.length == 0) {
log.warn(LogCategory.NETWORKING, tr("Invalid return code! (%o)"), json);
2019-02-23 13:15:22 +00:00
return;
}
let retListeners = this.connection["_retListener"];
for(let e of retListeners) {
if(e.code != code) continue;
retListeners.remove(e);
let result = new CommandResult(json);
if(result.success)
e.resolve(result);
else
e.reject(result);
break;
2019-02-23 13:15:22 +00:00
}
}
handleCommandServerInit(json){
//We could setup the voice channel
if(this.connection.support_voice()) {
log.debug(LogCategory.NETWORKING, tr("Setting up voice"));
} else {
log.debug(LogCategory.NETWORKING, tr("Skipping voice setup (No voice bridge available)"));
}
2019-02-23 13:15:22 +00:00
json = json[0]; //Only one bulk
2019-02-23 13:15:22 +00:00
this.connection_handler.channelTree.registerClient(this.connection_handler.getClient());
this.connection.client.side_bar.channel_conversations().reset();
this.connection.client.clientId = parseInt(json["aclid"]);
this.connection.client.getClient().updateVariables( {key: "client_nickname", value: json["acn"]});
2019-02-23 13:15:22 +00:00
let updates: {
key: string,
value: string
}[] = [];
for(let key in json) {
if(key === "aclid") continue;
if(key === "acn") continue;
2019-08-21 08:00:01 +00:00
updates.push({key: key, value: json[key]});
}
this.connection.client.channelTree.server.updateVariables(false, ...updates);
const properties = this.connection.client.channelTree.server.properties;
/* host message */
if(properties.virtualserver_hostmessage_mode > 0) {
if(properties.virtualserver_hostmessage_mode == 1) {
/* show in log */
this.connection_handler.log.log(log.server.Type.SERVER_HOST_MESSAGE, {
message: properties.virtualserver_hostmessage
});
} else {
/* create modal/create modal and quit */
createModal({
header: tr("Host message"),
body: MessageHelper.bbcode_chat(properties.virtualserver_hostmessage),
footer: undefined
}).open();
if(properties.virtualserver_hostmessage_mode == 3) {
/* first let the client initialize his stuff */
setTimeout(() => {
this.connection_handler.log.log(log.server.Type.SERVER_HOST_MESSAGE_DISCONNECT, {
message: properties.virtualserver_welcomemessage
});
2019-08-21 08:00:01 +00:00
this.connection.disconnect("host message disconnect");
this.connection_handler.handleDisconnect(DisconnectReason.SERVER_HOSTMESSAGE);
this.connection_handler.sound.play(Sound.CONNECTION_DISCONNECTED);
}, 100);
}
}
2019-08-21 08:00:01 +00:00
}
/* welcome message */
if(properties.virtualserver_welcomemessage) {
this.connection_handler.log.log(log.server.Type.SERVER_WELCOME_MESSAGE, {
message: properties.virtualserver_welcomemessage
});
}
2019-02-23 13:15:22 +00:00
/* priviledge key */
if(properties.virtualserver_ask_for_privilegekey) {
createInputModal(tr("Use a privilege key"), tr("This is a newly created server for which administrator privileges have not yet been claimed.<br>Please enter the \"privilege key\" that was automatically generated when this server was created to gain administrator permissions."), message => message.length > 0, result => {
if(!result) return;
const scon = server_connections.active_connection_handler();
if(scon.serverConnection.connected)
scon.serverConnection.send_command("tokenuse", {
token: result
}).then(() => {
createInfoModal(tr("Use privilege key"), tr("Privilege key successfully used!")).open();
}).catch(error => {
createErrorModal(tr("Use privilege key"), MessageHelper.formatMessage(tr("Failed to use privilege key: {}"), error instanceof CommandResult ? error.message : error)).open();
});
}, { field_placeholder: 'Enter Privilege Key' }).open();
2020-03-27 22:36:57 +00:00
}
2019-02-23 13:15:22 +00:00
this.connection_handler.log.log(log.server.Type.CONNECTION_CONNECTED, {
own_client: this.connection_handler.getClient().log_data()
});
this.connection_handler.sound.play(Sound.CONNECTION_CONNECTED);
this.connection.client.onConnected();
}
2019-08-21 08:00:01 +00:00
handleNotifyServerConnectionInfo(json) {
json = json[0];
2019-08-21 08:00:01 +00:00
/* everything is a number, so lets parse it */
for(const key of Object.keys(json))
json[key] = parseFloat(json[key]);
2019-08-21 08:00:01 +00:00
this.connection_handler.channelTree.server.set_connection_info(json);
}
2019-08-30 21:06:39 +00:00
handleNotifyConnectionInfo(json) {
json = json[0];
2020-03-27 22:36:57 +00:00
const object = new ClientConnectionInfo();
/* everything is a number (except ip), so lets parse it */
for(const key of Object.keys(json)) {
if(key === "connection_client_ip")
object[key] = json[key];
else
object[key] = parseFloat(json[key]);
}
2019-08-30 21:06:39 +00:00
const client = this.connection_handler.channelTree.findClient(parseInt(json["clid"]));
2019-08-30 21:06:39 +00:00
if(!client) {
log.warn(LogCategory.NETWORKING, tr("Received client connection info for unknown client (%o)"), json["clid"]);
return;
2020-03-27 22:36:57 +00:00
}
2019-08-30 21:06:39 +00:00
client.set_connection_info(object);
}
2019-02-23 13:15:22 +00:00
private createChannelFromJson(json, ignoreOrder: boolean = false) {
let tree = this.connection.client.channelTree;
2019-02-23 13:15:22 +00:00
let channel = new ChannelEntry(parseInt(json["cid"]), json["channel_name"], tree.findChannel(json["cpid"]));
tree.insertChannel(channel);
if(json["channel_order"] !== "0") {
let prev = tree.findChannel(json["channel_order"]);
if(!prev && json["channel_order"] != 0) {
if(!ignoreOrder) {
log.error(LogCategory.NETWORKING, tr("Invalid channel order id!"));
return;
}
}
let parent = tree.findChannel(json["cpid"]);
if(!parent && json["cpid"] != 0) {
log.error(LogCategory.NETWORKING, tr("Invalid channel parent"));
return;
}
tree.moveChannel(channel, prev, parent); //TODO test if channel exists!
}
if(ignoreOrder) {
for(let ch of tree.channels) {
if(ch.properties.channel_order == channel.channelId) {
tree.moveChannel(ch, channel, channel.parent); //Corrent the order :)
}
2019-02-23 13:15:22 +00:00
}
}
let updates: {
key: string,
value: string
}[] = [];
for(let key in json) {
if(key === "cid") continue;
if(key === "cpid") continue;
2019-02-23 13:15:22 +00:00
if(key === "invokerid") continue;
if(key === "invokername") continue;
if(key === "invokeruid") continue;
if(key === "reasonid") continue;
updates.push({key: key, value: json[key]});
2019-02-23 13:15:22 +00:00
}
channel.updateVariables(...updates);
}
2019-02-23 13:15:22 +00:00
handleCommandChannelList(json) {
this.connection.client.channelTree.hide_channel_tree(); /* dont perform channel inserts on the dom to prevent style recalculations */
log.debug(LogCategory.NETWORKING, tr("Got %d new channels"), json.length);
for(let index = 0; index < json.length; index++)
this.createChannelFromJson(json[index], true);
}
2019-02-23 13:15:22 +00:00
handleCommandChannelListFinished(json) {
this.connection.client.channelTree.show_channel_tree();
2019-02-23 13:15:22 +00:00
}
handleCommandChannelCreate(json) {
this.createChannelFromJson(json[0]);
}
handleCommandChannelShow(json) {
this.createChannelFromJson(json[0]); //TODO may chat?
}
2019-02-23 13:15:22 +00:00
handleCommandChannelDelete(json) {
2019-02-23 13:15:22 +00:00
let tree = this.connection.client.channelTree;
const conversations = this.connection.client.side_bar.channel_conversations();
log.info(LogCategory.NETWORKING, tr("Got %d channel deletions"), json.length);
for(let index = 0; index < json.length; index++) {
conversations.delete_conversation(parseInt(json[index]["cid"]));
let channel = tree.findChannel(json[index]["cid"]);
if(!channel) {
log.error(LogCategory.NETWORKING, tr("Invalid channel onDelete (Unknown channel)"));
continue;
}
tree.deleteChannel(channel);
2019-02-23 13:15:22 +00:00
}
}
handleCommandChannelHide(json) {
let tree = this.connection.client.channelTree;
const conversations = this.connection.client.side_bar.channel_conversations();
log.info(LogCategory.NETWORKING, tr("Got %d channel hides"), json.length);
for(let index = 0; index < json.length; index++) {
conversations.delete_conversation(parseInt(json[index]["cid"]));
let channel = tree.findChannel(json[index]["cid"]);
if(!channel) {
log.error(LogCategory.NETWORKING, tr("Invalid channel on hide (Unknown channel)"));
continue;
2019-02-23 13:15:22 +00:00
}
tree.deleteChannel(channel);
2019-02-23 13:15:22 +00:00
}
}
2019-02-23 13:15:22 +00:00
handleCommandClientEnterView(json) {
let tree = this.connection.client.channelTree;
2019-02-23 13:15:22 +00:00
let client: ClientEntry;
let channel = undefined;
let old_channel = undefined;
let reason_id, reason_msg;
let invokerid, invokername, invokeruid;
for(const entry of json) {
/* attempt to update properties if given */
channel = typeof(entry["ctid"]) !== "undefined" ? tree.findChannel(parseInt(entry["ctid"])) : channel;
old_channel = typeof(entry["cfid"]) !== "undefined" ? tree.findChannel(parseInt(entry["cfid"])) : old_channel;
reason_id = typeof(entry["reasonid"]) !== "undefined" ? entry["reasonid"] : reason_id;
reason_msg = typeof(entry["reason_msg"]) !== "undefined" ? entry["reason_msg"] : reason_msg;
invokerid = typeof(entry["invokerid"]) !== "undefined" ? parseInt(entry["invokerid"]) : invokerid;
invokername = typeof(entry["invokername"]) !== "undefined" ? entry["invokername"] : invokername;
invokeruid = typeof(entry["invokeruid"]) !== "undefined" ? entry["invokeruid"] : invokeruid;
client = tree.findClient(parseInt(entry["clid"]));
if(!client) {
if(parseInt(entry["client_type_exact"]) == ClientType.CLIENT_MUSIC) {
client = new MusicClientEntry(parseInt(entry["clid"]), entry["client_nickname"]);
} else {
client = new ClientEntry(parseInt(entry["clid"]), entry["client_nickname"]);
}
client.properties.client_type = parseInt(entry["client_type"]);
client = tree.insertClient(client, channel);
} else {
tree.moveClient(client, channel);
}
if(this.connection_handler.client_status.queries_visible || client.properties.client_type != ClientType.CLIENT_QUERY) {
const own_channel = this.connection.client.getClient().currentChannel();
this.connection_handler.log.log(log.server.Type.CLIENT_VIEW_ENTER, {
channel_from: old_channel ? old_channel.log_data() : undefined,
channel_to: channel ? channel.log_data() : undefined,
client: client.log_data(),
invoker: this.loggable_invoker(invokeruid, invokerid, invokername),
message:reason_msg,
reason: parseInt(reason_id),
own_channel: channel == own_channel
});
2019-04-15 13:33:51 +00:00
if(reason_id == ViewReasonId.VREASON_USER_ACTION) {
if(own_channel == channel)
if(old_channel)
this.connection_handler.sound.play(Sound.USER_ENTERED);
else
this.connection_handler.sound.play(Sound.USER_ENTERED_CONNECT);
2020-03-27 22:36:57 +00:00
} else if(reason_id == ViewReasonId.VREASON_MOVED) {
if(own_channel == channel)
this.connection_handler.sound.play(Sound.USER_ENTERED_MOVED);
} else if(reason_id == ViewReasonId.VREASON_CHANNEL_KICK) {
if(own_channel == channel)
this.connection_handler.sound.play(Sound.USER_ENTERED_KICKED);
} else if(reason_id == ViewReasonId.VREASON_SYSTEM) {
2019-04-15 13:33:51 +00:00
} else {
console.warn(tr("Unknown reasonid for %o"), reason_id);
2019-04-15 13:33:51 +00:00
}
}
2019-02-23 13:15:22 +00:00
let updates: {
key: string,
value: string
}[] = [];
for(let key in entry) {
if(key == "cfid") continue;
if(key == "ctid") continue;
if(key === "invokerid") continue;
if(key === "invokername") continue;
if(key === "invokeruid") continue;
if(key === "reasonid") continue;
updates.push({key: key, value: entry[key]});
}
client.updateVariables(...updates);
/* if its a new client join, or a system reason (like we joined) */
if(!old_channel || reason_id == 2) {
/* client new join */
2019-08-21 08:00:01 +00:00
const conversation_manager = this.connection_handler.side_bar.private_conversations();
const conversation = conversation_manager.find_conversation({
unique_id: client.properties.client_unique_identifier,
client_id: client.clientId(),
name: client.clientNickName()
}, {
create: false,
attach: true
2019-08-21 08:00:01 +00:00
});
if(conversation)
client.flag_text_unread = conversation.is_unread();
2019-04-15 13:33:51 +00:00
}
2020-03-27 22:36:57 +00:00
if(client instanceof LocalClientEntry) {
client.initializeListener();
this.connection_handler.update_voice_status();
this.connection_handler.side_bar.info_frame().update_channel_talk();
const conversations = this.connection.client.side_bar.channel_conversations();
conversations.set_current_channel(client.currentChannel().channelId);
}
}
2019-02-23 13:15:22 +00:00
}
handleCommandClientLeftView(json) {
let reason_id = -1;
2019-09-12 21:59:35 +00:00
for(const entry of json) {
reason_id = entry["reasonid"] || reason_id;
let tree = this.connection.client.channelTree;
let client = tree.findClient(entry["clid"]);
if(!client) {
log.error(LogCategory.NETWORKING, tr("Unknown client left!"));
return 0;
}
if(client == this.connection.client.getClient()) {
if(reason_id == ViewReasonId.VREASON_BAN) {
this.connection.client.handleDisconnect(DisconnectReason.CLIENT_BANNED, entry);
} else if(reason_id == ViewReasonId.VREASON_SERVER_KICK) {
this.connection.client.handleDisconnect(DisconnectReason.CLIENT_KICKED, entry);
} else if(reason_id == ViewReasonId.VREASON_SERVER_SHUTDOWN) {
this.connection.client.handleDisconnect(DisconnectReason.SERVER_CLOSED, entry);
} else if(reason_id == ViewReasonId.VREASON_SERVER_STOPPED) {
this.connection.client.handleDisconnect(DisconnectReason.SERVER_CLOSED, entry);
} else {
this.connection.client.handleDisconnect(DisconnectReason.UNKNOWN, entry);
}
this.connection_handler.side_bar.info_frame().update_channel_talk();
return;
}
2019-02-23 13:15:22 +00:00
if(this.connection_handler.client_status.queries_visible || client.properties.client_type != ClientType.CLIENT_QUERY) {
const own_channel = this.connection.client.getClient().currentChannel();
let channel_from = tree.findChannel(entry["cfid"]);
let channel_to = tree.findChannel(entry["ctid"]);
const is_own_channel = channel_from == own_channel;
this.connection_handler.log.log(log.server.Type.CLIENT_VIEW_LEAVE, {
channel_from: channel_from ? channel_from.log_data() : undefined,
channel_to: channel_to ? channel_to.log_data() : undefined,
client: client.log_data(),
invoker: this.loggable_invoker(entry["invokeruid"], entry["invokerid"], entry["invokername"]),
message: entry["reasonmsg"],
reason: parseInt(entry["reasonid"]),
ban_time: parseInt(entry["bantime"]),
own_channel: is_own_channel
});
2019-02-23 13:15:22 +00:00
if(is_own_channel) {
if(reason_id == ViewReasonId.VREASON_USER_ACTION) {
this.connection_handler.sound.play(Sound.USER_LEFT);
} else if(reason_id == ViewReasonId.VREASON_SERVER_LEFT) {
this.connection_handler.sound.play(Sound.USER_LEFT_DISCONNECT);
} else if(reason_id == ViewReasonId.VREASON_SERVER_KICK) {
this.connection_handler.sound.play(Sound.USER_LEFT_KICKED_SERVER);
} else if(reason_id == ViewReasonId.VREASON_CHANNEL_KICK) {
this.connection_handler.sound.play(Sound.USER_LEFT_KICKED_CHANNEL);
} else if(reason_id == ViewReasonId.VREASON_BAN) {
this.connection_handler.sound.play(Sound.USER_LEFT_BANNED);
} else if(reason_id == ViewReasonId.VREASON_TIMEOUT) {
this.connection_handler.sound.play(Sound.USER_LEFT_TIMEOUT);
} else if(reason_id == ViewReasonId.VREASON_MOVED) {
this.connection_handler.sound.play(Sound.USER_LEFT_MOVED);
} else {
log.error(LogCategory.NETWORKING, tr("Unknown client left reason %d!"), reason_id);
}
}
if(!channel_to) {
/* client left the server */
const conversation_manager = this.connection_handler.side_bar.private_conversations();
const conversation = conversation_manager.find_conversation({
unique_id: client.properties.client_unique_identifier,
client_id: client.clientId(),
name: client.clientNickName()
}, {
create: false,
attach: false
});
if(conversation) {
conversation.set_state(chat.PrivateConversationState.DISCONNECTED);
}
}
}
tree.deleteClient(client);
2019-02-23 13:15:22 +00:00
}
2020-03-27 22:36:57 +00:00
}
2019-02-23 13:15:22 +00:00
handleNotifyClientMoved(json) {
json = json[0]; //Only one bulk
let tree = this.connection.client.channelTree;
let client = tree.findClient(json["clid"]);
let self = client instanceof LocalClientEntry;
2019-08-21 08:00:01 +00:00
let channel_to = tree.findChannel(parseInt(json["ctid"]));
let channel_from = tree.findChannel(parseInt(json["cfid"]));
2019-08-21 08:00:01 +00:00
if(!client) {
log.error(LogCategory.NETWORKING, tr("Unknown client move (Client)!"));
return 0;
2019-02-23 13:15:22 +00:00
}
if(!channel_to) {
log.error(LogCategory.NETWORKING, tr("Unknown client move (Channel to)!"));
return 0;
}
2019-02-23 13:15:22 +00:00
if(!self) {
if(!channel_from) {
log.error(LogCategory.NETWORKING, tr("Unknown client move (Channel from)!"));
channel_from = client.currentChannel();
} else if(channel_from != client.currentChannel()) {
log.error(LogCategory.NETWORKING,
tr("Client move from invalid source channel! Local client registered in channel %d but server send %d."),
client.currentChannel().channelId, channel_from.channelId
);
}
} else {
channel_from = client.currentChannel();
2019-02-23 13:15:22 +00:00
}
tree.moveClient(client, channel_to);
2019-02-23 13:15:22 +00:00
2020-03-27 22:36:57 +00:00
if(self) {
this.connection_handler.update_voice_status(channel_to);
2019-02-23 13:15:22 +00:00
for(const entry of client.channelTree.clientsByChannel(channel_from)) {
if(entry !== client && entry.get_audio_handle()) {
entry.get_audio_handle().abort_replay();
entry.speaking = false;
}
}
2019-02-23 13:15:22 +00:00
const side_bar = this.connection_handler.side_bar;
side_bar.info_frame().update_channel_talk();
2019-02-23 13:15:22 +00:00
const conversation_to = side_bar.channel_conversations().conversation(channel_to.channelId, false);
if(conversation_to)
conversation_to.update_private_state();
2019-08-21 08:00:01 +00:00
if(channel_from) {
const conversation_from = side_bar.channel_conversations().conversation(channel_from.channelId, false);
if(conversation_from)
conversation_from.update_private_state();
}
2019-08-21 08:00:01 +00:00
side_bar.channel_conversations().update_chat_box();
}
2019-08-21 08:00:01 +00:00
const own_channel = this.connection.client.getClient().currentChannel();
this.connection_handler.log.log(log.server.Type.CLIENT_VIEW_MOVE, {
channel_from: channel_from ? {
channel_id: channel_from.channelId,
channel_name: channel_from.channelName()
} : undefined,
channel_from_own: channel_from == own_channel,
channel_to: channel_to ? {
channel_id: channel_to.channelId,
channel_name: channel_to.channelName()
} : undefined,
channel_to_own: channel_to == own_channel,
client: {
client_id: client.clientId(),
client_name: client.clientNickName(),
client_unique_id: client.properties.client_unique_identifier
},
client_own: self,
2019-08-21 08:00:01 +00:00
invoker: this.loggable_invoker(json["invokeruid"], json["invokerid"], json["invokername"]),
2019-08-21 08:00:01 +00:00
message: json["reasonmsg"],
reason: parseInt(json["reasonid"]),
});
if(json["reasonid"] == ViewReasonId.VREASON_MOVED) {
if(self)
this.connection_handler.sound.play(Sound.USER_MOVED_SELF);
else if(own_channel == channel_to)
this.connection_handler.sound.play(Sound.USER_ENTERED_MOVED);
else if(own_channel == channel_from)
this.connection_handler.sound.play(Sound.USER_LEFT_MOVED);
} else if(json["reasonid"] == ViewReasonId.VREASON_USER_ACTION) {
if(self) {} //If we do an action we wait for the error response
else if(own_channel == channel_to)
this.connection_handler.sound.play(Sound.USER_ENTERED);
else if(own_channel == channel_from)
this.connection_handler.sound.play(Sound.USER_LEFT);
} else if(json["reasonid"] == ViewReasonId.VREASON_CHANNEL_KICK) {
if(self) {
this.connection_handler.sound.play(Sound.CHANNEL_KICKED);
} else if(own_channel == channel_to)
this.connection_handler.sound.play(Sound.USER_ENTERED_KICKED);
else if(own_channel == channel_from)
this.connection_handler.sound.play(Sound.USER_LEFT_KICKED_CHANNEL);
} else {
console.warn(tr("Unknown reason id %o"), json["reasonid"]);
}
2020-03-27 22:36:57 +00:00
}
2019-08-21 08:00:01 +00:00
handleNotifyChannelMoved(json) {
json = json[0]; //Only one bulk
2019-08-21 08:00:01 +00:00
let tree = this.connection.client.channelTree;
let channel = tree.findChannel(json["cid"]);
if(!channel) {
log.error(LogCategory.NETWORKING, tr("Unknown channel move (Channel)!"));
return 0;
}
2019-08-21 08:00:01 +00:00
let prev = tree.findChannel(json["order"]);
if(!prev && json["order"] != 0) {
log.error(LogCategory.NETWORKING, tr("Unknown channel move (prev)!"));
return 0;
2019-02-23 13:15:22 +00:00
}
2019-08-30 21:06:39 +00:00
let parent = tree.findChannel(json["cpid"]);
if(!parent && json["cpid"] != 0) {
log.error(LogCategory.NETWORKING, tr("Unknown channel move (parent)!"));
return 0;
2020-03-27 22:36:57 +00:00
}
2019-08-30 21:06:39 +00:00
tree.moveChannel(channel, prev, parent);
}
2020-03-27 22:36:57 +00:00
handleNotifyChannelEdited(json) {
json = json[0]; //Only one bulk
let tree = this.connection.client.channelTree;
let channel = tree.findChannel(json["cid"]);
if(!channel) {
log.error(LogCategory.NETWORKING, tr("Unknown channel edit (Channel)!"));
return 0;
}
let updates: {
key: string,
value: string
}[] = [];
for(let key in json) {
if(key === "cid") continue;
if(key === "invokerid") continue;
if(key === "invokername") continue;
if(key === "invokeruid") continue;
if(key === "reasonid") continue;
updates.push({key: key, value: json[key]});
2020-03-27 22:36:57 +00:00
}
channel.updateVariables(...updates);
if(this.connection_handler.getClient().currentChannel() === channel) {
//TODO: Playback sound that your channel has been edited
this.connection_handler.update_voice_status();
2020-03-27 22:36:57 +00:00
}
}
2019-08-30 21:06:39 +00:00
handleNotifyTextMessage(json) {
json = json[0]; //Only one bulk
2019-03-17 11:15:39 +00:00
let mode = json["targetmode"];
if(mode == 1){
//json["invokerid"], json["invokername"], json["invokeruid"]
const target_client_id = parseInt(json["target"]);
const target_own = target_client_id === this.connection.client.getClientId();
if(target_own && target_client_id === json["invokerid"]) {
log.error(LogCategory.NETWORKING, tr("Received conversation message from invalid client id. Data: %o", json));
return;
}
const conversation_manager = this.connection_handler.side_bar.private_conversations();
const conversation = conversation_manager.find_conversation({
client_id: target_own ? parseInt(json["invokerid"]) : target_client_id,
unique_id: target_own ? json["invokeruid"] : undefined,
name: target_own ? json["invokername"] : undefined
}, {
create: target_own,
attach: target_own
});
if(!conversation) {
log.error(LogCategory.NETWORKING, tr("Received conversation message for unknown conversation! (%s)"), target_own ? tr("Remote message") : tr("Own message"));
return;
}
conversation.append_message(json["msg"], {
type: target_own ? "partner" : "self",
name: json["invokername"],
unique_id: json["invokeruid"],
2020-03-27 22:36:57 +00:00
client_id: parseInt(json["invokerid"])
});
if(target_own) {
this.connection_handler.sound.play(Sound.MESSAGE_RECEIVED, {default_volume: .5});
const client = this.connection_handler.channelTree.findClient(parseInt(json["invokerid"]));
if(client) /* the client itself might be invisible */
client.flag_text_unread = conversation.is_unread();
} else {
this.connection_handler.sound.play(Sound.MESSAGE_SEND, {default_volume: .5});
}
} else if(mode == 2) {
const invoker = this.connection_handler.channelTree.findClient(parseInt(json["invokerid"]));
const own_channel_id = this.connection.client.getClient().currentChannel().channelId;
const channel_id = typeof(json["cid"]) !== "undefined" ? parseInt(json["cid"]) : own_channel_id;
const channel = this.connection_handler.channelTree.findChannel(channel_id) || this.connection_handler.getClient().currentChannel();
if(json["invokerid"] == this.connection.client.clientId)
this.connection_handler.sound.play(Sound.MESSAGE_SEND, {default_volume: .5});
else if(channel_id == own_channel_id) {
this.connection_handler.sound.play(Sound.MESSAGE_RECEIVED, {default_volume: .5});
2020-03-27 22:36:57 +00:00
}
2019-03-17 11:15:39 +00:00
const conversations = this.connection_handler.side_bar.channel_conversations();
const conversation = conversations.conversation(channel_id);
conversation.register_new_message({
sender_database_id: invoker ? invoker.properties.client_database_id : 0,
sender_name: json["invokername"],
sender_unique_id: json["invokeruid"],
2019-03-17 11:15:39 +00:00
timestamp: typeof(json["timestamp"]) === "undefined" ? Date.now() : parseInt(json["timestamp"]),
message: json["msg"]
});
if(conversation.is_unread() && channel)
channel.flag_text_unread = true;
} else if(mode == 3) {
this.connection_handler.log.log(log.server.Type.GLOBAL_MESSAGE, {
message: json["msg"],
sender: {
client_unique_id: json["invokeruid"],
client_name: json["invokername"],
client_id: parseInt(json["invokerid"])
}
});
2019-03-17 11:15:39 +00:00
const invoker = this.connection_handler.channelTree.findClient(parseInt(json["invokerid"]));
const conversations = this.connection_handler.side_bar.channel_conversations();
const conversation = conversations.conversation(0);
conversation.register_new_message({
sender_database_id: invoker ? invoker.properties.client_database_id : 0,
sender_name: json["invokername"],
sender_unique_id: json["invokeruid"],
2019-02-23 13:15:22 +00:00
timestamp: typeof(json["timestamp"]) === "undefined" ? Date.now() : parseInt(json["timestamp"]),
message: json["msg"]
});
this.connection_handler.channelTree.server.flag_text_unread = conversation.is_unread();
}
2019-02-23 13:15:22 +00:00
}
notifyClientChatComposing(json) {
json = json[0];
2019-02-23 13:15:22 +00:00
const conversation_manager = this.connection_handler.side_bar.private_conversations();
const conversation = conversation_manager.find_conversation({
client_id: parseInt(json["clid"]),
unique_id: json["cluid"],
name: undefined
}, {
create: false,
attach: false
});
if(!conversation)
return;
2019-02-23 13:15:22 +00:00
conversation.trigger_typing();
2019-02-23 13:15:22 +00:00
}
handleNotifyClientChatClosed(json) {
json = json[0]; //Only one bulk
2019-02-23 13:15:22 +00:00
//Chat partner has closed the conversation
2019-02-23 13:15:22 +00:00
//clid: "6"
//cluid: "YoWmG+dRGKD+Rxb7SPLAM5+B9tY="
const conversation_manager = this.connection_handler.side_bar.private_conversations();
const conversation = conversation_manager.find_conversation({
client_id: parseInt(json["clid"]),
unique_id: json["cluid"],
name: undefined
}, {
create: false,
attach: false
});
if(!conversation) {
log.warn(LogCategory.GENERAL, tr("Received chat close for client, but we haven't a chat open."));
return;
}
conversation.set_state(chat.PrivateConversationState.CLOSED);
2019-02-23 13:15:22 +00:00
}
handleNotifyClientUpdated(json) {
json = json[0]; //Only one bulk
2019-02-23 13:15:22 +00:00
let client = this.connection.client.channelTree.findClient(json["clid"]);
if(!client) {
log.error(LogCategory.NETWORKING, tr("Tried to update an non existing client"));
return;
}
2019-02-23 13:15:22 +00:00
let updates: {
key: string,
value: string
}[] = [];
for(let key in json) {
if(key == "clid") continue;
updates.push({key: key, value: json[key]});
}
client.updateVariables(...updates);
2019-02-23 13:15:22 +00:00
}
handleNotifyServerEdited(json) {
json = json[0];
2019-02-23 13:15:22 +00:00
let updates: {
key: string,
value: string
}[] = [];
for(let key in json) {
if(key === "invokerid") continue;
if(key === "invokername") continue;
if(key === "invokeruid") continue;
if(key === "reasonid") continue;
2019-02-23 13:15:22 +00:00
updates.push({key: key, value: json[key]});
}
this.connection.client.channelTree.server.updateVariables(false, ...updates);
}
2019-02-23 13:15:22 +00:00
handleNotifyServerUpdated(json) {
json = json[0];
2019-02-23 13:15:22 +00:00
let updates: {
key: string,
value: string
}[] = [];
for(let key in json) {
if(key === "invokerid") continue;
if(key === "invokername") continue;
if(key === "invokeruid") continue;
if(key === "reasonid") continue;
2019-02-23 13:15:22 +00:00
updates.push({key: key, value: json[key]});
}
this.connection.client.channelTree.server.updateVariables(true, ...updates);
}
2019-02-23 13:15:22 +00:00
handleNotifyMusicPlayerInfo(json) {
json = json[0];
2019-03-17 11:15:39 +00:00
let bot = this.connection.client.channelTree.find_client_by_dbid(json["bot_id"]);
if(!bot || !(bot instanceof MusicClientEntry)) {
log.warn(LogCategory.CLIENT, tr("Got music player info for unknown or invalid bot! (ID: %i, Entry: %o)"), json["bot_id"], bot);
return;
}
2019-03-17 11:15:39 +00:00
bot.handlePlayerInfo(json);
2019-03-17 11:15:39 +00:00
}
handleNotifyClientPoke(json) {
json = json[0];
Modals.spawnPoke(this.connection_handler, {
id: parseInt(json["invokerid"]),
name: json["invokername"],
unique_id: json["invokeruid"]
}, json["msg"]);
2019-03-17 11:15:39 +00:00
this.connection_handler.sound.play(Sound.USER_POKED_SELF);
2019-03-17 11:15:39 +00:00
}
2019-08-21 08:00:01 +00:00
//TODO server chat message
handleNotifyServerGroupClientAdd(json) {
json = json[0];
2019-08-21 08:00:01 +00:00
const self = this.connection.client.getClient();
if(json["clid"] == self.clientId())
this.connection_handler.sound.play(Sound.GROUP_SERVER_ASSIGNED_SELF);
2020-03-27 22:36:57 +00:00
}
//TODO server chat message
handleNotifyServerGroupClientRemove(json) {
json = json[0];
const self = this.connection.client.getClient();
if(json["clid"] == self.clientId()) {
this.connection_handler.sound.play(Sound.GROUP_SERVER_REVOKED_SELF);
} else {
2019-08-21 08:00:01 +00:00
}
}
//TODO server chat message
handleNotifyClientChannelGroupChanged(json) {
json = json[0];
2019-09-12 21:59:35 +00:00
const self = this.connection.client.getClient();
if(json["clid"] == self.clientId()) {
this.connection_handler.sound.play(Sound.GROUP_CHANNEL_CHANGED_SELF);
}
2019-08-21 08:00:01 +00:00
}
handleNotifyChannelSubscribed(json) {
for(const entry of json) {
const channel = this.connection.client.channelTree.findChannel(entry["cid"]);
if(!channel) {
console.warn(tr("Received channel subscribed for not visible channel (cid: %d)"), entry['cid']);
continue;
}
channel.flag_subscribed = true;
}
2020-03-27 22:36:57 +00:00
}
2019-08-21 08:00:01 +00:00
handleNotifyChannelUnsubscribed(json) {
for(const entry of json) {
const channel = this.connection.client.channelTree.findChannel(entry["cid"]);
if(!channel) {
console.warn(tr("Received channel unsubscribed for not visible channel (cid: %d)"), entry['cid']);
continue;
}
channel.flag_subscribed = false;
for(const client of channel.clients(false))
this.connection.client.channelTree.deleteClient(client);
}
2019-08-21 08:00:01 +00:00
}
2020-02-02 14:05:36 +00:00
handleNotifyConversationHistory(json: any[]) {
const conversations = this.connection.client.side_bar.channel_conversations();
const conversation = conversations.conversation(parseInt(json[0]["cid"]));
if(!conversation) {
log.warn(LogCategory.NETWORKING, tr("Received conversation history for invalid or unknown conversation (%o)"), json[0]["cid"]);
return;
}
2020-02-02 14:05:36 +00:00
for(const entry of json) {
conversation.register_new_message({
message: entry["msg"],
sender_unique_id: entry["sender_unique_id"],
sender_name: entry["sender_name"],
timestamp: parseInt(entry["timestamp"]),
sender_database_id: parseInt(entry["sender_database_id"])
}, false);
}
2020-02-02 14:05:36 +00:00
/* now update the boxes */
/* No update needed because the command which triggers this notify should update the chat box on success
conversation.fix_scroll(true);
conversation.handle.update_chat_box();
*/
2020-02-02 14:05:36 +00:00
}
handleNotifyConversationMessageDelete(json: any[]) {
let conversation: Conversation;
const conversations = this.connection.client.side_bar.channel_conversations();
for(const entry of json) {
if(typeof(entry["cid"]) !== "undefined")
conversation = conversations.conversation(parseInt(entry["cid"]), false);
if(!conversation)
continue;
2020-02-02 14:05:36 +00:00
conversation.delete_messages(parseInt(entry["timestamp_begin"]), parseInt(entry["timestamp_end"]), parseInt(entry["cldbid"]), parseInt(entry["limit"]));
}
2020-03-27 22:36:57 +00:00
}
2020-02-02 14:05:36 +00:00
handleNotifyMusicStatusUpdate(json: any[]) {
json = json[0];
2020-02-02 14:05:36 +00:00
const bot_id = parseInt(json["bot_id"]);
const client = this.connection.client.channelTree.find_client_by_dbid(bot_id);
if(!client) {
log.warn(LogCategory.CLIENT, tr("Received music bot status update for unknown bot (%d)"), bot_id);
return;
}
2020-03-27 22:36:57 +00:00
client.events.fire("music_status_update", {
player_replay_index: parseInt(json["player_replay_index"]),
player_buffered_index: parseInt(json["player_buffered_index"])
});
2020-02-02 14:05:36 +00:00
}
handleMusicPlayerSongChange(json: any[]) {
json = json[0];
2020-02-02 14:05:36 +00:00
const bot_id = parseInt(json["bot_id"]);
const client = this.connection.client.channelTree.find_client_by_dbid(bot_id);
if(!client) {
log.warn(LogCategory.CLIENT, tr("Received music bot status update for unknown bot (%d)"), bot_id);
return;
}
2020-02-02 14:05:36 +00:00
const song_id = parseInt(json["song_id"]);
let song: SongInfo;
if(song_id) {
song = new SongInfo();
JSON.map_to(song, json);
}
2020-02-02 14:05:36 +00:00
client.events.fire("music_song_change", {
song: song
});
2020-02-02 14:05:36 +00:00
}
handleNotifyPlaylistSongAdd(json: any[]) {
json = json[0];
2020-02-02 14:05:36 +00:00
const playlist_id = parseInt(json["playlist_id"]);
const client = this.connection.client.channelTree.clients.find(e => e instanceof MusicClientEntry && e.properties.client_playlist_id === playlist_id);
if(!client) {
log.warn(LogCategory.CLIENT, tr("Received playlist song add event, but we've no music bot for the playlist (%d)"), playlist_id);
return;
2020-02-02 14:05:36 +00:00
}
client.events.fire("playlist_song_add", {
song: {
song_id: parseInt(json["song_id"]),
song_invoker: json["song_invoker"],
song_previous_song_id: parseInt(json["song_previous_song_id"]),
song_url: json["song_url"],
song_url_loader: json["song_url_loader"],
2020-03-27 22:36:57 +00:00
song_loaded: json["song_loaded"] == true || json["song_loaded"] == "1",
song_metadata: json["song_metadata"]
}
});
2020-02-02 14:05:36 +00:00
}
handleNotifyPlaylistSongRemove(json: any[]) {
json = json[0];
2020-02-02 14:05:36 +00:00
const playlist_id = parseInt(json["playlist_id"]);
const client = this.connection.client.channelTree.clients.find(e => e instanceof MusicClientEntry && e.properties.client_playlist_id === playlist_id);
if(!client) {
log.warn(LogCategory.CLIENT, tr("Received playlist song remove event, but we've no music bot for the playlist (%d)"), playlist_id);
return;
}
2020-02-02 14:05:36 +00:00
const song_id = parseInt(json["song_id"]);
client.events.fire("playlist_song_remove", { song_id: song_id });
2020-02-02 14:05:36 +00:00
}
handleNotifyPlaylistSongReorder(json: any[]) {
json = json[0];
2020-02-02 14:05:36 +00:00
const playlist_id = parseInt(json["playlist_id"]);
const client = this.connection.client.channelTree.clients.find(e => e instanceof MusicClientEntry && e.properties.client_playlist_id === playlist_id);
if(!client) {
log.warn(LogCategory.CLIENT, tr("Received playlist song reorder event, but we've no music bot for the playlist (%d)"), playlist_id);
return;
}
2020-02-02 14:05:36 +00:00
const song_id = parseInt(json["song_id"]);
const previous_song_id = parseInt(json["song_previous_song_id"]);
client.events.fire("playlist_song_reorder", { song_id: song_id, previous_song_id: previous_song_id });
2020-02-02 14:05:36 +00:00
}
2020-03-27 22:36:57 +00:00
handleNotifyPlaylistSongLoaded(json: any[]) {
json = json[0];
const playlist_id = parseInt(json["playlist_id"]);
const client = this.connection.client.channelTree.clients.find(e => e instanceof MusicClientEntry && e.properties.client_playlist_id === playlist_id);
if(!client) {
log.warn(LogCategory.CLIENT, tr("Received playlist song loaded event, but we've no music bot for the playlist (%d)"), playlist_id);
return;
}
const song_id = parseInt(json["song_id"]);
client.events.fire("playlist_song_loaded", {
song_id: song_id,
success: json["success"] == 1,
error_msg: json["load_error_msg"],
metadata: json["song_metadata"]
});
}
2019-02-23 13:15:22 +00:00
}
}