r8351: updates
[irc-logger.git] / logger.lisp
index 9123cb80814495c641756195d57aaccf59090242..1382ef49e434480fb596d29794c809abac14f3d6 100644 (file)
@@ -20,6 +20,8 @@
               :documentation "IRC connection object.")
    (nick :initarg :nick :reader nickname
         :documentation "Nickname of the bot.")
+   (password :initarg :password :reader password
+            :documentation "Nickname's nickserver password.")
    (server :initarg :server :reader server
           :documentation "Connected IRC server.")
    (channels :initarg :channels :reader channels
@@ -45,6 +47,9 @@
 (defun find-logger-with-nick (nick)
   (find nick (the list *loggers*) :test #'string-equal :key #'nickname))
 
+(defun find-logger-with-connection (conn)
+  (find conn (the list *loggers*) :test #'eq :key #'connection))
+
 (defun make-output-name (name year month day)
     (format nil "~A-~4,'0D.~2,'0D.~2,'0D"
            (string-left-trim '(#\#) name) year month day))
 (defgeneric write-file-header (format channel-name stream))
 
 (defmethod write-file-header ((format t) channel-name stream)
-  (declare (ignore format channel-name stream))
+  (declare (ignore channel-name stream))
   )
 
 (defgeneric write-file-footer (format channel-name stream))
 
 (defmethod write-file-footer ((format t) channel-name stream)
-  (declare (ignore format channel-name stream))
+  (declare (ignore channel-name stream))
   )
                               
 (defun %log-file-path (output-root channel-name year month day type)
@@ -80,7 +85,8 @@
 
 (defgeneric log-file-path (output-root channel-name year month day format))
 
-(defmethod log-file-path (output-root channel-name year month day (format (eql :raw)))
+(defmethod log-file-path (output-root channel-name year month day
+                         (format (eql :raw)))
   (%log-file-path output-root channel-name year month day "raw"))
 
 (defmethod log-file-path (output-root channel-name year month day (format (eql :sexp)))
 (defun need-user-address? (type)
   (not (or (eq :action type) (eq :privmsg type))))
 
-(defgeneric %output-event (format stream utime type channel source text msg unichannel))
+(defgeneric %output-event (format stream utime type channel source text msg 
+                          unichannel))
 
 (defmethod %output-event ((format t) stream utime type channel source text
                          msg unichannel)
   (%output-event :raw stream utime type channel source text msg unichannel))
 
-(defmethod %output-event ((format (eql :raw)) stream utime type channel source text
-                         msg unichannel)
-  (declare (ignore unichannel))
-  (format stream "~S~%" (string-right-trim '(#\return) (raw-message-string msg))))
+(defmethod %output-event ((format (eql :raw)) stream utime type channel source
+                         text msg unichannel)
+  (declare (ignore utime type channel source text text unichannel ))
+  (format stream "~S~%" (string-right-trim '(#\return) 
+                                          (raw-message-string msg))))
 
 (defmethod %output-event ((format (eql :sexp)) stream utime type channel source text
                          msg unichannel)
       (:kick
        (format stream "-!- ~A [~A] has been kicked from ~A" source user-address channel))
       (:quit
-       (format stream "-!- ~A [~A] has quit [~A]" source user-address text))
+       (format stream "-!- ~A [~A] has quit [~A]" source user-address (if text text "")))
       (:mode
        (format stream "-!- ~A has set mode ~A"  source text))
       (:topic
   (output-event msg :part (first (arguments msg))))
 
 (defun quit-hook (msg)
-  (output-event msg :quit (trailing-argument msg)))
+  (output-event msg :quit nil (trailing-argument msg)))
 
 (defun join-hook (msg)
   (output-event msg :join (trailing-argument msg)))
   (output-event msg :kick (first (arguments msg))))
 
 (defun notice-hook (msg)
-  (output-event msg :notice (first (arguments msg)) (trailing-argument msg)))
+  (if (and (string-equal (source msg) "NickServ")
+          (string-equal "owned by someone else" (trailing-argument msg)))
+      (let ((logger (find-logger-with-connection (connection msg))))
+       (if logger
+           (privmsg (connection msg) (source msg) (format nil "IDENTIFY ~A" (password logger)))
+           (warn "NickServ asks for identity with connection not found."))) 
+      (output-event msg :notice (first (arguments msg)) (trailing-argument msg))))
 
 (defun topic-hook (msg)
   (output-event msg :topic (first (arguments msg)) (trailing-argument msg)))
   "Returns T if output is setup for a single channel directory structure."
   (and (pathnamep user-output) (null (pathname-name user-output))))
                  
-(defun create-logger (nick server &key channels output
-                     (logging-stream t)
-                     (async t)
-                     (formats '(:text)))
+(defun create-logger (nick server &key channels output password
+                     (logging-stream t) (formats '(:text)))
   "OUTPUT may be a pathname or a stream"
   ;; check arguments
   (assert channels)
                  'logger
                  :connection conn
                  :nick nick
+                 :password password
                  :server server
                  :channels (make-channels channels formats output)
                  :user-output output
     (add-hook conn 'irc::irc-mode-message 'mode-hook)
     (add-hook conn 'irc::irc-topic-message 'topic-hook)
     (add-hook conn 'irc::irc-notice-message 'notice-hook)
-    (cond
-      (async
-       #+sbcl (add-asynchronous-message-handler conn)
-       #-sbcl (read-message-loop conn))
-      (t
-       (read-message-loop conn)))
     logger))
 
+(defun start-logger (logger async)
+  (if async
+      (start-background-message-handler (connection logger))
+      (read-message-loop (connection logger))))
+
 (defun quit-logger (nick)
   "Quit the active connection with nick and remove from active list."
   (let ((logger (find-logger-with-nick nick)))
             (delete nick *loggers*  :test #'string-equal :key #'nickname))
        t))))
 
-(defun add-logger (nick server &key channels output
-                  (logging-stream t)
-                  (async t)
-                  (formats '(:text)))
+(defun add-logger (nick server &key channels output (password "")
+                  (logging-stream t) (async t) (formats '(:text)))
   (when (find-logger-with-nick nick)
     (warn "Closing previously active connection.")
     (quit-logger nick))
-  (let ((logger
-        (create-logger nick server :channels channels :output output
-                       :logging-stream logging-stream
-                       :async async :formats formats)))
+  (let ((logger (create-logger nick server :channels channels :output output
+                              :logging-stream logging-stream :password password
+                              :formats formats)))
     (push logger *loggers*)
+    (start-logger logger async)
     logger))
 
-(defun add-hook-logger (logger msg hook)
-  (add-hook (connection logger) msg hook))
+(defun add-hook-logger (logger class hook)
+  (add-hook (connection logger) class hook))
 
-(defun remove-hook-logger (logger msg)
-  (remove-hook (connection logger) msg))
+(defun remove-hook-logger (logger class hook)
+  (remove-hook (connection logger) class hook))