Cleanup.
This commit is contained in:
parent
cfdbfca0a2
commit
d9b1cbceff
5 changed files with 144 additions and 133 deletions
|
@ -52,9 +52,6 @@ class FeedReader implements Runnable {
|
||||||
// Maximum number of feed items to display
|
// Maximum number of feed items to display
|
||||||
private static final int MAX_ITEMS = 5;
|
private static final int MAX_ITEMS = 5;
|
||||||
|
|
||||||
// Tab indent (4 spaces)
|
|
||||||
private static final String TAB_INDENT = " ";
|
|
||||||
|
|
||||||
// Bot
|
// Bot
|
||||||
private final Mobibot bot;
|
private final Mobibot bot;
|
||||||
|
|
||||||
|
@ -94,7 +91,7 @@ class FeedReader implements Runnable {
|
||||||
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(), false);
|
bot.send(sender, item.getTitle(), false);
|
||||||
bot.send(sender, TAB_INDENT + Utils.green(item.getLink()), false);
|
bot.send(sender, Utils.helpIndent(Utils.green(item.getLink()), false), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
|
|
|
@ -141,7 +141,7 @@ public class Mobibot extends PircBot {
|
||||||
// Tell object
|
// Tell object
|
||||||
public final Tell tell;
|
public final Tell tell;
|
||||||
// Commands
|
// Commands
|
||||||
private final List<AbstractCommand> commands = new ArrayList<>();
|
private final List<AbstractCommand> commands = new ArrayList<>(20);
|
||||||
// Commands Names
|
// Commands Names
|
||||||
private final List<String> commandsNames = new ArrayList<>();
|
private final List<String> commandsNames = new ArrayList<>();
|
||||||
// Main channel
|
// Main channel
|
||||||
|
|
|
@ -35,7 +35,7 @@ package net.thauvin.erik.mobibot;
|
||||||
import net.thauvin.erik.mobibot.entries.EntryLink;
|
import net.thauvin.erik.mobibot.entries.EntryLink;
|
||||||
import net.thauvin.erik.pinboard.PinboardPoster;
|
import net.thauvin.erik.pinboard.PinboardPoster;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.SwingWorker;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
|
@ -32,7 +32,6 @@
|
||||||
|
|
||||||
package net.thauvin.erik.mobibot.commands.tell;
|
package net.thauvin.erik.mobibot.commands.tell;
|
||||||
|
|
||||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|
||||||
import net.thauvin.erik.mobibot.Mobibot;
|
import net.thauvin.erik.mobibot.Mobibot;
|
||||||
import net.thauvin.erik.mobibot.Utils;
|
import net.thauvin.erik.mobibot.Utils;
|
||||||
import net.thauvin.erik.mobibot.commands.links.View;
|
import net.thauvin.erik.mobibot.commands.links.View;
|
||||||
|
@ -53,7 +52,8 @@ public class Tell {
|
||||||
* The tell command.
|
* The tell command.
|
||||||
*/
|
*/
|
||||||
public static final String TELL_CMD = "tell";
|
public static final String TELL_CMD = "tell";
|
||||||
|
// Arrow
|
||||||
|
private static final String ARROW = " --> ";
|
||||||
// Default maximum number of days to keep messages
|
// Default maximum number of days to keep messages
|
||||||
private static final int DEFAULT_TELL_MAX_DAYS = 7;
|
private static final int DEFAULT_TELL_MAX_DAYS = 7;
|
||||||
// Default message max queue size
|
// Default message max queue size
|
||||||
|
@ -64,7 +64,6 @@ public class Tell {
|
||||||
private static final String TELL_ALL_KEYWORD = "all";
|
private static final String TELL_ALL_KEYWORD = "all";
|
||||||
//T he delete command.
|
//T he delete command.
|
||||||
private static final String TELL_DEL_KEYWORD = "del";
|
private static final String TELL_DEL_KEYWORD = "del";
|
||||||
|
|
||||||
// Bot instance
|
// Bot instance
|
||||||
private final Mobibot bot;
|
private final Mobibot bot;
|
||||||
// Maximum number of days to keep messages
|
// Maximum number of days to keep messages
|
||||||
|
@ -111,100 +110,8 @@ public class Tell {
|
||||||
return TellMessagesMgr.clean(messages, maxDays);
|
return TellMessagesMgr.clean(messages, maxDays);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Delete message.
|
||||||
* Responds with Constants.
|
private void deleteMessage(final String sender, final String cmds, final boolean isPrivate) {
|
||||||
*
|
|
||||||
* @param sender The sender.
|
|
||||||
* @param isPrivate The private flag.
|
|
||||||
*/
|
|
||||||
public void helpResponse(final String sender, final boolean isPrivate) {
|
|
||||||
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>"), isPrivate);
|
|
||||||
|
|
||||||
bot.send(sender, "To view queued and sent messages:", isPrivate);
|
|
||||||
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."),
|
|
||||||
isPrivate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns <code>true</code> if enabled.
|
|
||||||
*
|
|
||||||
* @return <code>true</code> or <code>false</code>
|
|
||||||
*/
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return maxSize > 0 && maxDays > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Processes the commands.
|
|
||||||
*
|
|
||||||
* @param sender The sender's nick.
|
|
||||||
* @param cmds The commands string.
|
|
||||||
* @param isPrivate The private flag.
|
|
||||||
*/
|
|
||||||
@SuppressFBWarnings(value = "CC_CYCLOMATIC_COMPLEXITY",
|
|
||||||
justification = "Working on it.")
|
|
||||||
public void response(final String sender, final String cmds, final boolean isPrivate) {
|
|
||||||
final String arrow = " --> ";
|
|
||||||
if (StringUtils.isBlank(cmds)) {
|
|
||||||
helpResponse(sender, isPrivate);
|
|
||||||
} else if (cmds.startsWith(View.VIEW_CMD)) {
|
|
||||||
if (bot.isOp(sender) && (View.VIEW_CMD + ' ' + TELL_ALL_KEYWORD).equals(cmds)) {
|
|
||||||
if (!messages.isEmpty()) {
|
|
||||||
for (final TellMessage message : messages) {
|
|
||||||
bot.send(sender, Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
|
|
||||||
+ " [ID: " + message.getId() + ", "
|
|
||||||
+ (message.isReceived() ? "DELIVERED" : "QUEUED") + ']',
|
|
||||||
isPrivate);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
bot.send(sender, "There are no messages in the queue.", isPrivate);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
boolean hasMessage = false;
|
|
||||||
|
|
||||||
for (final TellMessage message : messages) {
|
|
||||||
if (message.isMatch(sender)) {
|
|
||||||
if (!hasMessage) {
|
|
||||||
hasMessage = true;
|
|
||||||
bot.send(sender, "Here are your messages: ", isPrivate);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.isReceived()) {
|
|
||||||
bot.send(sender,
|
|
||||||
Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
|
|
||||||
+ " [" + Utils.utcDateTime(message.getReceived()) + ", ID: "
|
|
||||||
+ message.getId() + ", DELIVERED]",
|
|
||||||
isPrivate);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
bot.send(sender,
|
|
||||||
Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
|
|
||||||
+ " [" + Utils.utcDateTime(message.getQueued()) + ", ID: "
|
|
||||||
+ message.getId() + ", QUEUED]",
|
|
||||||
isPrivate);
|
|
||||||
}
|
|
||||||
|
|
||||||
bot.send(sender, Utils.helpIndent(message.getMessage()), isPrivate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!hasMessage) {
|
|
||||||
bot.send(sender, "You have no messages in the queue.", isPrivate);
|
|
||||||
} else {
|
|
||||||
bot.send(sender, "To delete one or all delivered messages:", isPrivate);
|
|
||||||
bot.send(sender,
|
|
||||||
Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + ' ' + TELL_DEL_KEYWORD + " <id|"
|
|
||||||
+ TELL_ALL_KEYWORD + '>'), isPrivate);
|
|
||||||
bot.send(sender,
|
|
||||||
"Messages are kept for " + Utils.bold(maxDays) + Utils.plural(maxDays, " day.", " days."),
|
|
||||||
isPrivate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (cmds.startsWith(TELL_DEL_KEYWORD + ' ')) {
|
|
||||||
final String[] split = cmds.split(" ");
|
final String[] split = cmds.split(" ");
|
||||||
|
|
||||||
if (split.length == 2) {
|
if (split.length == 2) {
|
||||||
|
@ -253,7 +160,37 @@ public class Tell {
|
||||||
} else {
|
} else {
|
||||||
helpResponse(sender, isPrivate);
|
helpResponse(sender, isPrivate);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Responds with Constants.
|
||||||
|
*
|
||||||
|
* @param sender The sender.
|
||||||
|
* @param isPrivate The private flag.
|
||||||
|
*/
|
||||||
|
public void helpResponse(final String sender, final boolean isPrivate) {
|
||||||
|
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>"), isPrivate);
|
||||||
|
|
||||||
|
bot.send(sender, "To view queued and sent messages:", isPrivate);
|
||||||
|
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."),
|
||||||
|
isPrivate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns <code>true</code> if enabled.
|
||||||
|
*
|
||||||
|
* @return <code>true</code> or <code>false</code>
|
||||||
|
*/
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return maxSize > 0 && maxDays > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// New message.
|
||||||
|
private void newMessage(final String sender, final String cmds, final boolean isPrivate) {
|
||||||
final String[] split = cmds.split(" ", 2);
|
final String[] split = cmds.split(" ", 2);
|
||||||
|
|
||||||
if (split.length == 2 && (StringUtils.isNotBlank(split[1]) && split[1].contains(" "))) {
|
if (split.length == 2 && (StringUtils.isNotBlank(split[1]) && split[1].contains(" "))) {
|
||||||
|
@ -274,6 +211,28 @@ public class Tell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processes the commands.
|
||||||
|
*
|
||||||
|
* @param sender The sender's nick.
|
||||||
|
* @param cmds The commands string.
|
||||||
|
* @param isPrivate The private flag.
|
||||||
|
*/
|
||||||
|
public void response(final String sender, final String cmds, final boolean isPrivate) {
|
||||||
|
if (StringUtils.isBlank(cmds)) {
|
||||||
|
helpResponse(sender, isPrivate);
|
||||||
|
} else if (cmds.startsWith(View.VIEW_CMD)) {
|
||||||
|
if (bot.isOp(sender) && (View.VIEW_CMD + ' ' + TELL_ALL_KEYWORD).equals(cmds)) {
|
||||||
|
viewAll(sender, isPrivate);
|
||||||
|
} else {
|
||||||
|
viewMessages(sender, isPrivate);
|
||||||
|
}
|
||||||
|
} else if (cmds.startsWith(TELL_DEL_KEYWORD + ' ')) {
|
||||||
|
deleteMessage(sender, cmds, isPrivate);
|
||||||
|
} else {
|
||||||
|
newMessage(sender, cmds, isPrivate);
|
||||||
|
}
|
||||||
|
|
||||||
if (clean()) {
|
if (clean()) {
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
|
@ -351,4 +310,61 @@ public class Tell {
|
||||||
public int size() {
|
public int size() {
|
||||||
return messages.size();
|
return messages.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// View all messages.
|
||||||
|
private void viewAll(final String sender, final boolean isPrivate) {
|
||||||
|
if (!messages.isEmpty()) {
|
||||||
|
for (final TellMessage message : messages) {
|
||||||
|
bot.send(sender, Utils.bold(message.getSender()) + ARROW + Utils.bold(message.getRecipient())
|
||||||
|
+ " [ID: " + message.getId() + ", "
|
||||||
|
+ (message.isReceived() ? "DELIVERED" : "QUEUED") + ']',
|
||||||
|
isPrivate);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bot.send(sender, "There are no messages in the queue.", isPrivate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// View messages.
|
||||||
|
private void viewMessages(final String sender, final boolean isPrivate) {
|
||||||
|
boolean hasMessage = false;
|
||||||
|
|
||||||
|
for (final TellMessage message : messages) {
|
||||||
|
if (message.isMatch(sender)) {
|
||||||
|
if (!hasMessage) {
|
||||||
|
hasMessage = true;
|
||||||
|
bot.send(sender, "Here are your messages: ", isPrivate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.isReceived()) {
|
||||||
|
bot.send(sender,
|
||||||
|
Utils.bold(message.getSender()) + ARROW + Utils.bold(message.getRecipient())
|
||||||
|
+ " [" + Utils.utcDateTime(message.getReceived()) + ", ID: "
|
||||||
|
+ Utils.bold(message.getId()) + ", DELIVERED]",
|
||||||
|
isPrivate);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
bot.send(sender,
|
||||||
|
Utils.bold(message.getSender()) + ARROW + Utils.bold(message.getRecipient())
|
||||||
|
+ " [" + Utils.utcDateTime(message.getQueued()) + ", ID: "
|
||||||
|
+ Utils.bold(message.getId()) + ", QUEUED]",
|
||||||
|
isPrivate);
|
||||||
|
}
|
||||||
|
|
||||||
|
bot.send(sender, Utils.helpIndent(message.getMessage()), isPrivate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasMessage) {
|
||||||
|
bot.send(sender, "You have no messages in the queue.", isPrivate);
|
||||||
|
} else {
|
||||||
|
bot.send(sender, "To delete one or all delivered messages:", isPrivate);
|
||||||
|
bot.send(sender,
|
||||||
|
Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + ' ' + TELL_DEL_KEYWORD + " <id|"
|
||||||
|
+ TELL_ALL_KEYWORD + '>'), isPrivate);
|
||||||
|
bot.send(sender,
|
||||||
|
"Messages are kept for " + Utils.bold(maxDays) + Utils.plural(maxDays, " day.", " days."),
|
||||||
|
isPrivate);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,8 +66,6 @@ public final class GoogleSearch extends ThreadedModule {
|
||||||
static final String GOOGLE_CSE_KEY_PROP = "google-cse-cx";
|
static final String GOOGLE_CSE_KEY_PROP = "google-cse-cx";
|
||||||
// Google command
|
// Google command
|
||||||
private static final String GOOGLE_CMD = "google";
|
private static final String GOOGLE_CMD = "google";
|
||||||
// Tab indent (4 spaces)
|
|
||||||
private static final String TAB_INDENT = " ";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@link GoogleSearch} instance.
|
* Creates a new {@link GoogleSearch} instance.
|
||||||
|
@ -126,7 +124,7 @@ public final class GoogleSearch extends ThreadedModule {
|
||||||
final JSONObject j = ja.getJSONObject(i);
|
final JSONObject j = ja.getJSONObject(i);
|
||||||
results.add(new NoticeMessage(Utils.unescapeXml(j.getString("title"))));
|
results.add(new NoticeMessage(Utils.unescapeXml(j.getString("title"))));
|
||||||
results.add(
|
results.add(
|
||||||
new NoticeMessage(TAB_INDENT + j.getString("link"), Colors.DARK_GREEN));
|
new NoticeMessage(Utils.helpIndent(j.getString("link"), false), Colors.DARK_GREEN));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -135,7 +133,7 @@ public final class GoogleSearch extends ThreadedModule {
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
} else {
|
} else {
|
||||||
throw new ModuleException("Invalid query.");
|
throw new ModuleException("Invalid query. Please try again.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue