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.")
62 '((t :inherit font-lock-preprocessor-face))
63 "Face used for Lurk text.")
65 (defface lurk-your-nick
66 '((t :inherit font-lock-constant-face))
67 "Face used for highlighting your nick.")
70 '((t :inherit org-level-2))
71 "Face used for the prompt.")
74 '((t :inherit org-list-dt))
75 "Face used for the context name in the prompt.")
78 '((t :inherit font-lock-preprocessor-face))
79 "Face used for faded Lurk text.")
82 '((t :inherit font-lock-function-name-face))
83 "Face used for bold Lurk text.")
86 '((t :inherit font-lock-regexp-grouping-construct))
87 "Face used for Lurk error text.")
92 (defvar lurk-version "Lurk v0.1")
94 (defvar lurk-notice-prefix
97 "-" 'face 'lurk-faded)
101 "-" 'face 'lurk-faded)))
103 (defvar lurk-error-prefix
104 (propertize "!!!" 'face 'lurk-error))
107 (defvar lurk-prompt-string
108 (propertize "> " 'face 'lurk-prompt))
114 (defvar lurk-response "")
116 (defun lurk-filter (proc string)
117 (dolist (line (split-string (concat lurk-response string) "\n"))
118 (if (string-suffix-p "\r" line)
119 (lurk-eval-msg-string (string-trim line))
120 (setq lurk-response line))))
122 (defun lurk-sentinel (proc string)
123 (unless (equal "open" (string-trim string))
124 (lurk-display-error "Disconnected from server.")
125 (clrhash lurk-contexts)
126 (setq lurk-current-context nil)
128 (cancel-timer lurk-ping-timer)))
130 (defun lurk-start-process (network)
131 (let* ((row (assoc network lurk-networks))
134 (flags (seq-drop row 3)))
135 (make-network-process :name "lurk"
138 :family (if lurk-allow-ipv6 nil 'ipv4)
139 :filter #'lurk-filter
140 :sentinel #'lurk-sentinel
142 :tls-parameters (if (memq :notls flags)
144 (cons 'gnutls-x509pki
145 (gnutls-boot-parameters
146 :type 'gnutls-x509pki
150 (defvar lurk-ping-timer nil)
151 (defvar lurk-ping-period 60)
153 (defun lurk-ping-function ()
154 (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
155 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
157 (defun lurk-connect (network)
158 (if (get-process "lurk")
159 (lurk-display-error "Already connected. Disconnect first.")
160 (if (not (assoc network lurk-networks))
161 (lurk-display-error "Network '" network "' is unknown.")
162 (clrhash lurk-contexts)
163 (setq lurk-current-context nil)
164 (lurk-start-process network)
165 (lurk-send-msg (lurk-msg nil nil "USER" lurk-nick 0 "*" lurk-nick))
166 (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
167 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
169 (defun lurk-connected-p ()
170 (let ((proc (get-process "lurk")))
171 (and proc (eq (process-status proc) 'open))))
173 (defun lurk-send-msg (msg)
174 (let ((proc (get-process "lurk")))
175 (if (and proc (eq (process-status proc) 'open))
176 (process-send-string proc (concat (lurk-msg->string msg) "\r\n"))
177 (lurk-display-error "No server connection established.")
178 (error "No server connection established"))))
184 (defun lurk--as-string (obj)
186 (with-output-to-string (princ obj))
189 (defun lurk-msg (tags src cmd &rest params)
190 (list (lurk--as-string tags)
191 (lurk--as-string src)
192 (upcase (lurk--as-string cmd))
193 (mapcar #'lurk--as-string
194 (if (and params (listp (elt params 0)))
198 (defun lurk-msg-tags (msg) (elt msg 0))
199 (defun lurk-msg-src (msg) (elt msg 1))
200 (defun lurk-msg-cmd (msg) (elt msg 2))
201 (defun lurk-msg-params (msg) (elt msg 3))
202 (defun lurk-msg-trail (msg)
203 (let ((params (lurk-msg-params msg)))
205 (elt params (- (length params) 1)))))
207 (defvar lurk-msg-regex
209 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
211 (opt (: ":" (: (group (* (not (any space "!" "@"))))
212 (* (not (any space)))))
214 (group (: (* (not whitespace))))
216 (opt (group (+ not-newline))))
217 "Regex used to parse IRC messages.
218 Note that this regex is incomplete. Noteably, we discard the non-nick
219 portion of the source component of the message, as LURK doesn't use this.")
221 (defun lurk-string->msg (string)
222 (if (string-match lurk-msg-regex string)
223 (let* ((tags (match-string 1 string))
224 (src (match-string 2 string))
225 (cmd (upcase (match-string 3 string)))
226 (params-str (match-string 4 string))
229 (let* ((idx (cl-search ":" params-str))
230 (l (split-string (string-trim (substring params-str 0 idx))))
231 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
234 (apply #'lurk-msg (append (list tags src cmd) params)))
235 (error "Failed to parse string " string)))
237 (defun lurk--filtered-join (&rest args)
238 (string-join (seq-filter (lambda (el) el) args) " "))
240 (defun lurk-msg->string (msg)
241 (let ((tags (lurk-msg-tags msg))
242 (src (lurk-msg-src msg))
243 (cmd (lurk-msg-cmd msg))
244 (params (lurk-msg-params msg)))
246 (if tags (concat "@" tags) nil)
247 (if src (concat ":" src) nil)
249 (if (> (length params) 1)
250 (string-join (seq-take params (- (length params) 1)) " ")
252 (if (> (length params) 0)
253 (concat ":" (elt params (- (length params) 1)))
260 (defvar lurk-current-context nil)
261 (defvar lurk-contexts (make-hash-table :test #'equal))
263 (defun lurk-add-context (name)
264 (puthash name nil lurk-contexts))
266 (defun lurk-del-context (name)
267 (remhash name lurk-contexts))
269 (defun lurk-get-context-users (name)
270 (gethash name lurk-contexts))
272 (defun lurk-add-context-users (context users)
275 (gethash context lurk-contexts))
278 (defun lurk-del-context-user (context user)
280 (remove user (gethash context lurk-contexts))
283 (defun lurk-del-user (user)
284 (dolist (context (lurk-get-context-list))
285 (lurk-del-context-user context user)))
287 (defun lurk-get-context-type (name)
289 ((string-prefix-p "#" name) 'channel)
290 ((string-match-p (rx (or "." "localhost")) name) 'host)
293 (defun lurk-get-context-list ()
295 (maphash (lambda (key val)
296 (cl-pushnew key res))
300 (defun lurk-get-next-context (&optional prev)
301 (if lurk-current-context
302 (let* ((context-list (if prev
303 (reverse (lurk-get-context-list))
304 (lurk-get-context-list)))
305 (context-list* (member lurk-current-context context-list)))
306 (if (> (length context-list*) 1)
311 (defun lurk-cycle-contexts (&optional rev)
312 (if lurk-current-context
314 (setq lurk-current-context (lurk-get-next-context rev))
315 (lurk-render-prompt))
316 (lurk-display-error "No channels joined.")))
318 (defun lurk-cycle-contexts-forward ()
320 (lurk-cycle-contexts))
322 (defun lurk-cycle-contexts-reverse ()
324 (lurk-cycle-contexts t))
330 (defun lurk-display-string (&rest strings)
331 (with-current-buffer (get-buffer-create "*lurk*")
333 (goto-char lurk-prompt-marker)
334 (let ((inhibit-read-only t)
335 (old-pos (marker-position lurk-prompt-marker))
336 (adaptive-fill-regexp (rx (= 6 anychar))))
337 (insert-before-markers
338 (propertize (concat (format-time-string "%H:%M") " ")
341 (propertize (concat (apply #'concat strings) "\n")
343 (fill-region old-pos lurk-prompt-marker)))))
345 (defun lurk-render-prompt ()
346 (with-current-buffer "*lurk*"
347 (let ((update-point (= lurk-input-marker (point)))
348 (update-window-points (mapcar (lambda (w)
349 (list (= (window-point w) lurk-input-marker)
351 (get-buffer-window-list nil nil t))))
353 (set-marker-insertion-type lurk-prompt-marker nil)
354 (set-marker-insertion-type lurk-input-marker t)
355 (let ((inhibit-read-only t))
356 (delete-region lurk-prompt-marker lurk-input-marker)
357 (goto-char lurk-prompt-marker)
359 (propertize (if lurk-current-context
364 (propertize lurk-prompt-string
368 (set-marker-insertion-type lurk-input-marker nil))
370 (goto-char lurk-input-marker))
371 (dolist (v update-window-points)
373 (set-window-point (cadr v) lurk-input-marker))))))
375 (defvar lurk-prompt-marker nil
376 "Marker for prompt position in LURK buffer.")
378 (defvar lurk-input-marker nil
379 "Marker for prompt position in LURK buffer.")
381 (defun lurk-setup-buffer ()
382 (with-current-buffer (get-buffer-create "*lurk*")
383 (setq-local scroll-conservatively 1)
384 (if (markerp lurk-prompt-marker)
385 (set-marker lurk-prompt-marker (point-max))
386 (setq lurk-prompt-marker (point-max-marker)))
387 (if (markerp lurk-input-marker)
388 (set-marker lurk-input-marker (point-max))
389 (setq lurk-input-marker (point-max-marker)))
390 (goto-char (point-max))
391 (lurk-render-prompt)))
394 ;;; Output formatting
397 (defun lurk-display-message (from to text)
398 (let ((context (if (eq 'channel (lurk-get-context-type to))
400 (if (equal to lurk-nick) from to))))
403 (pcase (lurk-get-context-type to)
404 ('channel (concat to " <" from "> " text))
405 ('nick (concat "[" from " -> " to "] " text))
407 (error "Unsupported context type")))
409 'help-echo (concat "Context: " context)
412 (defun lurk-display-notice (context &rest notices)
415 (concat lurk-notice-prefix " " (apply #'concat notices))
416 'help-echo (concat "Context: " (or context "none"))
419 (defun lurk-display-error (&rest messages)
421 (concat lurk-error-prefix " "
422 (propertize (apply #'concat messages)
423 'face 'lurk-error))))
425 (defun lurk-highlight-context (context)
426 (with-current-buffer "*lurk*"
427 (let* ((pos lurk-prompt-marker)
428 (nextpos (previous-single-property-change pos 'context))
429 (inhibit-read-only t))
430 (while (> pos nextpos)
431 (let ((thiscontext (get-text-property nextpos 'context)))
433 (if (equal context thiscontext)
434 (add-text-properties nextpos pos
435 '(face (foreground-color . "green")))
436 (add-text-properties nextpos pos
437 '(face (foreground-color . "blue"))))
438 (add-text-properties nextpos pos
442 (setq nextpos (previous-single-property-change pos 'context nil 1)))))))
444 ;;; Message evaluation
447 (defun lurk-eval-msg-string (string)
448 ;; (lurk-display-string string)
449 (let* ((msg (lurk-string->msg string)))
450 (pcase (lurk-msg-cmd msg)
453 (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
454 ;; (lurk-display-notice nil "ping-pong (server initiated)"))
457 ;; (lurk-display-notice nil "ping-pong (client initiated)"))
460 (let* ((params (lurk-msg-params msg))
461 (nick (elt params 0))
462 (text (string-join (seq-drop params 1) " ")))
463 (setq lurk-nick nick)
464 (lurk-display-notice nil text)))
467 (let* ((params (lurk-msg-params msg))
468 (channel (elt params 2))
469 (names (split-string (elt params 3))))
470 (lurk-add-context-users channel names)))
473 (let* ((params (lurk-msg-params msg))
474 (channel (elt params 1)))
477 (lurk--as-string (length (lurk-get-context-users channel)))
478 " users in " channel)))
480 ((rx (= 3 (any digit)))
481 (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
484 (guard (equal lurk-nick (lurk-msg-src msg))))
485 (let ((channel (car (lurk-msg-params msg))))
486 (lurk-add-context channel)
487 (setq lurk-current-context channel)
488 (lurk-display-notice channel "Joining channel " channel)
489 (lurk-render-prompt)))
492 (let ((channel (car (lurk-msg-params msg)))
493 (nick (lurk-msg-src msg)))
494 (lurk-add-context-users channel (list nick))
495 (lurk-display-notice channel nick " joined channel " channel)))
498 (guard (equal lurk-nick (lurk-msg-src msg))))
499 (let ((channel (car (lurk-msg-params msg))))
500 (lurk-display-notice channel "Left channel " channel)
501 (lurk-del-context channel)
502 (if (equal channel lurk-current-context)
503 (setq lurk-current-context (lurk-get-next-context)))
504 (lurk-render-prompt)))
507 (let ((channel (car (lurk-msg-params msg)))
508 (nick (lurk-msg-src msg)))
509 (lurk-del-context-user channel nick)
510 (lurk-display-notice channel nick " left channel " channel)))
513 (let ((nick (lurk-msg-src msg))
514 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
516 (lurk-display-notice nil nick " quit: " reason)))
519 (guard (equal lurk-nick (lurk-msg-src msg))))
520 (setq lurk-nick (car (lurk-msg-params msg)))
521 (lurk-display-notice nil "Set nick to " lurk-nick))
524 (let ((old-nick (lurk-msg-src msg))
525 (new-nick (car (lurk-msg-params msg))))
526 (lurk-display-notice nil nick " is now known as " new-nick)
527 (lurk-rename-user nick new-nick)))
530 (let ((nick (lurk-msg-src msg))
531 (channel (car (lurk-msg-params msg)))
532 (text (cadr (lurk-msg-params msg))))
534 ((rx (: "\01VERSION "
535 (let version (* (not "\01")))
537 (lurk-display-notice nil "CTCP version reply from " nick ": " version))
539 (lurk-display-notice nil text)))))
542 (let* ((from (lurk-msg-src msg))
543 (params (lurk-msg-params msg))
545 (text (cadr params)))
548 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
549 (lurk-send-msg (lurk-msg nil nil "NOTICE"
550 (list from (concat "\01VERSION "
553 (lurk-display-notice nil "CTCP version request received from " from))
555 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
556 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
557 (lurk-display-notice "CTCP ping received from " from))
560 (lurk-display-notice "CTCP userinfo request from " from " (no response sent)"))
563 (lurk-display-message from to text)))))
565 (lurk-display-string (lurk-msg->string msg))))))
571 (defun lurk-enter-string (string)
572 (if (string-prefix-p "/" string)
573 (pcase (substring string 1)
574 ((rx (: "CONNECT " (let network (* not-newline))))
575 (lurk-display-notice nil "Attempting to connect to " network "...")
576 (lurk-connect network))
578 ((rx (: "TOPIC " (let new-topic (* not-newline))))
579 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
581 ((rx (: "ME " (let action (* not-newline))))
582 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
583 (list lurk-current-context
584 (concat "\01ACTION " action "\01"))))
585 (lurk-display-action lurk-nick action))
587 ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
588 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
589 (list nick "\01VERSION\01")))
590 (lurk-display-notice nil "CTCP version request sent to " nick))
592 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
593 (if (or lurk-current-context channel)
594 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
596 lurk-current-context)))
597 (lurk-display-error "No current channel to leave.")))
599 ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
600 (lurk-send-msg (lurk-msg nil nil "QUIT"
601 (or quit-msg lurk-default-quit-msg))))
603 ((rx (: "NICK" (* whitespace) string-end))
604 (lurk-display-notice nil "Current nick: " lurk-nick))
606 ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
607 (if (lurk-connected-p)
608 (lurk-send-msg (lurk-msg nil nil "NICK" nick))
609 (setq lurk-nick nick)
610 (lurk-display-notice nil "Set default nick to '" nick "'")))
613 (let to (* (not whitespace)))
615 (let text (* not-newline)))
616 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
617 (lurk-display-message lurk-nick to text))
619 ((rx (: (let cmd-str (+ (not whitespace)))
620 (opt (: " " (let params-str (* not-newline))))))
621 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
623 (split-string params-str)
626 (unless (string-empty-p string)
627 (if lurk-current-context
629 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
632 (lurk-display-message lurk-nick lurk-current-context string))
633 (lurk-display-error "No current context.")))))
636 "Enter current contents of line after prompt."
638 (with-current-buffer "*lurk*"
639 (let ((line (buffer-substring lurk-input-marker (point-max))))
640 (let ((inhibit-read-only t))
641 (delete-region lurk-input-marker (point-max)))
642 (lurk-enter-string line))))
648 (defvar lurk-mode-map
649 (let ((map (make-sparse-keymap)))
650 (define-key map (kbd "RET") 'lurk-enter)
651 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
652 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
655 (define-derived-mode lurk-mode text-mode "lurk"
656 "Major mode for LURK.")
658 (when (fboundp 'evil-set-initial-state)
659 (evil-set-initial-state 'lurk-mode 'insert))
661 ;;; Main start procedure
665 "Switch to *lurk* buffer."
667 (if (get-buffer "*lurk*")
668 (switch-to-buffer "*lurk*")
669 (switch-to-buffer "*lurk*"))
676 ;;; lurk.el ends here