Added ChannelFeed command.

This commit is contained in:
Erik C. Thauvin 2020-04-27 23:18:30 -07:00
parent bcf659c5a8
commit 34636ea9f9
3 changed files with 103 additions and 81 deletions

View file

@ -48,7 +48,7 @@ import java.util.List;
* @created Feb 1, 2004
* @since 1.0
*/
class FeedReader implements Runnable {
public class FeedReader implements Runnable {
// Maximum number of feed items to display
private static final int MAX_ITEMS = 5;
@ -68,7 +68,7 @@ class FeedReader implements Runnable {
* @param sender The nick of the person who sent the message.
* @param url The URL to fetch.
*/
FeedReader(final Mobibot bot, final String sender, final String url) {
public FeedReader(final Mobibot bot, final String sender, final String url) {
this.bot = bot;
this.sender = sender;
this.url = url;

View file

@ -168,8 +168,6 @@ public class Mobibot extends PircBot {
private final Twitter twitterModule;
// Backlogs URL
private String backLogsUrl = "";
// Feed URL
private String feedUrl = "";
// Ident message
private String identMsg = "";
// Ident nick
@ -229,41 +227,41 @@ public class Mobibot extends PircBot {
// Set the URLs
setWeblogUrl(getVersion());
setFeedUrl(p.getProperty("feed", ""));
setBacklogsUrl(Utils.ensureDir(p.getProperty("backlogs", weblogUrl), true));
// Set the pinboard authentication
setPinboardAuth(p.getProperty("pinboard-api-token"));
// Set the ignored nicks
ignoreCommand = new Ignore(p.getProperty("ignore", ""));
// Load the commands
commands.add(new AddLog());
commands.add(new Cycle());
commands.add(ignoreCommand);
commands.add(new Info());
commands.add(new Me());
commands.add(new Modules());
commands.add(new Msg());
commands.add(new Nick());
commands.add(new Recap());
commands.add(new Say());
commands.add(new Users());
commands.add(new Versions());
addCommand(new AddLog());
addCommand(new ChannelFeed(getChannelName(), p.getProperty("feed", "")));
addCommand(new Cycle());
addCommand(new Info());
addCommand(new Me());
addCommand(new Modules());
addCommand(new Msg());
addCommand(new Nick());
addCommand(new Recap());
addCommand(new Say());
addCommand(new Users());
addCommand(new Versions());
// Tell
// Ignore command
ignoreCommand = new Ignore(p.getProperty("ignore", ""));
addCommand(ignoreCommand);
// Tell command
tell = new Tell(this, p.getProperty("tell-max-days"), p.getProperty("tell-max-size"));
if (tell.isEnabled()) {
commands.add(tell);
addCommand(tell);
}
// Load the links commands
commands.add(new Comment());
commands.add(new Posting());
commands.add(new Tags());
commands.add(new UrlMgr(p.getProperty("tags", ""), p.getProperty("tags-keywords", "")));
commands.add(new View());
addCommand(new Comment());
addCommand(new Posting());
addCommand(new Tags());
addCommand(new UrlMgr(p.getProperty("tags", ""), p.getProperty("tags-keywords", "")));
addCommand(new View());
// Load the modules
addModule(new Calc());
@ -485,19 +483,6 @@ public class Mobibot extends PircBot {
}
}
/**
* Responds with the title and links from the RSS feed.
*
* @param sender The nick of the person who sent the message.
*/
private void feedResponse(final String sender) {
if (isNotBlank(feedUrl)) {
new Thread(new FeedReader(this, sender, feedUrl)).start();
} else {
send(sender, "There is no feed setup for this channel.", false);
}
}
/**
* Returns the backlogs URL.
*
@ -696,15 +681,9 @@ public class Mobibot extends PircBot {
if (StringUtils.isBlank(topic)) {
helpDefault(sender, isOp, isPrivate);
} else {
final String lcTopic = lowerCase(topic).trim();
if (lcTopic.equals(getChannelName())) {
send(sender, "To list the last 5 posts from the channel's weblog:", isPrivate);
send(sender, Utils.helpIndent(getNick() + ": " + getChannelName()), isPrivate);
} else {
// Command, Modules or Default
if (!helpCommands(sender, topic, isPrivate) && !helpModules(sender, lcTopic, isPrivate)) {
helpDefault(sender, isOp, isPrivate);
}
// Command, Modules or Default
if (!helpCommands(sender, topic, isPrivate) && !helpModules(sender, lowerCase(topic).trim(), isPrivate)) {
helpDefault(sender, isOp, isPrivate);
}
}
}
@ -790,47 +769,35 @@ public class Mobibot extends PircBot {
if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help
helpResponse(sender, args, false);
} else if (cmd.equalsIgnoreCase(getChannelName())) { // mobibot: <channel>
feedResponse(sender);
} else {
boolean skip = false;
// Commands
for (final AbstractCommand command : commands) {
if (command.isPublic() && command.getCommand().startsWith(cmd)) {
command.commandResponse(this, sender, login, args, isOp(sender), false);
skip = true;
break;
return;
}
}
if (!skip) {
// Modules
for (final AbstractModule module : modules) { // modules
for (final String c : module.getCommands()) {
if (cmd.startsWith(c)) {
module.commandResponse(this, sender, cmd, args, false);
break;
}
// Modules
for (final AbstractModule module : modules) { // modules
for (final String c : module.getCommands()) {
if (cmd.startsWith(c)) {
module.commandResponse(this, sender, cmd, args, false);
return;
}
}
}
}
} else { // Commands
} else {
// Commands, e.g.: https://www.example.com/
for (final AbstractCommand command : commands) {
if (command.matches(message)) {
command.commandResponse(this, sender, login, message, isOp(sender), false);
isCommand = true;
break;
return;
}
}
}
if (!isCommand) {
Recap.storeRecap(sender, message, false);
}
if (tell.isEnabled()) {
tell.send(sender, true);
}
Recap.storeRecap(sender, message, false);
}
/**
@ -1014,15 +981,6 @@ public class Mobibot extends PircBot {
backLogsUrl = url;
}
/**
* Sets the feed URL.
*
* @param feedUrl The feed URL.
*/
final void setFeedUrl(final String feedUrl) {
this.feedUrl = feedUrl;
}
/**
* Sets the bot's identification.
*

View file

@ -0,0 +1,64 @@
/*
* Feed.kt
*
* Copyright (c) 2004-2020, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.FeedReader
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import org.apache.commons.lang3.StringUtils.isNotBlank
class ChannelFeed(channel: String, private val feedUrl: String) : AbstractCommand() {
override val command = channel
override val help = listOf(
"To list the last 5 posts from the channel's weblog feed:",
Utils.helpIndent("%c $channel")
)
override val isOp = false
override val isPublic = true
override val isVisible = true
override fun commandResponse(
bot: Mobibot,
sender: String,
login: String,
args: String,
isOp: Boolean,
isPrivate: Boolean
) {
if (isNotBlank(feedUrl)) {
Thread(FeedReader(bot, sender, feedUrl)).start()
} else {
bot.send(sender, "There is no feed setup for this channel.", false)
}
}
}