dispatch/client/js/modules/socket.js

185 lines
4.5 KiB
JavaScript
Raw Normal View History

import { socketAction } from 'state/actions';
2020-06-15 08:58:51 +00:00
import { kicked } from 'state/channels';
2018-04-05 23:46:22 +00:00
import {
print,
addMessage,
addMessages,
addEvent,
broadcastEvent
2018-04-05 23:46:22 +00:00
} from 'state/messages';
import { openModal } from 'state/modals';
2020-06-15 08:58:51 +00:00
import { reconnect } from 'state/networks';
import { select } from 'state/tab';
import { find } from 'utils';
2020-06-15 08:58:51 +00:00
function findChannels(state, network, user) {
const channels = [];
2020-06-15 08:58:51 +00:00
Object.keys(state.channels[network]).forEach(channel => {
if (find(state.channels[network][channel].users, u => u.nick === user)) {
2018-04-25 03:36:27 +00:00
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) {
2020-06-15 08:58:51 +00:00
dispatch(addMessage(message, message.network, message.to));
return false;
},
pm(message) {
2020-06-15 08:58:51 +00:00
dispatch(addMessage(message, message.network, message.from));
return false;
},
2020-06-15 08:58:51 +00:00
messages({ messages, network, to, prepend, next }) {
dispatch(addMessages(messages, network, to, prepend, next));
return false;
},
2020-06-15 08:58:51 +00:00
join({ user, network, channels }) {
dispatch(addEvent(network, channels[0], 'join', user));
},
2020-06-15 08:58:51 +00:00
part({ user, network, channel, reason }) {
dispatch(addEvent(network, channel, 'part', user, reason));
},
2020-06-15 08:58:51 +00:00
quit({ user, network, reason }) {
const channels = findChannels(getState(), network, user);
dispatch(broadcastEvent(network, channels, 'quit', user, reason));
},
2020-06-15 08:58:51 +00:00
kick({ network, channel, sender, user, reason }) {
dispatch(kicked(network, channel, user));
dispatch(addEvent(network, channel, 'kick', user, sender, reason));
},
nick({ network, oldNick, newNick }) {
2018-12-31 01:20:22 +00:00
if (oldNick) {
2020-06-15 08:58:51 +00:00
const channels = findChannels(getState(), network, oldNick);
dispatch(broadcastEvent(network, channels, 'nick', oldNick, newNick));
2018-12-31 01:20:22 +00:00
}
},
2020-06-15 08:58:51 +00:00
topic({ network, channel, topic, nick }) {
if (nick) {
2020-06-15 08:58:51 +00:00
dispatch(addEvent(network, channel, 'topic', nick, topic));
}
},
2020-06-15 08:58:51 +00:00
motd({ content, network }) {
2020-04-30 05:54:30 +00:00
dispatch(
addMessages(
content.map(line => ({ content: line })),
2020-06-15 08:58:51 +00:00
network
2020-04-30 05:54:30 +00:00
)
);
return false;
},
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}`
],
2020-06-15 08:58:51 +00:00
tab.network,
2018-04-05 23:46:22 +00:00
tab.name
)
);
return false;
},
print(message) {
const tab = getState().tab.selected;
2020-06-15 08:58:51 +00:00
dispatch(addMessage(message, tab.network, tab.name));
return false;
2017-05-29 04:16:24 +00:00
},
2020-06-15 08:58:51 +00:00
error({ network, target, message }) {
dispatch(
addMessage({ content: message, type: 'error' }, network, target)
);
return false;
},
2020-06-15 08:58:51 +00:00
connection_update({ network, errorType }) {
if (errorType === 'verify') {
2018-04-05 23:46:22 +00:00
dispatch(
openModal('confirm', {
question:
2020-06-15 08:58:51 +00:00
'The network is using a self-signed certificate, continue anyway?',
onConfirm: () =>
dispatch(
2020-06-15 08:58:51 +00:00
reconnect(network, {
skipVerify: true
})
)
2018-04-05 23:46:22 +00:00
})
);
}
},
2020-06-15 08:58:51 +00:00
dcc_send({ network, from, filename, url }) {
const networkName = getState().networks[network]?.name || network;
2020-05-20 05:21:12 +00:00
dispatch(
openModal('confirm', {
2020-06-15 08:58:51 +00:00
question: `${from} on ${networkName} is sending you: ${filename}`,
2020-05-20 05:21:12 +00:00
confirmation: 'Download',
onConfirm: () => {
const a = document.createElement('a');
a.href = url;
a.click();
}
})
);
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;
2020-06-15 08:58:51 +00:00
if (
selected.network === forward.network &&
selected.name === forward.old
) {
dispatch(select(forward.network, 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 (handlers[type]?.(data) === false) {
return;
}
dispatch(action);
afterHandlers[type]?.(data);
2015-12-28 23:34:32 +00:00
});
}