Skip to content

Commit

Permalink
[FIXED JENKINS-11606]: configuration option to disable commands in ch…
Browse files Browse the repository at this point in the history
…at rooms
  • Loading branch information
kutzi committed Nov 6, 2011
1 parent d8b0330 commit a531942
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 53 deletions.
79 changes: 46 additions & 33 deletions src/main/java/hudson/plugins/im/GroupChatIMMessageTarget.java
Expand Up @@ -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() {
Expand All @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/hudson/plugins/im/IMChat.java
Expand Up @@ -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.
Expand Down
51 changes: 31 additions & 20 deletions src/main/java/hudson/plugins/im/bot/Bot.java
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -104,31 +106,27 @@ 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);
if (args.length > 0) {
// 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) {
Expand All @@ -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) {
Expand All @@ -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);
}

Expand Down

0 comments on commit a531942

Please sign in to comment.