1 ;;; murk.el --- Multiserver Unibuffer iRc Klient -*- lexical-binding:t -*-
3 ;; Copyright (C) 2024 plugd
5 ;; Author: plugd <plugd@thelambdalab.xyz>
6 ;; Created: 11 May 2024
8 ;; Homepage: http://thelambdalab.xyz/murk
10 ;; Package-Requires: ((emacs "26.1"))
12 ;; This file is not part of GNU Emacs.
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this file. If not, see <http://www.gnu.org/licenses/>.
29 ;; A very simple IRC server which uses only a single buffer.
41 "Multiserver Unibuffer iRc Klient"
44 (defcustom murk-default-nick "plugd"
48 (defcustom murk-default-quit-msg "Bye"
49 "Default quit message when none supplied."
52 (defcustom murk-networks
53 '(("debug" "localhost" 6697)
54 ("libera" "irc.libera.chat" 6697)
55 ("tilde" "tilde.chat" 6697)
56 ("freenode" "chat.freenode.net" 6697))
58 :type '(alist :key-type string))
60 (defcustom murk-show-joins nil
61 "Set to non-nil to be notified of joins, parts and quits.")
63 (defcustom murk-display-header t
64 "If non-nil, use buffer header to display current host and channel."
72 '((t :inherit default))
73 "Face used for murk text.")
76 '((t :inherit font-lock-keyword-face))
77 "Face used for the prompt.")
80 '((t :inherit murk-context))
81 "Face used for the context name in the prompt.")
84 '((t :inherit shadow))
85 "Face used for faded murk text.")
87 (defface murk-timestamp
88 '((t :inherit shadow))
89 "Face used for timestamps.")
93 "Face used for murk error text.")
96 '((t :inherit warning))
97 "Face used for murk notice text.")
103 (defvar murk-version "Murk v0.0"
104 "Value of this string is used in response to CTCP version queries.")
106 (defvar murk-notice-prefix "-!-")
107 (defvar murk-error-prefix "!!!")
108 (defvar murk-prompt-string ">")
110 (defvar murk-debug nil
111 "If non-nil, enable debug mode.")
114 ;;; Utility procedures
117 (defun murk--filtered-join (&rest args)
118 (string-join (seq-filter (lambda (el) el) args) " "))
120 (defun murk--as-string (obj)
122 (with-output-to-string (princ obj))
126 ;;; Network processes
129 (defvar murk-connection-table nil
130 "An alist associating servers to connection information.
131 This includes the process and the response string.")
133 (defun murk-connection-process (server)
134 (elt (assoc server murk-connection-table) 1))
136 (defun murk-connection-nick (server)
137 (elt (assoc server murk-connection-table) 2))
139 (defun murk-set-connection-nick (server nick)
140 (setf (elt (assoc server murk-connection-table) 2) nick))
142 (defun murk-connection-response (server)
143 (elt (assoc server murk-connection-table) 3))
145 (defun murk-set-connection-response (server string)
146 (setf (elt (assoc server murk-connection-table) 3) string))
148 (defun murk-connection-new (server process nick)
149 (add-to-list 'murk-connection-table
150 (list server process nick "")))
152 (defun murk-connection-remove (server)
153 (setq murk-connection-table
154 (seq-remove (lambda (row) (equal (car row) server))
155 murk-connection-table)))
157 (defun murk-make-server-filter (server)
158 (lambda (_proc string)
159 (dolist (line (split-string (concat (murk-connection-response server) string)
161 (if (string-suffix-p "\r" line)
162 (murk-eval-msg-string server (string-trim line))
163 (murk-set-connection-response server line)))))
165 (defun murk-make-server-sentinel (server)
166 (lambda (_proc string)
167 (unless (equal "open" (string-trim string))
168 (murk-display-error "Disconnected from server.")
169 (murk-connection-remove server)
170 (murk-remove-server-contexts server)
171 (murk-render-prompt))))
173 (defun murk-start-process (server)
174 (let* ((row (assoc server murk-networks))
177 (flags (seq-drop row 3)))
178 (make-network-process :name (concat "murk-" server)
182 :filter (murk-make-server-filter server)
183 :sentinel (murk-make-server-sentinel server)
185 :tls-parameters (if (memq :notls flags)
187 (cons 'gnutls-x509pki
188 (gnutls-boot-parameters
189 :type 'gnutls-x509pki
193 (defvar murk-ping-period 60)
195 ;; IDEA: Have a single ping timer which pings all connected hosts
197 (defun murk-connect (server)
198 (if (assoc server murk-connection-table)
199 (murk-display-error "Already connected to this network")
200 (if (not (assoc server murk-networks))
201 (murk-display-error "Network '" server "' is unknown.")
202 (let ((proc (murk-start-process server)))
203 (murk-connection-new server proc murk-default-nick))
204 (murk-send-msg server (murk-msg nil nil "USER" murk-default-nick 0 "*" murk-default-nick))
205 (murk-send-msg server (murk-msg nil nil "NICK" murk-default-nick))
206 (murk-add-context (list server))
207 (murk-render-prompt))))
209 (defun murk-send-msg (server msg)
211 (murk-display-string nil nil (murk-msg->string msg)))
212 (let ((proc (murk-connection-process server)))
213 (if (and proc (eq (process-status proc) 'open))
214 (process-send-string proc (concat (murk-msg->string msg) "\r\n"))
215 (murk-display-error "No server connection established"))))
221 (defun murk-msg (tags src cmd &rest params)
222 (list (murk--as-string tags)
223 (murk--as-string src)
224 (upcase (murk--as-string cmd))
225 (mapcar #'murk--as-string
226 (if (and params (listp (elt params 0)))
230 (defun murk-msg-tags (msg) (elt msg 0))
231 (defun murk-msg-src (msg) (elt msg 1))
232 (defun murk-msg-cmd (msg) (elt msg 2))
233 (defun murk-msg-params (msg) (elt msg 3))
234 (defun murk-msg-trail (msg)
235 (let ((params (murk-msg-params msg)))
237 (elt params (- (length params) 1)))))
239 (defvar murk-msg-regex
241 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
243 (opt (: ":" (: (group (* (not (any space "!" "@"))))
244 (* (not (any space)))))
246 (group (: (* (not whitespace))))
248 (opt (group (+ not-newline))))
249 "Regex used to parse IRC messages.
250 Note that this regex is incomplete. Noteably, we discard the non-nick
251 portion of the source component of the message, as mURK doesn't use this.")
253 (defun murk-string->msg (string)
254 (if (string-match murk-msg-regex string)
255 (let* ((tags (match-string 1 string))
256 (src (match-string 2 string))
257 (cmd (upcase (match-string 3 string)))
258 (params-str (match-string 4 string))
261 (let* ((idx (seq-position params-str ?:))
262 (l (split-string (string-trim (substring params-str 0 idx))))
263 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
266 (apply #'murk-msg (append (list tags src cmd) params)))
267 (error "Failed to parse string %s" string)))
269 (defun murk-msg->string (msg)
270 (let ((tags (murk-msg-tags msg))
271 (src (murk-msg-src msg))
272 (cmd (murk-msg-cmd msg))
273 (params (murk-msg-params msg)))
275 (if tags (concat "@" tags) nil)
276 (if src (concat ":" src) nil)
278 (if (> (length params) 1)
279 (string-join (seq-take params (- (length params) 1)) " ")
281 (if (> (length params) 0)
282 (concat ":" (elt params (- (length params) 1)))
289 ;; A context is a list (server channel users) identifying the server
290 ;; and channel. The tail of the list contains the nicks of users
291 ;; present in the channel.
293 ;; Each server has a special context (server) used for messages
294 ;; to/from the server itself.
296 (defvar murk-contexts nil
297 "List of currently-available contexts.
298 The head of this list is always the current context.")
300 (defun murk-current-context ()
301 "Return the current context."
306 (defun murk-contexts-equal (c1 c2)
307 (if (murk-server-context-p c1)
308 (and (murk-server-context-p c2)
309 (equal (murk-context-server c1)
310 (murk-context-server c2)))
311 (and (not (murk-server-context-p c2))
312 (equal (seq-take c1 2)
315 (defun murk-context-server (ctx) (elt ctx 0))
316 (defun murk-context-channel (ctx) (elt ctx 1))
317 (defun murk-context-users (ctx) (elt ctx 2))
318 (defun murk-set-context-users (ctx users)
319 (setcar (cddr ctx) users))
320 (defun murk-server-context-p (ctx) (not (cdr ctx)))
322 (defun murk-add-context (ctx)
323 (add-to-list 'murk-contexts ctx))
325 (defun murk-remove-context (ctx)
329 (murk-contexts-equal this-ctx ctx))
332 (defun murk-remove-server-contexts (server)
334 (seq-remove (lambda (row) (equal (car row) server))
337 (defun murk-context->string (ctx)
338 (if (murk-server-context-p ctx)
339 (concat "[" (murk-context-server ctx) "]")
340 (concat (murk-context-channel ctx) "@"
341 (murk-context-server ctx))))
343 (defun murk-get-context (server &optional name)
345 (let ((test-ctx (list server name)))
346 (seq-find (lambda (ctx)
347 (equal (seq-take ctx 2) test-ctx))
349 (assoc server murk-contexts)))
351 (defun murk-cycle-contexts (&optional reverse)
354 (let ((nminus1 (- (length murk-contexts) 1)))
356 (elt murk-contexts nminus1)
357 (seq-take murk-contexts nminus1)))
358 (append (cdr murk-contexts) (list (car murk-contexts))))))
360 (defun murk-add-context-users (ctx users)
361 (murk-set-context-users
363 (cl-union users (murk-context-users ctx))))
365 (defun murk-del-context-user (ctx user)
366 (murk-set-context-users
368 (delete user (murk-context-users ctx))))
370 (defun murk-del-server-user (server user)
371 (dolist (ctx murk-contexts)
372 (if (equal (murk-context-server ctx) server)
373 (murk-del-context-user ctx user))))
375 (defun murk-rename-server-user (server old-nick new-nick)
376 (dolist (ctx murk-contexts)
377 (when (and (equal (murk-context-server ctx) server)
378 (member old-nick (murk-context-users ctx)))
379 (murk-del-context-user ctx old-nick)
380 (murk-add-context-users ctx (list new-nick)))))
385 (defvar murk-prompt-marker nil
386 "Marker for prompt position in murk buffer.")
388 (defvar murk-input-marker nil
389 "Marker for prompt position in murk buffer.")
391 (defun murk-render-prompt ()
392 (with-current-buffer "*murk*"
393 (let ((update-point (= murk-input-marker (point)))
394 (update-window-points (mapcar (lambda (w)
395 (list (= (window-point w) murk-input-marker)
397 (get-buffer-window-list nil nil t))))
399 (set-marker-insertion-type murk-prompt-marker nil)
400 (set-marker-insertion-type murk-input-marker t)
401 (let ((inhibit-read-only t))
402 (delete-region murk-prompt-marker murk-input-marker)
403 (goto-char murk-prompt-marker)
405 (propertize (let ((ctx (murk-current-context)))
407 (murk-context->string ctx)
411 (propertize murk-prompt-string
414 (propertize " " ; Need this to be separate to mark it as rear-nonsticky
417 (set-marker-insertion-type murk-input-marker nil))
419 (goto-char murk-input-marker))
420 (dolist (v update-window-points)
422 (set-window-point (cadr v) murk-input-marker))))))
424 (defun murk-setup-header ()
425 (with-current-buffer "*murk*"
426 (setq-local header-line-format
428 (let* ((ctx (murk-current-context)))
430 (let ((server (murk-context-server ctx)))
432 "Network: " server ", "
433 (if (murk-server-context-p ctx)
437 (murk-context-channel ctx)
440 (length (murk-context-users ctx)))
442 "No connection")))))))
444 (defun murk-setup-buffer ()
445 (with-current-buffer (get-buffer-create "*murk*")
446 (setq-local scroll-conservatively 1)
447 (setq-local buffer-invisibility-spec nil)
448 (if (markerp murk-prompt-marker)
449 (set-marker murk-prompt-marker (point-max))
450 (setq murk-prompt-marker (point-max-marker)))
451 (if (markerp murk-input-marker)
452 (set-marker murk-input-marker (point-max))
453 (setq murk-input-marker (point-max-marker)))
454 (goto-char (point-max))
456 (if murk-display-header
457 (murk-setup-header))))
459 (defun murk-clear-buffer ()
460 "Completely erase all non-prompt and non-input text from murk buffer."
461 (with-current-buffer "*murk*"
462 (let ((inhibit-read-only t))
463 (delete-region (point-min) murk-prompt-marker))))
466 ;;; Output formatting and highlighting
469 (defun murk--fill-strings (col indent &rest strings)
471 (setq buffer-invisibility-spec nil)
472 (let ((fill-column col)
473 (adaptive-fill-regexp (rx-to-string `(= ,indent anychar))))
474 (apply #'insert strings)
475 (fill-region (point-min) (point-max) nil t)
478 (defun murk-display-string (context prefix &rest strings)
479 (with-current-buffer "*murk*"
481 (goto-char murk-prompt-marker)
482 (let* ((inhibit-read-only t)
483 (old-pos (marker-position murk-prompt-marker))
484 (padded-timestamp (concat (format-time-string "%H:%M ")))
485 (padded-prefix (if prefix (concat prefix " ") ""))
486 (context-atom (if context (intern (murk-context->string context)) nil)))
487 (insert-before-markers
490 (+ (length padded-timestamp)
491 (length padded-prefix))
492 (propertize padded-timestamp
493 'face 'murk-timestamp
496 'invisible context-atom)
497 (propertize padded-prefix
500 'invisible context-atom)
502 (propertize (concat (apply #'murk-buttonify-urls strings) "\n")
505 'invisible context-atom)))))))
506 (murk-scroll-windows-to-last-line))
508 (defun murk-display-message (server from to text)
509 (let ((context (if (string-prefix-p "#" to)
510 (murk-get-context server to)
511 (murk-get-context server))))
514 (if (murk-server-context-p context)
515 (concat "[" from " -> " to "]")
516 (concat (murk-context->string context) " <" from ">"))
519 (defun murk-display-notice (context &rest notices)
522 (propertize murk-notice-prefix 'face 'murk-notice)
523 (apply #'concat notices)))
525 (defun murk-display-error (&rest messages)
528 (propertize murk-error-prefix 'face 'murk-error)
529 (apply #'concat messages)))
531 (defun murk--start-of-final-line ()
532 (with-current-buffer "*murk*"
534 (goto-char (point-max))
535 (line-beginning-position))))
537 (defun murk-scroll-windows-to-last-line ()
538 (with-current-buffer "*murk*"
539 (dolist (window (get-buffer-window-list))
540 (if (>= (window-point window) (murk--start-of-final-line))
541 (with-selected-window window
544 (defconst murk-url-regex
548 (group (or (+ (any alnum "." "-"))
549 (+ (any alnum ":"))))
550 (opt (group (: ":" (+ digit))))
553 (* (any alnum "-/.,#:%=&_?~@+"))
554 (any alnum "-/#:%=&_~@+")))))))
555 "Imperfect regex used to find URLs in plain text.")
557 (defun murk-click-url (button)
558 (browse-url (button-get button 'url)))
560 (defun murk-buttonify-urls (&rest strings)
561 "Turn substrings which look like urls in STRING into clickable buttons."
563 (apply #'insert strings)
564 (goto-char (point-min))
565 (while (re-search-forward murk-url-regex nil t)
566 (let ((url (match-string 0)))
567 (make-text-button (match-beginning 0)
569 'action #'murk-click-url
573 'help-echo "Open URL in browser.")))
576 (defun murk-add-formatting (string)
579 (goto-char (point-min))
584 (prev-point (point)))
585 (while (re-search-forward (rx (or (any "\x02\x1D\x1F\x1E\x0F")
586 (: "\x03" (+ digit) (opt "," (* digit)))))
588 (let ((beg (+ (match-beginning 0) 1)))
590 (add-face-text-property prev-point beg '(:weight bold)))
592 (add-face-text-property prev-point beg '(:slant italic)))
594 (add-face-text-property prev-point beg '(:underline t)))
596 (add-face-text-property prev-point beg '(:strike-through t)))
597 (pcase (match-string 0)
598 ("\x02" (setq bold (not bold)))
599 ("\x1D" (setq italics (not italics)))
600 ("\x1F" (setq underline (not underline)))
601 ("\x1E" (setq strikethrough (not strikethrough)))
606 (setq strikethrough nil))
608 (delete-region (match-beginning 0) (match-end 0))
609 (setq prev-point (point)))))
613 ;;; Message evaluation
616 (defun murk-eval-msg-string (server string)
618 (murk-display-string nil nil string))
619 (let* ((msg (murk-string->msg string)))
620 (pcase (murk-msg-cmd msg)
622 (murk-send-msg server
623 (murk-msg nil nil "PONG" (murk-msg-params msg))))
628 (let* ((params (murk-msg-params msg))
629 (nick (elt params 0))
630 (text (string-join (seq-drop params 1) " ")))
631 (murk-set-connection-nick server nick)
632 (murk-display-notice nil text)))
635 (let* ((params (murk-msg-params msg))
636 (channel (elt params 2))
637 (names (split-string (elt params 3)))
638 (ctx (murk-get-context server channel)))
640 (murk-add-context-users ctx names)
641 (murk-display-notice nil "Users in " channel
642 ": " (string-join names " ")))))
645 (let* ((params (murk-msg-params msg))
646 (channel (elt params 1))
647 (ctx (murk-get-context server channel)))
651 (murk--as-string (length (murk-context-users ctx)))
652 " users in " channel)
653 (murk-display-notice nil "End of " channel " names list."))))
656 (let* ((params (murk-msg-params msg))
657 (channel (elt params 1))
658 (ctx (murk-get-context server channel)))
659 (murk-display-notice ctx "No topic set.")))
662 (let* ((params (murk-msg-params msg))
663 (channel (elt params 1))
664 (topic (elt params 2))
665 (ctx (murk-get-context server channel)))
666 (murk-display-notice ctx "Topic: " topic)))
668 ((rx (= 3 (any digit)))
669 (murk-display-notice nil (mapconcat 'identity (cdr (murk-msg-params msg)) " ")))
672 (guard (equal (murk-connection-nick server)
673 (murk-msg-src msg))))
674 (let ((channel (car (murk-msg-params msg))))
675 (murk-add-context (list server channel nil))
676 (murk-display-notice (murk-current-context)
677 "Joining channel " channel " on " server)
678 (murk-render-prompt)))
681 (let* ((channel (car (murk-msg-params msg)))
682 (nick (murk-msg-src msg))
683 (ctx (murk-get-context server channel)))
684 (murk-add-context-users ctx (list nick))
686 (murk-display-notice ctx nick " joined channel " channel
690 (guard (equal (murk-connection-nick server)
691 (murk-msg-src msg))))
692 (let ((channel (car (murk-msg-params msg))))
693 (murk-display-notice (murk-current-context) "Left channel " channel)
694 (murk-remove-context (list server channel))
695 (murk-render-prompt)))
698 (let* ((channel (car (murk-msg-params msg)))
699 (nick (murk-msg-src msg))
700 (ctx (murk-get-context server channel)))
701 (murk-del-context-user ctx nick)
703 (murk-display-notice ctx nick " left channel " channel
707 (guard (equal (murk-connection-nick server)
708 (murk-msg-src msg))))
709 (let ((new-nick (car (murk-msg-params msg))))
710 (murk-set-connection-nick server new-nick)
711 (murk-display-notice nil "Nick set to " new-nick " on " server)))
714 (let ((old-nick (murk-msg-src msg))
715 (new-nick (car (murk-msg-params msg))))
716 (murk-display-notice nil old-nick " is now known as " new-nick
718 (murk-rename-server-user server old-nick new-nick)))
721 (let ((channel (car (murk-msg-params msg)))
722 (nick (murk-msg-src msg))
723 (topic (cadr (murk-msg-params msg))))
724 (murk-display-notice (murk-get-context server channel)
725 nick " set the topic: " topic)))
728 (let ((nick (murk-msg-src msg))
729 (reason (mapconcat 'identity (murk-msg-params msg) " ")))
730 (murk-del-server-user server nick)
732 (murk-display-notice nil nick " quit: " reason))))
735 (let* ((from (murk-msg-src msg))
736 (params (murk-msg-params msg))
738 (text (cadr params)))
741 (let ((version-string (concat murk-version " - running on GNU Emacs " emacs-version)))
742 (murk-send-msg server
743 (murk-msg nil nil "NOTICE"
744 (list from (concat "\01VERSION "
747 (murk-display-notice nil "CTCP version request received from "
750 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
751 (murk-send-msg server (murk-msg nil nil "NOTICE" (list from ping)))
752 (murk-display-notice nil "CTCP ping received from " from " on " server))
755 (murk-display-notice nil "CTCP userinfo request from " from
756 " on " server " (no response sent)"))
759 (murk-display-notice nil "CTCP clientinfo request from " from
760 " on " server " (no response sent)"))
762 ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
763 (murk-display-action from to action-text))
766 (murk-display-message server from to text)))))
769 (murk-display-notice nil (murk-msg->string msg))))))
774 (defvar murk-command-table
775 '(("DEBUG" "Toggle debug mode on/off." murk-command-debug murk-boolean-completions)
776 ("HEADER" "Toggle display of header." murk-command-header murk-boolean-completions)
777 ("SHOWJOINS" "Toggles display of joins/parts." murk-command-showjoins murk-boolean-completions)
778 ("NETWORKS" "List known IRC networks." murk-command-networks)
779 ("CONNECT" "Connect to an IRC network." murk-command-connect murk-network-completions)
780 ("QUIT" "Disconnect from current network." murk-command-quit)
781 ("JOIN" "Join one or more channels." murk-command-join)
782 ("PART" "Leave channel." murk-command-part murk-context-completions)
783 ("NICK" "Change nick." murk-command-nick)
784 ("LIST" "Display details of one or more channels." murk-command-list)
785 ("TOPIC" "Set/query topic for current channel." murk-command-topic)
786 ("USERS" "List nicks of users in current context." murk-command-users)
787 ("MSG" "Send private message to user." murk-command-msg murk-nick-completions)
788 ("CLEAR" "Clear buffer text." murk-command-clear murk-context-completions)
789 ("HELP" "Display help on client commands." murk-command-help murk-help-completions))
790 "Table of commands explicitly supported by murk.")
792 (defun murk-boolean-completions ()
795 (defun murk-network-completions ()
796 (mapcar (lambda (row) (car row)) murk-networks))
798 (defun murk-command-help (params)
800 (let* ((cmd-str (upcase (car params)))
801 (row (assoc cmd-str murk-command-table #'equal)))
804 (murk-display-notice nil "Help for \x02" cmd-str "\x02:")
805 (murk-display-notice nil " " (elt row 1)))
806 (murk-display-notice nil "No such (client-interpreted) command.")))
807 (murk-display-notice nil "Client-interpreted commands:")
808 (dolist (row murk-command-table)
809 (murk-display-notice nil " \x02" (elt row 0) "\x02: " (elt row 1)))
810 (murk-display-notice nil "Use /HELP COMMAND to display information about a specific command.")))
812 (defun murk-command-debug (params)
815 (if (equal (upcase (car params)) "ON")
819 (murk-display-notice nil "Debug mode now " (if murk-debug "on" "off") "."))
821 (defun murk-command-header (params)
824 (equal (upcase (car params)) "ON")
825 (not header-line-format))
828 (murk-display-notice nil "Header enabled."))
829 (setq-local header-line-format nil)
830 (murk-display-notice nil "Header disabled.")))
832 (defun murk-command-showjoins (params)
833 (setq murk-show-joins
835 (if (equal (upcase (car params)) "ON")
838 (not murk-show-joins)))
839 (murk-display-notice nil "Joins/parts will now be "
840 (if murk-show-joins "shown" "hidden") "."))
842 (defun murk-command-connect (params)
844 (let ((network (car params)))
845 (murk-display-notice nil "Attempting to connect to " network "...")
846 (murk-connect network))
847 (murk-display-notice nil "Usage: /connect <network>")))
849 (defun murk-command-networks (_params)
850 (murk-display-notice nil "Currently-known networks:")
851 (dolist (row murk-networks)
852 (seq-let (network server port &rest _others) row
853 (murk-display-notice nil "\t" network
855 " " (number-to-string port) "]")))
856 (murk-display-notice nil "(Modify the `murk-networks' variable to add more.)"))
858 (defun murk-command-quit (params)
859 (let ((ctx (murk-current-context)))
861 (murk-display-error "No current context")
862 (let ((quit-msg (if params (string-join params " ") murk-default-quit-msg)))
864 (murk-context-server ctx)
865 (murk-msg nil nil "QUIT" quit-msg))))))
867 (defun murk-command-join (params)
869 (let ((server (murk-context-server (murk-current-context))))
870 (dolist (channel params)
871 (murk-send-msg server (murk-msg nil nil "JOIN" channel))))
872 (murk-display-notice nil "Usage: /join channel [channel2 ...]")))
874 (defun murk-command-part (params)
875 (let* ((server (murk-context-server (murk-current-context)))
878 (murk-context-channel (murk-current-context)))))
880 (murk-send-msg server (murk-msg nil nil "PART" channel))
881 (murk-display-error "No current channel to leave"))))
883 (defun murk-command-nick (params)
885 (let ((new-nick (string-join params " "))
886 (ctx (murk-current-context)))
888 (murk-send-msg (murk-context-server ctx)
889 (murk-msg nil nil "NICK" new-nick))
890 (murk-display-error "No current connection")))
891 (murk-display-notice nil "Usage: /nick <new-nick>")))
893 (defun murk-command-list (params)
894 (let ((ctx (murk-current-context)))
897 (murk-display-notice nil "This command can generate lots of output. Use `/LIST -yes' if you really want this, or `/LIST <channel_regexp>' to reduce the output.")
898 (let ((server (murk-context-server ctx)))
899 (if (equal (upcase (car params)) "-YES")
900 (murk-send-msg server (murk-msg nil nil "LIST"))
901 (murk-send-msg server (murk-msg nil nil "LIST"
903 (murk-display-error "No current connection"))))
905 (defun murk-command-topic (params)
906 (let ((ctx (murk-current-context)))
907 (if (and ctx (not (murk-server-context-p ctx)))
908 (let ((server (murk-context-server ctx))
909 (channel (murk-context-channel ctx)))
911 (murk-send-msg server
912 (murk-msg nil nil "TOPIC" channel
913 (string-join params " ")))
914 (murk-send-msg server
915 (murk-msg nil nil "TOPIC" channel))))
916 (murk-display-notice nil "No current channel."))))
918 (defun murk-command-msg (params)
919 (let ((server (murk-context-server (murk-current-context))))
920 (if (and params (>= (length params) 2))
921 (let ((to (car params))
922 (text (string-join (cdr params) " ")))
923 (murk-send-msg server (murk-msg nil nil "PRIVMSG" to text))
924 (murk-display-message server
925 (murk-connection-nick server)
927 (murk-display-notice nil "Usage: /msg <nick> <message>"))))
929 (defun murk-command-users (_params)
930 (let ((ctx (murk-current-context)))
931 (if (and ctx (not (murk-server-context-p ctx)))
932 (let ((channel (murk-context-channel ctx))
933 (server (murk-context-server ctx))
934 (users (murk-context-users ctx)))
935 (murk-display-notice ctx "Users in " channel " on " server ":")
936 (murk-display-notice ctx (string-join users " ")))
937 (murk-display-notice nil "No current channel."))))
943 (defun murk-enter-string (string)
944 (if (string-prefix-p "/" string)
946 ((rx (: "/" (let cmd-str (+ (not whitespace)))
948 (let params-str (+ anychar))
950 (let ((command-row (assoc (upcase cmd-str) murk-command-table #'equal))
951 (params (if params-str
952 (split-string params-str nil t)
954 (if (and command-row (elt command-row 2))
955 (funcall (elt command-row 2) params)
957 (murk-context-server (murk-current-context))
958 (murk-msg nil nil (upcase cmd-str) params)))))
960 (murk-display-error "Badly formed command")))
961 (unless (string-empty-p string)
962 (let ((ctx (murk-current-context)))
964 (if (not (murk-server-context-p ctx))
965 (let ((server (murk-context-server ctx))
966 (channel (murk-context-channel ctx)))
967 (murk-send-msg server
968 (murk-msg nil nil "PRIVMSG" channel string))
969 (murk-display-message server
970 (murk-connection-nick server)
972 (murk-display-error "No current channel"))
973 (murk-display-error "No current context"))))))
979 (defvar murk-history nil
980 "Commands and messages sent in current session.")
982 (defvar murk-history-index nil)
984 (defun murk-history-cycle (delta)
986 (with-current-buffer "*murk*"
987 (if murk-history-index
988 (setq murk-history-index
990 (min (- (length murk-history) 1)
991 (+ delta murk-history-index))))
992 (setq murk-history-index 0))
993 (delete-region murk-input-marker (point-max))
994 (insert (elt murk-history murk-history-index)))))
997 ;;; Interactive commands
1000 (defun murk-enter ()
1001 "Enter current contents of line after prompt."
1003 (with-current-buffer "*murk*"
1004 (let ((line (buffer-substring murk-input-marker (point-max))))
1005 (push line murk-history)
1006 (setq murk-history-index nil)
1007 (let ((inhibit-read-only t))
1008 (delete-region murk-input-marker (point-max)))
1009 (murk-enter-string line))))
1011 (defun murk-history-next ()
1013 (murk-history-cycle -1))
1015 (defun murk-history-prev ()
1017 (murk-history-cycle +1))
1019 (defun murk-cycle-contexts-forward ()
1021 (murk-cycle-contexts)
1022 (murk-render-prompt))
1024 (defun murk-cycle-contexts-reverse ()
1026 (murk-cycle-contexts t)
1027 (murk-render-prompt))
1029 (defun murk-complete-input ()
1031 (let ((completion-ignore-case t))
1032 (when (>= (point) murk-input-marker)
1033 (pcase (buffer-substring murk-input-marker (point))
1034 ((rx (: "/" (let cmd-str (+ (not whitespace))) (+ " ") (* (not whitespace)) string-end))
1035 (let ((space-idx (save-excursion
1036 (re-search-backward " " murk-input-marker t)))
1037 (table-row (assoc (upcase cmd-str) murk-command-table #'equal)))
1038 (if (and table-row (elt table-row 3))
1039 (let* ((completions-nospace (funcall (elt table-row 3)))
1040 (completions (mapcar (lambda (el) (concat el " ")) completions-nospace)))
1041 (completion-in-region (+ 1 space-idx) (point) completions)))))
1042 ((rx (: "/" (* (not whitespace)) string-end))
1043 (message (buffer-substring murk-input-marker (point)))
1044 (completion-in-region murk-input-marker (point)
1045 (mapcar (lambda (row) (concat "/" (car row) " "))
1046 murk-command-table)))
1048 (let* ((end (max murk-input-marker (point)))
1049 (space-idx (save-excursion
1050 (re-search-backward " " murk-input-marker t)))
1051 (start (if space-idx (+ 1 space-idx) murk-input-marker)))
1052 (unless (string-prefix-p "/" (buffer-substring start end))
1053 (let* ((users (murk-context-users (murk-current-context)))
1055 (lambda (u) (car (split-string u "@" t)))
1057 (completion-in-region start end users-no@)))))))))
1062 (defvar murk-mode-map
1063 (let ((map (make-sparse-keymap)))
1064 (define-key map (kbd "RET") 'murk-enter)
1065 (define-key map (kbd "TAB") 'murk-complete-input)
1066 (define-key map (kbd "<C-up>") 'murk-history-prev)
1067 (define-key map (kbd "<C-down>") 'murk-history-next)
1068 (define-key map (kbd "<C-tab>") 'murk-cycle-contexts-forward)
1069 (define-key map (kbd "<C-S-iso-lefttab>") 'murk-cycle-contexts-reverse)
1070 (define-key map (kbd "<C-S-tab>") 'murk-cycle-contexts-reverse)
1071 (when (fboundp 'evil-define-key*)
1072 (evil-define-key* 'motion map
1073 (kbd "TAB") 'murk-complete-input))
1076 (define-derived-mode murk-mode text-mode "murk"
1077 "Major mode for murk.")
1079 (when (fboundp 'evil-set-initial-state)
1080 (evil-set-initial-state 'murk-mode 'insert))
1082 ;;; Main start procedure
1086 "Start murk or just switch to the murk buffer if one already exists."
1088 (if (get-buffer "*murk*")
1089 (switch-to-buffer "*murk*")
1090 (switch-to-buffer "*murk*")
1092 (murk-setup-buffer))
1095 ;;; murk.el ends here