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 Unified 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))
100 (defvar lurk-response "")
102 (defun lurk-filter (proc string)
103 (dolist (line (split-string (concat lurk-response string) "\n"))
104 (if (string-suffix-p "\r" line)
105 (lurk-eval-msg-string (string-trim line))
106 (setq lurk-response line))))
108 (defun lurk-sentinel (proc string)
109 (unless (equal "open" (string-trim string))
110 (lurk-display-error "Disconnected from server.")
111 (clrhash lurk-contexts)
112 (lurk-set-current-context nil)
114 (cancel-timer lurk-ping-timer)))
116 (defun lurk-start-process (network)
117 (let* ((row (assoc network lurk-networks))
120 (flags (seq-drop row 3)))
121 (make-network-process :name "lurk"
124 :family (if lurk-allow-ipv6 nil 'ipv4)
125 :filter #'lurk-filter
126 :sentinel #'lurk-sentinel
128 :tls-parameters (if (memq :notls flags)
130 (cons 'gnutls-x509pki
131 (gnutls-boot-parameters
132 :type 'gnutls-x509pki
136 (defvar lurk-ping-timer nil)
137 (defvar lurk-ping-period 60)
139 (defun lurk-ping-function ()
140 (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
141 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
143 (defun lurk-connect (network)
144 (if (get-process "lurk")
145 (lurk-display-error "Already connected. Disconnect first.")
146 (if (not (assoc network lurk-networks))
147 (lurk-display-error "Network '" network "' is unknown.")
148 (clrhash lurk-contexts)
149 (lurk-set-current-context nil)
150 (lurk-start-process network)
151 (lurk-send-msg (lurk-msg nil nil "USER" lurk-nick 0 "*" lurk-nick))
152 (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
153 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
155 (defun lurk-connected-p ()
156 (let ((proc (get-process "lurk")))
157 (and proc (eq (process-status proc) 'open))))
159 (defun lurk-send-msg (msg)
160 (let ((proc (get-process "lurk")))
161 (if (and proc (eq (process-status proc) 'open))
162 (process-send-string proc (concat (lurk-msg->string msg) "\r\n"))
163 (lurk-display-error "No server connection established.")
164 (error "No server connection established"))))
170 (defun lurk--as-string (obj)
172 (with-output-to-string (princ obj))
175 (defun lurk-msg (tags src cmd &rest params)
176 (list (lurk--as-string tags)
177 (lurk--as-string src)
178 (upcase (lurk--as-string cmd))
179 (mapcar #'lurk--as-string
180 (if (and params (listp (elt params 0)))
184 (defun lurk-msg-tags (msg) (elt msg 0))
185 (defun lurk-msg-src (msg) (elt msg 1))
186 (defun lurk-msg-cmd (msg) (elt msg 2))
187 (defun lurk-msg-params (msg) (elt msg 3))
188 (defun lurk-msg-trail (msg)
189 (let ((params (lurk-msg-params msg)))
191 (elt params (- (length params) 1)))))
193 (defvar lurk-msg-regex
195 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
197 (opt (: ":" (: (group (* (not (any space "!" "@"))))
198 (* (not (any space)))))
200 (group (: (* (not whitespace))))
202 (opt (group (+ not-newline))))
203 "Regex used to parse IRC messages.
204 Note that this regex is incomplete. Noteably, we discard the non-nick
205 portion of the source component of the message, as LURK doesn't use this.")
207 (defun lurk-string->msg (string)
208 (if (string-match lurk-msg-regex string)
209 (let* ((tags (match-string 1 string))
210 (src (match-string 2 string))
211 (cmd (upcase (match-string 3 string)))
212 (params-str (match-string 4 string))
215 (let* ((idx (cl-search ":" params-str))
216 (l (split-string (string-trim (substring params-str 0 idx))))
217 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
220 (apply #'lurk-msg (append (list tags src cmd) params)))
221 (error "Failed to parse string " string)))
223 (defun lurk--filtered-join (&rest args)
224 (string-join (seq-filter (lambda (el) el) args) " "))
226 (defun lurk-msg->string (msg)
227 (let ((tags (lurk-msg-tags msg))
228 (src (lurk-msg-src msg))
229 (cmd (lurk-msg-cmd msg))
230 (params (lurk-msg-params msg)))
232 (if tags (concat "@" tags) nil)
233 (if src (concat ":" src) nil)
235 (if (> (length params) 1)
236 (string-join (seq-take params (- (length params) 1)) " ")
238 (if (> (length params) 0)
239 (concat ":" (elt params (- (length params) 1)))
246 (defvar lurk-current-context nil)
247 (defvar lurk-contexts (make-hash-table :test #'equal))
249 (defun lurk-add-context (name)
250 (puthash name nil lurk-contexts))
252 (defun lurk-del-context (name)
253 (remhash name lurk-contexts))
255 (defun lurk-get-context-users (name)
256 (gethash name lurk-contexts))
258 (defun lurk-add-context-users (context users)
261 (gethash context lurk-contexts))
264 (defun lurk-del-context-user (context user)
266 (remove user (gethash context lurk-contexts))
269 (defun lurk-del-user (user)
270 (dolist (context (lurk-get-context-list))
271 (lurk-del-context-user context user)))
273 (defun lurk-rename-user (old-nick new-nick)
274 (dolist (context (lurk-get-context-list))
275 (lurk-del-context-user context old-nick)
276 (lurk-add-context-users context (list new-nick))))
278 (defun lurk-get-context-type (name)
280 ((string-prefix-p "#" name) 'channel)
281 ((string-match-p (rx (or "." "localhost")) name) 'host)
284 (defun lurk-get-context-list ()
286 (maphash (lambda (key val)
287 (cl-pushnew key res))
291 (defun lurk-get-next-context (&optional prev)
292 (if lurk-current-context
293 (let* ((context-list (if prev
294 (reverse (lurk-get-context-list))
295 (lurk-get-context-list)))
296 (context-list* (member lurk-current-context context-list)))
297 (if (> (length context-list*) 1)
302 (defun lurk-set-current-context (context)
303 (setq lurk-current-context context)
304 (lurk-highlight-context context))
306 (defun lurk-cycle-contexts (&optional rev)
307 (if lurk-current-context
309 (lurk-set-current-context (lurk-get-next-context rev))
310 (lurk-render-prompt))
311 (lurk-display-error "No channels joined.")))
317 (defun lurk-render-prompt ()
318 (with-current-buffer "*lurk*"
319 (let ((update-point (= lurk-input-marker (point)))
320 (update-window-points (mapcar (lambda (w)
321 (list (= (window-point w) lurk-input-marker)
323 (get-buffer-window-list nil nil t))))
325 (set-marker-insertion-type lurk-prompt-marker nil)
326 (set-marker-insertion-type lurk-input-marker t)
327 (let ((inhibit-read-only t))
328 (delete-region lurk-prompt-marker lurk-input-marker)
329 (goto-char lurk-prompt-marker)
331 (propertize (if lurk-current-context
336 (propertize lurk-prompt-string
340 (set-marker-insertion-type lurk-input-marker nil))
342 (goto-char lurk-input-marker))
343 (dolist (v update-window-points)
345 (set-window-point (cadr v) lurk-input-marker))))))
347 (defvar lurk-prompt-marker nil
348 "Marker for prompt position in LURK buffer.")
350 (defvar lurk-input-marker nil
351 "Marker for prompt position in LURK buffer.")
353 (defun lurk-setup-buffer ()
354 (with-current-buffer (get-buffer-create "*lurk*")
355 (setq-local scroll-conservatively 1)
356 (setq-local buffer-invisibility-spec nil)
357 (if (markerp lurk-prompt-marker)
358 (set-marker lurk-prompt-marker (point-max))
359 (setq lurk-prompt-marker (point-max-marker)))
360 (if (markerp lurk-input-marker)
361 (set-marker lurk-input-marker (point-max))
362 (setq lurk-input-marker (point-max-marker)))
363 (goto-char (point-max))
364 (lurk-render-prompt)))
367 ;;; Output formatting and highlighting
370 ;; Partially-implemented idea: the face text property can be
371 ;; a list of faces, applied in order. By assigning each context
372 ;; a unique list and keeping track of these in a hash table, we can
373 ;; easily switch the face corresponding to a particular context
374 ;; by modifying the elements of this list.
376 ;; More subtly, we make only the cdrs of this list shared among
377 ;; all text of a given context, allowing the cars to be different
378 ;; and for different elements of the context-specific text to have
379 ;; different styling.
381 ;; Additionally, we can allow selective hiding of contexts via
382 ;; the buffer-invisibility-spec.
384 (defvar lurk-context-facelists (make-hash-table :test 'equal)
385 "List of seen contexts and associated face lists.")
387 (defun lurk-get-context-facelist (context)
388 (let ((facelist (gethash context lurk-context-facelists)))
390 (setq facelist (list 'lurk-text))
391 (puthash context facelist lurk-context-facelists))
394 (defun lurk-display-string (context &rest strings)
395 (with-current-buffer (get-buffer-create "*lurk*")
397 (goto-char lurk-prompt-marker)
398 (let ((inhibit-read-only t)
399 (old-pos (marker-position lurk-prompt-marker))
400 (adaptive-fill-regexp (rx (= 6 anychar)))
402 (context-atom (if context (intern context) nil)))
403 (insert-before-markers
404 (propertize (concat (format-time-string "%H:%M") " ")
405 'face (lurk-get-context-facelist context)
408 'invisible context-atom
409 'help-echo (concat "Context: " (or context "none")))
410 (propertize (concat (apply #'concat strings) "\n")
411 'face (lurk-get-context-facelist context)
414 'invisible context-atom
415 'help-echo (concat "Context: " (or context "none"))))
416 (fill-region old-pos lurk-prompt-marker nil t)))))
418 (defun lurk-display-message (from to text)
419 (let ((context (if (eq 'channel (lurk-get-context-type to))
421 (if (equal to lurk-nick) from to))))
424 (pcase (lurk-get-context-type to)
425 ('channel (concat to " <" from "> "))
426 ('nick (concat "[" from " -> " to "] "))
428 (error "Unsupported context type")))
431 (defun lurk-display-action (from to action-text)
432 (let ((context (if (eq 'channel (lurk-get-context-type to))
434 (if (equal to lurk-nick) from to))))
437 "* " from " " action-text)))
440 (defun lurk-display-notice (context &rest notices)
443 lurk-notice-prefix " "
444 (apply #'concat notices)))
446 (defun lurk-display-error (&rest messages)
449 lurk-error-prefix " "
450 (apply #'concat messages)))
452 (defun lurk-highlight-context (context)
454 (lambda (this-context facelist)
455 (if (equal this-context context)
456 (setcar facelist 'lurk-text)
457 (setcar facelist 'lurk-faded)))
458 lurk-context-facelists)
459 (force-window-update "*lurk*"))
461 (defun lurk-zoom-in (context)
462 (with-current-buffer "*lurk*"
464 (lambda (this-context _)
466 (let ((this-context-atom (intern this-context)))
467 (if (equal this-context context)
468 (remove-from-invisibility-spec this-context-atom)
469 (add-to-invisibility-spec this-context-atom)))))
470 lurk-context-facelists)
471 (force-window-update "*lurk*")))
473 (defun lurk-zoom-out ()
474 (with-current-buffer "*lurk*"
476 (lambda (this-context _)
477 (let ((this-context-atom (if this-context (intern this-context) nil)))
478 (remove-from-invisibility-spec this-context-atom)))
479 lurk-context-facelists)
480 (force-window-update "*lurk*")))
482 ;;; Message evaluation
485 (defun lurk-eval-msg-string (string)
486 ;; (lurk-display-string nil string)
487 (let* ((msg (lurk-string->msg string)))
488 (pcase (lurk-msg-cmd msg)
491 (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
492 ;; (lurk-display-notice nil "ping-pong (server initiated)"))
495 ;; (lurk-display-notice nil "ping-pong (client initiated)"))
498 (let* ((params (lurk-msg-params msg))
499 (nick (elt params 0))
500 (text (string-join (seq-drop params 1) " ")))
501 (setq lurk-nick nick)
502 (lurk-display-notice nil text)))
505 (let* ((params (lurk-msg-params msg))
506 (channel (elt params 2))
507 (names (split-string (elt params 3))))
508 (lurk-add-context-users channel names)))
511 (let* ((params (lurk-msg-params msg))
512 (channel (elt params 1)))
515 (lurk--as-string (length (lurk-get-context-users channel)))
516 " users in " channel)))
519 (let* ((params (lurk-msg-params msg))
520 (channel (elt params 1)))
526 (let* ((params (lurk-msg-params msg))
527 (channel (elt params 1))
528 (topic (elt params 2)))
529 (lurk-display-notice channel "Topic: " topic)))
531 ((rx (= 3 (any digit)))
532 (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
535 (guard (equal lurk-nick (lurk-msg-src msg))))
536 (let ((channel (car (lurk-msg-params msg))))
537 (lurk-add-context channel)
538 (lurk-set-current-context channel)
539 (lurk-display-notice channel "Joining channel " channel)
540 (lurk-render-prompt)))
543 (let ((channel (car (lurk-msg-params msg)))
544 (nick (lurk-msg-src msg)))
545 (lurk-add-context-users channel (list nick))
547 (lurk-display-notice channel nick " joined channel " channel))))
550 (guard (equal lurk-nick (lurk-msg-src msg))))
551 (let ((channel (car (lurk-msg-params msg))))
552 (lurk-display-notice channel "Left channel " channel)
553 (lurk-del-context channel)
554 (if (equal channel lurk-current-context)
555 (lurk-set-current-context (lurk-get-next-context)))
556 (lurk-render-prompt)))
559 (let ((channel (car (lurk-msg-params msg)))
560 (nick (lurk-msg-src msg)))
561 (lurk-del-context-user channel nick)
563 (lurk-display-notice channel nick " left channel " channel))))
566 (let ((nick (lurk-msg-src msg))
567 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
570 (lurk-display-notice nil nick " quit: " reason))))
573 (guard (equal lurk-nick (lurk-msg-src msg))))
574 (setq lurk-nick (car (lurk-msg-params msg)))
575 (lurk-display-notice nil "Set nick to " lurk-nick))
578 (let ((old-nick (lurk-msg-src msg))
579 (new-nick (car (lurk-msg-params msg))))
580 (lurk-display-notice nil old-nick " is now known as " new-nick)
581 (lurk-rename-user old-nick new-nick)))
584 (let ((nick (lurk-msg-src msg))
585 (channel (car (lurk-msg-params msg)))
586 (text (cadr (lurk-msg-params msg))))
588 ((rx (: "\01VERSION "
589 (let version (* (not "\01")))
591 (lurk-display-notice nil "CTCP version reply from " nick ": " version))
593 (lurk-display-notice nil text)))))
596 (let* ((from (lurk-msg-src msg))
597 (params (lurk-msg-params msg))
599 (text (cadr params)))
602 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
603 (lurk-send-msg (lurk-msg nil nil "NOTICE"
604 (list from (concat "\01VERSION "
607 (lurk-display-notice nil "CTCP version request received from " from))
609 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
610 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
611 (lurk-display-notice from "CTCP ping received from " from))
614 (lurk-display-notice from "CTCP userinfo request from " from " (no response sent)"))
616 ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
617 (lurk-display-action from to action-text))
620 (if (and (equal from "BitBot")
621 (equal to "##moshpit")
622 (cl-search "\\_o< QUACK!" text))
623 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to ",bef")))
624 (lurk-display-message from to text)))))
626 (lurk-display-notice nil (lurk-msg->string msg))))))
632 (defun lurk-enter-string (string)
633 (if (string-prefix-p "/" string)
634 (pcase (substring string 1)
635 ((rx (: "CONNECT " (let network (* not-newline))))
636 (lurk-display-notice nil "Attempting to connect to " network "...")
637 (lurk-connect network))
639 ((rx (: "TOPIC " (let new-topic (* not-newline))))
640 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
642 ((rx (: "ME " (let action (* not-newline))))
643 (let ((ctcp-text (concat "\01ACTION " action "\01")))
644 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
645 (list lurk-current-context ctcp-text)))
646 (lurk-display-action lurk-nick lurk-current-context action)))
648 ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
649 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
650 (list nick "\01VERSION\01")))
651 (lurk-display-notice nil "CTCP version request sent to " nick))
653 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
654 (if (or lurk-current-context channel)
655 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
657 lurk-current-context)))
658 (lurk-display-error "No current channel to leave.")))
660 ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
661 (lurk-send-msg (lurk-msg nil nil "QUIT"
662 (or quit-msg lurk-default-quit-msg))))
664 ((rx (: "NICK" (* whitespace) string-end))
665 (lurk-display-notice nil "Current nick: " lurk-nick))
667 ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
668 (if (lurk-connected-p)
669 (lurk-send-msg (lurk-msg nil nil "NICK" nick))
670 (setq lurk-nick nick)
671 (lurk-display-notice nil "Set default nick to '" nick "'")))
674 (let to (* (not whitespace)))
676 (let text (* not-newline)))
677 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
678 (lurk-display-message lurk-nick to text))
680 ((rx (: (let cmd-str (+ (not whitespace)))
681 (opt (: " " (let params-str (* not-newline))))))
682 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
684 (split-string params-str)
687 (unless (string-empty-p string)
688 (if lurk-current-context
690 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
693 (lurk-display-message lurk-nick lurk-current-context string))
694 (lurk-display-error "No current context.")))))
697 "Enter current contents of line after prompt."
699 (with-current-buffer "*lurk*"
700 (let ((line (buffer-substring lurk-input-marker (point-max))))
701 (let ((inhibit-read-only t))
702 (delete-region lurk-input-marker (point-max)))
703 (lurk-enter-string line))))
706 ;;; Command completion
709 ;;; Interactive functions
712 (defun lurk-cycle-contexts-forward ()
714 (lurk-cycle-contexts))
716 (defun lurk-cycle-contexts-reverse ()
718 (lurk-cycle-contexts t))
720 (defvar lurk-zoomed nil
721 "Keeps track of zoom status.")
723 (defun lurk-toggle-zoom ()
727 (lurk-zoom-in lurk-current-context))
728 (setq lurk-zoomed (not lurk-zoomed)))
733 (defvar lurk-mode-map
734 (let ((map (make-sparse-keymap)))
735 (define-key map (kbd "RET") 'lurk-enter)
736 (define-key map (kbd "<tab>") 'lurk-complete)
737 (define-key map (kbd "C-c C-z") 'lurk-toggle-zoom)
738 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
739 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
742 (define-derived-mode lurk-mode text-mode "lurk"
743 "Major mode for LURK.")
745 (when (fboundp 'evil-set-initial-state)
746 (evil-set-initial-state 'lurk-mode 'insert))
748 ;;; Main start procedure
752 "Switch to *lurk* buffer."
754 (if (get-buffer "*lurk*")
755 (switch-to-buffer "*lurk*")
756 (switch-to-buffer "*lurk*"))
762 ;;; lurk.el ends here