1 ;;;; -*- Mode: Lisp -*-
2 ;;;; $Id: logger.lisp,v 1.11 2003/12/16 21:19:56 krosenberg Exp $
4 ;;;; Purpose: A IRC logging bot
5 ;;;; Author: Kevin Rosenberg
7 (in-package irc-logger)
10 ((name :initarg :name :reader name
11 :documentation "Name of channel.")
12 (streams :initarg :streams :reader streams
13 :documentation "List of output streams.")
14 (output-root :initarg :output-root :reader output-root)
15 (current-output-names :initarg :current-output-names :accessor current-output-names)))
19 ((connection :initarg :connection :reader connection
20 :documentation "IRC connection object.")
21 (nick :initarg :nick :reader nickname
22 :documentation "Nickname of the bot.")
23 (server :initarg :server :reader server
24 :documentation "Connected IRC server.")
25 (channels :initarg :channels :reader channels
26 :documentation "List of channels.")
27 (user-output :initarg :user-output :reader user-output
29 "Output parameter from user, maybe stream or pathname.")
30 (unichannel :initarg :unichannel :reader unichannel :type boolean
31 :documentation "T if user-output is directory for individual channel output.")
32 (formats :initarg :formats :reader formats
34 "A list of output formats.")))
36 (defvar *loggers* nil "List of active loggers.")
38 (defparameter *user-address-scanner*
42 (:greedy-repetition 1 nil :non-whitespace-char-class)))
43 :case-insensitive-mode t))
45 (defun find-logger-with-nick (nick)
46 (find nick (the list *loggers*) :test #'string-equal :key #'nickname))
48 (defun make-output-name (name year month day)
49 (format nil "~A-~4,'0D.~2,'0D.~2,'0D"
50 (string-left-trim '(#\#) name) year month day))
52 (defun make-output-name-utime (name utime)
54 (second minute hour day-of-month month year day-of-week daylight-p zone)
55 (decode-universal-time utime)
56 (declare (ignore second minute hour day-of-week daylight-p zone))
57 (make-output-name name year month day-of-month)))
59 (defgeneric write-file-header (format channel-name stream))
61 (defmethod write-file-header ((format t) channel-name stream)
62 (declare (ignore format channel-name stream))
65 (defgeneric write-file-footer (format channel-name stream))
67 (defmethod write-file-footer ((format t) channel-name stream)
68 (declare (ignore format channel-name stream))
71 (defun %log-file-path (output-root channel-name year month day type)
74 :directory (append (pathname-directory output-root)
76 (string-left-trim '(#\#) channel-name)
77 (format nil "~4,'0D-~2,'0D" year month)))
78 :name (make-output-name channel-name year month day)
81 (defgeneric log-file-path (output-root channel-name year month day format))
83 (defmethod log-file-path (output-root channel-name year month day (format (eql :raw)))
84 (%log-file-path output-root channel-name year month day "raw"))
86 (defmethod log-file-path (output-root channel-name year month day (format (eql :sexp)))
87 (%log-file-path output-root channel-name year month day "sexp"))
89 (defmethod log-file-path (output-root channel-name year month day (format (eql :text)))
90 (%log-file-path output-root channel-name year month day "txt"))
93 (defun log-file-path-utime (output-root channel-name format utime)
95 (second minute hour day month year day-of-week daylight-p zone)
96 (decode-universal-time utime)
97 (declare (ignore second minute hour day-of-week daylight-p zone))
98 (log-file-path output-root channel-name year month day format)))
100 (defun get-stream (channel istream)
101 (elt (streams channel) istream))
103 (defun (setf get-stream) (value channel istream)
104 (setf (elt (streams channel) istream) value))
106 (defun get-format (logger istream)
107 (elt (formats logger) istream))
109 (defun get-output-name (channel istream)
110 (elt (current-output-names channel) istream))
112 (defun (setf get-output-name) (value channel istream)
113 (setf (elt (current-output-names channel) istream) value))
115 (defun ensure-output-stream-for-unichannel (utime logger channel istream)
116 (let ((name (make-output-name-utime (name channel) utime)))
117 (unless (string= name (get-output-name channel istream))
118 (when (get-stream channel istream)
119 (write-file-footer (get-format logger istream)
121 (get-stream channel istream))
122 (close (get-stream channel istream)))
123 (setf (get-output-name channel istream) name)
124 (let ((path (log-file-path-utime (output-root channel) (name channel)
125 (get-format logger istream) utime)))
126 (unless (probe-file path)
127 (ensure-directories-exist path)
128 (setf (get-stream channel istream)
129 (open path :direction :output :if-exists :error
130 :if-does-not-exist :create))
131 (write-file-header (get-format logger istream)
133 (get-stream channel istream))
134 (close (get-stream channel istream)))
135 (setf (get-stream channel istream)
136 (open path :direction :output :if-exists :append
137 :if-does-not-exist :error))))))
139 (defun ensure-output-stream (utime logger channel istream)
140 "Ensures that *output-stream* is correct."
142 ((streamp (user-output logger))
143 (unless (get-stream channel istream)
144 (setf (get-stream channel istream) (user-output logger))))
145 ((pathnamep (user-output logger))
148 (ensure-output-stream-for-unichannel utime logger channel istream))
150 (setf (get-stream channel istream)
151 (open (user-output logger) :direction :output :if-exists :append)))))))
153 (defun format-utime (utime)
155 (second minute hour day-of-month month year day-of-week daylight-p zone)
156 (decode-universal-time utime)
157 (declare (ignore day-of-month month year day-of-week daylight-p zone))
158 (format nil "~2,'0D:~2,'0D:~2,'0D" hour minute second)))
160 (defun format-utime-short(utime)
162 (second minute hour day-of-month month year day-of-week daylight-p zone)
163 (decode-universal-time utime)
164 (declare (ignore second day-of-month month year day-of-week daylight-p zone))
165 (format nil "~2,'0D:~2,'0D" hour minute)))
167 (defun user-address (msg)
168 (let ((split (split *user-address-scanner* (raw-message-string msg)
169 :with-registers-p t)))
174 (defun need-user-address? (type)
175 (not (or (eq :action type) (eq :privmsg type))))
177 (defgeneric %output-event (format stream utime type channel source text msg unichannel))
179 (defmethod %output-event ((format t) stream utime type channel source text
181 (%output-event :raw stream utime type channel source text msg unichannel))
183 (defmethod %output-event ((format (eql :raw)) stream utime type channel source text
185 (declare (ignore unichannel))
186 (format stream "~S~%" (string-right-trim '(#\return) (raw-message-string msg))))
188 (defmethod %output-event ((format (eql :sexp)) stream utime type channel source text
191 (format stream "(~S ~S ~S ~S ~S)~%" utime type source text
192 (when (need-user-address? type) (user-address msg)))
193 (format stream "(~S ~S ~S ~S ~S ~S)~%" utime type source channel
194 text (when (need-user-address? type) (user-address msg)))))
196 (defmethod %output-event ((format (eql :text)) stream utime type channel source text
198 (format stream "~A " (format-utime utime))
199 (when (and (null unichannel) channel)
200 (format stream "[~A] " channel))
202 (let ((user-address (when (need-user-address? type) (user-address msg))))
205 (format stream "<~A> ~A" source text))
207 (format stream "*~A* ~A" source text))
209 (format stream "~A [~A] has joined ~A" source user-address channel))
211 (format stream "-!- ~A [~A] has left ~A" source user-address channel))
213 (format stream "-!- ~A is now known as ~A" source text))
215 (format stream "-!- ~A [~A] has been kicked from ~A" source user-address channel))
217 (format stream "-!- ~A [~A] has quit [~A]" source user-address text))
219 (format stream "-!- ~A has set mode ~A" source text))
221 (format stream "-!- ~A changed the topic of ~A to: ~A" source channel text))
223 (format stream "-~A:~A- ~A" source channel text))
225 (warn "Unhandled msg type ~A." type))))
226 (write-char #\Newline stream))
228 (defun output-event-for-a-stream (msg type channel text logger istream)
229 (ensure-output-stream (received-time msg) logger channel istream)
230 (%output-event (get-format logger istream) (get-stream channel istream)
231 (received-time msg) type (name channel) (source msg) text msg
233 (force-output (get-stream channel istream)))
235 (defun output-event (msg type channel-name &optional text)
236 (dolist (logger *loggers*)
239 (dolist (channel (channels logger))
240 (dotimes (i (length (formats logger)))
241 (output-event-for-a-stream msg type channel text logger i))))
243 (let* ((channel (find channel-name (the list (channels logger))
244 :test #'string-equal :key #'name)))
246 (dotimes (i (length (formats logger)))
247 (output-event-for-a-stream msg type channel text logger i))))))))
249 (defun privmsg-hook (msg)
250 (output-event msg :privmsg (first (arguments msg)) (trailing-argument msg)))
252 (defun action-hook (msg)
253 (output-event msg :action (first (arguments msg))
254 (subseq (trailing-argument msg) 8
255 (- (length (trailing-argument msg)) 1))))
257 (defun nick-hook (msg)
258 (output-event msg :nick nil (trailing-argument msg)))
260 (defun part-hook (msg)
261 (output-event msg :part (first (arguments msg))))
263 (defun quit-hook (msg)
264 (output-event msg :quit (trailing-argument msg)))
266 (defun join-hook (msg)
267 (output-event msg :join (trailing-argument msg)))
269 (defun kick-hook (msg)
270 (output-event msg :kick (first (arguments msg))))
272 (defun notice-hook (msg)
273 (output-event msg :notice (first (arguments msg)) (trailing-argument msg)))
275 (defun topic-hook (msg)
276 (output-event msg :topic (first (arguments msg)) (trailing-argument msg)))
278 (defun mode-hook (msg)
279 (output-event msg :mode (first (arguments msg))))
281 (defun make-channels (names formats output)
282 (loop for i from 0 to (1- (length names))
284 (make-instance 'channel
286 :streams (make-array (length formats) :initial-element nil)
287 :output-root (when (and (pathnamep output)
288 (null (pathname-name output)))
290 :current-output-names (make-array (length formats)
291 :initial-element nil))))
293 (defun is-unichannel-output (user-output)
294 "Returns T if output is setup for a single channel directory structure."
295 (and (pathnamep user-output) (null (pathname-name user-output))))
297 (defun create-logger (nick server &key channels output
301 "OUTPUT may be a pathname or a stream"
306 (setq channels (list channels)))
308 (setq formats (list formats)))
310 (setq output (parse-namestring output)))
311 (let* ((conn (connect :nickname nick :server server
312 :logging-stream logging-stream))
313 (logger (make-instance
318 :channels (make-channels channels formats output)
321 :unichannel (is-unichannel-output output))))
323 (mapc #'(lambda (channel) (join conn channel)) channels)
324 (add-hook conn 'irc::irc-privmsg-message 'privmsg-hook)
325 (add-hook conn 'irc::ctcp-action-message 'action-hook)
326 (add-hook conn 'irc::irc-nick-message 'nick-hook)
327 (add-hook conn 'irc::irc-part-message 'part-hook)
328 (add-hook conn 'irc::irc-quit-message 'quit-hook)
329 (add-hook conn 'irc::irc-join-message 'join-hook)
330 (add-hook conn 'irc::irc-kick-message 'kick-hook)
331 (add-hook conn 'irc::irc-mode-message 'mode-hook)
332 (add-hook conn 'irc::irc-topic-message 'topic-hook)
333 (add-hook conn 'irc::irc-notice-message 'notice-hook)
336 #+sbcl (add-asynchronous-message-handler conn)
337 #-sbcl (read-message-loop conn))
339 (read-message-loop conn)))
342 (defun quit-logger (nick)
343 "Quit the active connection with nick and remove from active list."
344 (let ((logger (find-logger-with-nick nick)))
347 (warn "No active connection found with nick ~A." nick)
350 (irc:quit (connection logger))
352 (dolist (channel (channels logger))
353 (dotimes (i (length (streams channel)))
354 (when (streamp (get-stream channel i))
355 (close (get-stream channel i))
356 (setf (get-stream channel i) nil))))
358 (delete nick *loggers* :test #'string-equal :key #'nickname))
361 (defun add-logger (nick server &key channels output
365 (when (find-logger-with-nick nick)
366 (warn "Closing previously active connection.")
369 (create-logger nick server :channels channels :output output
370 :logging-stream logging-stream
371 :async async :formats formats)))
372 (push logger *loggers*)
375 (defun add-hook-logger (logger msg hook)
376 (add-hook (connection logger) msg hook))
378 (defun remove-hook-logger (logger msg)
379 (remove-hook (connection logger) msg))