Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created May 22, 2009 19:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/116323 to your computer and use it in GitHub Desktop.
Save cgrand/116323 to your computer and use it in GitHub Desktop.
An IRC logger which serves its archives over HTTP
(ns irclog
(:use net.cgrand.moustache)
(:use net.cgrand.enlive-html)
(:require [ring.httpcore :as hc])
(:import (org.jibble.pircbot PircBot)
(org.joda.time DateTime)
(org.joda.time.format ISODateTimeFormat)))
(defn yyyy-MM-dd [d] (-> (ISODateTimeFormat/date) (.print d)))
(def logs (agent {:current [] :current-day (yyyy-MM-dd (DateTime.)) :archives {}}))
(defn last-date [logs]
(-> logs :current peek :date))
(defn log-message [{:keys [current current-day archives] :as logs} date sender message]
(let [day (yyyy-MM-dd date)]
(if (= day current-day)
(assoc logs :current
(conj current {:date date :sender sender :message message}))
(assoc logs :current []
:current-day day
:archives (assoc archives current-day current)))))
(def bot
(proxy [PircBot] []
(onMessage [channel sender login hostname message]
(send logs log-message (DateTime.) sender message))))
(doto bot
(.connect "irc.freenode.net")
(.changeNick "cgrand-eye")
(.joinChannel "#clojure"))
(declare log-app)
(def server (doto (Thread. #(hc/run {:port 8080} #'log-app)) .start))
(defn hhmm [d] (-> (ISODateTimeFormat/hourMinute) (.print d)))
(defn w3c [d] (-> (ISODateTimeFormat/dateTime) (.print d)))
(deftemplate log-page "irclog/template.html" [day items]
[#{:h1 :title}] (content "#clojure logs for " day)
[:li] (clone-for [{:keys [date sender message]} items]
[:abbr] (do->
(content (hhmm date))
(set-attr :title (w3c date)))
[:.nick] (content sender)
[:.msg] (content message)))
(defn log-page-server [day logs]
(app :get
(app {:status 200
:headers {"Content-Type" "text/html"}
:body (log-page day logs)})))
(def log-app
(app
[""]
(let [{:keys [current-day current]} @logs]
(log-page-server current-day current))
[[day #(and (-> @logs :archives (get %)) %)]] ; checks that there's an archive for this day
(log-page-server day (-> @logs :archives (get day)))))
<html>
<head>
<title>Logs for date</title>
<style type="text/css">
body {
width: 66%;
margin: 0 17%;
}
ul {
margin: 0;
padding: 0;
}
li {display: block;
margin:1ex 0;
padding-left:10em;
}
span.nick {
float:left;
font-weight:bold;
margin-left:-6.8em;
overflow:hidden;
text-align:right;
width:6.3em;
}
abbr {
color: #555;
float:left;
margin-left:-10em;
}
</style>
</head>
<body>
<h1>Logs for date</h1>
<ul>
<li>
<abbr></abbr><span class="nick"></span><span class="msg"></span>
</li>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment