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 (password :initarg :password :reader password
24 :documentation "Nickname's nickserver password.")
25 (server :initarg :server :reader server
26 :documentation "Connected IRC server.")
27 (channels :initarg :channels :reader channels
28 :documentation "List of channels.")
29 (user-output :initarg :user-output :reader user-output
31 "Output parameter from user, maybe stream or pathname.")
32 (unichannel :initarg :unichannel :reader unichannel :type boolean
33 :documentation "T if user-output is directory for individual channel output.")
34 (formats :initarg :formats :reader formats
36 "A list of output formats.")))
38 (defvar *loggers* nil "List of active loggers.")
40 (defparameter *user-address-scanner*
44 (:greedy-repetition 1 nil :non-whitespace-char-class)))
45 :case-insensitive-mode t))
47 (defun find-logger-with-nick (nick)
48 (find nick (the list *loggers*) :test #'string-equal :key #'nickname))
50 (defun find-logger-with-connection (conn)
51 (find conn (the list *loggers*) :test #'eq :key #'connection))
53 (defun make-output-name (name year month day)
54 (format nil "~A-~4,'0D.~2,'0D.~2,'0D"
55 (string-left-trim '(#\#) name) year month day))
57 (defun make-output-name-utime (name utime)
59 (second minute hour day-of-month month year day-of-week daylight-p zone)
60 (decode-universal-time utime)
61 (declare (ignore second minute hour day-of-week daylight-p zone))
62 (make-output-name name year month day-of-month)))
64 (defgeneric write-file-header (format channel-name stream))
66 (defmethod write-file-header ((format t) channel-name stream)
67 (declare (ignore channel-name stream))
70 (defgeneric write-file-footer (format channel-name stream))
72 (defmethod write-file-footer ((format t) channel-name stream)
73 (declare (ignore channel-name stream))
76 (defun %log-file-path (output-root channel-name year month day type)
79 :directory (append (pathname-directory output-root)
81 (string-left-trim '(#\#) channel-name)
82 (format nil "~4,'0D-~2,'0D" year month)))
83 :name (make-output-name channel-name year month day)
86 (defgeneric log-file-path (output-root channel-name year month day format))
88 (defmethod log-file-path (output-root channel-name year month day
90 (%log-file-path output-root channel-name year month day "raw"))
92 (defmethod log-file-path (output-root channel-name year month day (format (eql :sexp)))
93 (%log-file-path output-root channel-name year month day "sexp"))
95 (defmethod log-file-path (output-root channel-name year month day (format (eql :text)))
96 (%log-file-path output-root channel-name year month day "txt"))
99 (defun log-file-path-utime (output-root channel-name format utime)
101 (second minute hour day month year day-of-week daylight-p zone)
102 (decode-universal-time utime)
103 (declare (ignore second minute hour day-of-week daylight-p zone))
104 (log-file-path output-root channel-name year month day format)))
106 (defun get-stream (channel istream)
107 (elt (streams channel) istream))
109 (defun (setf get-stream) (value channel istream)
110 (setf (elt (streams channel) istream) value))
112 (defun get-format (logger istream)
113 (elt (formats logger) istream))
115 (defun get-output-name (channel istream)
116 (elt (current-output-names channel) istream))
118 (defun (setf get-output-name) (value channel istream)
119 (setf (elt (current-output-names channel) istream) value))
121 (defun ensure-output-stream-for-unichannel (utime logger channel istream)
122 (let ((name (make-output-name-utime (name channel) utime)))
123 (unless (string= name (get-output-name channel istream))
124 (when (get-stream channel istream)
125 (write-file-footer (get-format logger istream)
127 (get-stream channel istream))
128 (close (get-stream channel istream)))
129 (setf (get-output-name channel istream) name)
130 (let ((path (log-file-path-utime (output-root channel) (name channel)
131 (get-format logger istream) utime)))
132 (unless (probe-file path)
133 (ensure-directories-exist path)
134 (setf (get-stream channel istream)
135 (open path :direction :output :if-exists :error
136 :if-does-not-exist :create))
137 (write-file-header (get-format logger istream)
139 (get-stream channel istream))
140 (close (get-stream channel istream)))
141 (setf (get-stream channel istream)
142 (open path :direction :output :if-exists :append
143 :if-does-not-exist :error))))))
145 (defun ensure-output-stream (utime logger channel istream)
146 "Ensures that *output-stream* is correct."
148 ((streamp (user-output logger))
149 (unless (get-stream channel istream)
150 (setf (get-stream channel istream) (user-output logger))))
151 ((pathnamep (user-output logger))
154 (ensure-output-stream-for-unichannel utime logger channel istream))
156 (setf (get-stream channel istream)
157 (open (user-output logger) :direction :output :if-exists :append)))))))
159 (defun format-utime (utime)
161 (second minute hour day-of-month month year day-of-week daylight-p zone)
162 (decode-universal-time utime)
163 (declare (ignore day-of-month month year day-of-week daylight-p zone))
164 (format nil "~2,'0D:~2,'0D:~2,'0D" hour minute second)))
166 (defun format-utime-short(utime)
168 (second minute hour day-of-month month year day-of-week daylight-p zone)
169 (decode-universal-time utime)
170 (declare (ignore second day-of-month month year day-of-week daylight-p zone))
171 (format nil "~2,'0D:~2,'0D" hour minute)))
173 (defun user-address (msg)
174 (let ((split (split *user-address-scanner* (raw-message-string msg)
175 :with-registers-p t)))
180 (defun need-user-address? (type)
181 (not (or (eq :action type) (eq :privmsg type))))
183 (defgeneric %output-event (format stream utime type channel source text msg
186 (defmethod %output-event ((format t) stream utime type channel source text
188 (%output-event :raw stream utime type channel source text msg unichannel))
190 (defmethod %output-event ((format (eql :raw)) stream utime type channel source
192 (declare (ignore utime type channel source text text unichannel ))
193 (format stream "~S~%" (string-right-trim '(#\return)
194 (raw-message-string msg))))
196 (defmethod %output-event ((format (eql :sexp)) stream utime type channel source text
199 (format stream "(~S ~S ~S ~S ~S)~%" utime type source text
200 (when (need-user-address? type) (user-address msg)))
201 (format stream "(~S ~S ~S ~S ~S ~S)~%" utime type source channel
202 text (when (need-user-address? type) (user-address msg)))))
204 (defmethod %output-event ((format (eql :text)) stream utime type channel source text
206 (format stream "~A " (format-utime utime))
207 (when (and (null unichannel) channel)
208 (format stream "[~A] " channel))
210 (let ((user-address (when (need-user-address? type) (user-address msg))))
213 (format stream "<~A> ~A" source text))
215 (format stream "*~A* ~A" source text))
217 (format stream "~A [~A] has joined ~A" source user-address channel))
219 (format stream "-!- ~A [~A] has left ~A" source user-address channel))
221 (format stream "-!- ~A is now known as ~A" source text))
223 (format stream "-!- ~A [~A] has been kicked from ~A" source user-address channel))
225 (format stream "-!- ~A [~A] has quit [~A]" source user-address (if text text "")))
227 (format stream "-!- ~A has set mode ~A" source text))
229 (format stream "-!- ~A changed the topic of ~A to: ~A" source channel text))
231 (format stream "-~A:~A- ~A" source channel text))
233 (warn "Unhandled msg type ~A." type))))
234 (write-char #\Newline stream))
236 (defun output-event-for-a-stream (msg type channel text logger istream)
237 (ensure-output-stream (received-time msg) logger channel istream)
238 (%output-event (get-format logger istream) (get-stream channel istream)
239 (received-time msg) type (name channel) (source msg) text msg
241 (force-output (get-stream channel istream)))
243 (defun output-event (msg type channel-name &optional text)
244 (dolist (logger *loggers*)
247 (dolist (channel (channels logger))
248 (dotimes (i (length (formats logger)))
249 (output-event-for-a-stream msg type channel text logger i))))
251 (let* ((channel (find channel-name (the list (channels logger))
252 :test #'string-equal :key #'name)))
254 (dotimes (i (length (formats logger)))
255 (output-event-for-a-stream msg type channel text logger i))))))))
257 (defun privmsg-hook (msg)
258 (output-event msg :privmsg (first (arguments msg)) (trailing-argument msg)))
260 (defun action-hook (msg)
261 (output-event msg :action (first (arguments msg))
262 (subseq (trailing-argument msg) 8
263 (- (length (trailing-argument msg)) 1))))
265 (defun nick-hook (msg)
266 (output-event msg :nick nil (trailing-argument msg)))
268 (defun part-hook (msg)
269 (output-event msg :part (first (arguments msg))))
271 (defun quit-hook (msg)
272 (output-event msg :quit nil (trailing-argument msg)))
274 (defun join-hook (msg)
275 (output-event msg :join (trailing-argument msg)))
277 (defun kick-hook (msg)
278 (output-event msg :kick (first (arguments msg))))
280 (defun notice-hook (msg)
281 (if (and (string-equal (source msg) "NickServ")
282 (string-equal "owned by someone else" (trailing-argument msg)))
283 (let ((logger (find-logger-with-connection (connection msg))))
285 (privmsg (connection msg) (source msg) (format nil "IDENTIFY ~A" (password logger)))
286 (warn "NickServ asks for identity with connection not found.")))
287 (output-event msg :notice (first (arguments msg)) (trailing-argument msg))))
289 (defun topic-hook (msg)
290 (output-event msg :topic (first (arguments msg)) (trailing-argument msg)))
292 (defun mode-hook (msg)
293 (output-event msg :mode (first (arguments msg))))
295 (defun make-channels (names formats output)
296 (loop for i from 0 to (1- (length names))
298 (make-instance 'channel
300 :streams (make-array (length formats) :initial-element nil)
301 :output-root (when (and (pathnamep output)
302 (null (pathname-name output)))
304 :current-output-names (make-array (length formats)
305 :initial-element nil))))
307 (defun is-unichannel-output (user-output)
308 "Returns T if output is setup for a single channel directory structure."
309 (and (pathnamep user-output) (null (pathname-name user-output))))
311 (defun create-logger (nick server &key channels output password
312 (logging-stream t) (formats '(:text)))
313 "OUTPUT may be a pathname or a stream"
318 (setq channels (list channels)))
320 (setq formats (list formats)))
322 (setq output (parse-namestring output)))
323 (let* ((conn (connect :nickname nick :server server
324 :logging-stream logging-stream))
325 (logger (make-instance
331 :channels (make-channels channels formats output)
334 :unichannel (is-unichannel-output output))))
336 (mapc #'(lambda (channel) (join conn channel)) channels)
337 (add-hook conn 'irc::irc-privmsg-message 'privmsg-hook)
338 (add-hook conn 'irc::ctcp-action-message 'action-hook)
339 (add-hook conn 'irc::irc-nick-message 'nick-hook)
340 (add-hook conn 'irc::irc-part-message 'part-hook)
341 (add-hook conn 'irc::irc-quit-message 'quit-hook)
342 (add-hook conn 'irc::irc-join-message 'join-hook)
343 (add-hook conn 'irc::irc-kick-message 'kick-hook)
344 (add-hook conn 'irc::irc-mode-message 'mode-hook)
345 (add-hook conn 'irc::irc-topic-message 'topic-hook)
346 (add-hook conn 'irc::irc-notice-message 'notice-hook)
349 (defun start-logger (logger async)
351 (start-background-message-handler (connection logger))
352 (read-message-loop (connection logger))))
354 (defun quit-logger (nick)
355 "Quit the active connection with nick and remove from active list."
356 (let ((logger (find-logger-with-nick nick)))
359 (warn "No active connection found with nick ~A." nick)
362 (irc:quit (connection logger))
364 (dolist (channel (channels logger))
365 (dotimes (i (length (streams channel)))
366 (when (streamp (get-stream channel i))
367 (close (get-stream channel i))
368 (setf (get-stream channel i) nil))))
370 (delete nick *loggers* :test #'string-equal :key #'nickname))
373 (defun add-logger (nick server &key channels output (password "")
374 (logging-stream t) (async t) (formats '(:text)))
375 (when (find-logger-with-nick nick)
376 (warn "Closing previously active connection.")
378 (let ((logger (create-logger nick server :channels channels :output output
379 :logging-stream logging-stream :password password
381 (push logger *loggers*)
382 (start-logger logger async)
385 (defun add-hook-logger (logger class hook)
386 (add-hook (connection logger) class hook))
388 (defun remove-hook-logger (logger class hook)
389 (remove-hook (connection logger) class hook))