dispatch/client/js/commands.js

196 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

import { COMMAND } from 'state/actions';
import { join, part, invite, kick, setTopic } from 'state/channels';
import { sendMessage, raw } from 'state/messages';
2020-06-15 08:58:51 +00:00
import { setNick, disconnect, whois, away } from 'state/networks';
2020-05-06 02:50:27 +00:00
import { openPrivateChat } from 'state/privateChats';
import { select } from 'state/tab';
2020-05-06 02:50:27 +00:00
import { find, isChannel } from 'utils';
2018-04-05 23:46:22 +00:00
import createCommandMiddleware, {
beforeHandler,
notFoundHandler
} from './middleware/command';
2015-12-28 23:34:32 +00:00
const help = [
'/join <channel> - Join a channel',
'/part [channel] - Leave the current or specified channel',
'/nick <nick> - Change nick',
2020-06-15 08:58:51 +00:00
'/quit - Disconnect from the current network',
2015-12-28 23:34:32 +00:00
'/me <message> - Send action message',
'/topic [topic] - Show or set topic in the current channel',
2015-12-28 23:34:32 +00:00
'/msg <target> <message> - Send message to the specified channel or user',
'/say <message> - Send message to the current chat',
'/invite <nick> [channel] - Invite user to the current or specified channel',
'/kick <nick> - Kick user from the current channel',
'/whois <nick> - Get information about user',
2016-01-27 19:48:47 +00:00
'/away [message] - Set or clear away message',
2020-06-15 08:58:51 +00:00
'/raw [message] - Send raw IRC message to the current network',
'/help [command]... - Print help for all or the specified command(s)'
2017-04-20 02:16:15 +00:00
];
2015-12-28 23:34:32 +00:00
const text = content => ({ content });
const error = content => ({ content, type: 'error' });
const prompt = content => ({ content, type: 'prompt' });
2018-04-05 23:46:22 +00:00
const findHelp = cmd =>
find(help, line => line.slice(1, line.indexOf(' ')) === cmd);
2015-12-28 23:34:32 +00:00
export default createCommandMiddleware(COMMAND, {
2020-06-15 08:58:51 +00:00
join({ dispatch, network }, channel) {
2015-12-28 23:34:32 +00:00
if (channel) {
if (channel[0] !== '#') {
return error('Bad channel name');
}
2020-06-15 08:58:51 +00:00
dispatch(join([channel], network));
} else {
return error('Missing channel');
2015-12-28 23:34:32 +00:00
}
},
2020-06-15 08:58:51 +00:00
part({ dispatch, network, channel, inChannel }, partChannel) {
2015-12-28 23:34:32 +00:00
if (partChannel) {
2020-06-15 08:58:51 +00:00
dispatch(part([partChannel], network));
2020-05-06 02:50:27 +00:00
} else if (inChannel) {
2020-06-15 08:58:51 +00:00
dispatch(part([channel], network));
} else {
return error('This is not a channel');
2015-12-28 23:34:32 +00:00
}
},
2020-06-15 08:58:51 +00:00
nick({ dispatch, network }, nick) {
2015-12-28 23:34:32 +00:00
if (nick) {
2020-06-15 08:58:51 +00:00
dispatch(setNick(nick, network));
} else {
return error('Missing nick');
2015-12-28 23:34:32 +00:00
}
},
2020-06-15 08:58:51 +00:00
quit({ dispatch, network }) {
dispatch(disconnect(network));
2015-12-28 23:34:32 +00:00
},
2020-06-15 08:58:51 +00:00
me({ dispatch, network, channel }, ...message) {
const msg = message.join(' ');
if (msg !== '') {
2020-06-15 08:58:51 +00:00
dispatch(sendMessage(`\x01ACTION ${msg}\x01`, channel, network));
} else {
return error('Messages can not be empty');
2015-12-28 23:34:32 +00:00
}
},
2020-06-15 08:58:51 +00:00
topic({ dispatch, getState, network, channel }, ...newTopic) {
if (newTopic.length > 0) {
2020-06-15 08:58:51 +00:00
dispatch(setTopic(newTopic.join(' '), channel, network));
2019-01-27 07:53:07 +00:00
return;
}
if (channel) {
2020-06-15 08:58:51 +00:00
const { topic } = getState().channels[network][channel];
if (topic) {
return text(topic);
}
2015-12-28 23:34:32 +00:00
}
2018-04-25 03:36:27 +00:00
return 'No topic set';
2015-12-28 23:34:32 +00:00
},
2020-06-15 08:58:51 +00:00
msg({ dispatch, network }, target, ...message) {
if (!target) {
return error('Missing nick/channel');
}
const msg = message.join(' ');
if (msg !== '') {
2020-06-15 08:58:51 +00:00
dispatch(sendMessage(message.join(' '), target, network));
2020-05-06 02:50:27 +00:00
if (!isChannel(target)) {
2020-06-15 08:58:51 +00:00
dispatch(openPrivateChat(network, target));
2020-05-06 02:50:27 +00:00
}
2020-06-15 08:58:51 +00:00
dispatch(select(network, target));
} else {
return error('Messages can not be empty');
2015-12-28 23:34:32 +00:00
}
},
2020-06-15 08:58:51 +00:00
say({ dispatch, network, channel }, ...message) {
if (!channel) {
return error('Messages can only be sent to channels or users');
}
const msg = message.join(' ');
if (msg !== '') {
2020-06-15 08:58:51 +00:00
dispatch(sendMessage(message.join(' '), channel, network));
} else {
return error('Messages can not be empty');
2015-12-28 23:34:32 +00:00
}
},
2020-06-15 08:58:51 +00:00
invite({ dispatch, network, channel, inChannel }, user, inviteChannel) {
2020-05-06 02:50:27 +00:00
if (!inviteChannel && !inChannel) {
return error('This is not a channel');
}
2015-12-28 23:34:32 +00:00
if (user && inviteChannel) {
2020-06-15 08:58:51 +00:00
dispatch(invite(user, inviteChannel, network));
2015-12-28 23:34:32 +00:00
} else if (user && channel) {
2020-06-15 08:58:51 +00:00
dispatch(invite(user, channel, network));
} else {
return error('Missing nick');
2015-12-28 23:34:32 +00:00
}
},
2020-06-15 08:58:51 +00:00
kick({ dispatch, network, channel, inChannel }, user) {
2020-05-06 02:50:27 +00:00
if (!inChannel) {
return error('This is not a channel');
}
if (user) {
2020-06-15 08:58:51 +00:00
dispatch(kick(user, channel, network));
} else {
return error('Missing nick');
2015-12-28 23:34:32 +00:00
}
},
2020-06-15 08:58:51 +00:00
whois({ dispatch, network }, user) {
2015-12-28 23:34:32 +00:00
if (user) {
2020-06-15 08:58:51 +00:00
dispatch(whois(user, network));
} else {
return error('Missing nick');
2015-12-28 23:34:32 +00:00
}
},
2020-06-15 08:58:51 +00:00
away({ dispatch, network }, ...message) {
const msg = message.join(' ');
2020-06-15 08:58:51 +00:00
dispatch(away(msg, network));
if (msg !== '') {
return 'Away message set';
}
return 'Away message cleared';
2015-12-28 23:34:32 +00:00
},
2020-06-15 08:58:51 +00:00
raw({ dispatch, network }, ...message) {
if (message.length > 0 && message[0] !== '') {
const cmd = `${message[0].toUpperCase()} ${message.slice(1).join(' ')}`;
2020-06-15 08:58:51 +00:00
dispatch(raw(cmd, network));
return prompt(`=> ${cmd}`);
}
2018-04-05 23:46:22 +00:00
return [prompt('=> /raw'), error('Missing message')];
},
help(_, ...commands) {
if (commands.length > 0) {
const cmdHelp = commands.filter(findHelp).map(findHelp);
if (cmdHelp.length > 0) {
return text(cmdHelp);
}
return error('Unable to find any help :(');
2016-01-27 19:48:47 +00:00
}
return text(help);
2016-01-27 19:48:47 +00:00
},
[beforeHandler](_, command, ...params) {
if (command !== 'raw') {
return prompt(`=> /${command} ${params.join(' ')}`);
}
2017-05-22 01:49:37 +00:00
},
2017-06-02 20:24:03 +00:00
[notFoundHandler](ctx, command, ...params) {
return this.raw(ctx, command, ...params);
2015-12-28 23:34:32 +00:00
}
});