Skip to content

Commit 3e90323

Browse files
committed
Added sandboxed Clojure evaluation functionality. Added a launcher and an example policy file. Added a TODO list. All sorts of shit.
1 parent 4d9f30e commit 3e90323

11 files changed

+81
-32
lines changed

TODO

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Clojure eval - sandboxed like a bitch. - DONE
2+
Spin off threads per user invoked command.
3+
TIMEOUTS!! - DONE
4+
Google codesearch
5+
> as prepend for eval.

example.policy

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
grant {
2+
permission java.security.AllPermission;
3+
};

project.clj

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
(defproject sexpbot "1.0.0-SNAPSHOT"
22
:description "FIXME: write"
3-
:dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
4-
[org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]
3+
:dependencies [[org.clojure/clojure "1.1.0"]
4+
[org.clojure/clojure-contrib "1.1.0"]
55
[pircbot/pircbot "1.4.2"]
66
[commons-lang/commons-lang "2.5"]
7-
[org.danlarkin/clojure-json "1.1-SNAPSHOT"]]
7+
[org.danlarkin/clojure-json "1.1-SNAPSHOT"]
8+
[clj-time "0.1.0-SNAPSHOT"]
9+
[clj-sandbox "0.1.0-SNAPSHOT"]]
810
:dev-dependencies [[swank-clojure "1.1.0"]
9-
[leiningen/lein-swank "1.1.0"]])
11+
[leiningen/lein-swank "1.1.0"]]
12+
:main sexpbot.core)

sexpbot

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java -cp src/:lib/* clojure.main src/sexpbot/core.clj

src/sexpbot/core.clj

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
(ns sexpbot.core
2-
(:use (sexpbot.plugins utils eball google lmgtfy translate)
3-
(sexpbot respond)
2+
(:use (sexpbot.plugins utils eball google lmgtfy translate eval)
3+
sexpbot.respond
44
[clojure.contrib.str-utils :only [re-split]])
55
(:import (org.jibble.pircbot PircBot)))
66

7-
(def botconfig
8-
(ref {:prepend \$
9-
:server "irc.freenode.net"
10-
:channel "#acidrayne"}))
7+
(def prepend \$)
8+
(def server "irc.freenode.net")
9+
(def channels ["#()" "#clojure-casual"])
1110

1211
(defn wall-hack-method [class-name name- params obj & args]
1312
(-> class-name (.getDeclaredMethod (name name-) (into-array Class params))
@@ -22,7 +21,7 @@
2221
(let [bot (proxy [PircBot] []
2322
(onMessage
2423
[chan send login host mess]
25-
(if (= (first mess) (@botconfig :prepend))
24+
(if (= (first mess) prepend)
2625
(respond (merge (split-args (apply str (rest mess)))
2726
{:bot this
2827
:sender send
@@ -32,8 +31,7 @@
3231
(wall-hack-method PircBot :setName [String] bot "sexpbot")
3332
(doto bot
3433
(.setVerbose true)
35-
(.connect "irc.freenode.net")
36-
(.joinChannel "#()"))
37-
(dosync (alter botconfig merge {:bot bot}))))
34+
(.connect server))
35+
(doseq [chan channels] (.joinChannel bot chan))))
3836

39-
(make-bot)
37+
(make-bot)

src/sexpbot/plugins/eball.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"Outlook not so good."
2626
"Very doubtful."])
2727

28-
(defmethod respond :8ball [{:keys [args bot sender channel]}]
28+
(defmethod respond :8ball [{:keys [bot sender channel]}]
2929
(let [answer (rand-int 20)]
3030
(.sendMessage bot channel (str sender ": " (nth responses answer)))))
3131

src/sexpbot/plugins/eval.clj

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(ns sexpbot.plugins.eval
2+
(:use net.licenser.sandbox
3+
(sexpbot respond commands)))
4+
5+
(def eval-cmds
6+
{"eval" :eval})
7+
8+
(enable-security-manager)
9+
(def sc (create-sandbox-compiler 'namespace
10+
*default-sandbox-tester*
11+
10000))
12+
13+
(def cap 100)
14+
15+
(defn trim [s]
16+
(apply str (take cap s)))
17+
18+
(defn execute-text [txt]
19+
(try
20+
(trim (str ((sc txt) {})))
21+
(catch SecurityException _ "DENIED!")
22+
(catch Exception e (.getMessage (.getCause e)))))
23+
24+
(defmethod respond :eval [{:keys [bot channel command args]}]
25+
(.sendMessage bot channel (->> args (interpose " ") (apply str) execute-text)))
26+
27+
(defmodule eval-cmds :eval)

src/sexpbot/plugins/translate.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns sexpbot.plugins.translate
2-
(:use (sexpbot respond commands)
2+
(:use (sexpbot respond commands utilities)
33
[clojure.contrib.duck-streams :only [slurp*]])
44
(:require [org.danlarkin.json :as json])
55
(:import (org.apache.commons.lang StringEscapeUtils)))
@@ -16,7 +16,7 @@
1616

1717
(defmethod respond :translate [{:keys [bot channel args]}]
1818
(let [[lang-from lang-to & text] args
19-
translation (translate lang-from lang-to (apply str (interpose " " text)))]
19+
translation (translate lang-from lang-to (stringify text))]
2020
(if (:responseData translation)
2121
(.sendMessage bot channel (-> translation
2222
:responseData

src/sexpbot/plugins/utils.clj

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns sexpbot.plugins.utils
2-
(:use (sexpbot commands respond)))
2+
(:use (sexpbot utilities commands respond)
3+
(clj-time core format)))
34

45
(def known-prefixes
56
[\& \+ \@ \% \! \~])
@@ -9,33 +10,40 @@
910
(apply str (rest x))
1011
x)) users))
1112

13+
(defn pangram? [s]
14+
(let [letters (into #{} "abcdefghijklmnopqrstuvwxyz")
15+
text (->> s .toLowerCase (filter letters) (into #{}))]
16+
(= text letters)))
17+
1218
(def util-cmds
13-
{"time" :time
14-
"rape" :rape
15-
"coin" :coin
16-
"help" :help
17-
"what" :what})
19+
{"time" :time
20+
"rape" :rape
21+
"coin" :coin
22+
"help" :help
23+
"what" :what
24+
"pangram?" :pangram})
1825

1926
(defmethod respond :time [{:keys [bot sender channel]}]
20-
(let [time (.toString (java.util.Date.))]
27+
(let [time (unparse (formatters :date-time-no-ms) (now))]
2128
(.sendMessage bot channel (str sender ": The time is now " time))))
2229

2330
(defmethod respond :rape [{:keys [args bot channel]}]
2431
(let [user-to-rape (if (= (first args) "*")
25-
(apply str (interpose
26-
" "
27-
(drop-modes (map #(.toString %) (.getUsers bot channel)))))
32+
(->> (map #(.toString %) (.getUsers bot channel)) drop-modes stringify)
2833
(first args))]
2934
(.sendAction bot channel (str "raepz " user-to-rape "."))))
3035

3136
(defmethod respond :coin [{:keys [bot sender channel]}]
3237
(.sendMessage bot channel (str sender ": " (if (= 0 (rand-int 2)) "Heads." "Tails."))))
3338

3439
(defmethod respond :help [{:keys [bot sender channel]}]
35-
(.sendMessage bot channel (str sender ": Help yourself, perverted fuck.")))
40+
(.sendMessage bot channel (str sender ": I can't help you, I'm afraid. You can only help yourself.")))
3641

3742
(defmethod respond :what [{:keys [bot channel]}]
3843
(.sendMessage bot channel "It's AWWWW RIGHT!"))
3944

45+
(defmethod respond :pangram [{:keys [bot channel args]}]
46+
(.sendMessage bot channel (-> args stringify pangram? str)))
47+
4048

4149
(defmodule util-cmds :utils)

src/sexpbot/respond.clj

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns sexpbot.respond
2-
(:use (sexpbot commands)))
2+
(:use sexpbot.commands))
33

44
(defn find-command [cmds command]
55
(if (cmds command)
@@ -13,7 +13,7 @@
1313
(defmulti respond cmd-respond)
1414

1515
(defmethod respond :quit [{:keys [bot channel]}]
16-
(.sendMessage bot channel "Okay, I'm fucking leaving. Fuck.")
16+
(.sendMessage bot channel "I bid thee adieu! Into the abyss I go!")
1717
(System/exit 0))
1818

1919
(defmethod respond :load [{:keys [bot channel args]}]
@@ -34,7 +34,7 @@
3434
(.sendMessage bot channel (str (keys (into {} (filter (comp map? second) @commands))))))
3535

3636
(defmethod respond :default [{:keys [bot channel]}]
37-
(.sendMessage bot channel "Command not found. Stop fucking with me."))
37+
(.sendMessage bot channel "Command not found. You can thank Rayne for this one."))
3838

3939
(defn defmodule [cmd-map m-name]
4040
(dosync (alter commands merge {m-name cmd-map})

src/sexpbot/utilities.clj

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(ns sexpbot.utilities)
2+
3+
(defn stringify [coll]
4+
(apply str (interpose " " coll)))

0 commit comments

Comments
 (0)