2017-06-21 06:40:28 +00:00
|
|
|
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';
|
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';
|
2018-04-25 03:36:27 +00:00
|
|
|
import { find, normalizeChannel } from 'utils';
|
2015-01-21 02:06:34 +00:00
|
|
|
|
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
|
|
|
|
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));
|
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));
|
|
|
|
},
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages({ messages, server, to, prepend, next }) {
|
|
|
|
dispatch(addMessages(messages, server, to, prepend, next));
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
join({ user, server, channels }) {
|
2017-04-11 01:49:52 +00:00
|
|
|
const state = getState();
|
2017-05-02 21:21:25 +00:00
|
|
|
const tab = state.tab.selected;
|
|
|
|
const [joinedChannel] = channels;
|
|
|
|
if (tab.server && tab.name) {
|
2018-04-25 03:36:27 +00:00
|
|
|
const { nick } = state.servers[tab.server];
|
2018-04-05 23:46:22 +00:00
|
|
|
if (
|
|
|
|
tab.server === server &&
|
2017-05-02 21:21:25 +00:00
|
|
|
nick === user &&
|
|
|
|
tab.name !== joinedChannel &&
|
2018-04-05 23:46:22 +00:00
|
|
|
normalizeChannel(tab.name) === normalizeChannel(joinedChannel)
|
|
|
|
) {
|
2017-04-11 01:49:52 +00:00
|
|
|
dispatch(select(server, joinedChannel));
|
|
|
|
}
|
2016-01-13 19:22:41 +00:00
|
|
|
}
|
2017-04-11 01:49:52 +00:00
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
dispatch(inform(`${user} joined the channel`, server, joinedChannel));
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
part({ user, server, channel, reason }) {
|
2018-04-05 23:46:22 +00:00
|
|
|
dispatch(
|
|
|
|
inform(withReason(`${user} left the channel`, reason), server, channel)
|
|
|
|
);
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
quit({ user, server, reason }) {
|
|
|
|
const channels = findChannels(getState(), server, user);
|
|
|
|
dispatch(broadcast(withReason(`${user} quit`, reason), server, channels));
|
|
|
|
},
|
|
|
|
|
2017-06-21 05:23:07 +00:00
|
|
|
nick({ server, oldNick, newNick }) {
|
|
|
|
const channels = findChannels(getState(), server, oldNick);
|
2018-04-05 23:46:22 +00:00
|
|
|
dispatch(
|
|
|
|
broadcast(`${oldNick} changed nick to ${newNick}`, server, channels)
|
|
|
|
);
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-11 01:49:52 +00:00
|
|
|
motd({ content, server }) {
|
2017-04-19 23:51:55 +00:00
|
|
|
dispatch(addMessages(content.map(line => ({ content: line })), server));
|
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
|
|
|
|
)
|
|
|
|
);
|
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));
|
2017-05-29 04:16:24 +00:00
|
|
|
},
|
|
|
|
|
2017-07-04 09:28:56 +00:00
|
|
|
connection_update({ server, errorType }) {
|
2018-04-05 23:46:22 +00:00
|
|
|
if (
|
|
|
|
errorType === 'verify' &&
|
|
|
|
confirm(
|
|
|
|
'The server is using a self-signed certificate, continue anyway?'
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
dispatch(
|
|
|
|
reconnect(server, {
|
|
|
|
skipVerify: true
|
|
|
|
})
|
|
|
|
);
|
2017-07-04 09:28:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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
|
|
|
}
|
2017-04-11 01:49:52 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +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) };
|
|
|
|
}
|
|
|
|
|
2017-04-11 01:49:52 +00:00
|
|
|
if (type in handlers) {
|
|
|
|
handlers[type](data);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 23:04:49 +00:00
|
|
|
if (type.charAt(0) === '_') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-02 22:52:16 +00:00
|
|
|
dispatch(action);
|
2015-12-28 23:34:32 +00:00
|
|
|
});
|
|
|
|
}
|