diff --git a/src/main/java/hudson/plugins/im/GroupChatIMMessageTarget.java b/src/main/java/hudson/plugins/im/GroupChatIMMessageTarget.java index 934937ed..7e3b73d7 100644 --- a/src/main/java/hudson/plugins/im/GroupChatIMMessageTarget.java +++ b/src/main/java/hudson/plugins/im/GroupChatIMMessageTarget.java @@ -18,15 +18,21 @@ public class GroupChatIMMessageTarget implements IMMessageTarget { private String name; private String password; + private boolean notificationOnly; + /** + * @deprecated use {@link GroupChatIMMessageTarget#GroupChatIMMessageTarget(String, String, boolean)}! + */ + @Deprecated public GroupChatIMMessageTarget(final String name) { - this(name, null); + this(name, null, false); } - public GroupChatIMMessageTarget(String name, String password) { + public GroupChatIMMessageTarget(String name, String password, boolean notificationOnly) { Assert.notNull(name, "Parameter 'name' must not be null."); this.name = name; this.password = password; + this.notificationOnly = notificationOnly; } public String getName() { @@ -40,40 +46,47 @@ public String getPassword() { public boolean hasPassword() { return Util.fixEmpty(this.password) != null; } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - result = prime * result - + ((password == null) ? 0 : password.hashCode()); - return result; + + public boolean isNotificationOnly() { + return this.notificationOnly; } - + @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - GroupChatIMMessageTarget other = (GroupChatIMMessageTarget) obj; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - if (password == null) { - if (other.password != null) - return false; - } else if (!password.equals(other.password)) - return false; - return true; - } + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + (notificationOnly ? 1231 : 1237); + result = prime * result + + ((password == null) ? 0 : password.hashCode()); + return result; + } - @Override + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + GroupChatIMMessageTarget other = (GroupChatIMMessageTarget) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (notificationOnly != other.notificationOnly) + return false; + if (password == null) { + if (other.password != null) + return false; + } else if (!password.equals(other.password)) + return false; + return true; + } + + @Override public String toString() { return this.name; } diff --git a/src/main/java/hudson/plugins/im/IMChat.java b/src/main/java/hudson/plugins/im/IMChat.java index 94aa881d..e918923e 100644 --- a/src/main/java/hudson/plugins/im/IMChat.java +++ b/src/main/java/hudson/plugins/im/IMChat.java @@ -39,6 +39,14 @@ public interface IMChat { * as opposed to a single user chat (IRC private message exchange). */ public boolean isMultiUserChat(); + + /** + * Returns if commands for Jenkins are accepted via this chat. + * Otherwise this chat only acts as a notification target. + * + * @since 1.21 + */ + public boolean isCommandsAccepted(); /** * Adds a new {@link IMMessageListener} to this chat. diff --git a/src/main/java/hudson/plugins/im/bot/Bot.java b/src/main/java/hudson/plugins/im/bot/Bot.java index 505686a5..e7bf6500 100644 --- a/src/main/java/hudson/plugins/im/bot/Bot.java +++ b/src/main/java/hudson/plugins/im/bot/Bot.java @@ -71,6 +71,7 @@ public String getHelp() { private final String nick; private final String imServer; private final String commandPrefix; + private boolean commandsAccepted; private String helpCache = null; private final AuthenticationHolder authentication; @@ -83,6 +84,7 @@ public Bot(IMChat chat, String nick, String imServer, this.imServer = imServer; this.commandPrefix = commandPrefix; this.authentication = authentication; + this.commandsAccepted = chat.isCommandsAccepted(); for (BotCommand cmd : BotCommand.all()) { for (String name : cmd.getCommandNames()) @@ -104,15 +106,20 @@ public void onMessage(IMMessage msg) { // is it a command for me ? (returns null if not, the payload if so) String payload = retrieveMessagePayLoad(msg.getBody()); if (payload != null) { - String sender = msg.getFrom(); - if (!msg.isAuthorized()) { - try { - this.chat.sendMessage(sender + " you're not a buddy of me. I won't take any commands from you."); - } catch (IMException e) { - LOGGER.warning(ExceptionHelper.dump(e)); - } - return; - } + Sender s = getSender(msg); + + try { + if (!this.commandsAccepted) { + this.chat.sendMessage(s.getNickname() + " you may not issue bot commands in this chat!"); + return; + } else if (!msg.isAuthorized()) { + this.chat.sendMessage(s.getNickname() + " you're not a buddy of me. I won't take any commands from you!"); + return; + } + } catch (IMException e) { + LOGGER.warning(ExceptionHelper.dump(e)); + return; + } // split words String[] args = MessageHelper.extractCommandLine(payload); @@ -120,15 +127,6 @@ public void onMessage(IMMessage msg) { // first word is the command name String cmd = args[0]; - String id = this.chat.getIMId(sender); - - final Sender s; - if (id != null) { - s = new Sender(this.chat.getNickName(sender), id); - } else { - s = new Sender(this.chat.getNickName(sender)); - } - try { BotCommand command = this.cmdsAndAliases.get(cmd); if (command != null) { @@ -144,7 +142,7 @@ public void onMessage(IMMessage msg) { } } } else { - this.chat.sendMessage(sender + " did you mean me? Unknown command '" + cmd + this.chat.sendMessage(s.getNickname() + " did you mean me? Unknown command '" + cmd + "'\nUse '" + this.commandPrefix + " help' to get help!"); } } catch (IMException e) { @@ -154,7 +152,20 @@ public void onMessage(IMMessage msg) { } } - private static boolean isNickSeparator(final String candidate) { + private Sender getSender(IMMessage msg) { + String sender = msg.getFrom(); + String id = this.chat.getIMId(sender); + + final Sender s; + if (id != null) { + s = new Sender(this.chat.getNickName(sender), id); + } else { + s = new Sender(this.chat.getNickName(sender)); + } + return s; + } + + private static boolean isNickSeparator(final String candidate) { return ":".equals(candidate) || ",".equals(candidate); }