Commits

David Lazar  committed 542ed14

Initial commit

  • Participants

Comments (0)

Files changed (5)

File bin/frinkbot.sh

+#!/bin/bash
+
+for dir in build/artifacts build/lib; do
+    for jarfile in $dir/*.jar; do
+        CLASSPATH=$CLASSPATH:$jarfile
+    done
+done
+
+exec java -cp $CLASSPATH FrinkBot $*
+<project xmlns:ivy="antlib:org.apache.ivy.ant" name="frinkbot" default="jar">
+  <description>
+    Build FrinkBot
+  </description>
+
+  <property name="src.dir" location="src"/>
+  <property name="build.dir" location="build"/>
+  <property name="classes.dir" location="${build.dir}/classes"/>
+  <property name="artifacts.dir" location="${build.dir}/artifacts"/>
+  <property name="reports.dir" location="${build.dir}/reports"/>
+  <property name="ivy.lib.dir" location="${build.dir}/lib"/>
+
+  <path id="lib.path">
+    <fileset dir="${ivy.lib.dir}"/>
+  </path>
+
+  <target name="resolve" description="--> retrieve dependencies with ivy">
+    <ivy:retrieve/>
+    <get src="http://futureboy.us/frinkjar/frink.jar" dest="${ivy.lib.dir}"/>
+  </target>
+
+  <target name="report" depends="resolve" description="--> generate a report of dependencies">
+    <ivy:report todir="${reports.dir}"/>
+  </target>
+
+  <target name="build" depends="resolve" description="--> build FrinkBot">
+    <mkdir dir="${classes.dir}"/>
+    <javac fork="true" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="lib.path">
+    </javac>
+  </target>
+
+  <target name="jar" depends="build" description="--> create frinkbot.jar">
+    <mkdir dir="${artifacts.dir}"/>
+    <jar jarfile="${artifacts.dir}/frinkbot.jar" basedir="${classes.dir}"/>
+  </target>
+
+  <target name="clean" description="--> delete the build directory">
+    <delete dir="${build.dir}"/>
+  </target>
+
+  <target name="clean-cache" description="--> clean the ivy cache">
+    <ivy:cleancache/>
+  </target>
+
+</project>
+#
+# Config file for FrinkBot
+#
+
+Verbose = true
+
+Nick = FrinkBot
+UserName = FrinkBot
+RealName = FrinkBot
+Version = FrinkBot
+
+Server = irc.example.org
+Port = 6667
+SSL = false
+VerifySSL = false
+
+Channels = #channel1,#channel2
+<ivy-module version="2.2">
+    <info organisation="org.jibble" module="frinkbot"/>
+    <dependencies>
+        <dependency org="org.jibble" name="pircbot" rev="latest.integration" changing="true"/>
+    </dependencies>
+</ivy-module>

File src/FrinkBot.java

+/*
+Author: David Lazar
+*/
+import org.jibble.pircbot.*;
+import frink.parser.*;
+
+public class FrinkBot extends ConfigurablePircBot {
+    private Frink interp;
+
+    public void onMessage(String channel, String sender,
+                       String login, String hostname, String message) {
+        if (message.startsWith("~ ")) {
+            try {
+                String result = interp.parseString(message.substring(2));
+                String lines[] = result.split("\n");
+                // TODO long lines are truncated
+                for (String line : lines)
+                    sendMessage(channel, line);
+            } catch (frink.errors.FrinkEvaluationException fee) {
+                sendMessage(channel, "Operation denied.");
+            }
+        }
+    }
+
+    public void onDisconnect() {
+        int reconnectDelay = 30; // seconds
+        while (!isConnected()) {
+            try {
+                this.log("*** Attempting to reconnect to server.");
+                reconnect();
+                // rejoin channels, if specified
+                if (this.getConfiguration().containsKey("Channels")) {
+                    joinChannel(this.getConfiguration().getString("Channels"));
+                }
+            }
+            catch (Exception e) {
+                this.log("*** Failed to reconnect to server. Sleeping " + reconnectDelay + " seconds.");
+                try {
+                    Thread.sleep(reconnectDelay * 1000);
+                } catch (Exception ignored) {
+                }
+            }
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        if (args.length < 1) {
+            System.out.println("Usage: frinkbot <config>");
+            System.exit(1);
+        }
+
+        FrinkBot bot = new FrinkBot();
+        // Initialize Frink interpretor
+        bot.interp = new Frink();
+        bot.interp.parseString("2 in -> feet");
+        bot.interp.setRestrictiveSecurity(true);
+
+        bot.initBot(args[0]);
+    }
+}