Skip to content

Commit

Permalink
Bot.java: add support for "commandPrefixRequired" toggle [JENKINS-17380]
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Jan 22, 2024
1 parent f6786a9 commit c78f880
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/main/java/hudson/plugins/im/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public void executeCommand(Bot bot, IMChat groupChat, IMMessage message,
for (final Entry<String, BotCommand> item : bot.cmdsAndAliases.entrySet()) {
// skip myself
if ((item.getValue() != this)
&& (item.getValue().getHelp() != null)) {
&& (item.getValue().getHelp() != null)
) {
msg.append("\n");
msg.append(item.getKey());
msg.append(item.getValue().getHelp());
Expand All @@ -73,6 +74,7 @@ public String getHelp() {
private final String nick;
private final String imServer;
private final String commandPrefix;
private final boolean commandPrefixRequired;
private boolean commandsAccepted;
private String helpCache = null;

Expand All @@ -81,18 +83,20 @@ public String getHelp() {
@SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
justification = "Need ecosystem change to separate Bot construction from IMChat connection")
public Bot(IMChat chat, String nick, String imServer,
String commandPrefix, AuthenticationHolder authentication
) {
String commandPrefix, AuthenticationHolder authentication,
boolean commandPrefixRequired
) {
this.chat = chat;
this.nick = nick;
this.imServer = imServer;
this.commandPrefix = commandPrefix;
this.commandPrefixRequired = commandPrefixRequired;
this.authentication = authentication;
this.commandsAccepted = chat.isCommandsAccepted();

for (BotCommand cmd : BotCommand.all()) {
for (String name : cmd.getCommandNames())
this.cmdsAndAliases.put(name,cmd);
this.cmdsAndAliases.put(name, cmd);
}

// MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR
Expand All @@ -102,6 +106,25 @@ public Bot(IMChat chat, String nick, String imServer,
chat.addMessageListener(this);
}

/**
* Long-time default constructor (and class) behavior, which requires
* the {@code commandPrefix} to be present. A different constructor is
* available to customize that toggle.
*
* @param chat
* @param nick
* @param imServer
* @param commandPrefix
* @param authentication
*/
@SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
justification = "Need ecosystem change to separate Bot construction from IMChat connection")
public Bot(IMChat chat, String nick, String imServer,
String commandPrefix, AuthenticationHolder authentication
) {
this(chat, nick, imServer, commandPrefix, authentication, true);
}

/**
* Returns an identifier describing the Im account used to send the build command.
* E.g. the Jabber ID of the Bot.
Expand Down Expand Up @@ -198,6 +221,18 @@ && isNickSeparator(body.substring(this.nick.length(), this.nick
return body.substring(this.nick.length() + 1).trim();
}

// By default, bots require the commandPrefix or nick prefix at least,
// but this can be an overkill in private chat sessions (admin to bot).
// If the caller like ircbot-plugin used commandPrefixRequired=false,
// they knew what they were doing - allow any message pattern to be
// treated as a potential bot command.
// Note they might not have an acceptable solution to just use an
// empty commandPrefix (e.g. legacy automation config vs. interactive
// chats, or a single setting for all chats in some consumer plugins).
if (!this.commandPrefixRequired)
return body.trim();

Check warning on line 233 in src/main/java/hudson/plugins/im/bot/Bot.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 54-233 are not covered by tests

// By default, this message was not destined to be seen by the bot.
return null;
}

Expand Down

0 comments on commit c78f880

Please sign in to comment.