Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move null sink to clojurebot.conduit

  • Loading branch information...
commit 04e59805c43035ab2745c9f6eb750a14bb1937e6 1 parent 51a8334
@hiredman authored
View
1  TODO
@@ -10,4 +10,3 @@
* logging
* clojars search
* pointfree transforms
-* use clj-sandbox
View
48 src/clojurebot/core.clj
@@ -1,7 +1,7 @@
(ns clojurebot.core
(:use [conduit.irc :only [a-irc irc-run]]
[conduit.core]
- [clojurebot.conduit :only [a-indirect a-if a-cond]]
+ [clojurebot.conduit :only [a-indirect a-if a-cond null]]
[hiredman.clojurebot.factoids :only [factoid-lookup
factoid-command?
factoid-command-run]]
@@ -14,6 +14,8 @@
get-contrib-ticket-n]]
[hiredman.clojurebot.sb :only [eval-request?
eval-message]]
+ [hiredman.clojurebot.code-lookup :only [code-lookup?
+ do-code-lookup]]
[clojure.contrib.logging :only [info]])
(:gen-class))
@@ -37,21 +39,18 @@
message
prefixes))))))
-(def-proc null [x]
- (info (str "Bit Bucket:" x))
- [])
-
(defn question? [{:keys [message]}]
(and message
(= 1 (count (.split message " ")))
(.endsWith message "?")))
(def-arr limit-length [x]
- (let [y (print-str x)
- out (apply str (take 200 y))]
- (if (> (count y) 200)
- (str out "...")
- out)))
+ (if (string? x)
+ (let [out (apply str (take 200 x))]
+ (if (> (count x) 200)
+ (str out "...")
+ out))
+ x))
(def clojurebot-eval
(a-comp (a-arr eval-message)
@@ -62,8 +61,17 @@
pass-through)))
(def-arr reconnect [{:keys [server bot config]}]
- (info "reconnecting")
- (.connect bot server))
+ (letfn [(reconnect-fn []
+ (try
+ (when-not (.isConnected bot)
+ (info "reconnecting")
+ (.connect bot server))
+ (catch Exception e
+ (info "Failed to reconnect" e)
+ (info "retrying in 60 seconds")
+ (Thread/sleep (* 60 1000))
+ reconnect-fn)))]
+ (trampoline reconnect-fn)))
(def-arr rejoin [{:keys [message bot config]}]
(doseq [c (:channels config)]
@@ -88,6 +96,7 @@
"*suffusion of yellow*"
out))))
+;; pipelines
(def addressed-pipeline
(a-comp remove-nick-prefix
(a-cond ticket-query?
@@ -99,6 +108,9 @@
ticket-search?
(a-arr search-tickets)
+ code-lookup?
+ (debug-proc "code-lookup" (a-arr do-code-lookup))
+
(comp factoid-command? :message)
(a-arr factoid-command-run)
@@ -107,9 +119,13 @@
(def pipeline
(a-except
- (a-comp (a-cond doc-lookup?
- (a-comp (a-arr (fn [_] (info "doc request")))
- null)
+ (a-comp (a-all pass-through ;;put user watching stuff here
+ pass-through)
+ (a-arr last)
+
+ (a-cond #_doc-lookup?
+ #_(a-comp (a-arr (fn [_] (info "doc request")))
+ null)
math?
da-math
@@ -134,6 +150,8 @@
limit-length)
(a-arr (comp #(doto % .printStackTrace) first))))
+;;/pipelines
+
(defn clojurebot [config]
(a-irc
(:server config)
View
146 src/hiredman/clojurebot/code_lookup.clj
@@ -1,9 +1,9 @@
;; DEPENDS clojure-json
(ns hiredman.clojurebot.code-lookup
- (:use (hiredman.clojurebot core))
- (:use (hiredman utilities))
- (:require [org.danlarkin.json :as json])
- (:import (java.io File InputStreamReader BufferedReader)))
+ (:use (hiredman.clojurebot core))
+ (:use (hiredman utilities))
+ (:require [org.danlarkin.json :as json])
+ (:import (java.io File InputStreamReader BufferedReader)))
(def google-code-url "http://code.google.com/p/clojure/source/browse/trunk/src/clj/")
(def google-java-code-url "http://code.google.com/p/clojure/source/browse/trunk/src/jvm/")
@@ -11,39 +11,41 @@
(def contrib-url "http://github.com/clojure/clojure-contrib/raw/gh-pages/api-index.json")
-(def contrib (try (json/decode-from-str (get-url contrib-url)) (catch Exception e nil)))
+(def contrib
+ (delay (try (json/decode-from-str (get-url contrib-url)) (catch Exception e nil))))
(defn get-rev-number []
- ((comp #(Integer/parseInt %)
- second
- #(.split % " ")
- first
- (partial filter #(re-find #"^Revision: " %))
- line-seq
- #(BufferedReader. %)
- #(InputStreamReader. %)
- #(.getInputStream %)
- #(.. Runtime getRuntime (exec % %2 %3)));end comp
- (into-array ["svn" "info"])
- nil
- (File. "/home/hiredman/clojure/")))
+ ((comp #(Integer/parseInt %)
+ second
+ #(.split % " ")
+ first
+ (partial filter #(re-find #"^Revision: " %))
+ line-seq
+ #(BufferedReader. %)
+ #(InputStreamReader. %)
+ #(.getInputStream %)
+ #(.. Runtime getRuntime (exec % %2 %3)));end comp
+ (into-array ["svn" "info"])
+ nil
+ (File. "/home/hiredman/clojure/")))
;;(def clojurebot-rev (get-rev-number))
(def clojurebot-rev 1)
(defn get-sha-number []
- ((comp; #(Integer/parseInt %)
- second
- #(.split % " ")
- first
- line-seq
- #(BufferedReader. %)
- #(InputStreamReader. %)
- #(.getInputStream %)
- #(.. Runtime getRuntime (exec % %2 %3)));end comp
- (into-array ["git" "log" "-1"])
- nil
- (File. "/home/hiredman/clojure/")))
+ "f128af9d36dfcb268b6e9ea63676cf254c0f1c40"
+ #_((comp; #(Integer/parseInt %)
+ second
+ #(.split % " ")
+ first
+ line-seq
+ #(BufferedReader. %)
+ #(InputStreamReader. %)
+ #(.getInputStream %)
+ #(.. Runtime getRuntime (exec % %2 %3)));end comp
+ (into-array ["git" "log" "-1"])
+ nil
+ (File. "/home/hiredman/clojure/")))
(def clojurebot-rev (get-sha-number))
@@ -54,69 +56,67 @@
"transforms a googlecode source url into a github url"
[url project rev]
(-> url
- (.replaceAll "http://code.google.com/p/clojure/source/browse/trunk"
- (str "http://github.com/clojure/" project "/blob/" rev))
- (.replaceAll "\\?r=(.*)#(\\d+)" "#LID$2")))
+ (.replaceAll "http://code.google.com/p/clojure/source/browse/trunk"
+ (str "http://github.com/clojure/" project "/blob/" rev))
+ (.replaceAll "\\?r=(.*)#(\\d+)" "#LID$2")))
(defn get-file-and-ln [string]
- (let [a (meta (try (resolve (symbol string))
- (catch Exception _ nil)))]
- [(:line a) (:file a)]))
+ (let [a (meta (try (resolve (symbol string))
+ (catch Exception _ nil)))]
+ [(:line a) (:file a)]))
(defn make-url [[line file]]
- (let [google (str google-code-url file "?r=" clojurebot-rev "#" line)
- google (google-code->github-url google "clojure" clojurebot-rev)]
- (tinyurl google)))
+ (let [google (str google-code-url file "?r=" clojurebot-rev "#" line)
+ google (google-code->github-url google "clojure" clojurebot-rev)]
+ (tinyurl google)))
(def make-url-cached (memoize make-url))
(def java-code-url (memoize (fn [url]
- (get-url
- (str "http://tinyurl.com/api-create.php?url="
- (java.net.URLEncoder/encode url))))))
+ (get-url
+ (str "http://tinyurl.com/api-create.php?url="
+ (java.net.URLEncoder/encode url))))))
(def java-code-url (fn [url] (tinyurl url)))
(defmulti lookup
- (fn [bot msg thing]
+ (fn [msg thing]
(cond
- (re-find #"c\.l\.[a-zA-z]+" thing) :clojure-java
- (re-find #"[a-zA-z]+\.[a-zA-z]+\.[a-zA-z]+" thing) :java
- :else :clojure)))
-
-(defmethod lookup :clojure-java [bot msg thing]
- (lookup bot msg (.replaceAll thing "c\\.l" "clojure.lang")))
-
-(defmethod lookup :java [bot msg thing]
- (send-out :notice bot (who msg)
- (str thing ": "
- (java-code-url
- (google-code->github-url
- (str google-java-code-url
- (.replaceAll thing "\\." "/") ".java?r=" clojurebot-rev)
- "clojure"
- clojurebot-rev)))))
+ (re-find #"c\.l\.[a-zA-z]+" thing) :clojure-java
+ (re-find #"[a-zA-z]+\.[a-zA-z]+\.[a-zA-z]+" thing) :java
+ :else :clojure)))
+
+(defmethod lookup :clojure-java [msg thing]
+ (lookup msg (.replaceAll thing "c\\.l" "clojure.lang")))
+
+(defmethod lookup :java [msg thing]
+ (str thing ": "
+ (java-code-url
+ (google-code->github-url
+ (str google-java-code-url
+ (.replaceAll thing "\\." "/") ".java?r=" clojurebot-rev)
+ "clojure"
+ clojurebot-rev))))
(defn contrib-lookup [thing]
(map (comp #(get-url (str "http://tinyurl.com/api-create.php?url="
- (java.net.URLEncoder/encode %)))
+ (java.net.URLEncoder/encode %)))
;;#(.replaceAll % "#(\\d)" "#L$1") ;;horrible patching for github
;;#(.replace % bar foo)
:source-url)
- (filter #(= (:name %) thing) (:vars contrib))))
+ (filter #(= (:name %) thing) (:vars @contrib))))
-(defmethod lookup :clojure [bot msg thing]
+(defmethod lookup :clojure [msg thing]
(let [[file line] (get-file-and-ln thing)]
(if (or (nil? file) (nil? line))
(if-let [results (seq (contrib-lookup thing))]
- (send-out :notice bot (who msg)
- (reduce #(str % " " %2) (str thing ":") results))
- (send-out :msg bot (who msg) (befuddled)))
- (send-out :notice bot (who msg) (str thing ": " (make-url-cached [file line]))))))
-
-(defresponder ::code-lookup 0
- (dfn (and (addressed? bot msg)
- (re-find #"^(def|source) " (extract-message bot msg))))
- (let [message (extract-message bot msg)
- thing (.replaceAll message "^(def|source) " "")]
- (lookup bot msg thing)))
+ (reduce #(str % " " %2) (str thing ":") results)
+ (befuddled))
+ (str thing ": " (make-url-cached [file line])))))
+
+(defn code-lookup? [{:keys [message]}]
+ (re-find #"^(def|source) " message))
+
+(defn do-code-lookup [{:keys [message channel sender bot]}]
+ (let [thing (.replaceAll message "^(def|source) " "")]
+ [:notice (lookup message thing)]))
Please sign in to comment.
Something went wrong with that request. Please try again.