2017-06-21 06:40:28 +00:00
|
|
|
import { COMMAND } from 'state/actions';
|
|
|
|
import { join, part, invite, kick, setTopic } from 'state/channels';
|
|
|
|
import { sendMessage, raw } from 'state/messages';
|
|
|
|
import { setNick, disconnect, whois, away } from 'state/servers';
|
|
|
|
import { select } from 'state/tab';
|
2018-04-05 19:13:32 +00:00
|
|
|
import { find } 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',
|
|
|
|
'/quit - Disconnect from the current server',
|
|
|
|
'/me <message> - Send action message',
|
2017-05-28 05:20:43 +00:00
|
|
|
'/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',
|
2017-05-28 05:20:43 +00:00
|
|
|
'/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',
|
2017-05-28 05:20:43 +00:00
|
|
|
'/raw [message] - Send raw IRC message to the current server',
|
|
|
|
'/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
|
|
|
|
2017-05-28 05:20:43 +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);
|
2017-05-28 05:20:43 +00:00
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
export default createCommandMiddleware(COMMAND, {
|
|
|
|
join({ dispatch, server }, channel) {
|
|
|
|
if (channel) {
|
2017-05-28 05:20:43 +00:00
|
|
|
if (channel[0] !== '#') {
|
|
|
|
return error('Bad channel name');
|
|
|
|
}
|
2015-12-28 23:34:32 +00:00
|
|
|
dispatch(join([channel], server));
|
|
|
|
dispatch(select(server, channel));
|
2017-05-28 05:20:43 +00:00
|
|
|
} else {
|
|
|
|
return error('Missing channel');
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
part({ dispatch, server, channel, isChannel }, partChannel) {
|
2015-12-28 23:34:32 +00:00
|
|
|
if (partChannel) {
|
|
|
|
dispatch(part([partChannel], server));
|
2017-05-28 05:20:43 +00:00
|
|
|
} else if (isChannel) {
|
2015-12-28 23:34:32 +00:00
|
|
|
dispatch(part([channel], server));
|
2017-05-28 05:20:43 +00:00
|
|
|
} else {
|
|
|
|
return error('This is not a channel');
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
nick({ dispatch, server }, nick) {
|
|
|
|
if (nick) {
|
|
|
|
dispatch(setNick(nick, server));
|
2017-05-28 05:20:43 +00:00
|
|
|
} else {
|
|
|
|
return error('Missing nick');
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
quit({ dispatch, server }) {
|
|
|
|
dispatch(disconnect(server));
|
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
me({ dispatch, server, channel }, ...message) {
|
|
|
|
const msg = message.join(' ');
|
|
|
|
if (msg !== '') {
|
|
|
|
dispatch(sendMessage(`\x01ACTION ${msg}\x01`, channel, server));
|
|
|
|
} else {
|
|
|
|
return error('Messages can not be empty');
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
topic({ dispatch, getState, server, channel }, ...newTopic) {
|
|
|
|
if (newTopic.length > 0) {
|
|
|
|
dispatch(setTopic(newTopic.join(' '), channel, server));
|
2018-04-25 03:36:27 +00:00
|
|
|
} else if (channel) {
|
|
|
|
const { topic } = getState().channels[server][channel];
|
2017-05-28 05:20:43 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
msg({ dispatch, server }, target, ...message) {
|
2017-05-28 05:20:43 +00:00
|
|
|
if (!target) {
|
|
|
|
return error('Missing nick/channel');
|
|
|
|
}
|
|
|
|
|
|
|
|
const msg = message.join(' ');
|
|
|
|
if (msg !== '') {
|
2015-12-28 23:34:32 +00:00
|
|
|
dispatch(sendMessage(message.join(' '), target, server));
|
2017-05-28 05:20:43 +00:00
|
|
|
dispatch(select(server, target));
|
|
|
|
} else {
|
|
|
|
return error('Messages can not be empty');
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
say({ dispatch, server, channel }, ...message) {
|
2017-05-28 05:20:43 +00:00
|
|
|
if (!channel) {
|
|
|
|
return error('Messages can only be sent to channels or users');
|
|
|
|
}
|
|
|
|
|
|
|
|
const msg = message.join(' ');
|
|
|
|
if (msg !== '') {
|
2015-12-28 23:34:32 +00:00
|
|
|
dispatch(sendMessage(message.join(' '), channel, server));
|
2017-05-28 05:20:43 +00:00
|
|
|
} else {
|
|
|
|
return error('Messages can not be empty');
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
invite({ dispatch, server, channel, isChannel }, user, inviteChannel) {
|
|
|
|
if (!inviteChannel && !isChannel) {
|
|
|
|
return error('This is not a channel');
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
if (user && inviteChannel) {
|
|
|
|
dispatch(invite(user, inviteChannel, server));
|
|
|
|
} else if (user && channel) {
|
|
|
|
dispatch(invite(user, channel, server));
|
2017-05-28 05:20:43 +00:00
|
|
|
} else {
|
|
|
|
return error('Missing nick');
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
kick({ dispatch, server, channel, isChannel }, user) {
|
|
|
|
if (!isChannel) {
|
|
|
|
return error('This is not a channel');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user) {
|
2015-12-28 23:34:32 +00:00
|
|
|
dispatch(kick(user, channel, server));
|
2017-05-28 05:20:43 +00:00
|
|
|
} else {
|
|
|
|
return error('Missing nick');
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
whois({ dispatch, server }, user) {
|
|
|
|
if (user) {
|
|
|
|
dispatch(whois(user, server));
|
2017-05-28 05:20:43 +00:00
|
|
|
} else {
|
|
|
|
return error('Missing nick');
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
away({ dispatch, server }, ...message) {
|
|
|
|
const msg = message.join(' ');
|
|
|
|
dispatch(away(msg, server));
|
|
|
|
if (msg !== '') {
|
|
|
|
return 'Away message set';
|
|
|
|
}
|
|
|
|
return 'Away message cleared';
|
2015-12-28 23:34:32 +00:00
|
|
|
},
|
|
|
|
|
2016-01-27 19:48:47 +00:00
|
|
|
raw({ dispatch, server }, ...message) {
|
2017-05-28 05:20:43 +00:00
|
|
|
if (message.length > 0 && message[0] !== '') {
|
|
|
|
const cmd = `${message[0].toUpperCase()} ${message.slice(1).join(' ')}`;
|
2017-05-22 01:49:37 +00:00
|
|
|
dispatch(raw(cmd, server));
|
2017-05-28 05:20:43 +00:00
|
|
|
return prompt(`=> ${cmd}`);
|
|
|
|
}
|
2018-04-05 23:46:22 +00:00
|
|
|
return [prompt('=> /raw'), error('Missing message')];
|
2017-05-28 05:20:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-05-28 05:20:43 +00:00
|
|
|
return text(help);
|
2016-01-27 19:48:47 +00:00
|
|
|
},
|
|
|
|
|
2017-05-28 05:20:43 +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) {
|
|
|
|
if (command === command.toUpperCase()) {
|
|
|
|
return this.raw(ctx, command, ...params);
|
|
|
|
}
|
2017-05-28 05:20:43 +00:00
|
|
|
return error(`=> /${command}: No such command`);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
});
|