1 o;;; -*- 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 (handler :initform nil :accessor handler
22 :documentation "Background handler process.")
23 (nick :initarg :nick :reader nickname
24 :documentation "Nickname of the bot.")
25 (password :initarg :password :reader password
26 :documentation "Nickname's nickserver password.")
27 (server :initarg :server :reader server
28 :documentation "Connected IRC server.")
29 (channels :initarg :channels :accessor channels
30 :documentation "List of channels.")
31 (user-output :initarg :user-output :reader user-output
33 "Output parameter from user, maybe stream or pathname.")
34 (unichannel :initarg :unichannel :reader unichannel :type boolean
35 :documentation "T if user-output is directory for individual channel output.")
36 (formats :initarg :formats :reader formats
38 "A list of output formats.")))
40 (defvar *loggers* nil "List of active loggers.")
42 (defparameter *user-address-scanner*
46 (:greedy-repetition 1 nil :non-whitespace-char-class)))
47 :case-insensitive-mode t))
49 (defun find-logger-with-nick (nick)
50 (find nick (the list *loggers*) :test #'string-equal :key #'nickname))
52 (defun find-logger-with-connection (conn)
53 (find conn (the list *loggers*) :test #'eq :key #'connection))
55 (defun canonicalize-channel-name (name)
56 (string-left-trim '(#\#) name))
58 (defun find-channel-with-name (logger name)
59 (find name (the list (channels logger)) :test #'string-equal :key #'name))
61 (defun make-output-name (name year month day)
62 (format nil "~A-~4,'0D.~2,'0D.~2,'0D" (canonicalize-channel-name name)
65 (defun make-output-name-utime (name utime)
67 (second minute hour day-of-month month year day-of-week daylight-p zone)
68 (decode-universal-time utime)
69 (declare (ignore second minute hour day-of-week daylight-p zone))
70 (make-output-name name year month day-of-month)))
72 (defgeneric write-file-header (format channel-name stream))
74 (defmethod write-file-header ((format t) channel-name stream)
75 (declare (ignore channel-name stream))
78 (defgeneric write-file-footer (format channel-name stream))
80 (defmethod write-file-footer ((format t) channel-name stream)
81 (declare (ignore channel-name stream))
84 (defun %log-file-path (output-root channel-name year month day type)
87 :directory (append (pathname-directory output-root)
89 (string-left-trim '(#\#) channel-name)
90 (format nil "~4,'0D-~2,'0D" year month)))
91 :name (make-output-name channel-name year month day)
94 (defgeneric log-file-path (output-root channel-name year month day format))
96 (defmethod log-file-path (output-root channel-name year month day
98 (%log-file-path output-root channel-name year month day "raw"))
100 (defmethod log-file-path (output-root channel-name year month day (format (eql :sexp)))
101 (%log-file-path output-root channel-name year month day "sexp"))
103 (defmethod log-file-path (output-root channel-name year month day (format (eql :text)))
104 (%log-file-path output-root channel-name year month day "txt"))
106 (defmethod log-file-path (output-root channel-name year month day (format string))
107 (%log-file-path output-root channel-name year month day format))
110 (defun log-file-path-utime (output-root channel-name format utime)
112 (second minute hour day month year day-of-week daylight-p zone)
113 (decode-universal-time utime)
114 (declare (ignore second minute hour day-of-week daylight-p zone))
115 (log-file-path output-root channel-name year month day format)))
117 (defun get-stream (channel istream)
118 (elt (streams channel) istream))
120 (defun (setf get-stream) (value channel istream)
121 (setf (elt (streams channel) istream) value))
123 (defun get-format (logger istream)
124 (elt (formats logger) istream))
126 (defun get-output-name (channel istream)
127 (elt (current-output-names channel) istream))
129 (defun (setf get-output-name) (value channel istream)
130 (setf (elt (current-output-names channel) istream) value))
132 (defun ensure-output-stream-for-unichannel (utime logger channel istream)
133 (let ((name (make-output-name-utime (name channel) utime)))
134 (unless (string= name (get-output-name channel istream))
135 (when (get-stream channel istream)
136 (write-file-footer (get-format logger istream)
138 (get-stream channel istream))
139 (close (get-stream channel istream)))
140 (setf (get-output-name channel istream) name)
141 (let ((path (log-file-path-utime (output-root channel) (name channel)
142 (get-format logger istream) utime)))
143 (unless (probe-file path)
144 (ensure-directories-exist path)
145 (setf (get-stream channel istream)
146 (open path :direction :output :if-exists :error
147 :if-does-not-exist :create))
148 (write-file-header (get-format logger istream)
150 (get-stream channel istream))
151 (close (get-stream channel istream)))
152 (setf (get-stream channel istream)
153 (open path :direction :output :if-exists :append
154 :if-does-not-exist :error))))))
156 (defun ensure-output-stream (utime logger channel istream)
157 "Ensures that *output-stream* is correct."
159 ((streamp (user-output logger))
160 (unless (get-stream channel istream)
161 (setf (get-stream channel istream) (user-output logger))))
162 ((pathnamep (user-output logger))
165 (ensure-output-stream-for-unichannel utime logger channel istream))
167 (setf (get-stream channel istream)
168 (open (user-output logger) :direction :output :if-exists :append)))))))
170 (defun format-utime (utime)
172 (second minute hour day-of-month month year day-of-week daylight-p zone)
173 (decode-universal-time utime)
174 (declare (ignore day-of-month month year day-of-week daylight-p zone))
175 (format nil "~2,'0D:~2,'0D:~2,'0D" hour minute second)))
177 (defun format-utime-short(utime)
179 (second minute hour day-of-month month year day-of-week daylight-p zone)
180 (decode-universal-time utime)
181 (declare (ignore second day-of-month month year day-of-week daylight-p zone))
182 (format nil "~2,'0D:~2,'0D" hour minute)))
184 (defun user-address (msg)
185 (let ((split (split *user-address-scanner* (raw-message-string msg)
186 :with-registers-p t)))
191 (defun need-user-address? (type)
192 (not (or (eq :action type) (eq :privmsg type))))
194 (defgeneric %output-event (format stream utime type channel source text msg
197 (defmethod %output-event ((format t) stream utime type channel source text
199 (%output-event :raw stream utime type channel source text msg unichannel))
201 (defmethod %output-event ((format (eql :raw)) stream utime type channel source
203 (declare (ignore utime type channel source text text unichannel ))
204 (format stream "~S~%" (string-right-trim '(#\return)
205 (raw-message-string msg))))
207 (defun last-sexp-field (type msg)
210 (trailing-argument msg))
211 ((need-user-address? type)
212 (user-address msg))))
214 (defmethod %output-event ((format (eql :sexp)) stream utime type channel source text
217 (format stream "(~S ~S ~S ~S ~S)~%" utime type source text (last-sexp-field type msg))
218 (format stream "(~S ~S ~S ~S ~S ~S)~%" utime type source channel text
219 (last-sexp-field type msg))))
221 (defmethod %output-event ((format (eql :text)) stream utime type channel source text
223 (format stream "~A " (format-utime utime))
224 (when (and (null unichannel) channel)
225 (format stream "[~A] " channel))
227 (let ((user-address (when (need-user-address? type) (user-address msg))))
230 (format stream "<~A> ~A" source text))
232 (format stream "*~A* ~A" source text))
234 (format stream "~A [~A] has joined ~A" source user-address channel))
236 (format stream "-!- ~A [~A] has left ~A" source user-address channel))
238 (format stream "-!- ~A is now known as ~A" source text))
240 (format stream "-!- ~A [~A] has been kicked from ~A" source user-address channel))
242 (format stream "-!- ~A [~A] has quit [~A]" source user-address (if text text "")))
244 (format stream "-!- ~A has set mode ~A" source text))
246 (format stream "-!- ~A changed the topic of ~A to: ~A" source channel text))
248 (format stream "-~A:~A- ~A" source channel text))
250 (warn "Unhandled msg type ~A." type))))
251 (write-char #\Newline stream))
253 (defun output-event-for-a-stream (msg type channel text logger istream)
254 (ensure-output-stream (received-time msg) logger channel istream)
255 (%output-event (get-format logger istream) (get-stream channel istream)
256 (received-time msg) type (name channel) (source msg) text msg
258 (force-output (get-stream channel istream)))
261 (defun output-event (msg type channel-name &optional text)
263 (dolist (logger *loggers*)
266 (let* ((user (find-user (connection logger)
269 (:quit (source msg)))))
270 (channels (when user (cl-irc::channels user))))
271 (dolist (channel (mapcar
272 #'(lambda (name) (find-channel-with-name logger name))
273 (mapcar #'cl-irc::name channels)))
275 (dotimes (i (length (formats logger)))
276 (output-event-for-a-stream msg type channel text logger i))))))
278 (let* ((channel (find-channel-with-name logger channel-name)))
280 (dotimes (i (length (formats logger)))
281 (output-event-for-a-stream msg type channel text logger i))))))))
283 (defun privmsg-hook (msg)
284 (output-event msg :privmsg (first (arguments msg)) (trailing-argument msg)))
286 (defun action-hook (msg)
287 (output-event msg :action (first (arguments msg))
288 (subseq (trailing-argument msg) 8
289 (- (length (trailing-argument msg)) 1))))
291 (defun nick-hook (msg)
292 (output-event msg :nick nil (trailing-argument msg)))
294 (defun part-hook (msg)
295 (output-event msg :part (first (arguments msg))))
297 (defun quit-hook (msg)
298 (output-event msg :quit nil (trailing-argument msg)))
300 (defun join-hook (msg)
301 (output-event msg :join (trailing-argument msg)))
303 (defun kick-hook (msg)
304 (output-event msg :kick (first (arguments msg))))
306 (defun notice-hook (msg)
307 (if (and (string-equal (source msg) "NickServ")
308 (string-equal "owned by someone else" (trailing-argument msg)))
309 (let ((logger (find-logger-with-connection (connection msg))))
311 (privmsg (connection msg) (source msg) (format nil "IDENTIFY ~A" (password logger)))
312 (warn "NickServ asks for identity with connection not found.")))
313 (output-event msg :notice (first (arguments msg)) (trailing-argument msg))))
315 (defun topic-hook (msg)
316 (output-event msg :topic (first (arguments msg)) (trailing-argument msg)))
318 (defun mode-hook (msg)
319 (output-event msg :mode (first (arguments msg))
320 (format nil "~{~A~^ ~}" (cdr (arguments msg)))))
322 (defun make-a-channel (name formats output)
323 (make-instance 'channel
325 :streams (make-array (length formats) :initial-element nil)
326 :output-root (when (and (pathnamep output)
327 (null (pathname-name output)))
329 :current-output-names (make-array (length formats)
330 :initial-element nil)))
332 (defun make-channels (names formats output)
333 (loop for i from 0 to (1- (length names))
334 collect (make-a-channel (elt names i) formats output)))
336 (defun is-unichannel-output (user-output)
337 "Returns T if output is setup for a single channel directory structure."
338 (and (pathnamep user-output) (null (pathname-name user-output))))
340 (defun create-logger (nick server &key channels output password
342 (logging-stream t) (formats '(:text)))
343 "OUTPUT may be a pathname or a stream"
346 (if (and channels (atom channels))
347 (setq channels (list channels)))
349 (setq formats (list formats)))
351 (setq output (parse-namestring output)))
352 (let* ((conn (connect :nickname nick :server server
353 :username username :realname realname
354 :logging-stream logging-stream))
355 (logger (make-instance
361 :channels (make-channels channels formats output)
364 :unichannel (is-unichannel-output output))))
366 (mapc #'(lambda (channel) (join conn channel)) channels)
367 (add-hook conn 'irc::irc-privmsg-message 'privmsg-hook)
368 (add-hook conn 'irc::ctcp-action-message 'action-hook)
369 (add-hook conn 'irc::irc-nick-message 'nick-hook)
370 (add-hook conn 'irc::irc-part-message 'part-hook)
371 (add-hook conn 'irc::irc-quit-message 'quit-hook)
372 (add-hook conn 'irc::irc-join-message 'join-hook)
373 (add-hook conn 'irc::irc-kick-message 'kick-hook)
374 (add-hook conn 'irc::irc-mode-message 'mode-hook)
375 (add-hook conn 'irc::irc-topic-message 'topic-hook)
376 (add-hook conn 'irc::irc-notice-message 'notice-hook)
379 (defun start-logger (logger async)
381 (setf (handler logger)
382 (start-background-message-handler (connection logger)))
383 (read-message-loop (connection logger))))
385 (defun remove-logger (nick)
386 "Quit the active connection with nick and remove from active list."
387 (let ((logger (find-logger-with-nick nick)))
390 (warn "No active connection found with nick ~A." nick)
393 (irc:quit (connection logger))
394 (stop-background-message-handler (handler logger))
396 (dolist (channel (channels logger))
397 (let ((c (connection logger)))
399 (ignore-errors (remove-channel c (find-channel c (name channel)))))))
401 (delete nick *loggers* :test #'string-equal :key #'nickname))
404 (defun add-logger (nick server &key channels output (password "")
406 (logging-stream t) (async t) (formats '(:text)))
407 (when (find-logger-with-nick nick)
408 (warn "Closing previously active connection.")
409 (remove-logger nick))
410 (let ((logger (create-logger nick server :channels channels :output output
411 :logging-stream logging-stream :password password
412 :realname realname :username username
414 (push logger *loggers*)
415 (start-logger logger async)
418 (defun add-channel-logger (logger channel-name)
420 ((find-channel-with-name logger channel-name)
421 (warn "Channel ~A already in logger ~A." channel-name logger)
424 (let ((channel (make-a-channel channel-name (formats logger) (user-output logger))))
425 (join (connection logger) channel-name)
426 (push channel (channels logger))))))
428 (defun remove-channel-logger (logger channel-name)
429 (let ((channel (find-channel-with-name logger channel-name)))
432 (part (connection logger) channel-name)
433 (dotimes (i (length (streams channel)))
434 (when (streamp (get-stream channel i))
435 (close (get-stream channel i))
436 (setf (get-stream channel i) nil)))
437 (setf (channels logger) (delete channel-name (channels logger)
442 (warn "Channel name ~A not found in logger ~A."
446 (defun add-hook-logger (logger class hook)
447 (add-hook (connection logger) class hook))
449 (defun remove-hook-logger (logger class hook)
450 (remove-hook (connection logger) class hook))
452 ;; before disconnect, server usually sends something like
454 ;; Closing Link: nick[ipaddr or hostname] by servername (reason)
455 ;; some other reasons are: "G-lined", "K-lined", and "Your host is
456 ;; trying to (re)connect too fast -- throttled"
458 ;; reconnect should wait 30 sec depending upon the throttle
460 ;; avoid too many reconnect messages in the logs
462 ;; grep'ing around the net-nittin-ir source, it looks like 361
463 ;; rpl_killdone and 362 rpl_closing may refer to the closing link
464 ;; error i mention, as in, modern ircds may use numerics for such
465 ;; logging of unhandled numerics is good to do also