2017-06-21 06:40:28 +00:00
|
|
|
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,
|
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';
|
2020-06-15 08:58:51 +00:00
|
|
|
import { reconnect } from 'state/networks';
|
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
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
function findChannels(state, network, user) {
|
2017-04-11 01:49:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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) {
|
2020-06-15 08:58:51 +00:00
|
|
|
dispatch(addMessage(message, message.network, message.to));
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
pm(message) {
|
2020-06-15 08:58:51 +00:00
|
|
|
dispatch(addMessage(message, message.network, message.from));
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-04-19 23:51:55 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
messages({ messages, network, to, prepend, next }) {
|
|
|
|
dispatch(addMessages(messages, network, to, prepend, next));
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
join({ user, network, channels }) {
|
|
|
|
dispatch(addEvent(network, channels[0], 'join', user));
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
part({ user, network, channel, reason }) {
|
|
|
|
dispatch(addEvent(network, channel, 'part', user, reason));
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
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));
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
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
|
|
|
}
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
topic({ network, channel, topic, nick }) {
|
2017-05-28 05:20:43 +00:00
|
|
|
if (nick) {
|
2020-06-15 08:58:51 +00:00
|
|
|
dispatch(addEvent(network, channel, 'topic', nick, topic));
|
2017-05-28 05:20:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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
|
|
|
)
|
|
|
|
);
|
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}`
|
|
|
|
],
|
2020-06-15 08:58:51 +00:00
|
|
|
tab.network,
|
2018-04-05 23:46:22 +00:00
|
|
|
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;
|
2020-06-15 08:58:51 +00:00
|
|
|
dispatch(addMessage(message, tab.network, tab.name));
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2017-05-29 04:16:24 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
error({ network, target, message }) {
|
2020-06-21 03:13:38 +00:00
|
|
|
const state = getState();
|
|
|
|
const tab = state.tab.selected;
|
|
|
|
|
|
|
|
if (network === tab.network) {
|
|
|
|
// Print it in the current channel if the error happened on
|
|
|
|
// the current network
|
|
|
|
target = tab.name;
|
|
|
|
} else if (!state.channels[network]?.[target]) {
|
|
|
|
// Print it the network tab if the target does not exist
|
|
|
|
target = null;
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
dispatch(
|
|
|
|
addMessage({ content: message, type: 'error' }, network, target)
|
|
|
|
);
|
2020-06-05 06:56:11 +00:00
|
|
|
return false;
|
2019-01-25 10:02:31 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
connection_update({ network, 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:
|
2020-06-15 08:58:51 +00:00
|
|
|
'The network is using a self-signed certificate, continue anyway?',
|
2019-01-05 06:08:34 +00:00
|
|
|
onConfirm: () =>
|
|
|
|
dispatch(
|
2020-06-15 08:58:51 +00:00
|
|
|
reconnect(network, {
|
2019-01-05 06:08:34 +00:00
|
|
|
skipVerify: true
|
|
|
|
})
|
|
|
|
)
|
2018-04-05 23:46:22 +00:00
|
|
|
})
|
|
|
|
);
|
2017-07-04 09:28:56 +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
|
|
|
}
|
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;
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
if (
|
|
|
|
selected.network === forward.network &&
|
|
|
|
selected.name === forward.old
|
|
|
|
) {
|
|
|
|
dispatch(select(forward.network, forward.new, true));
|
2019-01-25 10:02:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|