dispatch/client/js/modules/socket.js

191 lines
4.6 KiB
JavaScript
Raw Normal View History

import { socketAction } from 'state/actions';
import { setConnected } from 'state/app';
2018-04-05 23:46:22 +00:00
import {
broadcast,
inform,
print,
addMessage,
addMessages
} from 'state/messages';
import { openModal } from 'state/modals';
import { reconnect } from 'state/servers';
import { select } from 'state/tab';
import { find } from 'utils';
2015-12-28 23:34:32 +00:00
function withReason(message, reason) {
return message + (reason ? ` (${reason})` : '');
2015-02-21 06:48:25 +00:00
}
2015-01-17 01:37:21 +00:00
function findChannels(state, server, user) {
const channels = [];
2018-04-25 03:36:27 +00:00
Object.keys(state.channels[server]).forEach(channel => {
if (find(state.channels[server][channel].users, u => u.nick === user)) {
channels.push(channel);
2015-12-28 23:34:32 +00:00
}
});
return channels;
}
2018-04-05 23:46:22 +00:00
export default function handleSocket({
socket,
store: { dispatch, getState }
}) {
const handlers = {
message(message) {
dispatch(addMessage(message, message.server, message.to));
},
pm(message) {
dispatch(addMessage(message, message.server, message.from));
},
2017-05-02 21:21:25 +00:00
messages({ messages, server, to, prepend, next }) {
dispatch(addMessages(messages, server, to, prepend, next));
},
2017-05-02 21:21:25 +00:00
join({ user, server, channels }) {
dispatch(inform(`${user} joined the channel`, server, channels[0]));
},
part({ user, server, channel, reason }) {
2018-04-05 23:46:22 +00:00
dispatch(
inform(withReason(`${user} left the channel`, reason), server, channel)
);
},
quit({ user, server, reason }) {
const channels = findChannels(getState(), server, user);
dispatch(broadcast(withReason(`${user} quit`, reason), server, channels));
},
nick({ server, oldNick, newNick }) {
2018-12-31 01:20:22 +00:00
if (oldNick) {
const channels = findChannels(getState(), server, oldNick);
dispatch(
broadcast(`${oldNick} changed nick to ${newNick}`, server, channels)
);
}
},
topic({ server, channel, topic, nick }) {
if (nick) {
if (topic) {
dispatch(inform(`${nick} changed the topic to:`, server, channel));
dispatch(print(topic, server, channel));
} else {
dispatch(inform(`${nick} cleared the topic`, server, channel));
}
}
},
motd({ content, server }) {
2020-04-30 05:54:30 +00:00
dispatch(
addMessages(
content.map(line => ({ content: line })),
server
)
);
},
whois(data) {
const tab = getState().tab.selected;
2018-04-05 23:46:22 +00:00
dispatch(
print(
[
`Nick: ${data.nick}`,
`Username: ${data.username}`,
`Realname: ${data.realname}`,
`Host: ${data.host}`,
`Server: ${data.server}`,
`Channels: ${data.channels}`
],
tab.server,
tab.name
)
);
},
print(message) {
const tab = getState().tab.selected;
dispatch(addMessage(message, tab.server, tab.name));
2017-05-29 04:16:24 +00:00
},
error({ server, target, message }) {
dispatch(addMessage({ content: message, type: 'error' }, server, target));
},
connection_update({ server, errorType }) {
if (errorType === 'verify') {
2018-04-05 23:46:22 +00:00
dispatch(
openModal('confirm', {
question:
'The server is using a self-signed certificate, continue anyway?',
onConfirm: () =>
dispatch(
reconnect(server, {
skipVerify: true
})
)
2018-04-05 23:46:22 +00:00
})
);
}
},
2020-05-20 05:21:12 +00:00
dcc_send({ server, from, filename, url }) {
const serverName = getState().servers[server]?.name || server;
dispatch(
openModal('confirm', {
question: `${from} on ${serverName} is sending you: ${filename}`,
confirmation: 'Download',
onConfirm: () => {
const a = document.createElement('a');
a.href = url;
a.click();
}
})
);
},
2017-05-29 04:16:24 +00:00
_connected(connected) {
2017-06-06 23:03:35 +00:00
dispatch(setConnected(connected));
2015-12-28 23:34:32 +00:00
}
};
2015-12-28 23:34:32 +00:00
const afterHandlers = {
channel_forward(forward) {
const { selected } = getState().tab;
if (selected.server === forward.server && selected.name === forward.old) {
dispatch(select(forward.server, forward.new, true));
}
}
};
socket.onMessage((type, data) => {
let action;
if (Array.isArray(data)) {
action = { type: socketAction(type), data: [...data] };
} else {
action = { ...data, type: socketAction(type) };
}
if (type in handlers) {
handlers[type](data);
2015-12-28 23:34:32 +00:00
}
if (type.charAt(0) === '_') {
return;
}
dispatch(action);
if (type in afterHandlers) {
afterHandlers[type](data);
}
2015-12-28 23:34:32 +00:00
});
}