1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
Description: Changes the current channel of the calling socket
*/
// module main
export async function run(core, server, socket, data) {
// check for spam
if (server.police.frisk(socket.address, 6)) {
return server.reply({
cmd: 'warn',
text: 'You are changing channels too fast. Wait a moment before trying again.',
}, socket);
}
// check user input
if (typeof data.channel !== 'string') {
return true;
}
if (data.channel === '') {
return server.reply({
cmd: 'warn',
text: 'Cannot move to an empty channel.',
}, socket);
}
if (data.channel === socket.channel) {
// they are trying to rejoin the channel
return true;
}
// check that the nickname isn't already in target channel
const currentNick = socket.nick.toLowerCase();
const userExists = server.findSockets({
channel: data.channel,
nick: (targetNick) => targetNick.toLowerCase() === currentNick,
});
if (userExists.length > 0) {
// That nickname is already in that channel
return true;
}
// broadcast leave notice to peers
const peerList = server.findSockets({ channel: socket.channel });
if (peerList.length > 1) {
for (let i = 0, l = peerList.length; i < l; i += 1) {
server.reply({
cmd: 'onlineRemove',
nick: peerList[i].nick,
}, socket);
if (socket.nick !== peerList[i].nick) {
server.reply({
cmd: 'onlineRemove',
nick: socket.nick,
}, peerList[i]);
}
}
}
// TODO: import function from join module
// broadcast join notice to new peers
const newPeerList = server.findSockets({ channel: data.channel });
const moveAnnouncement = {
cmd: 'onlineAdd',
nick: socket.nick,
trip: socket.trip || 'null',
hash: socket.hash,
};
const nicks = [];
for (let i = 0, l = newPeerList.length; i < l; i += 1) {
server.reply(moveAnnouncement, newPeerList[i]);
nicks.push(newPeerList[i].nick);
}
nicks.push(socket.nick);
// reply with new user list
server.reply({
cmd: 'onlineSet',
nicks,
}, socket);
// commit change
socket.channel = data.channel;
return true;
}
// module hook functions
export function initHooks(server) {
server.registerHook('in', 'chat', this.moveCheck.bind(this), 29);
}
export function moveCheck(core, server, socket, payload) {
if (typeof payload.text !== 'string') {
return false;
}
if (payload.text.startsWith('/move ')) {
const input = payload.text.split(' ');
// If there is no channel target parameter
if (input[1] === undefined) {
server.reply({
cmd: 'warn',
text: 'Refer to `/help move` for instructions on how to use this command.',
}, socket);
return false;
}
this.run(core, server, socket, {
cmd: 'move',
channel: input[1],
});
return false;
}
return payload;
}
export const requiredData = ['channel'];
export const info = {
name: 'move',
description: 'This will change your current channel to the new one provided',
usage: `
API: { cmd: 'move', channel: '<target channel>' }
Text: /move <new channel>`,
};
|