(defclass logger ()
((connection :initarg :connection :reader connection
:documentation "IRC connection object.")
+ (handler :initform nil :accessor handler
+ :documentation "Background handler process.")
(nick :initarg :nick :reader nickname
:documentation "Nickname of the bot.")
(password :initarg :password :reader password
(defmethod log-file-path (output-root channel-name year month day (format (eql :text)))
(%log-file-path output-root channel-name year month day "txt"))
+(defmethod log-file-path (output-root channel-name year month day (format string))
+ (%log-file-path output-root channel-name year month day format))
+
(defun log-file-path-utime (output-root channel-name format utime)
(multiple-value-bind
(format stream "~S~%" (string-right-trim '(#\return)
(raw-message-string msg))))
+(defun last-sexp-field (type msg)
+ (cond
+ ((eq type :kick)
+ (trailing-argument msg))
+ ((need-user-address? type)
+ (user-address msg))))
+
(defmethod %output-event ((format (eql :sexp)) stream utime type channel source text
msg unichannel)
(if unichannel
- (format stream "(~S ~S ~S ~S ~S)~%" utime type source text
- (when (need-user-address? type) (user-address msg)))
- (format stream "(~S ~S ~S ~S ~S ~S)~%" utime type source channel
- text (when (need-user-address? type) (user-address msg)))))
+ (format stream "(~S ~S ~S ~S ~S)~%" utime type source text (last-sexp-field type msg))
+ (format stream "(~S ~S ~S ~S ~S ~S)~%" utime type source channel text
+ (last-sexp-field type msg))))
(defmethod %output-event ((format (eql :text)) stream utime type channel source text
msg unichannel)
(unichannel logger))
(force-output (get-stream channel istream)))
+(defvar *msg*)
(defun output-event (msg type channel-name &optional text)
+ (setq *msg* msg)
(dolist (logger *loggers*)
(case type
((:quit :nick)
- (dolist (channel (channels logger))
- (dotimes (i (length (formats logger)))
- (output-event-for-a-stream msg type channel text logger i))))
+ (let* ((user (find-user (connection logger)
+ (case type
+ (:nick (source msg))
+ (:quit (source msg)))))
+ (channels (when user (cl-irc::channels user))))
+ (dolist (channel (mapcar
+ #'(lambda (name) (find-channel-with-name logger name))
+ (mapcar #'cl-irc::name channels)))
+ (when channel
+ (dotimes (i (length (formats logger)))
+ (output-event-for-a-stream msg type channel text logger i))))))
(t
(let* ((channel (find-channel-with-name logger channel-name)))
(when channel
(and (pathnamep user-output) (null (pathname-name user-output))))
(defun create-logger (nick server &key channels output password
- (logging-stream t) (formats '(:text)))
+ realname username
+ (logging-stream t) (formats '(:text)))
"OUTPUT may be a pathname or a stream"
;; check arguments
(assert formats)
(if (stringp output)
(setq output (parse-namestring output)))
(let* ((conn (connect :nickname nick :server server
+ :username username :realname realname
:logging-stream logging-stream))
(logger (make-instance
'logger
(defun start-logger (logger async)
(if async
- (start-background-message-handler (connection logger))
+ (setf (handler logger)
+ (start-background-message-handler (connection logger)))
(read-message-loop (connection logger))))
(defun remove-logger (nick)
nil)
(t
(irc:quit (connection logger))
+ (stop-background-message-handler (handler logger))
(sleep 1)
(dolist (channel (channels logger))
- (remove-channel logger (name channel)))
+ (let ((c (connection logger)))
+ (when c
+ (ignore-errors (remove-channel c (find-channel c (name channel)))))))
(setq *loggers*
(delete nick *loggers* :test #'string-equal :key #'nickname))
t))))
(defun add-logger (nick server &key channels output (password "")
+ realname username
(logging-stream t) (async t) (formats '(:text)))
(when (find-logger-with-nick nick)
(warn "Closing previously active connection.")
(remove-logger nick))
(let ((logger (create-logger nick server :channels channels :output output
:logging-stream logging-stream :password password
+ :realname realname :username username
:formats formats)))
(push logger *loggers*)
(start-logger logger async)
(defun remove-hook-logger (logger class hook)
(remove-hook (connection logger) class hook))
+
+;; before disconnect, server usually sends something like
+;;
+;; Closing Link: nick[ipaddr or hostname] by servername (reason)
+;; some other reasons are: "G-lined", "K-lined", and "Your host is
+;; trying to (re)connect too fast -- throttled"
+;;
+;; reconnect should wait 30 sec depending upon the throttle
+;;
+;; avoid too many reconnect messages in the logs
+;;
+;; grep'ing around the net-nittin-ir source, it looks like 361
+;; rpl_killdone and 362 rpl_closing may refer to the closing link
+;; error i mention, as in, modern ircds may use numerics for such
+;; logging of unhandled numerics is good to do also