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.")
68 (defface lurk-your-nick
69 '((t :inherit font-lock-constant-face))
70 "Face used for highlighting your nick.")
73 '((t :inherit org-level-2))
74 "Face used for the prompt.")
77 '((t :inherit org-list-dt))
78 "Face used for the context name in the prompt.")
81 '((t :inherit font-lock-preprocessor-face))
82 "Face used for faded Lurk text.")
85 '((t :inherit font-lock-function-name-face))
86 "Face used for bold Lurk text.")
89 '((t :inherit font-lock-regexp-grouping-construct))
90 "Face used for Lurk error text.")
95 (defvar lurk-version "Lurk v0.1")
97 (defvar lurk-notice-prefix
100 "-" 'face 'lurk-faded)
102 "!" 'face 'lurk-bold)
104 "-" 'face 'lurk-faded)))
106 (defvar lurk-error-prefix
107 (propertize "!!!" 'face 'lurk-error))
109 (defvar lurk-prompt-string
110 (propertize "> " 'face 'lurk-prompt))
116 (defvar lurk-response "")
118 (defun lurk-filter (proc string)
119 (dolist (line (split-string (concat lurk-response string) "\n"))
120 (if (string-suffix-p "\r" line)
121 (lurk-eval-msg-string (string-trim line))
122 (setq lurk-response line))))
124 (defun lurk-sentinel (proc string)
125 (unless (equal "open" (string-trim string))
126 (lurk-display-error "Disconnected from server.")
127 (clrhash lurk-contexts)
128 (setq lurk-current-context nil)
130 (cancel-timer lurk-ping-timer)))
132 (defun lurk-start-process (network)
133 (let* ((row (assoc network lurk-networks))
136 (flags (seq-drop row 3)))
137 (make-network-process :name "lurk"
140 :family (if lurk-allow-ipv6 nil 'ipv4)
141 :filter #'lurk-filter
142 :sentinel #'lurk-sentinel
144 :tls-parameters (if (memq :notls flags)
146 (cons 'gnutls-x509pki
147 (gnutls-boot-parameters
148 :type 'gnutls-x509pki
152 (defvar lurk-ping-timer nil)
153 (defvar lurk-ping-period 60)
155 (defun lurk-ping-function ()
156 (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
157 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
159 (defun lurk-connect (network)
160 (if (get-process "lurk")
161 (lurk-display-error "Already connected. Disconnect first.")
162 (if (not (assoc network lurk-networks))
163 (lurk-display-error "Network '" network "' is unknown.")
164 (clrhash lurk-contexts)
165 (setq lurk-current-context nil)
166 (lurk-start-process network)
167 (lurk-send-msg (lurk-msg nil nil "USER" lurk-nick 0 "*" lurk-nick))
168 (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
169 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
171 (defun lurk-connected-p ()
172 (let ((proc (get-process "lurk")))
173 (and proc (eq (process-status proc) 'open))))
175 (defun lurk-send-msg (msg)
176 (let ((proc (get-process "lurk")))
177 (if (and proc (eq (process-status proc) 'open))
178 (process-send-string proc (concat (lurk-msg->string msg) "\r\n"))
179 (lurk-display-error "No server connection established.")
180 (error "No server connection established"))))
186 (defun lurk--as-string (obj)
188 (with-output-to-string (princ obj))
191 (defun lurk-msg (tags src cmd &rest params)
192 (list (lurk--as-string tags)
193 (lurk--as-string src)
194 (upcase (lurk--as-string cmd))
195 (mapcar #'lurk--as-string
196 (if (and params (listp (elt params 0)))
200 (defun lurk-msg-tags (msg) (elt msg 0))
201 (defun lurk-msg-src (msg) (elt msg 1))
202 (defun lurk-msg-cmd (msg) (elt msg 2))
203 (defun lurk-msg-params (msg) (elt msg 3))
204 (defun lurk-msg-trail (msg)
205 (let ((params (lurk-msg-params msg)))
207 (elt params (- (length params) 1)))))
209 (defvar lurk-msg-regex
211 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
213 (opt (: ":" (: (group (* (not (any space "!" "@"))))
214 (* (not (any space)))))
216 (group (: (* (not whitespace))))
218 (opt (group (+ not-newline))))
219 "Regex used to parse IRC messages.
220 Note that this regex is incomplete. Noteably, we discard the non-nick
221 portion of the source component of the message, as LURK doesn't use this.")
223 (defun lurk-string->msg (string)
224 (if (string-match lurk-msg-regex string)
225 (let* ((tags (match-string 1 string))
226 (src (match-string 2 string))
227 (cmd (upcase (match-string 3 string)))
228 (params-str (match-string 4 string))
231 (let* ((idx (cl-search ":" params-str))
232 (l (split-string (string-trim (substring params-str 0 idx))))
233 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
236 (apply #'lurk-msg (append (list tags src cmd) params)))
237 (error "Failed to parse string " string)))
239 (defun lurk--filtered-join (&rest args)
240 (string-join (seq-filter (lambda (el) el) args) " "))
242 (defun lurk-msg->string (msg)
243 (let ((tags (lurk-msg-tags msg))
244 (src (lurk-msg-src msg))
245 (cmd (lurk-msg-cmd msg))
246 (params (lurk-msg-params msg)))
248 (if tags (concat "@" tags) nil)
249 (if src (concat ":" src) nil)
251 (if (> (length params) 1)
252 (string-join (seq-take params (- (length params) 1)) " ")
254 (if (> (length params) 0)
255 (concat ":" (elt params (- (length params) 1)))
262 (defvar lurk-current-context nil)
263 (defvar lurk-contexts (make-hash-table :test #'equal))
265 (defun lurk-add-context (name)
266 (puthash name nil lurk-contexts))
268 (defun lurk-del-context (name)
269 (remhash name lurk-contexts))
271 (defun lurk-get-context-users (name)
272 (gethash name lurk-contexts))
274 (defun lurk-add-context-users (context users)
277 (gethash context lurk-contexts))
280 (defun lurk-del-context-user (context user)
282 (remove user (gethash context lurk-contexts))
285 (defun lurk-del-user (user)
286 (dolist (context (lurk-get-context-list))
287 (lurk-del-context-user context user)))
289 (defun lurk-rename-user (old-nick new-nick)
290 (dolist (context (lurk-get-context-list))
291 (lurk-del-context-user context old-nick)
292 (lurk-add-context-users context (list new-nick))))
294 (defun lurk-get-context-type (name)
296 ((string-prefix-p "#" name) 'channel)
297 ((string-match-p (rx (or "." "localhost")) name) 'host)
300 (defun lurk-get-context-list ()
302 (maphash (lambda (key val)
303 (cl-pushnew key res))
307 (defun lurk-get-next-context (&optional prev)
308 (if lurk-current-context
309 (let* ((context-list (if prev
310 (reverse (lurk-get-context-list))
311 (lurk-get-context-list)))
312 (context-list* (member lurk-current-context context-list)))
313 (if (> (length context-list*) 1)
318 (defun lurk-cycle-contexts (&optional rev)
319 (if lurk-current-context
321 (setq lurk-current-context (lurk-get-next-context rev))
322 (lurk-render-prompt))
323 (lurk-display-error "No channels joined.")))
325 (defun lurk-cycle-contexts-forward ()
327 (lurk-cycle-contexts))
329 (defun lurk-cycle-contexts-reverse ()
331 (lurk-cycle-contexts t))
336 (defun lurk-render-prompt ()
337 (with-current-buffer "*lurk*"
338 (let ((update-point (= lurk-input-marker (point)))
339 (update-window-points (mapcar (lambda (w)
340 (list (= (window-point w) lurk-input-marker)
342 (get-buffer-window-list nil nil t))))
344 (set-marker-insertion-type lurk-prompt-marker nil)
345 (set-marker-insertion-type lurk-input-marker t)
346 (let ((inhibit-read-only t))
347 (delete-region lurk-prompt-marker lurk-input-marker)
348 (goto-char lurk-prompt-marker)
350 (propertize (if lurk-current-context
355 (propertize lurk-prompt-string
359 (set-marker-insertion-type lurk-input-marker nil))
361 (goto-char lurk-input-marker))
362 (dolist (v update-window-points)
364 (set-window-point (cadr v) lurk-input-marker))))))
366 (defvar lurk-prompt-marker nil
367 "Marker for prompt position in LURK buffer.")
369 (defvar lurk-input-marker nil
370 "Marker for prompt position in LURK buffer.")
372 (defun lurk-setup-buffer ()
373 (with-current-buffer (get-buffer-create "*lurk*")
374 (setq-local scroll-conservatively 1)
375 (if (markerp lurk-prompt-marker)
376 (set-marker lurk-prompt-marker (point-max))
377 (setq lurk-prompt-marker (point-max-marker)))
378 (if (markerp lurk-input-marker)
379 (set-marker lurk-input-marker (point-max))
380 (setq lurk-input-marker (point-max-marker)))
381 (goto-char (point-max))
382 (lurk-render-prompt)))
385 ;;; Output formatting
388 ;; Partially-implemented idea: the face text property can be
389 ;; a list of faces, applied in order. By assigning each context
390 ;; a unique list and keeping track of these in a hash table, we can
391 ;; easily switch the face corresponding to a particular context
392 ;; by modifying the elements of this list.
394 ;; More subtly, we make only the cdrs of this list shared among
395 ;; all text of a given context, allowing the cars to be different
396 ;; and for different elements of the context-specific text to have
397 ;; different styling.
399 (defvar lurk-context-facelists (make-hash-table :test 'equal)
400 "List of seen contexts and associated face lists.")
402 (defun lurk-get-context-facelist (context)
403 (let ((facelist (gethash context lurk-context-facelists)))
405 (setq facelist (list 'lurk-text))
406 (puthash context facelist lurk-context-facelists))
409 (defun lurk-display-string (&rest strings)
410 (with-current-buffer (get-buffer-create "*lurk*")
412 (goto-char lurk-prompt-marker)
413 (let ((inhibit-read-only t)
414 (old-pos (marker-position lurk-prompt-marker))
415 (adaptive-fill-regexp (rx (= 6 anychar)))
417 (insert-before-markers
418 (propertize (concat (format-time-string "%H:%M") " ")
421 (propertize (concat (apply #'concat strings) "\n")
423 (fill-region old-pos lurk-prompt-marker nil t)))))
425 (defun lurk-display-message (from to text)
426 (let ((context (if (eq 'channel (lurk-get-context-type to))
428 (if (equal to lurk-nick) from to))))
431 (pcase (lurk-get-context-type to)
432 ('channel (concat to " <" from "> " text))
433 ('nick (concat "[" from " -> " to "] " text))
435 (error "Unsupported context type")))
436 'face (lurk-get-context-facelist context)
437 'help-echo (concat "Context: " context)
440 (defun lurk-display-notice (context &rest notices)
443 (concat lurk-notice-prefix " " (apply #'concat notices))
444 'help-echo (concat "Context: " (or context "none"))
447 (defun lurk-display-error (&rest messages)
449 (concat lurk-error-prefix " "
450 (propertize (apply #'concat messages)
451 'face 'lurk-error))))
453 (defun lurk-highlight-context (context)
454 (with-current-buffer "*lurk*"
455 (let* ((pos lurk-prompt-marker)
456 (nextpos (previous-single-property-change pos 'context))
457 (inhibit-read-only t))
458 (while (> pos nextpos)
459 (let ((thiscontext (get-text-property nextpos 'context)))
461 (if (equal context thiscontext)
462 (add-text-properties nextpos pos
463 '(face (foreground-color . "green")))
464 (add-text-properties nextpos pos
465 '(face (foreground-color . "blue"))))
466 (add-text-properties nextpos pos
470 (setq nextpos (previous-single-property-change pos 'context nil 1)))))))
472 ;;; Message evaluation
475 (defun lurk-eval-msg-string (string)
476 ;; (lurk-display-string string)
477 (let* ((msg (lurk-string->msg string)))
478 ;; (message (pp msg))
479 (pcase (lurk-msg-cmd msg)
482 (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
483 ;; (lurk-display-notice nil "ping-pong (server initiated)"))
486 ;; (lurk-display-notice nil "ping-pong (client initiated)"))
489 (let* ((params (lurk-msg-params msg))
490 (nick (elt params 0))
491 (text (string-join (seq-drop params 1) " ")))
492 (setq lurk-nick nick)
493 (lurk-display-notice nil text)))
496 (let* ((params (lurk-msg-params msg))
497 (channel (elt params 2))
498 (names (split-string (elt params 3))))
499 (lurk-add-context-users channel names)))
502 (let* ((params (lurk-msg-params msg))
503 (channel (elt params 1)))
506 (lurk--as-string (length (lurk-get-context-users channel)))
507 " users in " channel)))
509 ((rx (= 3 (any digit)))
510 (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
513 (guard (equal lurk-nick (lurk-msg-src msg))))
514 (let ((channel (car (lurk-msg-params msg))))
515 (lurk-add-context channel)
516 (setq lurk-current-context channel)
517 (lurk-display-notice channel "Joining channel " channel)
518 (lurk-render-prompt)))
521 (let ((channel (car (lurk-msg-params msg)))
522 (nick (lurk-msg-src msg)))
523 (lurk-add-context-users channel (list nick))
525 (lurk-display-notice channel nick " joined channel " channel))))
528 (guard (equal lurk-nick (lurk-msg-src msg))))
529 (let ((channel (car (lurk-msg-params msg))))
530 (lurk-display-notice channel "Left channel " channel)
531 (lurk-del-context channel)
532 (if (equal channel lurk-current-context)
533 (setq lurk-current-context (lurk-get-next-context)))
534 (lurk-render-prompt)))
537 (let ((channel (car (lurk-msg-params msg)))
538 (nick (lurk-msg-src msg)))
539 (lurk-del-context-user channel nick)
541 (lurk-display-notice channel nick " left channel " channel))))
544 (let ((nick (lurk-msg-src msg))
545 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
548 (lurk-display-notice nil nick " quit: " reason))))
551 (guard (equal lurk-nick (lurk-msg-src msg))))
552 (setq lurk-nick (car (lurk-msg-params msg)))
553 (lurk-display-notice nil "Set nick to " lurk-nick))
556 (let ((old-nick (lurk-msg-src msg))
557 (new-nick (car (lurk-msg-params msg))))
558 (lurk-display-notice nil old-nick " is now known as " new-nick)
559 (lurk-rename-user old-nick new-nick)))
562 (let ((nick (lurk-msg-src msg))
563 (channel (car (lurk-msg-params msg)))
564 (text (cadr (lurk-msg-params msg))))
566 ((rx (: "\01VERSION "
567 (let version (* (not "\01")))
569 (lurk-display-notice nil "CTCP version reply from " nick ": " version))
571 (lurk-display-notice nil text)))))
574 (let* ((from (lurk-msg-src msg))
575 (params (lurk-msg-params msg))
577 (text (cadr params)))
580 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
581 (lurk-send-msg (lurk-msg nil nil "NOTICE"
582 (list from (concat "\01VERSION "
585 (lurk-display-notice nil "CTCP version request received from " from))
587 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
588 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
589 (lurk-display-notice "CTCP ping received from " from))
592 (lurk-display-notice "CTCP userinfo request from " from " (no response sent)"))
595 (lurk-display-message from to text)))))
597 (lurk-display-notice nil (lurk-msg->string msg))))))
603 (defun lurk-enter-string (string)
604 (if (string-prefix-p "/" string)
605 (pcase (substring string 1)
606 ((rx (: "CONNECT " (let network (* not-newline))))
607 (lurk-display-notice nil "Attempting to connect to " network "...")
608 (lurk-connect network))
610 ((rx (: "TOPIC " (let new-topic (* not-newline))))
611 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
613 ((rx (: "ME " (let action (* not-newline))))
614 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
615 (list lurk-current-context
616 (concat "\01ACTION " action "\01"))))
617 (lurk-display-action lurk-nick action))
619 ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
620 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
621 (list nick "\01VERSION\01")))
622 (lurk-display-notice nil "CTCP version request sent to " nick))
624 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
625 (if (or lurk-current-context channel)
626 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
628 lurk-current-context)))
629 (lurk-display-error "No current channel to leave.")))
631 ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
632 (lurk-send-msg (lurk-msg nil nil "QUIT"
633 (or quit-msg lurk-default-quit-msg))))
635 ((rx (: "NICK" (* whitespace) string-end))
636 (lurk-display-notice nil "Current nick: " lurk-nick))
638 ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
639 (if (lurk-connected-p)
640 (lurk-send-msg (lurk-msg nil nil "NICK" nick))
641 (setq lurk-nick nick)
642 (lurk-display-notice nil "Set default nick to '" nick "'")))
645 (let to (* (not whitespace)))
647 (let text (* not-newline)))
648 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
649 (lurk-display-message lurk-nick to text))
651 ((rx (: (let cmd-str (+ (not whitespace)))
652 (opt (: " " (let params-str (* not-newline))))))
653 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
655 (split-string params-str)
658 (unless (string-empty-p string)
659 (if lurk-current-context
661 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
664 (lurk-display-message lurk-nick lurk-current-context string))
665 (lurk-display-error "No current context.")))))
668 "Enter current contents of line after prompt."
670 (with-current-buffer "*lurk*"
671 (let ((line (buffer-substring lurk-input-marker (point-max))))
672 (let ((inhibit-read-only t))
673 (delete-region lurk-input-marker (point-max)))
674 (lurk-enter-string line))))
680 (defvar lurk-mode-map
681 (let ((map (make-sparse-keymap)))
682 (define-key map (kbd "RET") 'lurk-enter)
683 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
684 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
687 (define-derived-mode lurk-mode text-mode "lurk"
688 "Major mode for LURK.")
690 (when (fboundp 'evil-set-initial-state)
691 (evil-set-initial-state 'lurk-mode 'insert))
693 ;;; Main start procedure
697 "Switch to *lurk* buffer."
699 (if (get-buffer "*lurk*")
700 (switch-to-buffer "*lurk*")
701 (switch-to-buffer "*lurk*"))
708 ;;; lurk.el ends here