1 ;;; lurk.el --- Little Unified 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 font-lock-preprocessor-face))
66 "Face used for Lurk text.")
69 '((t :inherit org-level-2))
70 "Face used for the prompt.")
73 '((t :inherit org-list-dt))
74 "Face used for the context name in the prompt.")
77 '((t :inherit font-lock-preprocessor-face))
78 "Face used for faded Lurk text.")
81 '((t :inherit font-lock-function-name-face))
82 "Face used for bold Lurk text.")
85 '((t :inherit font-lock-regexp-grouping-construct))
86 "Face used for Lurk error text.")
91 (defvar lurk-version "Lurk v0.1")
93 (defvar lurk-notice-prefix "-!-")
95 (defvar lurk-error-prefix "!!!")
97 (defvar lurk-prompt-string
98 (propertize "> " 'face 'lurk-prompt))
104 (defvar lurk-response "")
106 (defun lurk-filter (proc string)
107 (dolist (line (split-string (concat lurk-response string) "\n"))
108 (if (string-suffix-p "\r" line)
109 (lurk-eval-msg-string (string-trim line))
110 (setq lurk-response line))))
112 (defun lurk-sentinel (proc string)
113 (unless (equal "open" (string-trim string))
114 (lurk-display-error "Disconnected from server.")
115 (clrhash lurk-contexts)
116 (setq lurk-current-context nil)
118 (cancel-timer lurk-ping-timer)))
120 (defun lurk-start-process (network)
121 (let* ((row (assoc network lurk-networks))
124 (flags (seq-drop row 3)))
125 (make-network-process :name "lurk"
128 :family (if lurk-allow-ipv6 nil 'ipv4)
129 :filter #'lurk-filter
130 :sentinel #'lurk-sentinel
132 :tls-parameters (if (memq :notls flags)
134 (cons 'gnutls-x509pki
135 (gnutls-boot-parameters
136 :type 'gnutls-x509pki
140 (defvar lurk-ping-timer nil)
141 (defvar lurk-ping-period 60)
143 (defun lurk-ping-function ()
144 (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
145 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
147 (defun lurk-connect (network)
148 (if (get-process "lurk")
149 (lurk-display-error "Already connected. Disconnect first.")
150 (if (not (assoc network lurk-networks))
151 (lurk-display-error "Network '" network "' is unknown.")
152 (clrhash lurk-contexts)
153 (setq lurk-current-context nil)
154 (lurk-start-process network)
155 (lurk-send-msg (lurk-msg nil nil "USER" lurk-nick 0 "*" lurk-nick))
156 (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
157 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
159 (defun lurk-connected-p ()
160 (let ((proc (get-process "lurk")))
161 (and proc (eq (process-status proc) 'open))))
163 (defun lurk-send-msg (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-cycle-contexts (&optional rev)
307 (if lurk-current-context
309 (setq lurk-current-context (lurk-get-next-context rev))
310 (lurk-render-prompt))
311 (lurk-display-error "No channels joined.")))
313 (defun lurk-cycle-contexts-forward ()
315 (lurk-cycle-contexts)
316 (lurk-highlight-context lurk-current-context))
318 (defun lurk-cycle-contexts-reverse ()
320 (lurk-cycle-contexts t)
321 (lurk-highlight-context lurk-current-context))
326 (defun lurk-render-prompt ()
327 (with-current-buffer "*lurk*"
328 (let ((update-point (= lurk-input-marker (point)))
329 (update-window-points (mapcar (lambda (w)
330 (list (= (window-point w) lurk-input-marker)
332 (get-buffer-window-list nil nil t))))
334 (set-marker-insertion-type lurk-prompt-marker nil)
335 (set-marker-insertion-type lurk-input-marker t)
336 (let ((inhibit-read-only t))
337 (delete-region lurk-prompt-marker lurk-input-marker)
338 (goto-char lurk-prompt-marker)
340 (propertize (if lurk-current-context
345 (propertize lurk-prompt-string
349 (set-marker-insertion-type lurk-input-marker nil))
351 (goto-char lurk-input-marker))
352 (dolist (v update-window-points)
354 (set-window-point (cadr v) lurk-input-marker))))))
356 (defvar lurk-prompt-marker nil
357 "Marker for prompt position in LURK buffer.")
359 (defvar lurk-input-marker nil
360 "Marker for prompt position in LURK buffer.")
362 (defun lurk-setup-buffer ()
363 (with-current-buffer (get-buffer-create "*lurk*")
364 (setq-local scroll-conservatively 1)
365 (if (markerp lurk-prompt-marker)
366 (set-marker lurk-prompt-marker (point-max))
367 (setq lurk-prompt-marker (point-max-marker)))
368 (if (markerp lurk-input-marker)
369 (set-marker lurk-input-marker (point-max))
370 (setq lurk-input-marker (point-max-marker)))
371 (goto-char (point-max))
372 (lurk-render-prompt)))
375 ;;; Output formatting and highlighting
378 ;; Partially-implemented idea: the face text property can be
379 ;; a list of faces, applied in order. By assigning each context
380 ;; a unique list and keeping track of these in a hash table, we can
381 ;; easily switch the face corresponding to a particular context
382 ;; by modifying the elements of this list.
384 ;; More subtly, we make only the cdrs of this list shared among
385 ;; all text of a given context, allowing the cars to be different
386 ;; and for different elements of the context-specific text to have
387 ;; different styling.
389 ;; Additionally, we can allow selective hiding of contexts via
390 ;; the buffer-invisibility-spec.
392 (defvar lurk-context-facelists (make-hash-table :test 'equal)
393 "List of seen contexts and associated face lists.")
395 (defun lurk-get-context-facelist (context)
396 (let ((facelist (gethash context lurk-context-facelists)))
398 (setq facelist (list 'lurk-text))
399 (puthash context facelist lurk-context-facelists))
402 (defun lurk-display-string (context &rest strings)
403 (with-current-buffer (get-buffer-create "*lurk*")
405 (goto-char lurk-prompt-marker)
406 (let ((inhibit-read-only t)
407 (old-pos (marker-position lurk-prompt-marker))
408 (adaptive-fill-regexp (rx (= 6 anychar)))
410 (context-atom (if context (intern context) nil)))
411 (insert-before-markers
412 (propertize (concat (format-time-string "%H:%M") " ")
413 'face (lurk-get-context-facelist context)
416 'invisible context-atom
417 'help-echo (concat "Context: " (or context "none")))
418 (propertize (concat (apply #'concat strings) "\n")
419 'face (lurk-get-context-facelist context)
422 'invisible context-atom
423 'help-echo (concat "Context: " (or context "none"))))
424 (fill-region old-pos lurk-prompt-marker nil t)))))
426 (defun lurk-display-message (from to text)
427 (let ((context (if (eq 'channel (lurk-get-context-type to))
429 (if (equal to lurk-nick) from to))))
432 (pcase (lurk-get-context-type to)
433 ('channel (concat to " <" from "> " text))
434 ('nick (concat "[" from " -> " to "] " text))
436 (error "Unsupported context type"))))))
438 (defun lurk-display-notice (context &rest notices)
441 lurk-notice-prefix " "
442 (apply #'concat notices)))
444 (defun lurk-display-error (&rest messages)
447 lurk-error-prefix " "
448 (apply #'concat messages)))
450 (defun lurk-highlight-context (context)
452 (lambda (this-context facelist)
453 (if (equal this-context context)
454 (setcar facelist 'lurk-bold)
455 (setcar facelist 'lurk-faded)))
456 lurk-context-facelists)
457 (force-window-update "*lurk*"))
459 (defun lurk-zoom-in (context)
460 (with-current-buffer "*lurk*"
462 (lambda (this-context _)
463 (let ((this-context-atom (if this-context (intern this-context) nil)))
464 (if (equal this-context context)
465 (remove-from-invisibility-spec this-context-atom)
466 (add-to-invisibility-spec this-context-atom))))
467 lurk-context-facelists)
468 (force-window-update "*lurk*")))
470 (defun lurk-zoom-out ()
471 (with-current-buffer "*lurk*"
473 (lambda (this-context _)
474 (let ((this-context-atom (if this-context (intern this-context) nil)))
475 (remove-from-invisibility-spec this-context-atom)))
476 lurk-context-facelists)
477 (force-window-update "*lurk*")))
479 ;;; Message evaluation
482 (defun lurk-eval-msg-string (string)
483 ;; (lurk-display-string nil string)
484 (let* ((msg (lurk-string->msg string)))
485 ;; (message (pp msg))
486 (pcase (lurk-msg-cmd msg)
489 (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
490 ;; (lurk-display-notice nil "ping-pong (server initiated)"))
493 ;; (lurk-display-notice nil "ping-pong (client initiated)"))
496 (let* ((params (lurk-msg-params msg))
497 (nick (elt params 0))
498 (text (string-join (seq-drop params 1) " ")))
499 (setq lurk-nick nick)
500 (lurk-display-notice nil text)))
503 (let* ((params (lurk-msg-params msg))
504 (channel (elt params 2))
505 (names (split-string (elt params 3))))
506 (lurk-add-context-users channel names)))
509 (let* ((params (lurk-msg-params msg))
510 (channel (elt params 1)))
513 (lurk--as-string (length (lurk-get-context-users channel)))
514 " users in " channel)))
516 ((rx (= 3 (any digit)))
517 (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
520 (guard (equal lurk-nick (lurk-msg-src msg))))
521 (let ((channel (car (lurk-msg-params msg))))
522 (lurk-add-context channel)
523 (setq lurk-current-context channel)
524 (lurk-display-notice channel "Joining channel " channel)
525 (lurk-render-prompt)))
528 (let ((channel (car (lurk-msg-params msg)))
529 (nick (lurk-msg-src msg)))
530 (lurk-add-context-users channel (list nick))
532 (lurk-display-notice channel nick " joined channel " channel))))
535 (guard (equal lurk-nick (lurk-msg-src msg))))
536 (let ((channel (car (lurk-msg-params msg))))
537 (lurk-display-notice channel "Left channel " channel)
538 (lurk-del-context channel)
539 (if (equal channel lurk-current-context)
540 (setq lurk-current-context (lurk-get-next-context)))
541 (lurk-render-prompt)))
544 (let ((channel (car (lurk-msg-params msg)))
545 (nick (lurk-msg-src msg)))
546 (lurk-del-context-user channel nick)
548 (lurk-display-notice channel nick " left channel " channel))))
551 (let ((nick (lurk-msg-src msg))
552 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
555 (lurk-display-notice nil nick " quit: " reason))))
558 (guard (equal lurk-nick (lurk-msg-src msg))))
559 (setq lurk-nick (car (lurk-msg-params msg)))
560 (lurk-display-notice nil "Set nick to " lurk-nick))
563 (let ((old-nick (lurk-msg-src msg))
564 (new-nick (car (lurk-msg-params msg))))
565 (lurk-display-notice nil old-nick " is now known as " new-nick)
566 (lurk-rename-user old-nick new-nick)))
569 (let ((nick (lurk-msg-src msg))
570 (channel (car (lurk-msg-params msg)))
571 (text (cadr (lurk-msg-params msg))))
573 ((rx (: "\01VERSION "
574 (let version (* (not "\01")))
576 (lurk-display-notice nil "CTCP version reply from " nick ": " version))
578 (lurk-display-notice nil text)))))
581 (let* ((from (lurk-msg-src msg))
582 (params (lurk-msg-params msg))
584 (text (cadr params)))
587 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
588 (lurk-send-msg (lurk-msg nil nil "NOTICE"
589 (list from (concat "\01VERSION "
592 (lurk-display-notice nil "CTCP version request received from " from))
594 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
595 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
596 (lurk-display-notice "CTCP ping received from " from))
599 (lurk-display-notice "CTCP userinfo request from " from " (no response sent)"))
602 (if (and (equal from "BitBot")
603 (equal to "##moshpit")
604 (cl-search "\\_o< QUACK!" text))
605 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to ",bef")))
606 (lurk-display-message from to text)))))
608 (lurk-display-notice nil (lurk-msg->string msg))))))
614 (defun lurk-enter-string (string)
615 (if (string-prefix-p "/" string)
616 (pcase (substring string 1)
617 ((rx (: "CONNECT " (let network (* not-newline))))
618 (lurk-display-notice nil "Attempting to connect to " network "...")
619 (lurk-connect network))
621 ((rx (: "TOPIC " (let new-topic (* not-newline))))
622 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
624 ((rx (: "ME " (let action (* not-newline))))
625 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
626 (list lurk-current-context
627 (concat "\01ACTION " action "\01"))))
628 (lurk-display-action lurk-nick action))
630 ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
631 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
632 (list nick "\01VERSION\01")))
633 (lurk-display-notice nil "CTCP version request sent to " nick))
635 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
636 (if (or lurk-current-context channel)
637 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
639 lurk-current-context)))
640 (lurk-display-error "No current channel to leave.")))
642 ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
643 (lurk-send-msg (lurk-msg nil nil "QUIT"
644 (or quit-msg lurk-default-quit-msg))))
646 ((rx (: "NICK" (* whitespace) string-end))
647 (lurk-display-notice nil "Current nick: " lurk-nick))
649 ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
650 (if (lurk-connected-p)
651 (lurk-send-msg (lurk-msg nil nil "NICK" nick))
652 (setq lurk-nick nick)
653 (lurk-display-notice nil "Set default nick to '" nick "'")))
656 (let to (* (not whitespace)))
658 (let text (* not-newline)))
659 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
660 (lurk-display-message lurk-nick to text))
662 ((rx (: (let cmd-str (+ (not whitespace)))
663 (opt (: " " (let params-str (* not-newline))))))
664 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
666 (split-string params-str)
669 (unless (string-empty-p string)
670 (if lurk-current-context
672 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
675 (lurk-display-message lurk-nick lurk-current-context string))
676 (lurk-display-error "No current context.")))))
679 "Enter current contents of line after prompt."
681 (with-current-buffer "*lurk*"
682 (let ((line (buffer-substring lurk-input-marker (point-max))))
683 (let ((inhibit-read-only t))
684 (delete-region lurk-input-marker (point-max)))
685 (lurk-enter-string line))))
691 (defvar lurk-mode-map
692 (let ((map (make-sparse-keymap)))
693 (define-key map (kbd "RET") 'lurk-enter)
694 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
695 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
698 (define-derived-mode lurk-mode text-mode "lurk"
699 "Major mode for LURK.")
701 (when (fboundp 'evil-set-initial-state)
702 (evil-set-initial-state 'lurk-mode 'insert))
704 ;;; Main start procedure
708 "Switch to *lurk* buffer."
710 (if (get-buffer "*lurk*")
711 (switch-to-buffer "*lurk*")
712 (switch-to-buffer "*lurk*"))
719 ;;; lurk.el ends here