Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cleanup

  • Loading branch information...
commit 87d9fb75b1a0ca9afbf58c2608bb3fbf7fd6c077 1 parent 78f395b
@hiredman authored
View
14 project.clj
@@ -2,19 +2,21 @@
:repositories {"sonatype-snapshots"
"https://oss.sonatype.org/content/repositories/snapshots/"}
:description "An IRC bot written in Clojure"
+ :exclusions [commons-logging]
:dependencies [[org.clojure/clojure "1.2.1"]
- [org.clojure/tools.logging "0.1.2"]
+ [org.clojure/tools.logging "0.2.6"]
[conduit-irc "2.0.1-SNAPSHOT"]
- #_[org.clojars.thnetos/opennlp "0.0.3"]
[org.ccil.cowan.tagsoup/tagsoup "1.2"]
- [log4j "1.2.16"]
[org.danlarkin/clojure-json "1.1"]
- [clj-http "0.1.1"]
+ [clj-http "0.1.1"
+ :exclude [commons-logging]]
[swank-clojure "1.3.2"]
[com.thelastcitadel/apropos "0.0.1"]
- #_[conduit-xmpp "1.0.0-SNAPSHOT"]
[ring "1.1.8"]
- [compojure "1.1.5"]]
+ [compojure "1.1.5"]
+ ;; logging
+ [ch.qos.logback/logback-classic "1.0.9"]
+ [ch.qos.logback/logback-core "1.0.9"]]
:main clojurebot.core
:clean-non-project-classes false
:min-lein-version "2.0.0")
View
12 resources/log4j.properties
@@ -1,12 +0,0 @@
-log4j.appender.C = org.apache.log4j.ConsoleAppender
-log4j.appender.C.layout = org.apache.log4j.PatternLayout
-log4j.appender.C.layout.ConversionPattern = %d | %-5p | %c | %m%n
-
-log4j.appender.F = org.apache.log4j.FileAppender
-log4j.appender.F.File = clojurebot.log
-log4j.appender.F.Append = false
-log4j.appender.F.layout = org.apache.log4j.PatternLayout
-log4j.appender.F.layout.ConversionPattern = %d | %-5p | %c | %m%n
-
-log4j.rootLogger=info, F
-log4j.logger.clojurebot=debug
View
2  setup.sh
@@ -56,3 +56,5 @@ $1
exit 0
+
+
View
8 src/clojurebot/core.clj
@@ -32,6 +32,7 @@
[compojure.core :refer [defroutes]]
[compojure.route :refer :all]
[ring.adapter.jetty :refer [run-jetty]])
+ (:require [clojure.tools.logging :as log])
(:gen-class))
;; pipelines
@@ -107,7 +108,7 @@
(try
((resolve name) msg)
(catch Exception e
- (.printStackTrace e))))))
+ (log/error e "error running logging plugin"))))))
pass-through)
(a-arr last) ;we only want the passed through value
@@ -167,7 +168,7 @@
(a-comp (a-arr #(dissoc % :config :bot))
null)
))
- (a-arr (comp #(doto % .printStackTrace) first))))
+ (a-arr (comp #(log/error %) first))))
;;/pipelines
@@ -211,8 +212,7 @@
{:status 200
:headers {"Content-Type" "application/edn; charset=utf-8"}
:body (let [x (hiredman.clojurebot.core/befuddled)]
- (prn x)
- (println x)
+ (log/debug "/befuddled ·" x)
(pr-str x))})
(GET "/ok" []
{:status 200
View
40 src/clojurebot/delicious.clj
@@ -1,40 +0,0 @@
-;; posts lines containing urls to a delicious account
-(ns clojurebot.delicious
- (:require [clj-http.client :as http]))
-
-(def url-reg #"[A-Za-z]+://[^ ^/]+\.[^ ^/]+[^ ]+")
-
-(def pastebins
- #"(fpaste\.org|dpaste\.*|gist\.github\.com|paste.lisp.org/display|.*pastebin\.com|p\.hagelb\.org|pastebin\.org|paste\.pocoo\.org/show|pastie\.org|sprunge\.us)")
-
-;;#"(\w+://.*?)[.>]*(?: |$)"
-
-(defn post
- "posts a url to the delicious account of [user pass]"
- [[user pass] url descr tag]
- (http/get "https://api.del.icio.us/v1/posts/add"
- {:query-params
- {"url" url
- "description" descr
- "tags" tag}
- :basic-auth [user pass]}))
-
-(defn contains-url? [{:keys [message channel config]}]
- (and message
- (re-find url-reg message)
- channel
- (:delicious config)))
-
-(defn delicious [{:keys [message channel sender config]}]
- (try
- (let [url (re-find url-reg message)
- desc message
- tag (str sender " " channel
- (when (re-find #"lisppaste" sender)
- (str " " (first (.split desc " ")))))
- tag (if (re-find pastebins url)
- (str tag " pastbin")
- tag)]
- (post (:delicious config) url desc tag))
- (catch Exception e
- (println e))))
View
79 src/hiredman/clojurebot.clj
@@ -1,79 +0,0 @@
-(ns hiredman.clojurebot
- (:require (hiredman.clojurebot core dice sb seenx google delicious noise
- stock-quote factoids translate
- code-lookup javadoc ticket github xmpp
- simplyscala clojars tao)
- [hiredman.clojurebot.xmpp :as xmpp]
- [hiredman.utilities :as util]
- [hiredman.twitter :as twitter])
- (:import (org.jibble.pircbot PircBot)))
-
-(set! *warn-on-reflection* true)
-
-(import '(sun.misc Signal SignalHandler))
-
-(defn install [sig handler]
- (Signal/handle (Signal. (name sig))
- (proxy [SignalHandler] []
- (handle [sig] (handler sig)))))
-
-(defn props []
- (let [properties (java.util.Properties.)]
- (with-open [properties-file (-> (fn []) class
- (.getResourceAsStream
- "/clojurebot.properties"))]
- (.load properties properties-file)
- (into {} properties))))
-
-;;set up sandbox namespace for evaling code
-(binding [*ns* (create-ns 'sandbox)]
- (clojure.core/refer 'clojure.core)
- (import '(java.util Date))
- (intern *ns* 'Thread (fn [& _] (throw (Exception. "DENIED"))))
- (intern *ns* 'java.lang.Thread (fn [& _] (throw (Exception. "DENIED")))))
-
-(defn connect [bot]
- (.connect (:this bot) (:network bot))
- bot)
-
-(defn join [bot]
- (doseq [channel (cons (:channel bot) (:channels bot))]
- (.joinChannel (:this bot) channel))
- bot)
-
-(defn -main []
- (-> (let [p (props)]
- {:nick "clojurebot"
- :network "irc.freenode.net"
- :channel "#clojurebot"
- :tweet true
- :delicious [(p "delicious.user") (p "delicious.password")]
- :twitter [(p "twitter.user") (p "twitter.password")]
- :sandbox-ns 'sandbox
- :store (agent {})
- :factoid-server-port 4444
- :xmpp-connection (xmpp/connect (p "xmpp.jid") (p "xmpp.password"))
- ;; must include final slash
- :dict-dir (.concat (System/getProperty "user.dir") "/")})
- ((fn [attrs]
- (let [bot (pircbot attrs)]
- (wall-hack-method PircBot :setName [String] (:this bot) (:nick bot))
- bot)))
- connect
- join
- ((fn [bot]
- (doto bot
- load-dicts
- load-store
- watch-store
- start-dump-thread
- xmpp/setup-listener
- (xmpp/connect-to-muc "clojure@thelastcitadel.com")
- ((fn [bot] (factoid-server (:factoid-server-port bot) bot)))
- hiredman.clojurebot.clojars/go
- (hiredman.clojurebot.tao/go "#clojurebot" 5))))
- ((fn [bot]
- (intern (create-ns 'hiredman.clojurebot)
- (with-meta 'bot {:private true})
- bot))))
- (println "Done loading!"))
View
72 src/hiredman/clojurebot/svn.clj
@@ -1,72 +0,0 @@
-; Subversion announcements
-
-(ns hiredman.clojurebot.svn
- (:require [hiredman.clojurebot.core :as core]
- [hiredman.utilities :as util]
- [hiredman.twitter :as twitter])
- (:import (java.util.concurrent TimeUnit)))
-
-(defn summary
- "takes output of clojure.xml/parse on svn's xml log, returns
- a vector of [rev-number commit-message]"
- [tag-map]
- (map (fn [x]
- [(Integer/parseInt (:revision (:attrs x)))
- (first
- (:content
- (first
- (filter #(= (:tag %) :msg)
- (:content x)))))
- (-> x :content ((partial filter #(= (:tag %) :author))) first :content first)])
- (:content tag-map)))
-
-(def latest-revisions
- (comp summary
- clojure.xml/parse
- #(.getInputStream %)
- util/shell
- (partial str "svn -v --xml --limit 5 log ")))
-
-(def revision
- (comp first
- summary
- clojure.xml/parse
- #(.getInputStream %)
- util/shell
- #(str "svn -v --xml -r " %2 " log " %)))
-
-(def revision-cached (memoize revision))
-
-(def latest (atom {}))
-
-(defn start-svn-watcher [bot name url callback]
- (.scheduleAtFixedRate core/task-runner
- (fn []
- (try
- (println name " checking SVN revs")
- (when-let [revs (seq (filter #(> (first %) (get @latest name 0)) (latest-revisions url)))]
- (swap! latest assoc name (first (last (sort-by first revs))))
- (callback bot (reverse revs)))
- (catch Exception e
- (.printStackTrace e))))
- (long 1)
- (long 5)
- TimeUnit/MINUTES))
-
-;;(defn clojure-channel-helper-callback
-;; [bot revs]
-;; (doseq [r revs]
-;; (doseq [c (.getChannels (:this bot))]
-;; (core/send-out :notice bot c (str "r" (first r) " " (second r)))
-;; (when (:tweet bot)
-;; (twitter/send (first (:twitter bot)) (second (:twitter bot)) (str "r" (first r) " " (second r))))))
-;; (core/is! "latest" (.toString (first (last (sort-by first revs))))))
-
-(def default-repo (atom ""))
-
-(defmethod core/responder ::svn-rev-lookup [bot pojo]
- (let [r (Integer/parseInt (re-find #"[0-9]+" (:message pojo)))]
- (core/send-out :notice bot (core/who pojo) (let [x (revision-cached @default-repo r)]
- (str "r" (first x) " " (second x))))))
-
-(core/add-dispatch-hook (core/dfn (re-find #"^r[0-9]+$" (:message msg))) ::svn-rev-lookup)
View
58 src/hiredman/clojurebot/tweet.clj
@@ -1,58 +0,0 @@
-(ns hiredman.clojurebot.tweet
- (:require [hiredman.clojurebot.core :as core]
- [hiredman.clojurebot.seenx :as seenx])
- (:use (hiredman horizon))
- (:import (java.util.concurrent TimeUnit)
- (java.util Date)))
-
-(def urlx "http://search.twitter.com/search.atom?q=")
-
-(def nex (atom {}))
-
-(def next-url
- (comp :href
- :attrs
- first
- (partial filter #(and (= (:rel (:attrs %)) "refresh" ) (= :link (:tag %))))
- :content))
-
-(def xml
- (comp clojure.xml/parse
- #(.getContent %)
- #(java.net.URL. %)))
-
-(defn url [term]
- (if-let [u (@nex term)]
- u
- (str urlx term)))
-
-(defn interesting-tag? [x]
- (some #(= (:tag x) %) [:title :author]))
-
-(def entries
- (comp (partial filter #(= :entry (:tag %)))
- :content))
-
-(defn entry [t]
- (let [[content author] (map (comp first :content) (filter interesting-tag? (:content t)))
- author (first (re-find #"([^ ]+)" (first (:content author))))]
- [content author]))
-
-(defn get-tweets [term]
- (let [xml (xml (url term))]
- (swap! nex #(assoc % term (next-url xml)))
- (seq (map entry (entries xml)))))
-
-(defn get-latest-tweets [bot term channel]
- (let [ts (reverse (get-tweets "clojure"))]
- (when (seq ts)
- (core/send-out :notice bot channel "Tweets:")
- (doseq [t ts]
- (core/send-out :notice bot channel (str (first t) " --" (second t)))))))
-
-(defn watch [bot term channel]
- (.scheduleAtFixedRate core/task-runner
- #(get-latest-tweets bot term channel)
- (long 0)
- (long 60)
- TimeUnit/MINUTES))
View
81 src/hiredman/clojurebot/xmpp.clj
@@ -1,81 +0,0 @@
-;; DEPENDS smack
-(ns hiredman.clojurebot.xmpp
- (:require [hiredman.clojurebot.core :as core])
- (:import (org.jivesoftware.smack XMPPConnection ConnectionConfiguration
- RosterListener PrivacyListManager
- PacketListener MessageListener)
- (org.jivesoftware.smack.packet Message Message$Type IQ IQ$Type Presence Presence$Type)
- (org.jivesoftware.smackx.muc MultiUserChat)
- (org.jivesoftware.smack.filter PacketFilter MessageTypeFilter OrFilter)
- (org.jivesoftware.smackx.packet VCard)
- (org.jivesoftware.smackx.filetransfer FileTransferManager)
- (java.io File)))
-
-
-(defn connect [jid pass]
- (doto (XMPPConnection. (last (.split jid "@")))
- .connect
- (.login (first (.split jid "@")) pass)))
-
-(defn muc [connection room]
- (doto (MultiUserChat. connection room)
- (.join (first (.split (.getUser connection) "@")))))
-
-(declare msg->junks)
-
-(defn packet-processor [bot p]
- (when (not (nil? (.getBody p)))
- (trampoline core/responder
- (assoc bot :xmpp-message p)
- (let [g (msg->junks bot p)]
- (vary-meta g assoc :addressed? (core/addressed? bot g)
- :xmpp-message p)))))
-
-
-(defn msg->junks [bot msg]
- (struct core/junks
- (when (= (.getType msg) (Message$Type/fromString "groupchat"))
- (muc (:con bot) (first (.split (.getFrom msg) "/"))))
- (first (.split (.getFrom msg) "@"))
- nil
- nil
- (.getBody msg)))
-
-(defn packet-listener [bot]
- (proxy [PacketListener] []
- (processPacket [p]
- (try
- (binding [core/random-person #(do % "foo")]
- (packet-processor bot p))
- (catch Exception e
- (prn e))))))
-
-(defn clone-message [msg]
- (doto (Message.)
- (.setSubject (.getSubject msg))
- (.setThread (.getThread msg))
- (.setType (.getType msg))))
-
-(defmethod core/new-send-out :xmpp [bot msg-type r w]
- (when-not (= w "")
- (if (= (.getType (:xmpp-message bot)) (Message$Type/fromString "groupchat"))
- (.sendMessage (:channel r r) w)
- (.sendPacket (:con bot)
- (doto (clone-message (:xmpp-message bot))
- (.setFrom (.getTo (:xmpp-message bot)))
- (.setTo (.getFrom (:xmpp-message bot)))
- (.setBody w))))))
-
-;(def chat (muc con "clojure@conference.thelastcitadel.com"))
-
-(defn connect-to-muc [bot mu]
- (muc (:xmpp-connection bot) mu)
- bot)
-
-(defn setup-listener [bot]
- (.addPacketListener (:xmpp-connection bot)
- (packet-listener #^{:type :xmpp} {:con (:xmpp-connection bot) :nick "clojurebot"})
- (OrFilter.
- (MessageTypeFilter. (Message$Type/fromString "groupchat"))
- (MessageTypeFilter. (Message$Type/fromString "chat"))))
- bot)
View
50 src/hiredman/factoid_server.clj
@@ -1,50 +0,0 @@
-(ns hiredman.factoid-server
- (:use [hiredman.http-server :only (http-server put)]
- [org.danlarkin.json :only (decode-from-str encode-to-str)]
- [hiredman.triples :only (derby query)]
- [hiredman.clojurebot.storage :only (db-name)])
- (:import (java.io BufferedReader StringReader)))
-
-(defn handle [request bot]
- (-> request StringReader. BufferedReader. line-seq
- ((partial map #(.trim %)))
- ((partial drop-while #(not= "" %)))
- rest
- ((partial apply str))
- decode-from-str
- ((fn [x]
- (query (derby (db-name bot))
- (:s x :x)
- (:p x :y)
- (:o x :z))))
- ((partial sort-by :upper_subject))
- ((partial map #(update-in % [:created_at] (fn [x] (.toString x)))))
- ((fn [x]
- (try
- (encode-to-str x)
- (catch Exception e
- (print e)))))
- ((fn [x]
- {:status "200 OK"
- :content-type "application/json"
- :body x}))))
-
-(defn f [r bot]
- (if (zero? (.available r)) (Thread/sleep 100))
- (let [l (.available r)
- buf (make-array Byte/TYPE l)]
- (.read r buf)
- (let [request (String. buf)]
- (prn request)
- (cond
- (.startsWith request "PUT")
- (handle request bot)
- :else
- {:status "404 Not Found" :content-type "text/plain" :body "hello world"}))))
-
-(defn factoid-server-fn [bot]
- (fn [r] (f r bot)))
-
-(defn factoid-server [port bot]
- (http-server (factoid-server-fn bot) port)
- bot)
View
49 src/hiredman/http_server.clj
@@ -1,49 +0,0 @@
-(ns hiredman.http-server
- (:import (java.io InputStreamReader PrintWriter BufferedReader)
- (java.net ServerSocket URL)))
-
-(def running true)
-
-(defn- client-action [fn reader writer]
- (let [result (fn reader)]
- (binding [*out* writer]
- (println "HTTP/1.0" (:status result))
- (println "Server: a Simple Server")
- (println "Content-type:" (:content-type result))
- (println "Content-length:" (-> result :body .getBytes count))
- (println "")
- (println (:body result))))
- (doseq [x [reader writer]] (.close x)))
-
-(defn- handle-client [fn client-socket]
- (future
- (client-action fn
- (-> client-socket .getInputStream)
- (-> client-socket .getOutputStream PrintWriter.))))
-
-(defn- server-action [fn server-socket]
- (when running (send-off *agent* server-action server-socket))
- (try
- (handle-client fn (.accept server-socket))
- (catch Exception e
- (prn e)))
- fn)
-
-(defn http-server
- "fn is a fn that is passed the InputStream from the socket
- should return a map containing the keys :status :content-type :body"
- [fn portn]
- (send-off (agent fn)
- server-action
- (ServerSocket. portn)))
-
-(defn put
- "url is a string url and stuff is stuff to PUT to the url"
- [stuff url]
- (let [con (doto (.openConnection (URL. url))
- (.setDoInput true)
- (.setDoOutput true))]
- (with-open [wtr (-> con .getOutputStream PrintWriter.)]
- (binding [*out* wtr] (print stuff) (flush)))
- (with-open [rdr (-> con .getInputStream InputStreamReader. BufferedReader.)]
- (apply str (line-seq rdr)))))
View
83 src/hiredman/twitter.clj
@@ -1,83 +0,0 @@
-(ns hiredman.twitter
- (:refer-clojure :exclude [send])
- (:require [org.danlarkin.json :as json]
- [clojure.core :as cc]
- [hiredman.clojurebot.core :as core]
- [hiredman.sandbox :as sb])
- (:import (java.net URL URLEncoder)
- (java.io BufferedReader InputStreamReader OutputStreamWriter)
- (sun.misc BASE64Encoder)))
-
-(def #^{:private true} twit (java.util.concurrent.LinkedBlockingQueue.))
-
-(def #^{:private true} twitter-urls {:update (URL. "http://twitter.com/statuses/update.xml")
- :messages (URL. "http://twitter.com/direct_messages.json")
- :mentions (URL. "http://twitter.com/statuses/mentions.json")})
-
-(defstruct account :username :password)
-
-(defn- base64encode [string] (.trim (.encode (BASE64Encoder.) (.getBytes string))))
-
-(def tweet-a false)
-
-(defn send [username password text]
- (when tweet-a
- (let [creds (base64encode (str username ":" password))
- con (doto (.openConnection (:update twitter-urls))
- (.setDoInput true) (.setDoOutput true) (.setUseCaches false)
- (.setRequestProperty "Authorization" (str "Basic " creds))
- (.setRequestProperty "User-Agent" "clojurebot 10/10"))
- status (str "status=" (URLEncoder/encode text "UTF-8"))]
- (with-open [wrt (-> con .getOutputStream OutputStreamWriter.)]
- (.write wrt status))
- (with-open [rdr (-> con .getInputStream InputStreamReader. BufferedReader.)]
- (apply str (line-seq rdr))))))
-
-(defn get-api [call username password]
- (let [creds (base64encode (str username ":" password))
- con (doto (.openConnection (call twitter-urls))
- (.setUseCaches false)
- (.setRequestProperty "Authorization" (str "Basic " creds))
- (.setRequestProperty "User-Agent" "clojurebot 10/10"))]
- (with-open [rdr (-> con .getInputStream InputStreamReader. BufferedReader.)]
- (json/decode-from-str (apply str (line-seq rdr))))))
-
-(defn open-connection [url creds]
- (doto (.openConnection url)
- (.setDoInput true) (.setDoOutput true) (.setUseCaches false)
- (.setRequestProperty "Authorization" (str "Basic " creds))
- (.setRequestProperty "User-Agent" "clojurebot 10/10")))
-
-(defn get-mentions [username password & id]
- (let [creds (base64encode (str username ":" password))
- con (open-connection (if (nil? id)
- (:mentions twitter-urls)
- (URL. (str (:mentions twitter-urls) "?since_id=" (first id)))) creds)]
- (with-open [rdr (-> con .getInputStream InputStreamReader. BufferedReader.)]
- ;(take-while identity (repeatedly #(.readLine rdr)))
- (json/decode-from-str (apply str (line-seq rdr))))))
-
-
-(defn login [username password]
- (vary-meta (struct account username password) assoc :type ::twitter))
-
-(defmethod core/new-send-out ::twitter [acct _ thing message]
- (when (not= "" message)
- (send (:username acct) (:password acct) (str "@" (:sender thing) " " message))))
-
-(defn tweet [x]
- (vary-meta (struct core/junks nil (-> x :user :screen_name) nil nil
- (.replaceAll (:text x) "^@clojurebot " ""))
- assoc :addressed? true))
-
-(def running true)
-
-(defn twitter-loop [_ acct]
- (let [m (get-mentions (:username acct) (:password acct) (:x (meta acct)))]
- (doseq [i m]
- (try (trampoline core/responder acct (tweet i))
- (catch Exception e
- (.printStackTrace e))))
- (when running
- (send-off *agent* twitter-loop (vary-meta acct assoc :x (:id (last (sort-by :id m)))))
- (Thread/sleep 100000))))
Please sign in to comment.
Something went wrong with that request. Please try again.