2015-12-28 23:34:32 +00:00
|
|
|
import { broadcast, inform, addMessage, addMessages } from './actions/message';
|
|
|
|
import { select } from './actions/tab';
|
2017-05-07 20:19:15 +00:00
|
|
|
import { replace } from './util/router';
|
2015-12-28 23:34:32 +00:00
|
|
|
import { normalizeChannel } from './util';
|
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 = [];
|
|
|
|
|
|
|
|
state.channels.get(server).forEach((channel, channelName) => {
|
|
|
|
if (channel.get('users').find(u => u.nick === user)) {
|
|
|
|
channels.push(channelName);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-11 01:49:52 +00:00
|
|
|
return channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function handleSocket(socket, { dispatch, getState }) {
|
|
|
|
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) {
|
|
|
|
const { nick } = state.servers.get(tab.server);
|
|
|
|
if (tab.server === server &&
|
|
|
|
nick === user &&
|
|
|
|
tab.name !== joinedChannel &&
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
servers(data) {
|
|
|
|
if (!data) {
|
2017-05-07 20:19:15 +00:00
|
|
|
dispatch(replace('/connect'));
|
2017-04-11 01:49:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
part({ user, server, channel, reason }) {
|
|
|
|
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(data) {
|
|
|
|
const channels = findChannels(getState(), data.server, data.old);
|
|
|
|
dispatch(broadcast(`${data.old} changed nick to ${data.new}`, data.server, channels));
|
|
|
|
},
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const tab = getState().tab.selected;
|
|
|
|
|
|
|
|
dispatch(inform([
|
|
|
|
`Nick: ${data.nick}`,
|
|
|
|
`Username: ${data.username}`,
|
|
|
|
`Realname: ${data.realname}`,
|
|
|
|
`Host: ${data.host}`,
|
|
|
|
`Server: ${data.server}`,
|
|
|
|
`Channels: ${data.channels}`
|
2017-04-19 23:51:55 +00:00
|
|
|
], tab.server, tab.name));
|
2017-04-11 01:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
print({ server, message }) {
|
|
|
|
dispatch(inform(message, server));
|
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) => {
|
|
|
|
if (type in handlers) {
|
|
|
|
handlers[type](data);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 01:49:52 +00:00
|
|
|
type = `SOCKET_${type.toUpperCase()}`;
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
dispatch({ type, data });
|
|
|
|
} else {
|
|
|
|
dispatch({ type, ...data });
|
|
|
|
}
|
2015-12-28 23:34:32 +00:00
|
|
|
});
|
|
|
|
}
|