Skip to content

Commit

Permalink
[FIXED JENKINS-3543] SSL support for ircbot
Browse files Browse the repository at this point in the history
  • Loading branch information
kutzi committed Jun 26, 2011
1 parent 6efc31a commit 456931f
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 123 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Expand Up @@ -33,9 +33,9 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>pircbot</groupId>
<artifactId>pircbot</artifactId>
<version>1.4.6</version>
<groupId>org.pircbotx</groupId>
<artifactId>pircbotx</artifactId>
<version>1.3-Beta-2</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/hudson/plugins/ircbot/IrcPublisher.java
Expand Up @@ -160,6 +160,8 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Publisher>
String hostname = null;

Integer port = 194;

private boolean ssl;

String password = null;

Expand All @@ -185,7 +187,7 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Publisher>
private boolean useNotice;

private String charset;

DescriptorImpl() {
super(IrcPublisher.class);
load();
Expand Down Expand Up @@ -224,6 +226,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
throw new FormException("port field must be an Integer",
"irc_publisher.port");
}
this.ssl = "on".equals(req.getParameter("irc_publisher.ssl"));
this.commandPrefix = req.getParameter("irc_publisher.commandPrefix");
this.commandPrefix = Util.fixEmptyAndTrim(commandPrefix);

Expand Down Expand Up @@ -253,7 +256,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc

this.useNotice = "on".equals(req.getParameter(PARAMETERNAME_USE_NOTICE));

this.charset = req.getParameter("charset");
this.charset = req.getParameter("irc_publisher.charset");

// try to establish the connection
try {
Expand Down Expand Up @@ -396,6 +399,10 @@ public String getPassword() {
public int getPort() {
return port;
}

public boolean isSsl() {
return this.ssl;
}

@Override
public boolean isEnabled() {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/hudson/plugins/ircbot/v2/IRCChannel.java
Expand Up @@ -7,11 +7,13 @@
public class IRCChannel implements IMChat {

private final String channelName;
private final PircConnection connection;
private final PircListener listener;
private IRCConnection connection;

public IRCChannel(String channelName, PircConnection connection) {
public IRCChannel(String channelName, IRCConnection connection, PircListener listener) {
this.channelName = channelName;
this.connection = connection;
this.listener = listener;
}

@Override
Expand All @@ -31,16 +33,16 @@ public boolean isMultiUserChat() {

@Override
public void addMessageListener(IMMessageListener listener) {
this.connection.addMessageListener(this.channelName, listener);
this.listener.addMessageListener(this.channelName, listener);
}

@Override
public void removeMessageListener(IMMessageListener listener) {
this.connection.removeMessageListener(this.channelName, listener);
this.listener.removeMessageListener(this.channelName, listener);
}

@Override
public void sendMessage(String message) throws IMException {
this.connection.sendIMMessage(channelName, message);
this.connection.send(channelName, message);
}
}
95 changes: 70 additions & 25 deletions src/main/java/hudson/plugins/ircbot/v2/IRCConnection.java
Expand Up @@ -13,9 +13,9 @@
import hudson.plugins.im.bot.Bot;
import hudson.plugins.im.tools.ExceptionHelper;
import hudson.plugins.ircbot.IrcPublisher.DescriptorImpl;
import hudson.plugins.ircbot.v2.PircConnection.JoinListener;
import hudson.plugins.ircbot.v2.PircConnection.InviteListener;
import hudson.plugins.ircbot.v2.PircConnection.PartListener;
import hudson.plugins.ircbot.v2.PircListener.InviteListener;
import hudson.plugins.ircbot.v2.PircListener.JoinListener;
import hudson.plugins.ircbot.v2.PircListener.PartListener;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -26,16 +26,22 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

import org.pircbotx.Channel;
import org.pircbotx.PircBotX;
import org.pircbotx.exception.IrcException;
import org.pircbotx.exception.NickAlreadyInUseException;

public class IRCConnection implements IMConnection, JoinListener, InviteListener, PartListener {

private static final Logger LOGGER = Logger.getLogger(IRCConnection.class.getName());

private final DescriptorImpl descriptor;
private final AuthenticationHolder authentication;
private PircConnection pircConnection;
private final PircBotX pircConnection = new PircBotX();
private final PircListener listener;

private List<IMMessageTarget> groupChats;

Expand All @@ -52,14 +58,26 @@ public IRCConnection(DescriptorImpl descriptor, AuthenticationHolder authenticat
} else {
this.groupChats = Collections.emptyList();
}

this.pircConnection.setName(this.descriptor.getNick());

// lower delay between sending 2 messages to 500ms as we will sometimes send
// output which will consist of multiple lines (see comment in send method)
// (lowering further than this doesn't seem to work as we will otherwise be easily
// be throttled by IRC servers)
this.pircConnection.setMessageDelay(500);

this.listener = new PircListener(this.pircConnection, this.descriptor.getNick());
}

@Override
public void close() {
this.listener.explicitDisconnect = true;

if (this.pircConnection != null && this.pircConnection.isConnected()) {
this.pircConnection.removeJoinListener(this);
this.pircConnection.removePartListener(this);
this.pircConnection.removeInviteListener(this);
this.listener.removeJoinListener(this);
this.listener.removePartListener(this);
this.listener.removeInviteListener(this);
this.pircConnection.disconnect();
this.pircConnection.dispose();
}
Expand All @@ -73,17 +91,27 @@ public boolean isConnected() {
@Override
public boolean connect() {
try {
this.pircConnection = new PircConnection(this.descriptor.getNick(), this.descriptor.isUseNotice());

this.pircConnection.setEncoding(this.descriptor.getCharset());

LOGGER.info(String.format("Connecting to %s:%s as %s using charset %s",
this.descriptor.getHost(), this.descriptor.getPort(), this.descriptor.getNick(), this.descriptor.getCharset()));
this.pircConnection.connect(this.descriptor.getHost(), this.descriptor.getPort(), this.descriptor.getPassword());

String password = Util.fixEmpty(this.descriptor.getPassword());

final SocketFactory sf;
if (this.descriptor.isSsl()) {
sf = SSLSocketFactory.getDefault();
} else {
sf = SocketFactory.getDefault();
}

this.pircConnection.connect(this.descriptor.getHost(), this.descriptor.getPort(), password, sf);

LOGGER.info("connected to IRC");
this.pircConnection.addJoinListener(this);
this.pircConnection.addInviteListener(this);
this.pircConnection.addPartListener(this);
this.pircConnection.getListenerManager().addListener(this.listener);
this.listener.addJoinListener(this);
this.listener.addInviteListener(this);
this.listener.addPartListener(this);

final String nickServPassword = this.descriptor.getNickServPassword();
if(Util.fixEmpty(nickServPassword) != null) {
Expand Down Expand Up @@ -114,8 +142,8 @@ public boolean connect() {
}
}

pircConnection.addMessageListener(this.descriptor.getNick(),
PircConnection.CHAT_ESTABLISHER, new ChatEstablishedListener());
listener.addMessageListener(this.descriptor.getNick(),
PircListener.CHAT_ESTABLISHER, new ChatEstablishedListener());

return true;
} catch (NickAlreadyInUseException e) {
Expand Down Expand Up @@ -164,7 +192,7 @@ public void channelJoined(String channelName) {
LOGGER.log(Level.INFO, "Joined to channel {0} but I don't seem to belong here", channelName);
return;
}
Bot bot = new Bot(new IRCChannel(channelName, this.pircConnection),
Bot bot = new Bot(new IRCChannel(channelName, this, this.listener),
this.descriptor.getNick(), this.descriptor.getHost(),
this.descriptor.getCommandPrefix(), this.authentication);
bots.put(channelName, bot);
Expand Down Expand Up @@ -200,19 +228,36 @@ public void channelParted(String channelName) {

@Override
public void addConnectionListener(IMConnectionListener listener) {
if (this.pircConnection != null)
this.pircConnection.addConnectionListener(listener);
this.listener.addConnectionListener(listener);
}

@Override
public void removeConnectionListener(IMConnectionListener listener) {
if (this.pircConnection != null)
this.pircConnection.removeConnectionListener(listener);
this.listener.removeConnectionListener(listener);
}

@Override
public void send(IMMessageTarget target, String text) throws IMException {
this.pircConnection.sendIMMessage(target.toString(), text);
public void send(IMMessageTarget target, String text) throws IMException {
send(target.toString(), text);
}

public void send(String target, String text) throws IMException {
// many IRC clients don't seem to handle new lines well (see e.g. https://bugzilla.redhat.com/show_bug.cgi?id=136542)
// Therefore the following won't work most of the time:
// message = message.replace("\n", "\020n");
// sendNotice(target, message);

// send multiple messages instead:
Channel channel = this.pircConnection.getChannel(target);

String[] lines = text.split("\\r?\\n|\\r");
for (String line : lines) {
if (this.descriptor.isUseNotice()) {
this.pircConnection.sendNotice(channel, line);
} else {
this.pircConnection.sendMessage(channel, line);
}
}
}

@Override
Expand Down Expand Up @@ -248,7 +293,7 @@ public void onMessage(IMMessage message) {
return;
}

IRCPrivateChat chat = new IRCPrivateChat(pircConnection, descriptor.getUserName(), message.getFrom());
IRCPrivateChat chat = new IRCPrivateChat(IRCConnection.this, listener, descriptor.getUserName(), message.getFrom());
Bot bot = new Bot(chat,
descriptor.getNick(), descriptor.getHost(),
descriptor.getCommandPrefix(), authentication);
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/hudson/plugins/ircbot/v2/IRCPrivateChat.java
Expand Up @@ -6,13 +6,14 @@

public class IRCPrivateChat implements IMChat {

private final PircConnection connection;
private final PircListener listener;
private final String nick;
private final String chatPartner;
private IRCConnection connection;

public IRCPrivateChat(PircConnection connection, String nick, String chatPartner) {
super();
this.connection = connection;
public IRCPrivateChat(IRCConnection connection, PircListener listener, String nick, String chatPartner) {
this.connection = connection;
this.listener = listener;
this.nick = nick;
this.chatPartner = chatPartner;
}
Expand All @@ -33,16 +34,16 @@ public boolean isMultiUserChat() {
}
@Override
public void addMessageListener(IMMessageListener listener) {
this.connection.addMessageListener(this.nick, this.chatPartner, listener);
this.listener.addMessageListener(this.nick, this.chatPartner, listener);
}

@Override
public void removeMessageListener(IMMessageListener listener) {
this.connection.removeMessageListener(this.nick, listener);
this.listener.removeMessageListener(this.nick, listener);
}

@Override
public void sendMessage(String message) throws IMException {
this.connection.sendIMMessage(chatPartner, message);
this.connection.send(chatPartner, message);
}
}

0 comments on commit 456931f

Please sign in to comment.