/**
* IRC publisher for CruiseControl using the moepii irc library
* (http: */
package net.sourceforge.cruisecontrol.publishers;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import net.sourceforge.cruisecontrol.CruiseControlException;
import net.sourceforge.cruisecontrol.Publisher;
import net.sourceforge.cruisecontrol.util.ValidationHelper;
import net.sourceforge.cruisecontrol.util.XMLLogHelper;
import org.apache.log4j.Logger;
import org.jdom.Element;
import org.schwering.irc.lib.IRCConnection;
import org.schwering.irc.lib.IRCEventListener;
import org.schwering.irc.lib.IRCModeParser;
import org.schwering.irc.lib.IRCUser;
/**
* @author Ilkka Poutanen <PoutanenIlkka@JohnDeere.com>
*
*/
public class IRCPublisher implements Publisher {
/**
* Our logger object.
*/
private static final Logger LOG = Logger.getLogger(IRCPublisher.class);
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The IRC connection.
*/
private IRCConnection conn;
/**
* The IRC server
*/
private String host;
/**
* The IRC server's port
*/
private int port = 6667;
/**
* The nickname to use.
*/
private String nickname = "buildbot";
/**
* The username to use.
*/
private String username = "CruiseControl";
/**
* The realname to use.
*/
private String realname = "CruiseControl build daemon";
/**
* The channel to use.
*/
private String channel;
/**
* The quit message.
*/
private String quitmsg = "Buh-bye";
/**
* The URL to the build results.
*/
private String buildResultsURL;
/*
* (non-Javadoc)
*
* @see net.sourceforge.cruisecontrol.Publisher#publish(org.jdom.Element)
*/
public void publish(Element cruisecontrolLog) throws CruiseControlException {
init();
try {
conn.connect();
} catch (IOException ioe) {
LOG.error("Couldn't connect to IRC server", ioe);
}
conn.doJoin(channel);
XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
String msg = "";
if (helper.isBuildSuccessful()) {
msg += "Build successful for " + helper.getProjectName();
} else if (helper.isBuildFix()) {
msg += "Build fixed for " + helper.getProjectName();
} else {
msg += "Build FAILURE for " + helper.getProjectName() + ". ";
msg += "Includes modifications by ";
Set modificationset = helper.getBuildParticipants();
Iterator iter = modificationset.iterator();
while (iter.hasNext()) {
String modname = (String) iter.next();
msg += modname;
if (iter.hasNext()) {
msg += ", ";
}
}
}
msg += ". Result URL: " + getLogfileURL(helper);
conn.doPrivmsg(channel, msg);
conn.doQuit(quitmsg);
}
/**
* @return the channel
*/
public String getChannel() {
return channel;
}
/**
* @param channel
* the channel to set
*/
public void setChannel(String channel) {
this.channel = channel;
}
/**
* @return the host
*/
public String getHost() {
return host;
}
/**
* @param host
* the host to set
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return the nickname
*/
public String getNickname() {
return nickname;
}
/**
* @param nickname
* the nickname to set
*/
public void setNickname(String nickname) {
this.nickname = nickname;
}
/**
* @return the port
*/
public int getPort() {
return port;
}
/**
* @param port
* the port to set
*/
public void setPort(int port) {
this.port = port;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username
* the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Initialize the connection.
*/
protected void init() {
conn = new IRCConnection(host, new int[] { port }, null, nickname,
username, realname);
conn.addIRCEventListener(new Listener());
conn.setEncoding("UTF-8");
conn.setPong(true);
conn.setDaemon(false);
conn.setColors(true);
}
/*
* (non-Javadoc)
*
* @see net.sourceforge.cruisecontrol.Publisher#validate()
*/
public void validate() throws CruiseControlException {
ValidationHelper.assertIsSet(host, "host", this.getClass());
ValidationHelper.assertIsSet(nickname, "nickname", this.getClass());
ValidationHelper.assertIsSet(username, "username", this.getClass());
ValidationHelper.assertIsSet(channel, "channel", this.getClass());
ValidationHelper.assertIsSet(buildResultsURL, "buildResultsURL", this.getClass());
}
/**
* This method returns the build result URL as a string.
* @param logHelper the log helper.
* @return the URL as string.
*/
protected String getLogfileURL(XMLLogHelper logHelper) {
String logFileName = "";
try {
logFileName = logHelper.getLogFileName();
} catch (CruiseControlException e) {
LOG.error("Error getting logfile name from helper", e);
}
String baseLogFileName =
logFileName.substring(
logFileName.lastIndexOf(File.separator) + 1,
logFileName.lastIndexOf("."));
StringBuffer str = new StringBuffer();
str.append(getBuildResultsURL());
if (buildResultsURL.indexOf("?") == -1) {
str.append("?");
} else {
str.append("&");
}
str.append("log=");
str.append(baseLogFileName);
return str.toString();
}
/**
* @return the realname
*/
public String getRealname() {
return realname;
}
/**
* @param realname
* the realname to set
*/
public void setRealname(String realname) {
this.realname = realname;
}
public class Listener implements IRCEventListener {
public void onDisconnected() {
LOG.info("Disconnected");
}
public void onError(String msg) {
LOG.warn("Error: " + msg);
}
public void onError(int num, String msg) {
LOG.warn("Error: " + num + " : " + msg);
}
public void onInvite(String chan, IRCUser user, String passiveNick) {
LOG.info("Invite to " + chan + " from " + user);
}
public void onJoin(String chan, IRCUser user) {
LOG.info("Joining " + chan);
}
public void onKick(String chan, IRCUser user, String passiveNick,
String msg) {
LOG.info("Kick on " + chan + ": " + user + " (" + msg + ")");
}
public void onMode(String chan, IRCUser user, IRCModeParser modeParser) {
LOG.info("Mode " + modeParser.getLine() + " (" + user + "@" + chan
+ ")");
}
public void onMode(IRCUser user, String passiveNick, String mode) {
LOG.info("Mode " + mode + " (" + user + ")");
}
public void onNick(IRCUser user, String newNick) {
LOG.info("Nick change for " + user + ": " + newNick);
}
public void onNotice(String target, IRCUser user, String msg) {
LOG.info("Notice: " + target + " " + user + ": " + msg);
}
public void onPart(String chan, IRCUser user, String msg) {
LOG.info("Part: " + chan + " " + user + " " + msg);
}
public void onPing(String ping) {
LOG.info("Ping");
}
public void onPrivmsg(String target, IRCUser user, String msg) {
LOG.info("PrivMsg: " + target + " " + user + " " + msg);
}
public void onQuit(IRCUser user, String msg) {
LOG.info("Quite: " + user + " " + msg);
}
public void onRegistered() {
LOG.info("Registered");
}
public void onReply(int num, String value, String msg) {
LOG.info("Reply: " + num + " " + value + " " + msg);
}
public void onTopic(String chan, IRCUser user, String topic) {
LOG.info("Topic: " + chan + " " + user + " " + topic);
}
public void unknown(String prefix, String command, String middle,
String trailing) {
LOG.warn("Unknown: " + command);
}
}
/**
* @return the quitmsg
*/
public String getQuitmsg() {
return quitmsg;
}
/**
* @param quitmsg the quitmsg to set
*/
public void setQuitmsg(String quitmsg) {
this.quitmsg = quitmsg;
}
/**
* @return the buildResultsURL
*/
public String getBuildResultsURL() {
return buildResultsURL;
}
/**
* @param buildResultsURL the buildResultsURL to set
*/
public void setBuildResultsURL(String buildResultsURL) {
this.buildResultsURL = buildResultsURL;
}
}