1 ;;; lurk.el --- Little Uni-buffer iRc Klient -*- lexical-binding:t -*-
3 ;; Copyright (C) 2021 Tim Vaughan
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 14 June 2021
9 ;; Homepage: http://thelambdalab.xyz/lurk
10 ;; Package-Requires: ((emacs "26"))
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/>.
38 "Little Uni-buffer iRc Klient."
41 (defcustom lurk-nick "plugd"
44 (defcustom lurk-default-quit-msg "Bye"
45 "Default quit message when none supplied.")
47 (defcustom lurk-networks
48 '(("libera" "irc.libera.chat" 6697)
49 ("freenode" "chat.freenode.net" 6697)
50 ("tilde" "tilde.chat" 6697)
51 ("mbr" "mbrserver.com" 6667 :notls)
52 ("local" "localhost" 6697))
55 (defcustom lurk-allow-ipv6 nil
56 "Set to non-nil to allow use of IPv6.")
58 (defcustom lurk-show-joins nil
59 "Set to non-nil to be notified of joins, parts and quits.")
65 '((t :inherit default))
66 "Face used for Lurk text.")
69 '((t :inherit org-priority))
70 "Face used for the prompt.")
73 '((t :inherit org-tag))
74 "Face used for the context name in the prompt.")
77 '((t :inherit org-agenda-dimmed-todo-face))
78 "Face used for faded Lurk text.")
81 '((t :inherit font-lock-regexp-grouping-construct))
82 "Face used for Lurk error text.")
87 (defvar lurk-version "Lurk v0.1")
89 (defvar lurk-notice-prefix "-!-")
91 (defvar lurk-error-prefix "!!!")
93 (defvar lurk-prompt-string
94 (propertize "> " 'face 'lurk-prompt))
96 (defvar lurk-debug nil
97 "If non-nil, enable debug mode.")
102 (defvar lurk-response "")
104 (defun lurk-filter (proc string)
105 (dolist (line (split-string (concat lurk-response string) "\n"))
106 (if (string-suffix-p "\r" line)
107 (lurk-eval-msg-string (string-trim line))
108 (setq lurk-response line))))
110 (defun lurk-sentinel (proc string)
111 (unless (equal "open" (string-trim string))
112 (lurk-display-error "Disconnected from server.")
113 (clrhash lurk-contexts)
114 (lurk-set-current-context nil)
116 (cancel-timer lurk-ping-timer)))
118 (defun lurk-start-process (network)
119 (let* ((row (assoc network lurk-networks))
122 (flags (seq-drop row 3)))
123 (make-network-process :name "lurk"
126 :family (if lurk-allow-ipv6 nil 'ipv4)
127 :filter #'lurk-filter
128 :sentinel #'lurk-sentinel
130 :tls-parameters (if (memq :notls flags)
132 (cons 'gnutls-x509pki
133 (gnutls-boot-parameters
134 :type 'gnutls-x509pki
138 (defvar lurk-ping-timer nil)
139 (defvar lurk-ping-period 60)
141 (defun lurk-ping-function ()
142 (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
143 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
145 (defun lurk-connect (network)
146 (if (get-process "lurk")
147 (lurk-display-error "Already connected. Disconnect first.")
148 (if (not (assoc network lurk-networks))
149 (lurk-display-error "Network '" network "' is unknown.")
150 (clrhash lurk-contexts)
151 (lurk-set-current-context nil)
152 (lurk-start-process network)
153 (lurk-send-msg (lurk-msg nil nil "USER" lurk-nick 0 "*" lurk-nick))
154 (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
155 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
157 (defun lurk-connected-p ()
158 (let ((proc (get-process "lurk")))
159 (and proc (eq (process-status proc) 'open))))
161 (defun lurk-send-msg (msg)
163 (lurk-display-string nil (lurk-msg->string msg)))
164 (let ((proc (get-process "lurk")))
165 (if (and proc (eq (process-status proc) 'open))
166 (process-send-string proc (concat (lurk-msg->string msg) "\r\n"))
167 (lurk-display-error "No server connection established.")
168 (error "No server connection established"))))
174 (defun lurk--as-string (obj)
176 (with-output-to-string (princ obj))
179 (defun lurk-msg (tags src cmd &rest params)
180 (list (lurk--as-string tags)
181 (lurk--as-string src)
182 (upcase (lurk--as-string cmd))
183 (mapcar #'lurk--as-string
184 (if (and params (listp (elt params 0)))
188 (defun lurk-msg-tags (msg) (elt msg 0))
189 (defun lurk-msg-src (msg) (elt msg 1))
190 (defun lurk-msg-cmd (msg) (elt msg 2))
191 (defun lurk-msg-params (msg) (elt msg 3))
192 (defun lurk-msg-trail (msg)
193 (let ((params (lurk-msg-params msg)))
195 (elt params (- (length params) 1)))))
197 (defvar lurk-msg-regex
199 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
201 (opt (: ":" (: (group (* (not (any space "!" "@"))))
202 (* (not (any space)))))
204 (group (: (* (not whitespace))))
206 (opt (group (+ not-newline))))
207 "Regex used to parse IRC messages.
208 Note that this regex is incomplete. Noteably, we discard the non-nick
209 portion of the source component of the message, as LURK doesn't use this.")
211 (defun lurk-string->msg (string)
212 (if (string-match lurk-msg-regex string)
213 (let* ((tags (match-string 1 string))
214 (src (match-string 2 string))
215 (cmd (upcase (match-string 3 string)))
216 (params-str (match-string 4 string))
219 (let* ((idx (cl-search ":" params-str))
220 (l (split-string (string-trim (substring params-str 0 idx))))
221 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
224 (apply #'lurk-msg (append (list tags src cmd) params)))
225 (error "Failed to parse string " string)))
227 (defun lurk--filtered-join (&rest args)
228 (string-join (seq-filter (lambda (el) el) args) " "))
230 (defun lurk-msg->string (msg)
231 (let ((tags (lurk-msg-tags msg))
232 (src (lurk-msg-src msg))
233 (cmd (lurk-msg-cmd msg))
234 (params (lurk-msg-params msg)))
236 (if tags (concat "@" tags) nil)
237 (if src (concat ":" src) nil)
239 (if (> (length params) 1)
240 (string-join (seq-take params (- (length params) 1)) " ")
242 (if (> (length params) 0)
243 (concat ":" (elt params (- (length params) 1)))
250 (defvar lurk-current-context nil)
251 (defvar lurk-contexts (make-hash-table :test #'equal))
253 (defun lurk-add-context (name)
254 (puthash name nil lurk-contexts))
256 (defun lurk-del-context (name)
257 (remhash name lurk-contexts))
259 (defun lurk-get-context-users (name)
260 (gethash name lurk-contexts))
262 (defun lurk-add-context-users (context users)
265 (gethash context lurk-contexts))
268 (defun lurk-del-context-user (context user)
270 (remove user (gethash context lurk-contexts))
273 (defun lurk-del-user (user)
274 (dolist (context (lurk-get-context-list))
275 (lurk-del-context-user context user)))
277 (defun lurk-rename-user (old-nick new-nick)
278 (dolist (context (lurk-get-context-list))
279 (lurk-del-context-user context old-nick)
280 (lurk-add-context-users context (list new-nick))))
282 (defun lurk-get-context-type (name)
284 ((string-prefix-p "#" name) 'channel)
285 ((string-match-p (rx (or "." "localhost")) name) 'host)
288 (defun lurk-get-context-list ()
290 (maphash (lambda (key val)
291 (cl-pushnew key res))
295 (defun lurk-get-next-context (&optional prev)
296 (if lurk-current-context
297 (let* ((context-list (if prev
298 (reverse (lurk-get-context-list))
299 (lurk-get-context-list)))
300 (context-list* (member lurk-current-context context-list)))
301 (if (> (length context-list*) 1)
306 (defun lurk-set-current-context (context)
307 (setq lurk-current-context context)
308 (lurk-highlight-context context)
310 (lurk-zoom-in lurk-current-context)))
312 (defun lurk-cycle-contexts (&optional rev)
313 (if lurk-current-context
315 (lurk-set-current-context (lurk-get-next-context rev))
316 (lurk-render-prompt))
317 (lurk-display-error "No channels joined.")))
323 (defun lurk-render-prompt ()
324 (with-current-buffer "*lurk*"
325 (let ((update-point (= lurk-input-marker (point)))
326 (update-window-points (mapcar (lambda (w)
327 (list (= (window-point w) lurk-input-marker)
329 (get-buffer-window-list nil nil t))))
331 (set-marker-insertion-type lurk-prompt-marker nil)
332 (set-marker-insertion-type lurk-input-marker t)
333 (let ((inhibit-read-only t))
334 (delete-region lurk-prompt-marker lurk-input-marker)
335 (goto-char lurk-prompt-marker)
337 (propertize (if lurk-current-context
342 (propertize lurk-prompt-string
346 (set-marker-insertion-type lurk-input-marker nil))
348 (goto-char lurk-input-marker))
349 (dolist (v update-window-points)
351 (set-window-point (cadr v) lurk-input-marker))))))
353 (defvar lurk-prompt-marker nil
354 "Marker for prompt position in LURK buffer.")
356 (defvar lurk-input-marker nil
357 "Marker for prompt position in LURK buffer.")
359 (defun lurk-setup-buffer ()
360 (with-current-buffer (get-buffer-create "*lurk*")
361 (setq-local scroll-conservatively 1)
362 (setq-local buffer-invisibility-spec nil)
363 (if (markerp lurk-prompt-marker)
364 (set-marker lurk-prompt-marker (point-max))
365 (setq lurk-prompt-marker (point-max-marker)))
366 (if (markerp lurk-input-marker)
367 (set-marker lurk-input-marker (point-max))
368 (setq lurk-input-marker (point-max-marker)))
369 (goto-char (point-max))
370 (lurk-render-prompt)))
374 (defconst lurk-url-regex
378 (group (or (+ (any alnum "." "-"))
379 (+ (any alnum ":"))))
380 (opt (group (: ":" (+ digit))))
383 (* (any alnum ",.-~/@|:%#=&_"))
384 (+ (any alnum "-~/@|:%#=&")))))))))
386 (defun lurk-click-url (button)
387 (browse-url (button-get button 'url)))
389 (defun lurk-buttonify-urls (string)
390 "Turn substrings which look like urls in STRING into clickable buttons."
393 (goto-char (point-min))
394 (while (re-search-forward lurk-url-regex nil t)
395 (let ((url (match-string 0)))
396 (make-text-button (match-beginning 0)
398 'action #'lurk-click-url
402 'help-echo "Open URL in browser.")))
406 ;;; Output formatting and highlighting
409 ;; Partially-implemented idea: the face text property can be
410 ;; a list of faces, applied in order. By assigning each context
411 ;; a unique list and keeping track of these in a hash table, we can
412 ;; easily switch the face corresponding to a particular context
413 ;; by modifying the elements of this list.
415 ;; More subtly, we make only the cdrs of this list shared among
416 ;; all text of a given context, allowing the cars to be different
417 ;; and for different elements of the context-specific text to have
418 ;; different styling.
420 ;; Additionally, we can allow selective hiding of contexts via
421 ;; the buffer-invisibility-spec.
423 (defvar lurk-context-facelists (make-hash-table :test 'equal)
424 "List of seen contexts and associated face lists.")
426 (defun lurk-get-context-facelist (context)
427 (let ((facelist (gethash context lurk-context-facelists)))
429 (setq facelist (list 'lurk-text))
430 (puthash context facelist lurk-context-facelists))
433 (defun lurk-display-string (context &rest strings)
434 (with-current-buffer (get-buffer-create "*lurk*")
436 (goto-char lurk-prompt-marker)
437 (let ((inhibit-read-only t)
438 (old-pos (marker-position lurk-prompt-marker))
439 (adaptive-fill-regexp (rx (= 6 anychar)))
441 (context-atom (if context (intern context) nil)))
442 (insert-before-markers
443 (propertize (concat (format-time-string "%H:%M") " ")
444 'face (lurk-get-context-facelist context)
447 'invisible context-atom)
448 (propertize (concat (lurk-buttonify-urls (apply #'concat strings)) "\n")
449 'face (lurk-get-context-facelist context)
452 'invisible context-atom))
453 (fill-region old-pos lurk-prompt-marker nil t)))))
455 (defun lurk-display-message (from to text)
456 (let ((context (if (eq 'channel (lurk-get-context-type to))
458 (if (equal to lurk-nick) from to))))
461 (pcase (lurk-get-context-type to)
462 ('channel (concat to " <" from "> "))
463 ('nick (concat "[" from " -> " to "] "))
465 (error "Unsupported context type")))
468 (defun lurk-display-action (from to action-text)
469 (let ((context (if (eq 'channel (lurk-get-context-type to))
471 (if (equal to lurk-nick) from to))))
474 "* " from " " action-text)))
477 (defun lurk-display-notice (context &rest notices)
480 lurk-notice-prefix " "
481 (apply #'concat notices)))
483 (defun lurk-display-error (&rest messages)
486 lurk-error-prefix " "
487 (apply #'concat messages)))
489 (defun lurk-highlight-context (context)
491 (lambda (this-context facelist)
492 (if (equal this-context context)
493 (setcar facelist 'lurk-text)
494 (setcar facelist 'lurk-faded)))
495 lurk-context-facelists)
496 (force-window-update "*lurk*"))
498 (defun lurk-zoom-in (context)
499 (with-current-buffer "*lurk*"
501 (lambda (this-context _)
503 (let ((this-context-atom (intern this-context)))
504 (if (equal this-context context)
505 (remove-from-invisibility-spec this-context-atom)
506 (add-to-invisibility-spec this-context-atom)))))
507 lurk-context-facelists)
508 (force-window-update "*lurk*")))
510 (defun lurk-zoom-out ()
511 (with-current-buffer "*lurk*"
513 (lambda (this-context _)
514 (let ((this-context-atom (if this-context (intern this-context) nil)))
515 (remove-from-invisibility-spec this-context-atom)))
516 lurk-context-facelists)
517 (force-window-update "*lurk*")))
519 ;;; Message evaluation
522 (defun lurk-eval-msg-string (string)
524 (lurk-display-string nil string))
525 (let* ((msg (lurk-string->msg string)))
526 (pcase (lurk-msg-cmd msg)
529 (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
530 ;; (lurk-display-notice nil "ping-pong (server initiated)"))
533 ;; (lurk-display-notice nil "ping-pong (client initiated)"))
536 (let* ((params (lurk-msg-params msg))
537 (nick (elt params 0))
538 (text (string-join (seq-drop params 1) " ")))
539 (setq lurk-nick nick)
540 (lurk-display-notice nil text)))
543 (let* ((params (lurk-msg-params msg))
544 (channel (elt params 2))
545 (names (split-string (elt params 3))))
546 (lurk-add-context-users channel names)))
549 (let* ((params (lurk-msg-params msg))
550 (channel (elt params 1)))
553 (lurk--as-string (length (lurk-get-context-users channel)))
554 " users in " channel)))
557 (let* ((params (lurk-msg-params msg))
558 (channel (elt params 1)))
564 (let* ((params (lurk-msg-params msg))
565 (channel (elt params 1))
566 (topic (elt params 2)))
567 (lurk-display-notice channel "Topic: " topic)))
569 ("333") ; Avoid displaying these
571 ((rx (= 3 (any digit)))
572 (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
575 (guard (equal lurk-nick (lurk-msg-src msg))))
576 (let ((channel (car (lurk-msg-params msg))))
577 (lurk-add-context channel)
578 (lurk-set-current-context channel)
579 (lurk-display-notice channel "Joining channel " channel)
580 (lurk-render-prompt)))
583 (let ((channel (car (lurk-msg-params msg)))
584 (nick (lurk-msg-src msg)))
585 (lurk-add-context-users channel (list nick))
587 (lurk-display-notice channel nick " joined channel " channel))))
590 (guard (equal lurk-nick (lurk-msg-src msg))))
591 (let ((channel (car (lurk-msg-params msg))))
592 (lurk-display-notice channel "Left channel " channel)
593 (lurk-del-context channel)
594 (if (equal channel lurk-current-context)
595 (lurk-set-current-context (lurk-get-next-context)))
596 (lurk-render-prompt)))
599 (let ((channel (car (lurk-msg-params msg)))
600 (nick (lurk-msg-src msg)))
601 (lurk-del-context-user channel nick)
603 (lurk-display-notice channel nick " left channel " channel))))
606 (let ((nick (lurk-msg-src msg))
607 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
610 (lurk-display-notice nil nick " quit: " reason))))
613 (guard (equal lurk-nick (lurk-msg-src msg))))
614 (setq lurk-nick (car (lurk-msg-params msg)))
615 (lurk-display-notice nil "Set nick to " lurk-nick))
618 (let ((old-nick (lurk-msg-src msg))
619 (new-nick (car (lurk-msg-params msg))))
620 (lurk-display-notice nil old-nick " is now known as " new-nick)
621 (lurk-rename-user old-nick new-nick)))
624 (let ((nick (lurk-msg-src msg))
625 (channel (car (lurk-msg-params msg)))
626 (text (cadr (lurk-msg-params msg))))
628 ((rx (: "\01VERSION "
629 (let version (* (not "\01")))
631 (lurk-display-notice nil "CTCP version reply from " nick ": " version))
633 (lurk-display-notice nil text)))))
636 (let* ((from (lurk-msg-src msg))
637 (params (lurk-msg-params msg))
639 (text (cadr params)))
642 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
643 (lurk-send-msg (lurk-msg nil nil "NOTICE"
644 (list from (concat "\01VERSION "
647 (lurk-display-notice nil "CTCP version request received from " from))
649 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
650 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
651 (lurk-display-notice from "CTCP ping received from " from))
654 (lurk-display-notice from "CTCP userinfo request from " from " (no response sent)"))
656 ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
657 (lurk-display-action from to action-text))
660 (lurk-display-message from to text)))))
662 (lurk-display-notice nil (lurk-msg->string msg))))))
668 (defun lurk-enter-string (string)
669 (if (string-prefix-p "/" string)
670 (pcase (substring string 1)
672 (setq lurk-debug (not lurk-debug))
673 (lurk-display-notice nil "Debug mode now " (if lurk-debug "on" "off") "."))
675 ((rx (: "CONNECT " (let network (* not-newline))))
676 (lurk-display-notice nil "Attempting to connect to " network "...")
677 (lurk-connect network))
679 ((rx (: "TOPIC " (let new-topic (* not-newline))))
680 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
682 ((rx (: "ME " (let action (* not-newline))))
683 (let ((ctcp-text (concat "\01ACTION " action "\01")))
684 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
685 (list lurk-current-context ctcp-text)))
686 (lurk-display-action lurk-nick lurk-current-context action)))
688 ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
689 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
690 (list nick "\01VERSION\01")))
691 (lurk-display-notice nil "CTCP version request sent to " nick))
693 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
694 (if (or lurk-current-context channel)
695 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
697 lurk-current-context)))
698 (lurk-display-error "No current channel to leave.")))
700 ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
701 (lurk-send-msg (lurk-msg nil nil "QUIT"
702 (or quit-msg lurk-default-quit-msg))))
704 ((rx (: "NICK" (* whitespace) string-end))
705 (lurk-display-notice nil "Current nick: " lurk-nick))
707 ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
708 (if (lurk-connected-p)
709 (lurk-send-msg (lurk-msg nil nil "NICK" nick))
710 (setq lurk-nick nick)
711 (lurk-display-notice nil "Set default nick to '" nick "'")))
714 (lurk-display-notice nil "This command can generate lots of output. Use `LIST -yes' if you're sure."))
716 ((rx (: "LIST" (+ whitespace) "-YES"))
717 (lurk-send-msg (lurk-msg nil nil "LIST")))
720 (let to (* (not whitespace)))
722 (let text (* not-newline)))
723 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
724 (lurk-display-message lurk-nick to text))
726 ((rx (: (let cmd-str (+ (not whitespace)))
727 (opt (: " " (let params-str (* not-newline))))))
728 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
730 (split-string params-str)
733 (unless (string-empty-p string)
734 (if lurk-current-context
736 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
739 (lurk-display-message lurk-nick lurk-current-context string))
740 (lurk-display-error "No current context.")))))
743 "Enter current contents of line after prompt."
745 (with-current-buffer "*lurk*"
746 (let ((line (buffer-substring lurk-input-marker (point-max))))
747 (let ((inhibit-read-only t))
748 (delete-region lurk-input-marker (point-max)))
749 (lurk-enter-string line))))
752 ;;; Command completion
755 ;;; Interactive functions
758 (defun lurk-cycle-contexts-forward ()
760 (lurk-cycle-contexts))
762 (defun lurk-cycle-contexts-reverse ()
764 (lurk-cycle-contexts t))
766 (defvar lurk-zoomed nil
767 "Keeps track of zoom status.")
769 (defun lurk-toggle-zoom ()
773 (lurk-zoom-in lurk-current-context))
774 (setq lurk-zoomed (not lurk-zoomed)))
779 (defvar lurk-mode-map
780 (let ((map (make-sparse-keymap)))
781 (define-key map (kbd "RET") 'lurk-enter)
782 (define-key map (kbd "<tab>") 'lurk-complete)
783 (define-key map (kbd "C-c C-z") 'lurk-toggle-zoom)
784 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
785 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
788 (define-derived-mode lurk-mode text-mode "lurk"
789 "Major mode for LURK.")
791 (when (fboundp 'evil-set-initial-state)
792 (evil-set-initial-state 'lurk-mode 'insert))
794 ;;; Main start procedure
798 "Switch to *lurk* buffer."
800 (if (get-buffer "*lurk*")
801 (switch-to-buffer "*lurk*")
802 (switch-to-buffer "*lurk*")
808 ;;; lurk.el ends here