2017-06-21 06:40:28 +00:00
|
|
|
import { socketAction } from 'state/actions';
|
2018-04-05 23:46:22 +00:00
|
|
|
import {
|
|
|
|
print,
|
|
|
|
addMessage,
|
2020-06-03 01:04:38 +00:00
|
|
|
addMessages,
|
|
|
|
addEvent,
|
|
|
|
broadcastEvent
|
2018-04-05 23:46:22 +00:00
|
|
|
} from 'state/messages';
|
2019-01-05 06:08:34 +00:00
|
|
|
import { openModal } from 'state/modals';
|
2017-07-04 09:28:56 +00:00
|
|
|
import { reconnect } from 'state/servers';
|
2017-06-21 06:40:28 +00:00
|
|
|
import { select } from 'state/tab';
|
2019-01-25 10:02:31 +00:00
|
|
|
import { find } from 'utils';
|
2015-01-21 02:06:34 +00:00
|
|
|
|
2017-04-11 01:49:52 +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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-11 01:49:52 +00:00
|
|
|
return channels;
|
|
|
|
}
|
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
export default function handleSocket({
|
|
|
|
socket,
|
|
|
|
store: { dispatch, getState }
|
|
|
|
}) {
|
2017-04-11 01:49:52 +00:00
|
|
|
const handlers = {
|
|
|
|
message(message) {
|
2017-04-19 23:51:55 +00:00
|
|
|
dispatch(addMessage(message, message.server, message.to));
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
pm(message) {
|
2017-04-19 23:51:55 +00:00
|
|
|
dispatch(addMessage(message, message.server, message.from));
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-04-19 23:51:55 +00:00
|
|
|
},
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages({ messages, server, to, prepend, next }) {
|
|
|
|
dispatch(addMessages(messages, server, to, prepend, next));
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
join({ user, server, channels }) {
|
2020-06-03 01:04:38 +00:00
|
|
|
dispatch(addEvent(server, channels[0], 'join', user));
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
part({ user, server, channel, reason }) {
|
2020-06-03 01:04:38 +00:00
|
|
|
dispatch(addEvent(server, channel, 'part', user, reason));
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
quit({ user, server, reason }) {
|
|
|
|
const channels = findChannels(getState(), server, user);
|
2020-06-03 01:04:38 +00:00
|
|
|
dispatch(broadcastEvent(server, channels, 'quit', user, reason));
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
2017-06-21 05:23:07 +00:00
|
|
|
nick({ server, oldNick, newNick }) {
|
2018-12-31 01:20:22 +00:00
|
|
|
if (oldNick) {
|
|
|
|
const channels = findChannels(getState(), server, oldNick);
|
2020-06-03 01:04:38 +00:00
|
|
|
dispatch(broadcastEvent(server, channels, 'nick', oldNick, newNick));
|
2018-12-31 01:20:22 +00:00
|
|
|
}
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
topic({ server, channel, topic, nick }) {
|
|
|
|
if (nick) {
|
2020-06-03 01:04:38 +00:00
|
|
|
dispatch(addEvent(server, channel, 'topic', nick, topic));
|
2017-05-28 05:20:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-11 01:49:52 +00:00
|
|
|
motd({ content, server }) {
|
2020-04-30 05:54:30 +00:00
|
|
|
dispatch(
|
|
|
|
addMessages(
|
|
|
|
content.map(line => ({ content: line })),
|
|
|
|
server
|
|
|
|
)
|
|
|
|
);
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
whois(data) {
|
2017-05-28 22:52:19 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
);
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
print(message) {
|
|
|
|
const tab = getState().tab.selected;
|
|
|
|
dispatch(addMessage(message, tab.server, tab.name));
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-05-29 04:16:24 +00:00
|
|
|
},
|
|
|
|
|
2019-01-25 10:02:31 +00:00
|
|
|
error({ server, target, message }) {
|
|
|
|
dispatch(addMessage({ content: message, type: 'error' }, server, target));
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2019-01-25 10:02:31 +00:00
|
|
|
},
|
|
|
|
|
2017-07-04 09:28:56 +00:00
|
|
|
connection_update({ server, errorType }) {
|
2019-01-05 06:08:34 +00:00
|
|
|
if (errorType === 'verify') {
|
2018-04-05 23:46:22 +00:00
|
|
|
dispatch(
|
2019-01-05 06:08:34 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
);
|
2017-07-04 09:28:56 +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();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
2017-04-11 01:49:52 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2019-01-25 10:02:31 +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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-11 01:49:52 +00:00
|
|
|
socket.onMessage((type, data) => {
|
2017-07-02 22:52:16 +00:00
|
|
|
let action;
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
action = { type: socketAction(type), data: [...data] };
|
|
|
|
} else {
|
|
|
|
action = { ...data, type: socketAction(type) };
|
|
|
|
}
|
|
|
|
|
2020-06-05 06:56:11 +00:00
|
|
|
if (handlers[type]?.(data) === false) {
|
2018-10-15 23:04:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-02 22:52:16 +00:00
|
|
|
dispatch(action);
|
2019-01-25 10:02:31 +00:00
|
|
|
|
2020-06-05 06:56:11 +00:00
|
|
|
afterHandlers[type]?.(data);
|
2015-12-28 23:34:32 +00:00
|
|
|
});
|
|
|
|
}
|