r8421: improve quit/nick change messages
authorKevin M. Rosenberg <kevin@rosenberg.net>
Tue, 30 Dec 2003 18:05:45 +0000 (18:05 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Tue, 30 Dec 2003 18:05:45 +0000 (18:05 +0000)
logger.lisp

index 7baa2415d8a227a1a54e0c781d53bac067d76e7b..3aab458ec665fc7ca826c8b2d8fc4bfe003c5856 100644 (file)
@@ -18,6 +18,8 @@ o;;;  -*- Mode: Lisp -*-
 (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
@@ -101,6 +103,9 @@ o;;;  -*- Mode: Lisp -*-
 (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
@@ -199,13 +204,19 @@ o;;;  -*- Mode: Lisp -*-
   (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)
@@ -246,13 +257,23 @@ o;;;  -*- Mode: Lisp -*-
                  (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
@@ -317,7 +338,8 @@ o;;;  -*- Mode: Lisp -*-
   (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)
@@ -328,6 +350,7 @@ o;;;  -*- Mode: Lisp -*-
   (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
@@ -355,7 +378,8 @@ o;;;  -*- Mode: Lisp -*-
 
 (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)
@@ -367,20 +391,25 @@ o;;;  -*- Mode: Lisp -*-
        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)
@@ -419,3 +448,18 @@ o;;;  -*- Mode: Lisp -*-
 
 (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