Fixed isPrivate() and bold() usage.

This commit is contained in:
Erik C. Thauvin 2020-03-30 15:38:43 -07:00
parent 363a7cda40
commit 89eac6b025
31 changed files with 292 additions and 272 deletions

View file

@ -89,20 +89,20 @@ class FeedReader implements Runnable {
SyndEntry item; SyndEntry item;
final List<SyndEntry> items = feed.getEntries(); final List<SyndEntry> items = feed.getEntries();
if (items.isEmpty()) { if (items.isEmpty()) {
bot.send(sender, "There is currently nothing to view. Why don't you post something?"); bot.send(sender, "There is currently nothing to view. Why don't you post something?", false);
} else { } else {
for (int i = 0; (i < items.size()) && (i < MAX_ITEMS); i++) { for (int i = 0; (i < items.size()) && (i < MAX_ITEMS); i++) {
item = items.get(i); item = items.get(i);
bot.send(sender, item.getTitle()); bot.send(sender, item.getTitle(), false);
bot.send(sender, TAB_INDENT + Utils.green(item.getLink())); bot.send(sender, TAB_INDENT + Utils.green(item.getLink()), false);
} }
} }
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
bot.getLogger().debug("Invalid feed URL.", e); bot.getLogger().debug("Invalid feed URL.", e);
bot.send(sender, "The feed URL is invalid."); bot.send(sender, "The feed URL is invalid.", false);
} catch (Exception e) { } catch (Exception e) {
bot.getLogger().debug("Unable to fetch the feed.", e); bot.getLogger().debug("Unable to fetch the feed.", e);
bot.send(sender, "An error has occurred while fetching the feed: " + e.getMessage()); bot.send(sender, "An error has occurred while fetching the feed: " + e.getMessage(), false);
} }
} }
} }

View file

@ -482,13 +482,13 @@ public class Mobibot extends PircBot {
/** /**
* Responds with the title and links from the RSS feed. * Responds with the title and links from the RSS feed.
* *
* @param sender The nick of the person who sent the private message. * @param sender The nick of the person who sent the message.
*/ */
private void feedResponse(final String sender) { private void feedResponse(final String sender) {
if (isNotBlank(feedUrl)) { if (isNotBlank(feedUrl)) {
new Thread(new FeedReader(this, sender, feedUrl)).start(); new Thread(new FeedReader(this, sender, feedUrl)).start();
} else { } else {
send(sender, "There is no weblog setup for this channel."); send(sender, "There is no weblog setup for this channel.", false);
} }
} }
@ -599,12 +599,13 @@ public class Mobibot extends PircBot {
* *
* @param sender The nick of the person requesting Constants. * @param sender The nick of the person requesting Constants.
* @param topic The help topic. * @param topic The help topic.
* @param isPrivate The private flag.
* @return {@code true} if the topic was found, {@code false} otherwise. * @return {@code true} if the topic was found, {@code false} otherwise.
*/ */
private boolean helpCommands(final String sender, final String topic) { private boolean helpCommands(final String sender, final String topic, final boolean isPrivate) {
for (final AbstractCommand command : commands) { for (final AbstractCommand command : commands) {
if (command.isVisible() && command.getCommand().startsWith(topic)) { if (command.isVisible() && command.getCommand().startsWith(topic)) {
return command.helpResponse(this, topic, sender, isOp(sender), true); return command.helpResponse(this, topic, sender, isOp(sender), isPrivate);
} }
} }
return false; return false;
@ -615,12 +616,16 @@ public class Mobibot extends PircBot {
* *
* @param sender The nick of the person requesting Constants. * @param sender The nick of the person requesting Constants.
* @param isOp The channel operator flag. * @param isOp The channel operator flag.
* @param isPrivate The private flag.
*/ */
public void helpDefault(final String sender, final boolean isOp) { public void helpDefault(final String sender, final boolean isOp, final boolean isPrivate) {
send(sender, Utils.bold("Type a URL on " + ircChannel + " to post it.")); send(sender, "Type a URL on " + ircChannel + " to post it.", isPrivate);
send(sender, "For more information on a specific command, type:"); send(sender, "For more information on a specific command, type:", isPrivate);
send(sender, Utils.helpIndent(getNick() + ": " + Constants.HELP_CMD + " <command>")); send(sender,
send(sender, Utils.bold("The commands are:")); Utils.helpIndent(
((isPrivate) ? "/msg " + getNick() : getNick() + ':') + " " + Constants.HELP_CMD + " <command>"),
isPrivate);
send(sender, "The commands are:", isPrivate);
if (commandsNames.isEmpty()) { if (commandsNames.isEmpty()) {
// Feed command // Feed command
@ -650,10 +655,10 @@ public class Mobibot extends PircBot {
Collections.sort(opsCommandsNames); Collections.sort(opsCommandsNames);
} }
sendCommandsList(sender, commandsNames, false); sendCommandsList(sender, commandsNames, 8, isPrivate, true);
if (isOp) { if (isOp) {
send(sender, Utils.bold("The op commands are:"), false); send(sender, "The op commands are:", isPrivate);
sendCommandsList(sender, opsCommandsNames, false); sendCommandsList(sender, opsCommandsNames, 8, isPrivate, true);
} }
} }
@ -662,17 +667,20 @@ public class Mobibot extends PircBot {
* *
* @param sender The nick of the person requesting Constants. * @param sender The nick of the person requesting Constants.
* @param topic The help topic. * @param topic The help topic.
* @param isPrivate The private flag.
* @return {@code true} if the topic was found, {@code false} otherwise. * @return {@code true} if the topic was found, {@code false} otherwise.
*/ */
private boolean helpModules(final String sender, final String topic) { private boolean helpModules(final String sender, final String topic, final boolean isPrivate) {
for (final AbstractModule module : modules) { for (final AbstractModule module : modules) {
if (module.isEnabled()) {
for (final String cmd : module.getCommands()) { for (final String cmd : module.getCommands()) {
if (topic.equals(cmd)) { if (topic.equals(cmd)) {
module.helpResponse(this, sender, topic, true); module.helpResponse(this, sender, isPrivate);
return true; return true;
} }
} }
} }
}
return false; return false;
} }
@ -681,22 +689,23 @@ public class Mobibot extends PircBot {
* *
* @param sender The nick of the person who sent the private message. * @param sender The nick of the person who sent the private message.
* @param topic The help topic, if any. * @param topic The help topic, if any.
* @param isPrivate The private flag.
*/ */
private void helpResponse(final String sender, final String topic) { private void helpResponse(final String sender, final String topic, final boolean isPrivate) {
final boolean isOp = isOp(sender); final boolean isOp = isOp(sender);
if (StringUtils.isBlank(topic)) { if (StringUtils.isBlank(topic)) {
helpDefault(sender, isOp); helpDefault(sender, isOp, isPrivate);
} else { } else {
final String lcTopic = topic.toLowerCase(Constants.LOCALE).trim(); final String lcTopic = topic.toLowerCase(Constants.LOCALE).trim();
if (lcTopic.equals(getChannelName())) { if (lcTopic.equals(getChannelName())) {
send(sender, Utils.bold("To list the last 5 posts from the channel's weblog:")); send(sender, "To list the last 5 posts from the channel's weblog:", isPrivate);
send(sender, Utils.helpIndent(getNick() + ": " + getChannelName())); send(sender, Utils.helpIndent(getNick() + ": " + getChannelName()), isPrivate);
} else if (Tell.TELL_CMD.equals(lcTopic) && tell.isEnabled()) { } else if (Tell.TELL_CMD.equals(lcTopic) && tell.isEnabled()) {
tell.helpResponse(sender); tell.helpResponse(sender, isPrivate);
} else { } else {
// Command, Modules or Default // Command, Modules or Default
if (!helpCommands(sender, topic) && !helpModules(sender, lcTopic)) { if (!helpCommands(sender, topic, isPrivate) && !helpModules(sender, lcTopic, isPrivate)) {
helpDefault(sender, isOp); helpDefault(sender, isOp, isPrivate);
} }
} }
} }
@ -782,11 +791,11 @@ public class Mobibot extends PircBot {
} }
if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help
helpResponse(sender, args); helpResponse(sender, args, false);
} else if (cmd.equalsIgnoreCase(getChannelName())) { // mobibot: <channel> } else if (cmd.equalsIgnoreCase(getChannelName())) { // mobibot: <channel>
feedResponse(sender); feedResponse(sender);
} else if (cmd.startsWith(Tell.TELL_CMD) && tell.isEnabled()) { // mobibot: tell } else if (cmd.startsWith(Tell.TELL_CMD) && tell.isEnabled()) { // mobibot: tell
tell.response(sender, args); tell.response(sender, args, false);
} else { } else {
boolean skip = false; boolean skip = false;
// Commands // Commands
@ -850,12 +859,12 @@ public class Mobibot extends PircBot {
final boolean isOp = isOp(sender); final boolean isOp = isOp(sender);
if (cmd.startsWith(Constants.HELP_CMD)) { if (cmd.startsWith(Constants.HELP_CMD)) {
helpResponse(sender, args); helpResponse(sender, args, true);
} else if (isOp && "kill".equals(cmd)) { } else if (isOp && "kill".equals(cmd)) {
sendRawLine("QUIT : Poof!"); sendRawLine("QUIT : Poof!");
System.exit(0); System.exit(0);
} else if (isOp && Constants.DIE_CMD.equals(cmd)) { } else if (isOp && Constants.DIE_CMD.equals(cmd)) {
send(ircChannel, sender + " has just signed my death sentence."); send(sender + " has just signed my death sentence.");
timer.cancel(); timer.cancel();
twitterShutdown(); twitterShutdown();
twitterNotification("killed by " + sender + " on " + ircChannel); twitterNotification("killed by " + sender + " on " + ircChannel);
@ -870,10 +879,10 @@ public class Mobibot extends PircBot {
UrlMgr.addHistory(0, args); UrlMgr.addHistory(0, args);
send(sender, UrlMgr.getHistory().toString(), true); send(sender, UrlMgr.getHistory().toString(), true);
} else { } else {
send(sender, "The specified log could not be found."); send(sender, "The specified log could not be found.", true);
} }
} else if (Tell.TELL_CMD.equals(cmd) && tell.isEnabled()) { } else if (Tell.TELL_CMD.equals(cmd) && tell.isEnabled()) {
tell.response(sender, args); tell.response(sender, args, true);
} else if (isOp && Constants.DEBUG_CMD.equals(cmd)) { } else if (isOp && Constants.DEBUG_CMD.equals(cmd)) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
Configurator.setLevel(logger.getName(), loggerLevel); Configurator.setLevel(logger.getName(), loggerLevel);
@ -898,7 +907,7 @@ public class Mobibot extends PircBot {
} }
} }
} }
helpDefault(sender, isOp); helpDefault(sender, isOp, true);
} }
} }
@ -961,20 +970,10 @@ public class Mobibot extends PircBot {
* @param notice The notice message. * @param notice The notice message.
*/ */
public final void send(final String notice) { public final void send(final String notice) {
send(getChannel(), notice); send(getChannel(), notice, false);
} }
/**
* Sends a message.
*
* @param who The channel or nick of the person who sent the command.
* @param message The actual message.
*/
public final void send(final String who, final String message) {
send(who, message, false);
}
/** /**
* Sends a message. * Sends a message.
* *
@ -997,29 +996,23 @@ public class Mobibot extends PircBot {
send(who, Utils.colorize(message, color), isPrivate); send(who, Utils.colorize(message, color), isPrivate);
} }
/**
* Sends a message.
*
* @param who The channel or nick of the person who sent the command.
* @param message The actual message.
* @param color The message's color.
*/
@SuppressWarnings("unused")
public final void send(final String who, final String message, final String color) {
send(who, Utils.colorize(message, color), false);
}
/** /**
* Send a formatted commands/modules, etc. list. * Send a formatted commands/modules, etc. list.
* *
* @param nick The nick to send the list to. * @param nick The nick to send the list to.
* @param list The list to format. * @param list The list to format.
* @param size The number of items per line.
* @param isPrivate The private flag.
* @param isBold The bold flag
*/ */
public final void sendCommandsList(final String nick, final List<String> list, final boolean isPrivate) { public final void sendCommandsList(final String nick,
final int chunk = 8; // 8 commands per line. final List<String> list,
for (int i = 0; i < list.size(); i += chunk) { final int size,
final boolean isPrivate,
final boolean isBold) {
for (int i = 0; i < list.size(); i += size) {
send(nick, Utils.helpIndent( send(nick, Utils.helpIndent(
String.join(" ", list.subList(i, Math.min(list.size(), i + chunk)))), isPrivate); String.join(" ", list.subList(i, Math.min(list.size(), i + size))), isBold), isPrivate);
} }
} }

View file

@ -164,9 +164,21 @@ public final class Utils {
* *
* @param help The help string. * @param help The help string.
* @return The indented help string. * @return The indented help string.
* @see #helpIndent(String, boolean)
*/ */
public static String helpIndent(final String help) { public static String helpIndent(final String help) {
return " " + help; return helpIndent(help, true);
}
/**
* Returns indented help string.
*
* @param help The help string.
* @param isBold The bold flag.
* @return The indented help string.
*/
public static String helpIndent(final String help, final boolean isBold) {
return " " + (isBold ? bold(help) : help);
} }
/** /**

View file

@ -53,7 +53,12 @@ abstract class AbstractCommand {
open fun helpResponse(bot: Mobibot, command: String, sender: String, isOp: Boolean, isPrivate: Boolean): Boolean { open fun helpResponse(bot: Mobibot, command: String, sender: String, isOp: Boolean, isPrivate: Boolean): Boolean {
if (!this.isOp || this.isOp == isOp) { if (!this.isOp || this.isOp == isOp) {
for (h in help) { for (h in help) {
bot.send(sender, String.format(h, bot.nick)) if (isPrivate) {
bot.send(sender, String.format(h, "/msg ${bot.nick}"), true)
} else if (isPublic) {
bot.send(sender, String.format(h, "${bot.nick}:"), false)
}
} }
return true return true
} }

View file

@ -39,8 +39,8 @@ class Cycle : AbstractCommand() {
private val wait = 10 private val wait = 10
override val command = "cycle" override val command = "cycle"
override val help = listOf( override val help = listOf(
Utils.bold("To have the bot leave the channel and come back:"), "To have the bot leave the channel and come back:",
Utils.helpIndent("/msg %s $command") Utils.helpIndent("%s $command")
) )
override val isOp = true override val isOp = true
override val isPublic = false override val isPublic = false
@ -56,13 +56,13 @@ class Cycle : AbstractCommand() {
isPrivate: Boolean isPrivate: Boolean
) { ) {
if (isOp) { if (isOp) {
bot.send(bot.channel, "$sender has just asked me to leave. I'll be back!") bot.send("$sender has just asked me to leave. I'll be back!")
bot.sleep(wait) bot.sleep(wait)
bot.partChannel(bot.channel) bot.partChannel(bot.channel)
bot.sleep(wait) bot.sleep(wait)
bot.joinChannel(bot.channel) bot.joinChannel(bot.channel)
} else { } else {
bot.helpDefault(sender, isOp) bot.helpDefault(sender, isOp, isPrivate)
} }
} }
} }

View file

@ -47,18 +47,18 @@ class Ignore(defaultIgnore: String) : AbstractCommand() {
override val command = IGNORE_CMD override val command = IGNORE_CMD
override val help = listOf( override val help = listOf(
Utils.bold("To ignore a link posted to the channel:"), "To ignore a link posted to the channel:",
Utils.helpIndent("https://www.foo.bar %s"), Utils.helpIndent("https://www.foo.bar %s"),
Utils.bold("To check your ignore status:"), "To check your ignore status:",
Utils.helpIndent("%s: $command"), Utils.helpIndent("%s $command"),
Utils.bold("To toggle your ignore status:"), "To toggle your ignore status:",
Utils.helpIndent("%s: $command $me") Utils.helpIndent("%s $command $me")
) )
private val helpOp = listOf( private val helpOp = listOf(
Utils.bold("To ignore a link posted to the channel:"), "To ignore a link posted to the channel:",
Utils.helpIndent("https://www.foo.bar %s"), Utils.helpIndent("https://www.foo.bar %s"),
Utils.bold("To add/remove nicks from the ignored list:"), "To add/remove nicks from the ignored list:",
Utils.helpIndent("/msg %s $command <nick>|$me [<nick> ...]") Utils.helpIndent("%s $command <nick>|$me [<nick> ...]")
) )
override val isOp = false override val isOp = false
@ -86,9 +86,9 @@ class Ignore(defaultIgnore: String) : AbstractCommand() {
if (!isOp) { if (!isOp) {
val nick = sender.toLowerCase() val nick = sender.toLowerCase()
val isMe = args.toLowerCase().startsWith(me) val isMe = args.toLowerCase().startsWith(me)
ignoreNick(bot, nick, isMe) ignoreNick(bot, nick, isMe, isPrivate)
} else { } else {
ignoreOp(bot, sender, args) ignoreOp(bot, sender, args, isPrivate)
} }
} }
@ -101,7 +101,7 @@ class Ignore(defaultIgnore: String) : AbstractCommand() {
): Boolean { ): Boolean {
return if (isOp) { return if (isOp) {
for (h in helpOp) { for (h in helpOp) {
bot.send(sender, String.format(h, bot.nick)) bot.send(sender, String.format(h, bot.nick), isPrivate)
} }
true true
} else { } else {
@ -109,24 +109,24 @@ class Ignore(defaultIgnore: String) : AbstractCommand() {
} }
} }
private fun ignoreNick(bot: Mobibot, sender: String, isMe: Boolean) { private fun ignoreNick(bot: Mobibot, sender: String, isMe: Boolean, isPrivate: Boolean) {
if (isMe) { if (isMe) {
if (ignored.remove(sender)) { if (ignored.remove(sender)) {
bot.send(sender, "You are no longer ignored.") bot.send(sender, "You are no longer ignored.", isPrivate)
} else { } else {
ignored.add(sender) ignored.add(sender)
bot.send(sender, "You are now ignored.") bot.send(sender, "You are now ignored.", isPrivate)
} }
} else { } else {
if (ignored.contains(sender)) { if (ignored.contains(sender)) {
bot.send(sender, "You are currently ignored.") bot.send(sender, "You are currently ignored.", isPrivate)
} else { } else {
bot.send(sender, "You are not currently ignored.") bot.send(sender, "You are not currently ignored.", isPrivate)
} }
} }
} }
private fun ignoreOp(bot: Mobibot, sender: String, args: String) { private fun ignoreOp(bot: Mobibot, sender: String, args: String, isPrivate: Boolean) {
if (args.isNotEmpty()) { if (args.isNotEmpty()) {
val nicks = args.toLowerCase().split(" ") val nicks = args.toLowerCase().split(" ")
for (nick in nicks) { for (nick in nicks) {
@ -142,10 +142,10 @@ class Ignore(defaultIgnore: String) : AbstractCommand() {
} }
if (ignored.size > 0) { if (ignored.size > 0) {
bot.send(sender, Utils.bold("The following nicks are ignored:")) bot.send(sender, "The following nicks are ignored:", isPrivate)
bot.sendCommandsList(sender, ignored.toList(), false) bot.sendCommandsList(sender, ignored.toList(), 8, isPrivate, true)
} else { } else {
bot.send(sender, "No one is currently ${Utils.bold("ignored")}.") bot.send(sender, "No one is currently ${Utils.bold("ignored")}.", isPrivate)
} }
} }
} }

View file

@ -40,8 +40,8 @@ import java.lang.management.ManagementFactory
class Info : AbstractCommand() { class Info : AbstractCommand() {
override val command = "info" override val command = "info"
override val help = listOf( override val help = listOf(
Utils.bold("To view information about the bot:"), "To view information about the bot:",
Utils.helpIndent("%s: $command") Utils.helpIndent("%s $command")
) )
override val isOp = false override val isOp = false
override val isPublic = true override val isPublic = true
@ -82,6 +82,6 @@ class Info : AbstractCommand() {
append(']') append(']')
} }
bot.send(sender, info.toString()) bot.send(sender, info.toString(), isPrivate)
} }
} }

View file

@ -38,8 +38,8 @@ import net.thauvin.erik.mobibot.Utils
class Me : AbstractCommand() { class Me : AbstractCommand() {
override val command = "me" override val command = "me"
override val help = listOf( override val help = listOf(
Utils.bold("To have the bot perform an action:"), "To have the bot perform an action:",
Utils.helpIndent("/msg %s $command <action>") Utils.helpIndent("%s $command <action>")
) )
override val isOp = true override val isOp = true
override val isPublic = false override val isPublic = false
@ -56,7 +56,7 @@ class Me : AbstractCommand() {
if (isOp) { if (isOp) {
bot.action(args) bot.action(args)
} else { } else {
bot.helpDefault(sender, isOp) bot.helpDefault(sender, isOp, isPrivate)
} }
} }
} }

View file

@ -38,8 +38,8 @@ import net.thauvin.erik.mobibot.Utils
class Modules : AbstractCommand() { class Modules : AbstractCommand() {
override val command = "modules" override val command = "modules"
override val help = listOf( override val help = listOf(
Utils.bold("To view a list of enabled modules:"), "To view a list of enabled modules:",
Utils.helpIndent("/msg %s $command") Utils.helpIndent("%s $command")
) )
override val isOp = true override val isOp = true
override val isPublic = false override val isPublic = false
@ -58,11 +58,11 @@ class Modules : AbstractCommand() {
if (modulesNames.isEmpty()) { if (modulesNames.isEmpty()) {
bot.send(sender, "There are no enabled modules.", isPrivate) bot.send(sender, "There are no enabled modules.", isPrivate)
} else { } else {
bot.send(sender, Utils.bold("The enabled modules are: "), false) bot.send(sender, "The enabled modules are: ", isPrivate)
bot.sendCommandsList(sender, modulesNames, false ) bot.sendCommandsList(sender, modulesNames, 7, isPrivate, false)
} }
} else { } else {
bot.helpDefault(sender, isOp) bot.helpDefault(sender, isOp, isPrivate)
} }
} }
} }

View file

@ -38,8 +38,8 @@ import net.thauvin.erik.mobibot.Utils
class Msg : AbstractCommand() { class Msg : AbstractCommand() {
override val command = "msg" override val command = "msg"
override val help = listOf( override val help = listOf(
Utils.bold("To have the bot send a private message to someone:"), "To have the bot send a private message to someone:",
Utils.helpIndent("/msg %s $command <nick> <text>") Utils.helpIndent("%s $command <nick> <text>")
) )
override val isOp = true override val isOp = true
override val isPublic = true override val isPublic = true
@ -61,7 +61,7 @@ class Msg : AbstractCommand() {
helpResponse(bot, command, sender, isOp, isPrivate) helpResponse(bot, command, sender, isOp, isPrivate)
} }
} else { } else {
bot.helpDefault(sender, isOp) bot.helpDefault(sender, isOp, isPrivate)
} }
} }
} }

View file

@ -38,8 +38,8 @@ import net.thauvin.erik.mobibot.Utils
class Nick : AbstractCommand() { class Nick : AbstractCommand() {
override val command = "nick" override val command = "nick"
override val help = listOf( override val help = listOf(
Utils.bold("To change the bot's nickname:"), "To change the bot's nickname:",
Utils.helpIndent("/msg %s $command <nick>") Utils.helpIndent("%s $command <nick>")
) )
override val isOp = true override val isOp = true
override val isPublic = true override val isPublic = true
@ -56,7 +56,7 @@ class Nick : AbstractCommand() {
if (isOp) { if (isOp) {
bot.changeNick(args) bot.changeNick(args)
} else { } else {
bot.helpDefault(sender, isOp) bot.helpDefault(sender, isOp, isPrivate)
} }
} }
} }

View file

@ -41,9 +41,8 @@ import java.util.*
class Recap : AbstractCommand() { class Recap : AbstractCommand() {
override val command = "recap" override val command = "recap"
override val help = listOf( override val help = listOf(
Utils.bold("To list the last 10 public channel messages:"), "To list the last 10 public channel messages:",
Utils.helpIndent("%s: $command"), Utils.helpIndent("%s $command")
Utils.helpIndent("/msg %s $command")
) )
override val isOp = false override val isOp = false
override val isPublic = true override val isPublic = true
@ -89,7 +88,7 @@ class Recap : AbstractCommand() {
bot.send(sender, r, isPrivate) bot.send(sender, r, isPrivate)
} }
} else { } else {
bot.send(sender, "Sorry, nothing to recap.", true) bot.send(sender, "Sorry, nothing to recap.", isPrivate)
} }
} }
} }

View file

@ -38,8 +38,8 @@ import net.thauvin.erik.mobibot.Utils
class Say : AbstractCommand() { class Say : AbstractCommand() {
override val command = "say" override val command = "say"
override val help = listOf( override val help = listOf(
Utils.bold("To have the bot say something on the channel:"), "To have the bot say something on the channel:",
Utils.helpIndent("/msg %s $command <text>") Utils.helpIndent("%s $command <text>")
) )
override val isOp = true override val isOp = true
override val isPublic = false override val isPublic = false
@ -55,9 +55,9 @@ class Say : AbstractCommand() {
isPrivate: Boolean isPrivate: Boolean
) { ) {
if (isOp) { if (isOp) {
bot.send(bot.channel, args, true) bot.send(args)
} else { } else {
bot.helpDefault(sender, isOp) bot.helpDefault(sender, isOp, isPrivate)
} }
} }
} }

View file

@ -40,8 +40,8 @@ import java.util.*
class Users : AbstractCommand() { class Users : AbstractCommand() {
override val command = "users" override val command = "users"
override val help = listOf( override val help = listOf(
Utils.bold("To list the users present on the channel:"), "To list the users present on the channel:",
Utils.helpIndent("/msg %s $command") Utils.helpIndent("%s $command")
) )
override val isOp = false override val isOp = false
override val isPublic = true override val isPublic = true
@ -66,6 +66,6 @@ class Users : AbstractCommand() {
} }
} }
bot.send(sender, nicks.sorted().joinToString(" ")) bot.send(sender, nicks.sorted().joinToString(" "), isPrivate)
} }
} }

View file

@ -60,8 +60,8 @@ public class Versions extends AbstractCommand {
@NotNull @NotNull
@Override @Override
public List<String> getHelp() { public List<String> getHelp() {
return List.of(Utils.bold("To view the versions data (bot, java, etc.):"), return List.of("To view the versions data (bot, java, etc.):",
Utils.helpIndent("/msg %s $command")); Utils.helpIndent("%s " + getCommand()));
} }
@Override @Override
@ -88,10 +88,10 @@ public class Versions extends AbstractCommand {
final boolean isPrivate) { final boolean isPrivate) {
if (isOp) { if (isOp) {
for (final String v : versions) { for (final String v : versions) {
bot.send(sender, v); bot.send(sender, v, isPrivate);
} }
} else { } else {
bot.helpDefault(sender, false); bot.helpDefault(sender, false, isPrivate);
} }
} }

View file

@ -42,12 +42,12 @@ import net.thauvin.erik.mobibot.entries.EntryLink
class Comment : AbstractCommand() { class Comment : AbstractCommand() {
override val command = COMMAND override val command = COMMAND
override val help = listOf( override val help = listOf(
Utils.bold("To add a comment:"), "To add a comment:",
Utils.helpIndent("${Constants.LINK_CMD}1:This is a comment"), Utils.helpIndent("${Constants.LINK_CMD}1:This is a comment"),
"I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1.1", "I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1.1",
Utils.bold("To edit a comment, use its label: "), "To edit a comment, use its label: ",
Utils.helpIndent("${Constants.LINK_CMD}1.1:This is an edited comment"), Utils.helpIndent("${Constants.LINK_CMD}1.1:This is an edited comment"),
Utils.bold("To delete a comment, use its label and a minus sign: "), "To delete a comment, use its label and a minus sign: ",
Utils.helpIndent("${Constants.LINK_CMD}1.1:-") Utils.helpIndent("${Constants.LINK_CMD}1.1:-")
) )
override val isOp = false override val isOp = false
@ -75,9 +75,14 @@ class Comment : AbstractCommand() {
if (commentIndex < entry.commentsCount) { if (commentIndex < entry.commentsCount) {
when (val cmd = cmds[2].trim()) { when (val cmd = cmds[2].trim()) {
"" -> showComment(bot, entry, index, commentIndex) // L1.1: "" -> showComment(bot, entry, index, commentIndex) // L1.1:
"-" -> deleteComment(bot, entry, index, commentIndex) // L11:- "-" -> deleteComment(bot, sender, isOp, entry, index, commentIndex) // L11:-
"?" -> changeAuthor(bot, cmd, sender, isOp, entry, index, commentIndex) // L1.1:?<author> else -> {
else -> setComment(bot, cmd, sender, entry, index, commentIndex) if (cmd.startsWith('?')) { // L1.1:?<author>
changeAuthor(bot, cmd, sender, isOp, entry, index, commentIndex)
} else { // L1.1:<comment>
setComment(bot, cmd, sender, entry, index, commentIndex)
}
}
} }
} }
} }
@ -92,8 +97,8 @@ class Comment : AbstractCommand() {
): Boolean { ): Boolean {
if (super.helpResponse(bot, command, sender, isOp, isPrivate)) { if (super.helpResponse(bot, command, sender, isOp, isPrivate)) {
if (isOp) { if (isOp) {
bot.send(sender, Utils.bold("To change a comment's author:")) bot.send(sender, "To change a comment's author:", isPrivate)
bot.send(sender, Utils.helpIndent("/msg ${bot.nick} ${Constants.LINK_CMD}1.1:?<nick>")) bot.send(sender, Utils.helpIndent("/msg ${bot.nick} ${Constants.LINK_CMD}1.1:?<nick>"), isPrivate)
} }
return true return true
} }
@ -116,27 +121,38 @@ class Comment : AbstractCommand() {
if (isOp && cmd.length > 1) { if (isOp && cmd.length > 1) {
val comment = entry.getComment(commentIndex) val comment = entry.getComment(commentIndex)
comment.nick = cmd.substring(1) comment.nick = cmd.substring(1)
bot.send(bot.channel, EntriesUtils.buildComment(index, commentIndex, comment)) bot.send(EntriesUtils.buildComment(index, commentIndex, comment))
UrlMgr.saveEntries(bot, false) UrlMgr.saveEntries(bot, false)
} else { } else {
bot.send(sender, "Please ask a channel op to change the author of this comment for you.") bot.send(sender, "Please ask a channel op to change the author of this comment for you.", false)
} }
} }
private fun deleteComment(bot: Mobibot, entry: EntryLink, index: Int, commentIndex: Int) { private fun deleteComment(
bot: Mobibot,
sender: String,
isOp: Boolean,
entry: EntryLink,
index: Int,
commentIndex: Int
) {
if (isOp || sender == entry.getComment(commentIndex).nick) {
entry.deleteComment(commentIndex) entry.deleteComment(commentIndex)
bot.send(bot.channel, "Comment ${Constants.LINK_CMD}${index + 1}.${commentIndex + 1} removed.") bot.send("Comment ${Constants.LINK_CMD}${index + 1}.${commentIndex + 1} removed.")
UrlMgr.saveEntries(bot, false) UrlMgr.saveEntries(bot, false)
} else {
bot.send(sender, "Please ask a channel op to delete this comment for you.", false)
}
} }
private fun setComment(bot: Mobibot, cmd: String, sender: String, entry: EntryLink, index: Int, commentIndex: Int) { private fun setComment(bot: Mobibot, cmd: String, sender: String, entry: EntryLink, index: Int, commentIndex: Int) {
entry.setComment(commentIndex, cmd, sender) entry.setComment(commentIndex, cmd, sender)
val comment = entry.getComment(commentIndex) val comment = entry.getComment(commentIndex)
bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment)) bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment), false)
UrlMgr.saveEntries(bot, false) UrlMgr.saveEntries(bot, false)
} }
private fun showComment(bot: Mobibot, entry: EntryLink, index: Int, commentIndex: Int) { private fun showComment(bot: Mobibot, entry: EntryLink, index: Int, commentIndex: Int) {
bot.send(bot.channel, EntriesUtils.buildComment(index, commentIndex, entry.getComment(commentIndex))) bot.send(EntriesUtils.buildComment(index, commentIndex, entry.getComment(commentIndex)))
} }
} }

View file

@ -42,16 +42,16 @@ import net.thauvin.erik.mobibot.entries.EntryLink
class Posting : AbstractCommand() { class Posting : AbstractCommand() {
override val command = "posting" override val command = "posting"
override val help = listOf( override val help = listOf(
Utils.bold("Post a URL, by saying it on a line on its own:"), "Post a URL, by saying it on a line on its own:",
Utils.helpIndent("<url> [<title>] ${Tags.COMMAND}}: <+tag> [...]]"), Utils.helpIndent("<url> [<title>] ${Tags.COMMAND}}: <+tag> [...]]"),
"I will reply with a label, for example:" + Utils.bold("${Constants.LINK_CMD}1"), "I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1",
Utils.bold("To add a title, use its label and a pipe:"), "To add a title, use its label and a pipe:",
Utils.helpIndent("${Constants.LINK_CMD}1:|This is the title"), Utils.helpIndent("${Constants.LINK_CMD}1:|This is the title"),
Utils.bold("To add a comment:"), "To add a comment:",
Utils.helpIndent("${Constants.LINK_CMD}1:This is a comment"), Utils.helpIndent("${Constants.LINK_CMD}1:This is a comment"),
"I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1.1", "I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1.1",
Utils.bold("To edit a comment, see: "), "To edit a comment, see: ",
Utils.helpIndent("/msg %s ${Constants.HELP_CMD} ${Comment.COMMAND}") Utils.helpIndent("%s ${Constants.HELP_CMD} ${Comment.COMMAND}")
) )
override val isOp = false override val isOp = false
override val isPublic = true override val isPublic = true
@ -77,7 +77,7 @@ class Posting : AbstractCommand() {
'|' -> changeTitle(bot, cmd, index) // L1:|<title> '|' -> changeTitle(bot, cmd, index) // L1:|<title>
'=' -> changeUrl(bot, cmd, login, isOp, index) // L1:=<url> '=' -> changeUrl(bot, cmd, login, isOp, index) // L1:=<url>
'?' -> changeAuthor(bot, cmd, sender, isOp, index) // L1:?<author> '?' -> changeAuthor(bot, cmd, sender, isOp, index) // L1:?<author>
else -> addComment(bot, cmd, sender, index) else -> addComment(bot, cmd, sender, index) // L1:<comment>
} }
} }
} }
@ -92,7 +92,7 @@ class Posting : AbstractCommand() {
val entry: EntryLink = UrlMgr.getEntry(index) val entry: EntryLink = UrlMgr.getEntry(index)
val commentIndex = entry.addComment(cmd, sender) val commentIndex = entry.addComment(cmd, sender)
val comment = entry.getComment(commentIndex) val comment = entry.getComment(commentIndex)
bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment)) bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment), false)
UrlMgr.saveEntries(bot, false) UrlMgr.saveEntries(bot, false)
} }
@ -101,7 +101,7 @@ class Posting : AbstractCommand() {
val entry: EntryLink = UrlMgr.getEntry(index) val entry: EntryLink = UrlMgr.getEntry(index)
entry.title = cmd.substring(1).trim() entry.title = cmd.substring(1).trim()
bot.updatePin(entry.link, entry) bot.updatePin(entry.link, entry)
bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) bot.send(EntriesUtils.buildLink(index, entry))
UrlMgr.saveEntries(bot, false) UrlMgr.saveEntries(bot, false)
} }
} }
@ -114,7 +114,7 @@ class Posting : AbstractCommand() {
val oldLink = entry.link val oldLink = entry.link
entry.link = link entry.link = link
bot.updatePin(oldLink, entry) bot.updatePin(oldLink, entry)
bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) bot.send(EntriesUtils.buildLink(index, entry))
UrlMgr.saveEntries(bot, false) UrlMgr.saveEntries(bot, false)
} }
} }
@ -125,11 +125,11 @@ class Posting : AbstractCommand() {
if (cmd.length > 1) { if (cmd.length > 1) {
val entry: EntryLink = UrlMgr.getEntry(index) val entry: EntryLink = UrlMgr.getEntry(index)
entry.nick = cmd.substring(1) entry.nick = cmd.substring(1)
bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) bot.send(EntriesUtils.buildLink(index, entry))
UrlMgr.saveEntries(bot, false) UrlMgr.saveEntries(bot, false)
} }
} else { } else {
bot.send(sender, "Please ask a channel op to change the author of this link for you.") bot.send(sender, "Please ask a channel op to change the author of this link for you.", false)
} }
} }
@ -141,23 +141,23 @@ class Posting : AbstractCommand() {
bot.twitterRemoveEntry(index) bot.twitterRemoveEntry(index)
} }
UrlMgr.removeEntry(index) UrlMgr.removeEntry(index)
bot.send(bot.channel, "Entry ${Constants.LINK_CMD}${index + 1} removed.") bot.send("Entry ${Constants.LINK_CMD}${index + 1} removed.")
UrlMgr.saveEntries(bot, false) UrlMgr.saveEntries(bot, false)
} else { } else {
bot.send(sender, "Please ask a channel op to remove this entry for you.") bot.send(sender, "Please ask a channel op to remove this entry for you.", false)
} }
} }
private fun showEntry(bot: Mobibot, index: Int) { private fun showEntry(bot: Mobibot, index: Int) {
val entry: EntryLink = UrlMgr.getEntry(index) val entry: EntryLink = UrlMgr.getEntry(index)
bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) bot.send(EntriesUtils.buildLink(index, entry))
if (entry.hasTags()) { if (entry.hasTags()) {
bot.send(bot.channel, EntriesUtils.buildTags(index, entry)) bot.send(EntriesUtils.buildTags(index, entry))
} }
if (entry.hasComments()) { if (entry.hasComments()) {
val comments = entry.comments val comments = entry.comments
for (i in comments.indices) { for (i in comments.indices) {
bot.send(bot.channel, EntriesUtils.buildComment(index, i, comments[i])) bot.send(EntriesUtils.buildComment(index, i, comments[i]))
} }
} }
} }

View file

@ -42,7 +42,7 @@ import net.thauvin.erik.mobibot.entries.EntryLink
class Tags : AbstractCommand() { class Tags : AbstractCommand() {
override val command = COMMAND override val command = COMMAND
override val help = listOf( override val help = listOf(
Utils.bold("To categorize or tag a URL, use its label and a T:"), "To categorize or tag a URL, use its label and a T:",
Utils.helpIndent("${Constants.LINK_CMD}1T:<+tag|-tag> [...]") Utils.helpIndent("${Constants.LINK_CMD}1T:<+tag|-tag> [...]")
) )
override val isOp = false override val isOp = false
@ -71,16 +71,16 @@ class Tags : AbstractCommand() {
if (entry.login == login || isOp) { if (entry.login == login || isOp) {
entry.setTags(cmd) entry.setTags(cmd)
bot.updatePin(entry.link, entry) bot.updatePin(entry.link, entry)
bot.send(bot.channel, EntriesUtils.buildTags(index, entry)) bot.send(EntriesUtils.buildTags(index, entry))
UrlMgr.saveEntries(bot, false) UrlMgr.saveEntries(bot, false)
} else { } else {
bot.send(sender, "Please ask a channel op to change the tags for you.") bot.send(sender, "Please ask a channel op to change the tags for you.",isPrivate)
} }
} else { } else {
if (entry.hasTags()) { if (entry.hasTags()) {
bot.send(bot.channel, EntriesUtils.buildTags(index, entry)) bot.send(EntriesUtils.buildTags(index, entry))
} else { } else {
bot.send(sender, "The entry has no tags. Why don't add some?") bot.send(sender, "The entry has no tags. Why don't add some?", isPrivate)
} }
} }
} }

View file

@ -128,7 +128,7 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
if (Ignore.isNotIgnored(sender) && (cmds.size == 1 || !cmds[1].contains(bot.nick))) { if (Ignore.isNotIgnored(sender) && (cmds.size == 1 || !cmds[1].contains(bot.nick))) {
val link = cmds[0].trim() val link = cmds[0].trim()
if (!isDupEntry(bot, sender, link)) { if (!isDupEntry(bot, sender, link, isPrivate)) {
val isBackup = saveDayBackup(bot) val isBackup = saveDayBackup(bot)
val tags: StringBuilder = StringBuilder(defaultTags) val tags: StringBuilder = StringBuilder(defaultTags)
var title = Constants.NO_TITLE var title = Constants.NO_TITLE
@ -145,7 +145,7 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
entries.add(EntryLink(link, title, sender, login, bot.channel, tags.toString())) entries.add(EntryLink(link, title, sender, login, bot.channel, tags.toString()))
val index: Int = entries.size - 1 val index: Int = entries.size - 1
val entry: EntryLink = entries[index] val entry: EntryLink = entries[index]
bot.send(bot.channel, EntriesUtils.buildLink(index, entry)) bot.send(EntriesUtils.buildLink(index, entry))
// Add Entry to pinboard. // Add Entry to pinboard.
bot.addPin(entry) bot.addPin(entry)
@ -156,10 +156,11 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
saveEntries(bot, isBackup) saveEntries(bot, isBackup)
if (Constants.NO_TITLE == entry.title) { if (Constants.NO_TITLE == entry.title) {
bot.send(sender, Utils.bold("Please specify a title, by typing:")) bot.send(sender, "Please specify a title, by typing:", isPrivate)
bot.send( bot.send(
sender, sender,
Utils.helpIndent(Constants.LINK_CMD + (index + 1) + ":|This is the title") Utils.helpIndent(Constants.LINK_CMD + (index + 1) + ":|This is the title"),
isPrivate
) )
} }
} }
@ -196,12 +197,12 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
return title return title
} }
private fun isDupEntry(bot: Mobibot, sender: String, link: String): Boolean { private fun isDupEntry(bot: Mobibot, sender: String, link: String, isPrivate: Boolean): Boolean {
synchronized(entries) { synchronized(entries) {
for (i in entries.indices) { for (i in entries.indices) {
if (link == entries[i].link) { if (link == entries[i].link) {
val entry: EntryLink = entries[i] val entry: EntryLink = entries[i]
bot.send(sender, Utils.bold("Duplicate") + " >> " + EntriesUtils.buildLink(i, entry)) bot.send(sender, Utils.bold("Duplicate") + " >> " + EntriesUtils.buildLink(i, entry), isPrivate)
return true return true
} }
} }

View file

@ -45,8 +45,8 @@ class View : AbstractCommand() {
private val maxEntries = 8 private val maxEntries = 8
override val command = VIEW_CMD override val command = VIEW_CMD
override val help = listOf( override val help = listOf(
Utils.bold("To list or search the current URL posts:"), "To list or search the current URL posts:",
Utils.helpIndent("%s: $command [<start>] [<query>]") Utils.helpIndent("%s $command [<start>] [<query>]")
) )
override val isOp = false override val isOp = false
override val isPublic = true override val isPublic = true
@ -67,7 +67,7 @@ class View : AbstractCommand() {
if (entriesCount != 0) { if (entriesCount != 0) {
showPosts(bot, args, sender) showPosts(bot, args, sender)
} else { } else {
bot.send(sender, "There is currently nothing to view. Why don't you post something?") bot.send(sender, "There is currently nothing to view. Why don't you post something?", isPrivate)
} }
} }
@ -101,17 +101,17 @@ class View : AbstractCommand() {
entry = getEntry(i) entry = getEntry(i)
if (lcArgs.isNotBlank()) { if (lcArgs.isNotBlank()) {
if (entry.matches(lcArgs)) { if (entry.matches(lcArgs)) {
bot.send(sender, EntriesUtils.buildLink(i, entry, true)) bot.send(sender, EntriesUtils.buildLink(i, entry, true), false)
sent++ sent++
} }
} else { } else {
bot.send(sender, EntriesUtils.buildLink(i, entry, true)) bot.send(sender, EntriesUtils.buildLink(i, entry, true), false)
sent++ sent++
} }
i++ i++
if (sent == maxEntries && i < max) { if (sent == maxEntries && i < max) {
bot.send( bot.send(
sender, "To view more, try: " + Utils.bold("${bot.nick}: $command ${i + 1} $lcArgs") sender, "To view more, try: " + Utils.bold("${bot.nick}: $command ${i + 1} $lcArgs"), false
) )
} }
} }

View file

@ -115,15 +115,18 @@ public class Tell {
* Responds with Constants. * Responds with Constants.
* *
* @param sender The sender. * @param sender The sender.
* @param isPrivate The private flag.
*/ */
public void helpResponse(final String sender) { public void helpResponse(final String sender, final boolean isPrivate) {
bot.send(sender, "To send a message to someone when they join the channel:"); bot.send(sender, "To send a message to someone when they join the channel:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + " <nick> <message>")); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + " <nick> <message>"), isPrivate);
bot.send(sender, "To view queued and sent messages:"); bot.send(sender, "To view queued and sent messages:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + ' ' + View.VIEW_CMD)); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + ' ' + View.VIEW_CMD), isPrivate);
bot.send(sender, "Messages are kept for " + Utils.bold(maxDays) + Utils.plural(maxDays, " day.", " days.")); bot.send(sender,
"Messages are kept for " + Utils.bold(maxDays) + Utils.plural(maxDays, " day.", " days."),
isPrivate);
} }
/** /**
@ -140,13 +143,14 @@ public class Tell {
* *
* @param sender The sender's nick. * @param sender The sender's nick.
* @param cmds The commands string. * @param cmds The commands string.
* @param isPrivate The private flag.
*/ */
@SuppressFBWarnings(value = "CC_CYCLOMATIC_COMPLEXITY", @SuppressFBWarnings(value = "CC_CYCLOMATIC_COMPLEXITY",
justification = "Working on it.") justification = "Working on it.")
public void response(final String sender, final String cmds) { public void response(final String sender, final String cmds, final boolean isPrivate) {
final String arrow = " --> "; final String arrow = " --> ";
if (StringUtils.isBlank(cmds)) { if (StringUtils.isBlank(cmds)) {
helpResponse(sender); helpResponse(sender, isPrivate);
} else if (cmds.startsWith(View.VIEW_CMD)) { } else if (cmds.startsWith(View.VIEW_CMD)) {
if (bot.isOp(sender) && (View.VIEW_CMD + ' ' + TELL_ALL_KEYWORD).equals(cmds)) { if (bot.isOp(sender) && (View.VIEW_CMD + ' ' + TELL_ALL_KEYWORD).equals(cmds)) {
if (!messages.isEmpty()) { if (!messages.isEmpty()) {
@ -154,10 +158,10 @@ public class Tell {
bot.send(sender, Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient()) bot.send(sender, Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
+ " [ID: " + message.getId() + ", " + " [ID: " + message.getId() + ", "
+ (message.isReceived() ? "DELIVERED" : "QUEUED") + ']', + (message.isReceived() ? "DELIVERED" : "QUEUED") + ']',
true); isPrivate);
} }
} else { } else {
bot.send(sender, "There are no messages in the queue.", true); bot.send(sender, "There are no messages in the queue.", isPrivate);
} }
} else { } else {
boolean hasMessage = false; boolean hasMessage = false;
@ -166,7 +170,7 @@ public class Tell {
if (message.isMatch(sender)) { if (message.isMatch(sender)) {
if (!hasMessage) { if (!hasMessage) {
hasMessage = true; hasMessage = true;
bot.send(sender, "Here are your messages: ", true); bot.send(sender, "Here are your messages: ", isPrivate);
} }
if (message.isReceived()) { if (message.isReceived()) {
@ -174,29 +178,30 @@ public class Tell {
Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient()) Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
+ " [" + Utils.utcDateTime(message.getReceived()) + ", ID: " + " [" + Utils.utcDateTime(message.getReceived()) + ", ID: "
+ message.getId() + ", DELIVERED]", + message.getId() + ", DELIVERED]",
true); isPrivate);
} else { } else {
bot.send(sender, bot.send(sender,
Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient()) Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
+ " [" + Utils.utcDateTime(message.getQueued()) + ", ID: " + " [" + Utils.utcDateTime(message.getQueued()) + ", ID: "
+ message.getId() + ", QUEUED]", + message.getId() + ", QUEUED]",
true); isPrivate);
} }
bot.send(sender, Utils.helpIndent(message.getMessage()), true); bot.send(sender, Utils.helpIndent(message.getMessage()), isPrivate);
} }
} }
if (!hasMessage) { if (!hasMessage) {
bot.send(sender, "You have no messages in the queue.", true); bot.send(sender, "You have no messages in the queue.", isPrivate);
} else { } else {
bot.send(sender, "To delete one or all delivered messages:"); bot.send(sender, "To delete one or all delivered messages:", isPrivate);
bot.send(sender, bot.send(sender,
Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + ' ' + TELL_DEL_KEYWORD + " <id|" Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + ' ' + TELL_DEL_KEYWORD + " <id|"
+ TELL_ALL_KEYWORD + '>')); + TELL_ALL_KEYWORD + '>'), isPrivate);
bot.send(sender, "Messages are kept for " + Utils.bold(maxDays) bot.send(sender,
+ Utils.plural(maxDays, " day.", " days.")); "Messages are kept for " + Utils.bold(maxDays) + Utils.plural(maxDays, " day.", " days."),
isPrivate);
} }
} }
} else if (cmds.startsWith(TELL_DEL_KEYWORD + ' ')) { } else if (cmds.startsWith(TELL_DEL_KEYWORD + ' ')) {
@ -216,9 +221,9 @@ public class Tell {
if (deleted) { if (deleted) {
save(); save();
bot.send(sender, "Delivered messages have been deleted.", true); bot.send(sender, "Delivered messages have been deleted.", isPrivate);
} else { } else {
bot.send(sender, "No delivered messages were found.", true); bot.send(sender, "No delivered messages were found.", isPrivate);
} }
} else { } else {
@ -231,7 +236,7 @@ public class Tell {
messages.remove(message); messages.remove(message);
save(); save();
bot.send(sender, "Your message was deleted from the queue.", true); bot.send(sender, "Your message was deleted from the queue.", isPrivate);
deleted = true; deleted = true;
break; break;
} }
@ -239,14 +244,14 @@ public class Tell {
if (!deleted) { if (!deleted) {
if (found) { if (found) {
bot.send(sender, "Only messages that you sent can be deleted.", true); bot.send(sender, "Only messages that you sent can be deleted.", isPrivate);
} else { } else {
bot.send(sender, "The specified message [ID " + id + "] could not be found.", true); bot.send(sender, "The specified message [ID " + id + "] could not be found.", isPrivate);
} }
} }
} }
} else { } else {
helpResponse(sender); helpResponse(sender, isPrivate);
} }
} else { } else {
final String[] split = cmds.split(" ", 2); final String[] split = cmds.split(" ", 2);
@ -265,7 +270,7 @@ public class Tell {
bot.send(sender, "Sorry, the messages queue is currently full.", true); bot.send(sender, "Sorry, the messages queue is currently full.", true);
} }
} else { } else {
helpResponse(sender); helpResponse(sender, isPrivate);
} }
} }

View file

@ -96,16 +96,13 @@ public abstract class AbstractModule {
/** /**
* Responds with the module's Constants. * Responds with the module's Constants.
*
* @param bot The bot's instance. * @param bot The bot's instance.
* @param sender The sender. * @param sender The sender.
* @param args The help arguments.
* @param isPrivate Set to <code>true</code> if the response should be sent as a private message. * @param isPrivate Set to <code>true</code> if the response should be sent as a private message.
*/ */
public abstract void helpResponse(final Mobibot bot, public abstract void helpResponse(final Mobibot bot,
final String sender, final String sender,
final String args, final boolean isPrivate);
@SuppressWarnings("unused") final boolean isPrivate);
/** /**
* Returns <code>true</code> if the module is enabled. * Returns <code>true</code> if the module is enabled.

View file

@ -40,8 +40,6 @@ import org.apache.commons.lang3.StringUtils;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import static net.thauvin.erik.mobibot.Utils.bold;
/** /**
* The Calc module. * The Calc module.
* *
@ -74,7 +72,7 @@ public class Calc extends AbstractModule {
try { try {
final Expression calc = new ExpressionBuilder(query).build(); final Expression calc = new ExpressionBuilder(query).build();
return query.replace(" ", "") + " = " + decimalFormat.format(calc.evaluate()); return query.replace(" ", "") + " = " + Utils.bold(decimalFormat.format(calc.evaluate()));
} catch (Exception e) { } catch (Exception e) {
return "No idea. This is the kind of math I don't get."; return "No idea. This is the kind of math I don't get.";
} }
@ -92,7 +90,7 @@ public class Calc extends AbstractModule {
if (StringUtils.isNotBlank(args)) { if (StringUtils.isNotBlank(args)) {
bot.send(calc(args)); bot.send(calc(args));
} else { } else {
helpResponse(bot, sender, args, isPrivate); helpResponse(bot, sender, isPrivate);
} }
} }
@ -100,8 +98,8 @@ public class Calc extends AbstractModule {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
bot.send(sender, bold("To solve a mathematical calculation:")); bot.send(sender, "To solve a mathematical calculation:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + CALC_CMD + " <calculation>")); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + CALC_CMD + " <calculation>"), isPrivate);
} }
} }

View file

@ -75,7 +75,7 @@ public final class Dice extends AbstractModule {
bot.send(bot.getChannel(), bot.send(bot.getChannel(),
sender + " rolled two dice: " + bold(i) + " and " + bold(y) + " for a total of " sender + " rolled two dice: " + bold(i) + " and " + bold(y) + " for a total of "
+ bold(playerTotal)); + bold(playerTotal), isPrivate);
i = r.nextInt(6) + 1; i = r.nextInt(6) + 1;
y = r.nextInt(6) + 1; y = r.nextInt(6) + 1;
@ -97,8 +97,8 @@ public final class Dice extends AbstractModule {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
bot.send(sender, bold("To roll the dice:")); bot.send(sender, "To roll the dice:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + DICE_CMD)); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + DICE_CMD), isPrivate);
} }
} }

View file

@ -52,8 +52,6 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.thauvin.erik.mobibot.Utils.bold;
/** /**
* The GoogleSearch module. * The GoogleSearch module.
* *
@ -145,12 +143,12 @@ public final class GoogleSearch extends ThreadedModule {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
if (isEnabled()) { if (isEnabled()) {
bot.send(sender, bold("To search Google:")); bot.send(sender, "To search Google:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + GOOGLE_CMD + " <query>")); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + GOOGLE_CMD + " <query>"), isPrivate);
} else { } else {
bot.send(sender, "The Google search module is disabled."); bot.send(sender, "The Google search module is disabled.", isPrivate);
} }
} }
@ -158,7 +156,7 @@ public final class GoogleSearch extends ThreadedModule {
* Searches Google. * Searches Google.
*/ */
@Override @Override
void run(final Mobibot bot, final String sender, final String cmd, final String query) { void run(final Mobibot bot, final String sender, final String cmd, final String query, final boolean isPrivate) {
if (StringUtils.isNotBlank(query)) { if (StringUtils.isNotBlank(query)) {
try { try {
final List<Message> results = searchGoogle(query, properties.get(GOOGLE_API_KEY_PROP), final List<Message> results = searchGoogle(query, properties.get(GOOGLE_API_KEY_PROP),
@ -168,10 +166,10 @@ public final class GoogleSearch extends ThreadedModule {
} }
} catch (ModuleException e) { } catch (ModuleException e) {
bot.getLogger().warn(e.getDebugMessage(), e); bot.getLogger().warn(e.getDebugMessage(), e);
bot.send(sender, e.getMessage()); bot.send(sender, e.getMessage(), isPrivate);
} }
} else { } else {
helpResponse(bot, sender, query, true); helpResponse(bot, sender, isPrivate);
} }
} }
} }

View file

@ -44,8 +44,6 @@ import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static net.thauvin.erik.mobibot.Utils.bold;
/** /**
* The Joke module. * The Joke module.
* *
@ -107,19 +105,19 @@ public final class Joke extends ThreadedModule {
final String cmd, final String cmd,
final String args, final String args,
final boolean isPrivate) { final boolean isPrivate) {
new Thread(() -> run(bot, sender, cmd, args)).start(); new Thread(() -> run(bot, sender, cmd, args, isPrivate)).start();
} }
/** /**
* Returns a random joke from <a href="http://www.icndb.com/">The Internet Chuck Norris Database</a>. * Returns a random joke from <a href="http://www.icndb.com/">The Internet Chuck Norris Database</a>.
*/ */
@Override @Override
void run(final Mobibot bot, final String sender, final String cmd, final String arg) { void run(final Mobibot bot, final String sender, final String cmd, final String arg, final boolean isPrivate) {
try { try {
bot.send(Utils.cyan(randomJoke().getMessage())); bot.send(Utils.cyan(randomJoke().getMessage()));
} catch (ModuleException e) { } catch (ModuleException e) {
bot.getLogger().warn(e.getDebugMessage(), e); bot.getLogger().warn(e.getDebugMessage(), e);
bot.send(sender, e.getMessage()); bot.send(sender, e.getMessage(), isPrivate);
} }
} }
@ -127,8 +125,8 @@ public final class Joke extends ThreadedModule {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
bot.send(sender, bold("To retrieve a random joke:")); bot.send(sender, "To retrieve a random joke:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + JOKE_CMD)); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + JOKE_CMD), isPrivate);
} }
} }

View file

@ -41,8 +41,6 @@ import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import static net.thauvin.erik.mobibot.Utils.bold;
/** /**
* The Lookup module. * The Lookup module.
* *
@ -189,7 +187,7 @@ public final class Lookup extends AbstractModule {
} }
} }
} else { } else {
helpResponse(bot, sender, args, true); helpResponse(bot, sender, true);
} }
} }
@ -197,8 +195,8 @@ public final class Lookup extends AbstractModule {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
bot.send(sender, bold("To perform a DNS lookup query:")); bot.send(sender, "To perform a DNS lookup query:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + LOOKUP_CMD + " <ip address or hostname>")); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + LOOKUP_CMD + " <ip address or hostname>"), isPrivate);
} }
} }

View file

@ -39,8 +39,6 @@ import java.security.SecureRandom;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static net.thauvin.erik.mobibot.Utils.bold;
/** /**
* The Ping module. * The Ping module.
* *
@ -96,8 +94,8 @@ public class Ping extends AbstractModule {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
bot.send(sender, bold("To ping the bot:")); bot.send(sender, "To ping the bot:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + PING_CMD)); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + PING_CMD), isPrivate);
} }
} }

View file

@ -52,14 +52,18 @@ public abstract class ThreadedModule extends AbstractModule {
final String args, final String args,
final boolean isPrivate) { final boolean isPrivate) {
if (isEnabled() && args.length() > 0) { if (isEnabled() && args.length() > 0) {
new Thread(() -> run(bot, sender, cmd, args)).start(); new Thread(() -> run(bot, sender, cmd, args, isPrivate)).start();
} else { } else {
helpResponse(bot, sender, args, isPrivate); helpResponse(bot, sender, isPrivate);
} }
} }
/** /**
* Runs the thread. * Runs the thread.
*/ */
abstract void run(Mobibot bot, String sender, @SuppressWarnings("unused") String cmd, String args); abstract void run(Mobibot bot,
String sender,
@SuppressWarnings("unused") String cmd,
String args,
boolean isPrivate);
} }

View file

@ -41,8 +41,6 @@ import twitter4j.Status;
import twitter4j.TwitterFactory; import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder; import twitter4j.conf.ConfigurationBuilder;
import static net.thauvin.erik.mobibot.Utils.bold;
/** /**
* The Twitter module. * The Twitter module.
* *
@ -116,12 +114,12 @@ public final class Twitter extends ThreadedModule {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
if (isEnabled()) { if (isEnabled()) {
bot.send(sender, bold("To post to Twitter:")); bot.send(sender, "To post to Twitter:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TWITTER_CMD + " <message>")); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TWITTER_CMD + " <message>"), isPrivate);
} else { } else {
bot.send(sender, "The Twitter posting facility is disabled."); bot.send(sender, "The Twitter posting facility is " + Utils.bold("disabled") + '.', isPrivate);
} }
} }
@ -149,13 +147,14 @@ public final class Twitter extends ThreadedModule {
* Posts to twitter. * Posts to twitter.
*/ */
@Override @Override
void run(final Mobibot bot, final String sender, final String cmd, final String message) { void run(final Mobibot bot, final String sender, final String cmd, final String message, final boolean isPrivate) {
try { try {
bot.send(sender, bot.send(sender,
post(sender, message + " (by " + sender + " on " + bot.getChannel() + ')', false).getMessage()); post(sender, message + " (by " + sender + " on " + bot.getChannel() + ')', false).getMessage(),
isPrivate);
} catch (ModuleException e) { } catch (ModuleException e) {
bot.getLogger().warn(e.getDebugMessage(), e); bot.getLogger().warn(e.getDebugMessage(), e);
bot.send(sender, e.getMessage()); bot.send(sender, e.getMessage(), isPrivate);
} }
} }
} }

View file

@ -81,9 +81,8 @@ public final class War extends AbstractModule {
i = r.nextInt(WAR_DECK.length); i = r.nextInt(WAR_DECK.length);
y = r.nextInt(WAR_DECK.length); y = r.nextInt(WAR_DECK.length);
bot.send(bot.getChannel(), bot.send(sender + " drew the " + bold(WAR_DECK[i]) + " of " + bold(WAR_SUITS[r.nextInt(WAR_SUITS.length)]));
sender + " drew the " + bold(WAR_DECK[i]) + " of " + WAR_SUITS[r.nextInt(WAR_SUITS.length)]); bot.action("drew the " + bold(WAR_DECK[y]) + " of " + bold(WAR_SUITS[r.nextInt(WAR_SUITS.length)]));
bot.action("drew the " + bold(WAR_DECK[y]) + " of " + WAR_SUITS[r.nextInt(WAR_SUITS.length)]);
if (i != y) { if (i != y) {
break; break;
@ -103,8 +102,8 @@ public final class War extends AbstractModule {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void helpResponse(final Mobibot bot, final String sender, final boolean isPrivate) {
bot.send(sender, bold("To play war:")); bot.send(sender, "To play war:", isPrivate);
bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + WAR_CMD)); bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + WAR_CMD), isPrivate);
} }
} }