Messages kinda working.
[lurk.git] / murk.el
diff --git a/murk.el b/murk.el
index 652b3c2..e89df65 100644 (file)
--- a/murk.el
+++ b/murk.el
@@ -300,6 +300,7 @@ The head of this list is always the current context.")
 
 (defun murk-context-server (ctx) (elt ctx 0))
 (defun murk-context-name (ctx) (elt ctx 1))
+(defun murk-context-users (ctx) (seq-drop ctx 2))
 (defun murk-server-context-p (ctx) (not (cdr ctx)))
 
 (defun murk-add-context (ctx)
@@ -322,6 +323,13 @@ The head of this list is always the current context.")
      (concat (murk-context-name ctx) "@"
              (murk-context-server ctx))))
 
+(defun murk-get-context (server &optional name)
+  (if name
+      (assoc server murk-contexts)
+    (let ((test-ctx (list server name)))
+      (seq-find (lambda (ctx)
+                  (equal (seq-take ctx 2) test-ctx))
+                murk-contexts))))
 
 ;;; Buffer
 ;;
@@ -366,8 +374,24 @@ The head of this list is always the current context.")
   "Marker for prompt position in murk buffer.")
 
 (defun murk-setup-header ()
-  ;; To do
-  )
+  (with-current-buffer "*murk*"
+    (setq-local header-line-format
+                '((:eval
+                   (let* ((ctx (murk-current-context)))
+                     (if ctx
+                         (let ((server (murk-context-server ctx)))
+                           (concat
+                            "Network: " server ", "
+                            (if (murk-server-context-p ctx)
+                                "Server"
+                              (concat
+                               "Channel: "
+                               (murk-context-name ctx)
+                               " ("
+                               (number-to-string
+                                (length (murk-context-users ctx)))
+                               ")"))))
+                       "No connection")))))))
 
 (defun murk-setup-buffer ()
   (with-current-buffer (get-buffer-create "*murk*")
@@ -433,6 +457,17 @@ The head of this list is always the current context.")
                        'invisible context-atom)))))))
   (murk-scroll-windows-to-last-line))
 
+(defun murk-display-message (server from to text)
+  (let ((context (if (string-prefix-p "#" to)
+                     (murk-get-context server to)
+                   (murk-get-context server))))
+    (murk-display-string
+     context
+     (if (murk-server-context-p context)
+         (concat "[" from " -> " to "]")
+       (concat (murk-context->string context) " <" from ">"))
+     text)))
+
 (defun murk-display-notice (context &rest notices)
   (murk-display-string
    context
@@ -577,6 +612,40 @@ The head of this list is always the current context.")
          (if murk-show-joins
              (murk-display-notice nil nick " quit: " reason))))
 
+      ("PRIVMSG"
+       (let* ((from (murk-msg-src msg))
+              (params (murk-msg-params msg))
+              (to (car params))
+              (text (cadr params)))
+         (pcase text
+           ("\01VERSION\01"
+            (let ((version-string (concat murk-version " - running on GNU Emacs " emacs-version)))
+              (murk-send-msg server
+                             (murk-msg nil nil "NOTICE"
+                                       (list from (concat "\01VERSION "
+                                                          version-string
+                                                          "\01")))))
+            (murk-display-notice nil "CTCP version request received from "
+                                 from " on " server))
+
+           ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
+            (murk-send-msg server (lurk-msg nil nil "NOTICE" (list from ping)))
+            (murk-display-notice nil "CTCP ping received from " from " on " server))
+
+           ("\01USERINFO\01"
+            (murk-display-notice nil "CTCP userinfo request from " from
+                                 " on " server " (no response sent)"))
+
+           ("\01CLIENTINFO\01"
+            (murk-display-notice nil "CTCP clientinfo request from " from
+                                 " on " server " (no response sent)"))
+
+           ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
+            (murk-display-action from to action-text))
+
+           (_
+            (murk-display-message server from to text)))))
+
       (_
        (murk-display-notice nil (murk-msg->string msg))))))
 
@@ -626,6 +695,17 @@ The head of this list is always the current context.")
           (not murk-debug)))
   (murk-display-notice nil "Debug mode now " (if murk-debug "on" "off") "."))
 
+(defun murk-command-header (params)
+  (if
+      (if params
+          (equal (upcase (car params)) "ON")
+        (not header-line-format))
+      (progn
+        (murk-setup-header)
+        (murk-display-notice nil "Header enabled."))
+    (setq-local header-line-format nil)
+    (murk-display-notice nil "Header disabled.")))
+
 (defun murk-command-clear (params)
   (if (not params)
       (murk-clear-buffer)
@@ -673,6 +753,16 @@ The head of this list is always the current context.")
         (murk-send-msg server (murk-msg nil nil "PART" channel))
       (murk-display-error "No current channel to leave."))))
 
+(defun murk-command-msg (params)
+  (let ((server (murk-context-server (murk-current-context))))
+    (if (and params (>= (length params) 2))
+        (let ((to (car params))
+              (text (string-join (cdr params) " ")))
+          (murk-send-msg server (lurk-msg nil nil "PRIVMSG" to text))
+          (murk-display-message server
+                                (murk-connection-nick server)
+                                to text))
+      (murk-display-notice nil "Usage: /msg <nick> <message>"))))
 
 ;;; Command entering
 ;;
@@ -697,12 +787,15 @@ The head of this list is always the current context.")
          (murk-display-error "Badly formed command.")))
     (unless (string-empty-p string)
       (if (murk-current-context)
-          (progn
+          (let ((server (murk-context-server (murk-current-context))))
             (murk-send-msg server
                            (murk-msg nil nil "PRIVMSG"
-                                     (murk-context-name murk-current-context)
+                                     (murk-context-name (murk-current-context))
                                      string))
-            (murk-display-message murk-nick (murk-context->string (murk-current-context)) string))
+            (murk-display-message server
+                                  murk-nick
+                                  (murk-context->string (murk-current-context))
+                                  string))
         (murk-display-error "No current context.")))))