Skip to content
Permalink
Browse files
[FIXED JENKINS-11606]: configuration option to disable commands in ch…
…at rooms
  • Loading branch information
kutzi committed Nov 6, 2011
1 parent d8b0330 commit a531942907f9fd2615a029092b27ebf154b57812
@@ -18,15 +18,21 @@

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;
}
@@ -39,6 +39,14 @@
* 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.
@@ -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,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) {
@@ -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);
}

0 comments on commit a531942

Please sign in to comment.