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 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 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
85 (%log-file-path output-root channel-name year month day "raw"))
87 (defmethod log-file-path (output-root channel-name year month day (format (eql :sexp)))
88 (%log-file-path output-root channel-name year month day "sexp"))
90 (defmethod log-file-path (output-root channel-name year month day (format (eql :text)))
91 (%log-file-path output-root channel-name year month day "txt"))
94 (defun log-file-path-utime (output-root channel-name format utime)
96 (second minute hour day month year day-of-week daylight-p zone)
97 (decode-universal-time utime)
98 (declare (ignore second minute hour day-of-week daylight-p zone))
99 (log-file-path output-root channel-name year month day format)))
101 (defun get-stream (channel istream)
102 (elt (streams channel) istream))
104 (defun (setf get-stream) (value channel istream)
105 (setf (elt (streams channel) istream) value))
107 (defun get-format (logger istream)
108 (elt (formats logger) istream))
110 (defun get-output-name (channel istream)
111 (elt (current-output-names channel) istream))
113 (defun (setf get-output-name) (value channel istream)
114 (setf (elt (current-output-names channel) istream) value))
116 (defun ensure-output-stream-for-unichannel (utime logger channel istream)
117 (let ((name (make-output-name-utime (name channel) utime)))
118 (unless (string= name (get-output-name channel istream))
119 (when (get-stream channel istream)
120 (write-file-footer (get-format logger istream)
122 (get-stream channel istream))
123 (close (get-stream channel istream)))
124 (setf (get-output-name channel istream) name)
125 (let ((path (log-file-path-utime (output-root channel) (name channel)
126 (get-format logger istream) utime)))
127 (unless (probe-file path)
128 (ensure-directories-exist path)
129 (setf (get-stream channel istream)
130 (open path :direction :output :if-exists :error
131 :if-does-not-exist :create))
132 (write-file-header (get-format logger istream)
134 (get-stream channel istream))
135 (close (get-stream channel istream)))
136 (setf (get-stream channel istream)
137 (open path :direction :output :if-exists :append
138 :if-does-not-exist :error))))))
140 (defun ensure-output-stream (utime logger channel istream)
141 "Ensures that *output-stream* is correct."
143 ((streamp (user-output logger))
144 (unless (get-stream channel istream)
145 (setf (get-stream channel istream) (user-output logger))))
146 ((pathnamep (user-output logger))
149 (ensure-output-stream-for-unichannel utime logger channel istream))
151 (setf (get-stream channel istream)
152 (open (user-output logger) :direction :output :if-exists :append)))))))
154 (defun format-utime (utime)
156 (second minute hour day-of-month month year day-of-week daylight-p zone)
157 (decode-universal-time utime)
158 (declare (ignore day-of-month month year day-of-week daylight-p zone))
159 (format nil "~2,'0D:~2,'0D:~2,'0D" hour minute second)))
161 (defun format-utime-short(utime)
163 (second minute hour day-of-month month year day-of-week daylight-p zone)
164 (decode-universal-time utime)
165 (declare (ignore second day-of-month month year day-of-week daylight-p zone))
166 (format nil "~2,'0D:~2,'0D" hour minute)))
168 (defun user-address (msg)
169 (let ((split (split *user-address-scanner* (raw-message-string msg)
170 :with-registers-p t)))
175 (defun need-user-address? (type)
176 (not (or (eq :action type) (eq :privmsg type))))
178 (defgeneric %output-event (format stream utime type channel source text msg
181 (defmethod %output-event ((format t) stream utime type channel source text
183 (%output-event :raw stream utime type channel source text msg unichannel))
185 (defmethod %output-event ((format (eql :raw)) stream utime type channel source
187 (declare (ignore utime type channel source text text unichannel ))
188 (format stream "~S~%" (string-right-trim '(#\return)
189 (raw-message-string msg))))
191 (defmethod %output-event ((format (eql :sexp)) stream utime type channel source text
194 (format stream "(~S ~S ~S ~S ~S)~%" utime type source text
195 (when (need-user-address? type) (user-address msg)))
196 (format stream "(~S ~S ~S ~S ~S ~S)~%" utime type source channel
197 text (when (need-user-address? type) (user-address msg)))))
199 (defmethod %output-event ((format (eql :text)) stream utime type channel source text
201 (format stream "~A " (format-utime utime))
202 (when (and (null unichannel) channel)
203 (format stream "[~A] " channel))
205 (let ((user-address (when (need-user-address? type) (user-address msg))))
208 (format stream "<~A> ~A" source text))
210 (format stream "*~A* ~A" source text))
212 (format stream "~A [~A] has joined ~A" source user-address channel))
214 (format stream "-!- ~A [~A] has left ~A" source user-address channel))
216 (format stream "-!- ~A is now known as ~A" source text))
218 (format stream "-!- ~A [~A] has been kicked from ~A" source user-address channel))
220 (format stream "-!- ~A [~A] has quit [~A]" source user-address text))
222 (format stream "-!- ~A has set mode ~A" source text))
224 (format stream "-!- ~A changed the topic of ~A to: ~A" source channel text))
226 (format stream "-~A:~A- ~A" source channel text))
228 (warn "Unhandled msg type ~A." type))))
229 (write-char #\Newline stream))
231 (defun output-event-for-a-stream (msg type channel text logger istream)
232 (ensure-output-stream (received-time msg) logger channel istream)
233 (%output-event (get-format logger istream) (get-stream channel istream)
234 (received-time msg) type (name channel) (source msg) text msg
236 (force-output (get-stream channel istream)))
238 (defun output-event (msg type channel-name &optional text)
239 (dolist (logger *loggers*)
242 (dolist (channel (channels logger))
243 (dotimes (i (length (formats logger)))
244 (output-event-for-a-stream msg type channel text logger i))))
246 (let* ((channel (find channel-name (the list (channels logger))
247 :test #'string-equal :key #'name)))
249 (dotimes (i (length (formats logger)))
250 (output-event-for-a-stream msg type channel text logger i))))))))
252 (defun privmsg-hook (msg)
253 (output-event msg :privmsg (first (arguments msg)) (trailing-argument msg)))
255 (defun action-hook (msg)
256 (output-event msg :action (first (arguments msg))
257 (subseq (trailing-argument msg) 8
258 (- (length (trailing-argument msg)) 1))))
260 (defun nick-hook (msg)
261 (output-event msg :nick nil (trailing-argument msg)))
263 (defun part-hook (msg)
264 (output-event msg :part (first (arguments msg))))
266 (defun quit-hook (msg)
267 (output-event msg :quit (trailing-argument msg)))
269 (defun join-hook (msg)
270 (output-event msg :join (trailing-argument msg)))
272 (defun kick-hook (msg)
273 (output-event msg :kick (first (arguments msg))))
275 (defun notice-hook (msg)
276 (output-event msg :notice (first (arguments msg)) (trailing-argument msg)))
278 (defun topic-hook (msg)
279 (output-event msg :topic (first (arguments msg)) (trailing-argument msg)))
281 (defun mode-hook (msg)
282 (output-event msg :mode (first (arguments msg))))
284 (defun make-channels (names formats output)
285 (loop for i from 0 to (1- (length names))
287 (make-instance 'channel
289 :streams (make-array (length formats) :initial-element nil)
290 :output-root (when (and (pathnamep output)
291 (null (pathname-name output)))
293 :current-output-names (make-array (length formats)
294 :initial-element nil))))
296 (defun is-unichannel-output (user-output)
297 "Returns T if output is setup for a single channel directory structure."
298 (and (pathnamep user-output) (null (pathname-name user-output))))
300 (defun create-logger (nick server &key channels output
303 "OUTPUT may be a pathname or a stream"
308 (setq channels (list channels)))
310 (setq formats (list formats)))
312 (setq output (parse-namestring output)))
313 (let* ((conn (connect :nickname nick :server server
314 :logging-stream logging-stream))
315 (logger (make-instance
320 :channels (make-channels channels formats output)
323 :unichannel (is-unichannel-output output))))
325 (mapc #'(lambda (channel) (join conn channel)) channels)
326 (add-hook conn 'irc::irc-privmsg-message 'privmsg-hook)
327 (add-hook conn 'irc::ctcp-action-message 'action-hook)
328 (add-hook conn 'irc::irc-nick-message 'nick-hook)
329 (add-hook conn 'irc::irc-part-message 'part-hook)
330 (add-hook conn 'irc::irc-quit-message 'quit-hook)
331 (add-hook conn 'irc::irc-join-message 'join-hook)
332 (add-hook conn 'irc::irc-kick-message 'kick-hook)
333 (add-hook conn 'irc::irc-mode-message 'mode-hook)
334 (add-hook conn 'irc::irc-topic-message 'topic-hook)
335 (add-hook conn 'irc::irc-notice-message 'notice-hook)
338 (defun start-logger (logger async)
340 (read-message-loop-background (connection logger))
341 (read-message-loop (connection logger))))
343 (defun quit-logger (nick)
344 "Quit the active connection with nick and remove from active list."
345 (let ((logger (find-logger-with-nick nick)))
348 (warn "No active connection found with nick ~A." nick)
351 (irc:quit (connection logger))
353 (dolist (channel (channels logger))
354 (dotimes (i (length (streams channel)))
355 (when (streamp (get-stream channel i))
356 (close (get-stream channel i))
357 (setf (get-stream channel i) nil))))
359 (delete nick *loggers* :test #'string-equal :key #'nickname))
362 (defun add-logger (nick server &key channels output
366 (when (find-logger-with-nick nick)
367 (warn "Closing previously active connection.")
369 (let ((logger (create-logger nick server :channels channels :output output
370 :logging-stream logging-stream
372 (push logger *loggers*)
373 (start-logger logger async)
376 (defun add-hook-logger (logger class hook)
377 (add-hook (connection logger) class hook))
379 (defun remove-hook-logger (logger class hook)
380 (remove-hook (connection logger) class hook))