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))
337 (defun lurk-display-string (&rest strings)
338 (with-current-buffer (get-buffer-create "*lurk*")
340 (goto-char lurk-prompt-marker)
341 (let ((inhibit-read-only t)
342 (old-pos (marker-position lurk-prompt-marker))
343 (adaptive-fill-regexp (rx (= 6 anychar)))
345 (insert-before-markers
346 (propertize (concat (format-time-string "%H:%M") " ")
349 (propertize (concat (apply #'concat strings) "\n")
351 (fill-region old-pos lurk-prompt-marker nil t)))))
353 (defun lurk-render-prompt ()
354 (with-current-buffer "*lurk*"
355 (let ((update-point (= lurk-input-marker (point)))
356 (update-window-points (mapcar (lambda (w)
357 (list (= (window-point w) lurk-input-marker)
359 (get-buffer-window-list nil nil t))))
361 (set-marker-insertion-type lurk-prompt-marker nil)
362 (set-marker-insertion-type lurk-input-marker t)
363 (let ((inhibit-read-only t))
364 (delete-region lurk-prompt-marker lurk-input-marker)
365 (goto-char lurk-prompt-marker)
367 (propertize (if lurk-current-context
372 (propertize lurk-prompt-string
376 (set-marker-insertion-type lurk-input-marker nil))
378 (goto-char lurk-input-marker))
379 (dolist (v update-window-points)
381 (set-window-point (cadr v) lurk-input-marker))))))
383 (defvar lurk-prompt-marker nil
384 "Marker for prompt position in LURK buffer.")
386 (defvar lurk-input-marker nil
387 "Marker for prompt position in LURK buffer.")
389 (defun lurk-setup-buffer ()
390 (with-current-buffer (get-buffer-create "*lurk*")
391 (setq-local scroll-conservatively 1)
392 (if (markerp lurk-prompt-marker)
393 (set-marker lurk-prompt-marker (point-max))
394 (setq lurk-prompt-marker (point-max-marker)))
395 (if (markerp lurk-input-marker)
396 (set-marker lurk-input-marker (point-max))
397 (setq lurk-input-marker (point-max-marker)))
398 (goto-char (point-max))
399 (lurk-render-prompt)))
402 ;;; Output formatting
405 (defun lurk-display-message (from to text)
406 (let ((context (if (eq 'channel (lurk-get-context-type to))
408 (if (equal to lurk-nick) from to))))
411 (pcase (lurk-get-context-type to)
412 ('channel (concat to " <" from "> " text))
413 ('nick (concat "[" from " -> " to "] " text))
415 (error "Unsupported context type")))
417 'help-echo (concat "Context: " context)
420 (defun lurk-display-notice (context &rest notices)
423 (concat lurk-notice-prefix " " (apply #'concat notices))
424 'help-echo (concat "Context: " (or context "none"))
427 (defun lurk-display-error (&rest messages)
429 (concat lurk-error-prefix " "
430 (propertize (apply #'concat messages)
431 'face 'lurk-error))))
433 (defun lurk-highlight-context (context)
434 (with-current-buffer "*lurk*"
435 (let* ((pos lurk-prompt-marker)
436 (nextpos (previous-single-property-change pos 'context))
437 (inhibit-read-only t))
438 (while (> pos nextpos)
439 (let ((thiscontext (get-text-property nextpos 'context)))
441 (if (equal context thiscontext)
442 (add-text-properties nextpos pos
443 '(face (foreground-color . "green")))
444 (add-text-properties nextpos pos
445 '(face (foreground-color . "blue"))))
446 (add-text-properties nextpos pos
450 (setq nextpos (previous-single-property-change pos 'context nil 1)))))))
452 ;;; Message evaluation
455 (defun lurk-eval-msg-string (string)
456 ;; (lurk-display-string string)
457 (let* ((msg (lurk-string->msg string)))
458 ;; (message (pp msg))
459 (pcase (lurk-msg-cmd msg)
462 (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
463 ;; (lurk-display-notice nil "ping-pong (server initiated)"))
466 ;; (lurk-display-notice nil "ping-pong (client initiated)"))
469 (let* ((params (lurk-msg-params msg))
470 (nick (elt params 0))
471 (text (string-join (seq-drop params 1) " ")))
472 (setq lurk-nick nick)
473 (lurk-display-notice nil text)))
476 (let* ((params (lurk-msg-params msg))
477 (channel (elt params 2))
478 (names (split-string (elt params 3))))
479 (lurk-add-context-users channel names)))
482 (let* ((params (lurk-msg-params msg))
483 (channel (elt params 1)))
486 (lurk--as-string (length (lurk-get-context-users channel)))
487 " users in " channel)))
489 ((rx (= 3 (any digit)))
490 (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
493 (guard (equal lurk-nick (lurk-msg-src msg))))
494 (let ((channel (car (lurk-msg-params msg))))
495 (lurk-add-context channel)
496 (setq lurk-current-context channel)
497 (lurk-display-notice channel "Joining channel " channel)
498 (lurk-render-prompt)))
501 (let ((channel (car (lurk-msg-params msg)))
502 (nick (lurk-msg-src msg)))
503 (lurk-add-context-users channel (list nick))
504 (lurk-display-notice channel nick " joined channel " channel)))
507 (guard (equal lurk-nick (lurk-msg-src msg))))
508 (let ((channel (car (lurk-msg-params msg))))
509 (lurk-display-notice channel "Left channel " channel)
510 (lurk-del-context channel)
511 (if (equal channel lurk-current-context)
512 (setq lurk-current-context (lurk-get-next-context)))
513 (lurk-render-prompt)))
516 (let ((channel (car (lurk-msg-params msg)))
517 (nick (lurk-msg-src msg)))
518 (lurk-del-context-user channel nick)
519 (lurk-display-notice channel nick " left channel " channel)))
522 (let ((nick (lurk-msg-src msg))
523 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
525 (lurk-display-notice nil nick " quit: " reason)))
528 (guard (equal lurk-nick (lurk-msg-src msg))))
529 (setq lurk-nick (car (lurk-msg-params msg)))
530 (lurk-display-notice nil "Set nick to " lurk-nick))
533 (let ((old-nick (lurk-msg-src msg))
534 (new-nick (car (lurk-msg-params msg))))
535 (lurk-display-notice nil old-nick " is now known as " new-nick)
536 (lurk-rename-user old-nick new-nick)))
539 (let ((nick (lurk-msg-src msg))
540 (channel (car (lurk-msg-params msg)))
541 (text (cadr (lurk-msg-params msg))))
543 ((rx (: "\01VERSION "
544 (let version (* (not "\01")))
546 (lurk-display-notice nil "CTCP version reply from " nick ": " version))
548 (lurk-display-notice nil text)))))
551 (let* ((from (lurk-msg-src msg))
552 (params (lurk-msg-params msg))
554 (text (cadr params)))
557 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
558 (lurk-send-msg (lurk-msg nil nil "NOTICE"
559 (list from (concat "\01VERSION "
562 (lurk-display-notice nil "CTCP version request received from " from))
564 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
565 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
566 (lurk-display-notice "CTCP ping received from " from))
569 (lurk-display-notice "CTCP userinfo request from " from " (no response sent)"))
572 (lurk-display-message from to text)))))
574 (lurk-display-string (lurk-msg->string msg))))))
580 (defun lurk-enter-string (string)
581 (if (string-prefix-p "/" string)
582 (pcase (substring string 1)
583 ((rx (: "CONNECT " (let network (* not-newline))))
584 (lurk-display-notice nil "Attempting to connect to " network "...")
585 (lurk-connect network))
587 ((rx (: "TOPIC " (let new-topic (* not-newline))))
588 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
590 ((rx (: "ME " (let action (* not-newline))))
591 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
592 (list lurk-current-context
593 (concat "\01ACTION " action "\01"))))
594 (lurk-display-action lurk-nick action))
596 ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
597 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
598 (list nick "\01VERSION\01")))
599 (lurk-display-notice nil "CTCP version request sent to " nick))
601 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
602 (if (or lurk-current-context channel)
603 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
605 lurk-current-context)))
606 (lurk-display-error "No current channel to leave.")))
608 ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
609 (lurk-send-msg (lurk-msg nil nil "QUIT"
610 (or quit-msg lurk-default-quit-msg))))
612 ((rx (: "NICK" (* whitespace) string-end))
613 (lurk-display-notice nil "Current nick: " lurk-nick))
615 ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
616 (if (lurk-connected-p)
617 (lurk-send-msg (lurk-msg nil nil "NICK" nick))
618 (setq lurk-nick nick)
619 (lurk-display-notice nil "Set default nick to '" nick "'")))
622 (let to (* (not whitespace)))
624 (let text (* not-newline)))
625 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
626 (lurk-display-message lurk-nick to text))
628 ((rx (: (let cmd-str (+ (not whitespace)))
629 (opt (: " " (let params-str (* not-newline))))))
630 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
632 (split-string params-str)
635 (unless (string-empty-p string)
636 (if lurk-current-context
638 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
641 (lurk-display-message lurk-nick lurk-current-context string))
642 (lurk-display-error "No current context.")))))
645 "Enter current contents of line after prompt."
647 (with-current-buffer "*lurk*"
648 (let ((line (buffer-substring lurk-input-marker (point-max))))
649 (let ((inhibit-read-only t))
650 (delete-region lurk-input-marker (point-max)))
651 (lurk-enter-string line))))
657 (defvar lurk-mode-map
658 (let ((map (make-sparse-keymap)))
659 (define-key map (kbd "RET") 'lurk-enter)
660 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
661 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
664 (define-derived-mode lurk-mode text-mode "lurk"
665 "Major mode for LURK.")
667 (when (fboundp 'evil-set-initial-state)
668 (evil-set-initial-state 'lurk-mode 'insert))
670 ;;; Main start procedure
674 "Switch to *lurk* buffer."
676 (if (get-buffer "*lurk*")
677 (switch-to-buffer "*lurk*")
678 (switch-to-buffer "*lurk*"))
685 ;;; lurk.el ends here