Skip to content

Commit

Permalink
Added com.buglabs.bug.ircbot.impl.irclib, based on irclib.jar.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Turley committed Apr 12, 2011
1 parent ab6e142 commit b9d2ba4
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bot-osgi/com.buglabs.bug.ircbot.impl.irclib/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="con" path="com.buglabs.dragonfly.jdt.BugClasspathContainerInitializer"/>
<classpathentry kind="lib" path="libs/irclib.jar"/>
<classpathentry kind="output" path=""/>
</classpath>
29 changes: 29 additions & 0 deletions bot-osgi/com.buglabs.bug.ircbot.impl.irclib/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.buglabs.bug.ircbot.impl.irclib</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.buglabs.dragonfly.BugApplicationNature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Mon Apr 11 12:28:54 EDT 2011
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.problem.assertIdentifier=warning
org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning
org.eclipse.jdt.core.compiler.source=1.6
13 changes: 13 additions & 0 deletions bot-osgi/com.buglabs.bug.ircbot.impl.irclib/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: com.buglabs.bug.ircbot.impl.irclib
Bundle-Activator: com.buglabs.bug.ircbot.impl.irclib.Activator
Bundle-SymbolicName: com.buglabs.bug.ircbot.impl.irclib
Bundle-Version: 1.0.0
Bundle-Vendor: aturley
Bug-Bundle-Type: Application
BUG-API-Version: 2.0.2
Import-Package: com.buglabs.bug.ircbot.pub,
org.osgi.framework,
org.osgi.util.tracker
Bundle-Classpath: .,/libs/irclib.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.buglabs.bug.ircbot.impl.irclib;

import java.io.IOException;

import com.buglabs.bug.ircbot.pub.IChannelMessageConsumer;

import org.schwering.irc.lib.IRCConnection;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

public class Activator implements BundleActivator {
private IRCConnection manager;
private static final String botName = "bot-bundle";
private static final String serverName = "bugcamp.net";
private static final String channelName = "#buglabs";
// private static final String serverName = "irc.freenode.net";
// private static final String channelName = "#botwar";
private ServiceTracker channelMessageConsumerTracker;

public void start(BundleContext context) throws Exception {
System.out.println("starting irclib based irc bot");
manager = new IRCConnection(
serverName,
6667,
6669,
null,
botName,
botName + "1",
botName + "@buglabs"
);
channelMessageConsumerTracker = new ServiceTracker(context, IChannelMessageConsumer.class.getName(), null);
channelMessageConsumerTracker.open();
manager.addIRCEventListener(new IRCEventListener(channelMessageConsumerTracker, botName, manager));
manager.setDaemon(true);
manager.setColors(false);
manager.setPong(true);

try {
System.out.println("calling connect");
manager.connect(); // Try to connect!!! Don't forget this!!!
System.out.println("called connect");
} catch (IOException ioexc) {
ioexc.printStackTrace();
}
}

public void stop(BundleContext context) throws Exception {
if (manager != null) {
manager.close();
manager = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.buglabs.bug.ircbot.impl.irclib;

import com.buglabs.bug.ircbot.pub.IChannelMessageEvent;
import org.schwering.irc.lib.IRCUser;

public class ChannelMessageEvent implements IChannelMessageEvent {
private String channel;
private String hostName;
private String message;
private String nick;
private String userName;
private String botName;

public ChannelMessageEvent(String channel, IRCUser user, String message, String botName) {
this.channel = channel;
hostName = user.getServername();
this.message = message;
nick = user.getNick();
userName = user.getUsername();
this.botName = botName;
}
@Override
public String getChannel() {
return channel;
}

@Override
public String getHostName() {
return hostName;
}

@Override
public String getMessage() {
return message;
}

@Override
public String getNick() {
return nick;
}

@Override
public String getUserName() {
return userName;
}

@Override
public String getBotName() {
return botName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.buglabs.bug.ircbot.impl.irclib;

import com.buglabs.bug.ircbot.pub.IChannelMessageEvent;
import com.buglabs.bug.ircbot.pub.IChannelMessageConsumer;

import org.schwering.irc.lib.IRCEventAdapter;
import org.schwering.irc.lib.IRCUser;
import org.schwering.irc.lib.IRCConnection;

import org.osgi.util.tracker.ServiceTracker;

public class IRCEventListener extends IRCEventAdapter {
private ServiceTracker channelMessageConsumerTracker;
private String botName;
IRCConnection connection;
public IRCEventListener(ServiceTracker tracker, String botName, IRCConnection connection) {
super();
channelMessageConsumerTracker = tracker;
this.botName = botName;
this.connection = connection;
}

@Override
public void onRegistered() {
System.out.println("registered!");
System.out.println("calling doJoin");
connection.doJoin("#botwar");
System.out.println("called doJoin");
}
@Override
public void onPrivmsg(java.lang.String target, IRCUser user, java.lang.String msg) {
System.out.println("received channel message");
Object channelMessageConsumers[] = channelMessageConsumerTracker.getServices();

if (channelMessageConsumers != null) {
IChannelMessageEvent cme = new ChannelMessageEvent(target, user, msg, botName);
for (Object cmc : channelMessageConsumers) {
String message = ((IChannelMessageConsumer) cmc).onChannelMessage(cme);
if (message != null) {
connection.doPrivmsg(target, message);
}
}
} else {
System.out.println("Sigh ... no consumers");
}
}
}
Binary file not shown.

0 comments on commit b9d2ba4

Please sign in to comment.