Skip to content

Commit 2ff17b7

Browse files
committed
Squashed version of @noisesmith's irclj version update
1 parent c77ea2e commit 2ff17b7

38 files changed

+283
-181
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ logs
88
logs
99
*log
1010
.cake
11-
target
11+
target
12+
.lein-failures
13+
.lein-repl-history

.lazybot/config.clj

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
(let [plugins #{"dictionary" "lmgtfy" "google" "eball" "utils" "leet"
2-
"clojure" "login" "log" "brainfuck" "whatis" "shorturl" "haskell"
3-
"mail" "timer" "rss" "title" "operator" "seen" "sed" "help"
4-
"load" "embedded" "karma" "fortune" "yesno" "autoreply" "weather" "unix-jokes"}]
1+
(let [plugins #{"autoreply" "brainfuck" "clojure" "clojuredocs" "debug" "dictionary" "eball"
2+
"embedded" "fortune" "google" "haskell" "hello-world" "help" "javadoc"
3+
"jruby" "karma" "leet" "lmgtfy" "load" "log" "logger" "login"
4+
"macro" "mail" "max" "mute" "operator" "ping" "rss"
5+
"sed" "seen" "shorturl" "timer" "title" "unix-jokes" "utils" "weather"
6+
"whatis" "yesno"
7+
;; "github" how does this even work?
8+
;; "rotten-tomatoes", "lastfm" needs key to verify
9+
;; api broken -- "knowledge" "metacritic" (getting 403 error)
10+
;; "notifo" this service went out of business
11+
;; "shorturl" is.gd is good, bit.ly seems broken
12+
}]
513
{:servers ["irc.freenode.net"] ; A list of servers.
614
:prepends #{"@"} ; The character you want for a prepend. Currently set to @
715
:weather {:token ""} ; Wunderground token.

README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Several of this bot's plugins require that you have MongoDB installed and runnin
1010

1111
As for what OSes this bot actually runs on: I have no clue. I've seen it run on several Ubuntu distros and OS X, but nobody (that I know of) has yet to venture into the world of lazybot-on-windows. If you do, please let me know how it goes.
1212

13-
The easiest way to get the bot going is to pull the repository, install [leiningen](http://github.com/technomancy/leiningen), and run `lein deps`. Additionally, you will need MongoDB installed (in some future version, this will be an optional dependency). Edit .lazybot/info.clj to put the servers and other information the bot needs to have in there, get MongoDB running, and then run ./lazybot. After you run the bot the first time, you'll have to edit configuration in ~/.lazybot/info.clj (the .lazybot directory is copied to your home directory the first time).
13+
The easiest way to get the bot going is to pull the repository, install [leiningen](http://github.com/technomancy/leiningen), and run `lein deps`. Additionally, you will need MongoDB installed (in some future version, this will be an optional dependency). Edit .lazybot/config.clj to put the servers and other information the bot needs to have in there, get MongoDB running, and then run ./lazybot. After you run the bot the first time, you'll have to edit configuration in ~/.lazybot/config.clj (the .lazybot directory is copied to your home directory the first time).
1414

1515
You can also run `lein uberjar` which will create a standalone jar file for you to use to run the bot. You can just do java -jar jarfile to run it.
1616

project.clj

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
(defproject lazybot "0.7.0-alpha1"
22
:description "FIXME: write"
3-
:dependencies [[org.clojure/clojure "1.4.0"]
3+
:dependencies [[org.clojure/clojure "1.7.0-alpha1"]
44
[org.clojure/tools.reader "0.7.3"]
55
[commons-lang/commons-lang "2.5"]
66
[commons-io/commons-io "1.4"]
77
[clj-time "0.6.0"]
88
[clojail "1.0.6"]
99
[clj-http "0.2.6"]
10-
[irclj "0.4.1"]
10+
[irclj "0.5.0-alpha4"]
1111
[congomongo "0.4.0"]
1212
[clj-config "0.2.0"]
1313
[compojure "1.1.5"]
@@ -30,7 +30,8 @@
3030
[me.raynes/laser "0.1.16"]
3131
[hiccup "1.0.2"]
3232
[me.raynes/least "0.1.3"]
33-
[me.raynes/moments "0.1.1"]]
33+
[me.raynes/moments "0.1.1"]
34+
[org.clojure-grimoire/lib-grimoire "0.1.1"]]
3435
:uberjar-name "lazybot.jar"
3536
:main lazybot.run
3637
:copy-deps true

src/lazybot/core.clj

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
[clojure.set :refer [intersection]]
88
[compojure.core :refer [routes]]
99
[ring.middleware.params :refer [wrap-params]]
10-
[ring.adapter.jetty :refer [run-jetty]])
10+
[ring.adapter.jetty :refer [run-jetty]]
11+
[irclj.core :as ircb])
1112
(:import [java.io File FileReader]))
1213

1314
;; This is pretty much the only global mutable state in the entire bot, and it
@@ -34,10 +35,18 @@
3435
(doseq [hook (registry/pull-hooks bot hook-key)]
3536
(hook ircm))))
3637

38+
(defn join-channels
39+
[irc-map]
40+
(let [network (:network irc-map)
41+
channels (-> irc-map :bot deref :config (get network) :channels)]
42+
;; delay here because join expects it should deref
43+
(apply ircb/join (delay irc-map) channels)))
44+
3745
;; Note that even the actual handling of commands is done via a hook.
3846
(def initial-hooks
3947
"The hooks that every bot, even without plugins, needs to have."
40-
{:on-message [{:fn (fn [irc-map] (registry/try-handle irc-map))}]
48+
{:001 [{:fn join-channels}]
49+
:privmsg [{:fn (fn [irc-map] (registry/try-handle irc-map))}]
4150
:on-quit []
4251
:on-join []})
4352

@@ -67,8 +76,8 @@
6776
(defn load-plugins
6877
"Load all plugins specified in the bot's configuration."
6978
[irc refzors]
70-
(let [info (:config @refzors)]
71-
(doseq [plug (:plugins (info (:server @irc)))]
79+
(let [plugins (-> @refzors :config (get (:network @irc)) :plugins)]
80+
(doseq [plug plugins]
7281
(load-plugin irc refzors plug))))
7382

7483
(defn reload-configs
@@ -111,3 +120,5 @@
111120
(alter bot assoc-in [:modules :internal :hooks] initial-hooks)
112121
(reload-config bot))
113122
(load-plugins com bot)))
123+
124+
(comment (apply lz/reload-all (vals @lz/bots)))

src/lazybot/irc.clj

+30-20
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,64 @@
11
(ns lazybot.irc
2-
(:require [lazybot.core :as core]
2+
(:require [lazybot.core :as lazybot]
33
[lazybot.info :as info]
44
[useful.fn :refer [decorate]]
55
[useful.map :refer [keyed]]
66
[irclj.core :as ircb]))
77

8+
(defn make-hook
9+
[actions]
10+
(fn [& args]
11+
(when-not (empty? @actions)
12+
(doseq [action @actions]
13+
(apply action args)))))
14+
815
(defn base-maps
916
"Create the base callback and bot maps."
1017
[config]
11-
(let [refzors (ref {:modules {:internal {:hooks core/initial-hooks}}
18+
(let [refzors (ref {:modules {:internal {:hooks lazybot/initial-hooks}}
1219
:config config
1320
:pending-ops 0})]
1421
[(into {}
1522
(map
1623
(decorate
17-
#(fn [irc-map]
18-
(core/call-all (-> irc-map
19-
(assoc :bot refzors :com (:irc irc-map))
20-
(dissoc :irc))
21-
%)))
22-
[:on-any :on-message :on-quit :on-join]))
24+
#(fn dispatch-hooks [irc-map event]
25+
(dosync (alter irc-map assoc :server (:network @irc-map)))
26+
(lazybot/call-all (-> @irc-map
27+
(assoc :bot refzors :com irc-map)
28+
(dissoc :irc)
29+
(assoc :event event)
30+
(assoc :bot-nick (:nick @irc-map))
31+
(assoc :nick (:nick event))
32+
(assoc :message (:text event))
33+
(assoc :channel ((:params event) 0)))
34+
%)))
35+
[:001 :privmsg :quit :join]))
2336
refzors]))
2437

25-
(defn make-bot-run
26-
"Create an irclj param map to pass to connect."
27-
[name password server fnmap]
28-
(ircb/create-irc (keyed [name password server fnmap])))
29-
3038
(defn make-bot
3139
"Creates a new bot and connects it."
3240
[server]
3341
(let [bot-config (info/read-config)
42+
port 6667
3443
[name pass channels] ((juxt :bot-name :bot-password :channels)
35-
(bot-config server))
44+
(bot-config server))
3645
[fnmap refzors] (base-maps bot-config)
37-
irc (ircb/connect (make-bot-run name pass server fnmap)
38-
:channels channels, :identify-after-secs 3)]
46+
irc (ircb/connect server port name
47+
:callbacks fnmap
48+
:identify-after-secs 3)]
3949
[irc refzors]))
4050

4151
(defn init-bot
4252
"Initialize a new bot."
4353
[server]
4454
(let [[irc refzors] (make-bot server)]
45-
(swap! core/bots assoc server {:com irc :bot refzors})
46-
(dosync (core/reload-config refzors))
47-
(core/load-plugins irc refzors)))
55+
(swap! lazybot/bots assoc server {:com irc :bot refzors})
56+
(dosync (lazybot/reload-config refzors))
57+
(lazybot/load-plugins irc refzors)))
4858

4959
(defn start-bots
5060
"Starts bots for servers."
5161
[servers]
5262
(doseq [serv servers]
5363
(init-bot serv))
54-
(core/route (core/extract-routes (vals @core/bots))))
64+
(lazybot/route (lazybot/extract-routes (vals @lazybot/bots))))

src/lazybot/plugins/autoreply.clj

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
(registry/defplugin
66
(:hook
7-
:on-message
8-
(fn [{:keys [bot com message channel] :as com-m}]
9-
(when-let [reply (first
10-
(for [[find replace]
11-
(get-in @bot [:config (:server @com) :autoreply :autoreplies channel])
12-
:when (re-find find message)]
13-
(s/replace message find replace)))]
14-
(registry/send-message com-m reply)))))
7+
:privmsg
8+
(fn [{:keys [bot com message channel network] :as com-m}]
9+
(let [autos (get-in @bot [:config network :autoreply :autoreplies channel])]
10+
(when-let [reply (first
11+
(for [[find replace] autos
12+
:when (re-find find message)]
13+
(s/replace message find replace)))]
14+
(registry/send-message com-m reply))))))

src/lazybot/plugins/clojure.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177

178178
(registry/defplugin
179179
(:hook
180-
:on-message
180+
:privmsg
181181
(fn [{:keys [com bot nick channel message] :as com-m}]
182182
(let [config (:config @bot)
183183
clj-config (:clojure config)

src/lazybot/plugins/debug.clj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns lazybot.plugins.debug
22
(:require [clojure.string :as string]
3+
[clojure.pprint :as pprint]
34
[lazybot.registry :as registry]
45
[lazybot.plugins.login :refer [when-privs]]))
56

@@ -39,4 +40,4 @@
3940
(when (get-in @bot debug-keypath)
4041
(when-privs com-m :admin
4142
(let [code (string/join " " args)]
42-
(println (str code " evaluates to:\n" (eval (read-string code))))))))))
43+
(println (str code " evaluates to:\n" (pprint/pprint (eval (read-string code)))))))))))

src/lazybot/plugins/embedded.clj

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
(registry/defplugin
55
(:hook
6-
:on-message
6+
:privmsg
77
(fn [{:keys [message bot] :as irc-map}]
8-
(doseq [x (reverse (re-seq #"\$#(.*?)#\$" message))]
9-
(->> x
10-
second
11-
(-> @bot :config :prepends first str)
12-
(assoc irc-map :message)
13-
registry/try-handle)))))
8+
(doseq [command (reverse (re-seq #"\$#(.*?)#\$" message))]
9+
(as-> command x
10+
(second x)
11+
(str (-> @bot :config :prepends first) x)
12+
(assoc irc-map :message x)
13+
(registry/try-handle x))))))

src/lazybot/plugins/github.clj

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[clojure.string :as s]
66
[tentacles.issues :refer [specific-issue]]
77
[useful.map :refer [update]]
8-
[clojure.pprint :refer [pprint]]
98
[cheshire.core :refer [parse-string]])
109
(:import java.net.InetAddress))
1110

@@ -105,13 +104,13 @@ specified in config.clj."
105104
(defplugin
106105
(:init
107106
(fn [com bot]
108-
(swap! bots assoc (:server @com) {:com com :bot bot})))
107+
(swap! bots assoc (:network @com) {:com com :bot bot})))
109108
(:routes (POST "/commits" req (handler req)))
110109

111110
(:hook
112-
:on-message
111+
:privmsg
113112
(fn [{:keys [message nick bot com] :as com-m}]
114-
(when-not ((get-in @bot [:config (:server @com) :user-blacklist]) nick)
113+
(when-not ((get-in @bot [:config (:network @com) :user-blacklist]) nick)
115114
(doseq [issue (extract-issues message)]
116115
(when-let [message (issue-message (parse-issue issue))]
117-
(send-message com-m message)))))))
116+
(send-message com-m message)))))))

src/lazybot/plugins/grimoire.clj

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(ns lazybot.plugins.grimoire
2+
(:require [grimoire.util :as util]
3+
[lazybot.registry :refer [defplugin send-message]]))
4+
5+
(def nss #{"clojure.core"
6+
"clojure.core.protocols"
7+
"clojure.core.reducers"
8+
"clojure.data"
9+
"clojure.edn"
10+
"clojure.inspector"
11+
"clojure.instant"
12+
"clojure.java.browse"
13+
"clojure.java.io"
14+
"clojure.java.javadoc"
15+
"clojure.java.shell"
16+
"clojure.main"
17+
"clojure.pprint"
18+
"clojure.reflect"
19+
"clojure.repl"
20+
"clojure.set"
21+
"clojure.stacktrace"
22+
"clojure.string"
23+
"clojure.template"
24+
"clojure.test"
25+
"clojure.test.junit"
26+
"clojure.test.tap"
27+
"clojure.uuid"
28+
"clojure.walk"
29+
"clojure.xml"
30+
"clojure.zip"})
31+
32+
(defplugin
33+
(:cmd
34+
"Print the Grimoire URL for a symbol"
35+
#{"grim"}
36+
(fn [{:keys [args] :as com-m}]
37+
(let [sym (first args)
38+
[_ ns s] (re-matches #"(.*?)/(.*)" sym)]
39+
(when (nss ns)
40+
(send-message
41+
com-m
42+
(format "http://grimoire.arrdem.com/1.6.0/%s/%s"
43+
ns (util/munge s))))))))

src/lazybot/plugins/haskell.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@
5555
(:cmd
5656
"Gets the type of an expression via GHC's :t."
5757
#{"htype" "ht"}
58-
(heval-cmd ghc-type)))
58+
(heval-cmd ghc-type)))

src/lazybot/plugins/help.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
(not topic) (registry/send-message com-m "You're going to need to tell me what you want help with.")
6060
content (registry/send-message com-m (prefix nick (:content content)))
6161
:else (registry/send-message com-m (str "Topic: \"" topic "\" doesn't exist!"))))
62-
(registry/send-message com-m (prefix nick help-msg))))))
63-
62+
(registry/send-message com-m (prefix nick help-msg))))))
63+
6464
(:cmd
6565
"Lists the available help topics in the DB."
6666
#{"list"}

0 commit comments

Comments
 (0)