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)))
338 (insert-before-markers
339 (propertize (concat (format-time-string "%H:%M") " ")
342 (propertize (concat (apply #'concat strings) "\n")
344 (fill-region old-pos lurk-prompt-marker nil t)))))
346 (defun lurk-render-prompt ()
347 (with-current-buffer "*lurk*"
348 (let ((update-point (= lurk-input-marker (point)))
349 (update-window-points (mapcar (lambda (w)
350 (list (= (window-point w) lurk-input-marker)
352 (get-buffer-window-list nil nil t))))
354 (set-marker-insertion-type lurk-prompt-marker nil)
355 (set-marker-insertion-type lurk-input-marker t)
356 (let ((inhibit-read-only t))
357 (delete-region lurk-prompt-marker lurk-input-marker)
358 (goto-char lurk-prompt-marker)
360 (propertize (if lurk-current-context
365 (propertize lurk-prompt-string
369 (set-marker-insertion-type lurk-input-marker nil))
371 (goto-char lurk-input-marker))
372 (dolist (v update-window-points)
374 (set-window-point (cadr v) lurk-input-marker))))))
376 (defvar lurk-prompt-marker nil
377 "Marker for prompt position in LURK buffer.")
379 (defvar lurk-input-marker nil
380 "Marker for prompt position in LURK buffer.")
382 (defun lurk-setup-buffer ()
383 (with-current-buffer (get-buffer-create "*lurk*")
384 (setq-local scroll-conservatively 1)
385 (if (markerp lurk-prompt-marker)
386 (set-marker lurk-prompt-marker (point-max))
387 (setq lurk-prompt-marker (point-max-marker)))
388 (if (markerp lurk-input-marker)
389 (set-marker lurk-input-marker (point-max))
390 (setq lurk-input-marker (point-max-marker)))
391 (goto-char (point-max))
392 (lurk-render-prompt)))
395 ;;; Output formatting
398 (defun lurk-display-message (from to text)
399 (let ((context (if (eq 'channel (lurk-get-context-type to))
401 (if (equal to lurk-nick) from to))))
404 (pcase (lurk-get-context-type to)
405 ('channel (concat to " <" from "> " text))
406 ('nick (concat "[" from " -> " to "] " text))
408 (error "Unsupported context type")))
410 'help-echo (concat "Context: " context)
413 (defun lurk-display-notice (context &rest notices)
416 (concat lurk-notice-prefix " " (apply #'concat notices))
417 'help-echo (concat "Context: " (or context "none"))
420 (defun lurk-display-error (&rest messages)
422 (concat lurk-error-prefix " "
423 (propertize (apply #'concat messages)
424 'face 'lurk-error))))
426 (defun lurk-highlight-context (context)
427 (with-current-buffer "*lurk*"
428 (let* ((pos lurk-prompt-marker)
429 (nextpos (previous-single-property-change pos 'context))
430 (inhibit-read-only t))
431 (while (> pos nextpos)
432 (let ((thiscontext (get-text-property nextpos 'context)))
434 (if (equal context thiscontext)
435 (add-text-properties nextpos pos
436 '(face (foreground-color . "green")))
437 (add-text-properties nextpos pos
438 '(face (foreground-color . "blue"))))
439 (add-text-properties nextpos pos
443 (setq nextpos (previous-single-property-change pos 'context nil 1)))))))
445 ;;; Message evaluation
448 (defun lurk-eval-msg-string (string)
449 ;; (lurk-display-string string)
450 (let* ((msg (lurk-string->msg string)))
451 ;; (message (pp msg))
452 (pcase (lurk-msg-cmd msg)
455 (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
456 ;; (lurk-display-notice nil "ping-pong (server initiated)"))
459 ;; (lurk-display-notice nil "ping-pong (client initiated)"))
462 (let* ((params (lurk-msg-params msg))
463 (nick (elt params 0))
464 (text (string-join (seq-drop params 1) " ")))
465 (setq lurk-nick nick)
466 (lurk-display-notice nil text)))
469 (let* ((params (lurk-msg-params msg))
470 (channel (elt params 2))
471 (names (split-string (elt params 3))))
472 (lurk-add-context-users channel names)))
475 (let* ((params (lurk-msg-params msg))
476 (channel (elt params 1)))
479 (lurk--as-string (length (lurk-get-context-users channel)))
480 " users in " channel)))
482 ((rx (= 3 (any digit)))
483 (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
486 (guard (equal lurk-nick (lurk-msg-src msg))))
487 (let ((channel (car (lurk-msg-params msg))))
488 (lurk-add-context channel)
489 (setq lurk-current-context channel)
490 (lurk-display-notice channel "Joining channel " channel)
491 (lurk-render-prompt)))
494 (let ((channel (car (lurk-msg-params msg)))
495 (nick (lurk-msg-src msg)))
496 (lurk-add-context-users channel (list nick))
497 (lurk-display-notice channel nick " joined channel " channel)))
500 (guard (equal lurk-nick (lurk-msg-src msg))))
501 (let ((channel (car (lurk-msg-params msg))))
502 (lurk-display-notice channel "Left channel " channel)
503 (lurk-del-context channel)
504 (if (equal channel lurk-current-context)
505 (setq lurk-current-context (lurk-get-next-context)))
506 (lurk-render-prompt)))
509 (let ((channel (car (lurk-msg-params msg)))
510 (nick (lurk-msg-src msg)))
511 (lurk-del-context-user channel nick)
512 (lurk-display-notice channel nick " left channel " channel)))
515 (let ((nick (lurk-msg-src msg))
516 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
518 (lurk-display-notice nil nick " quit: " reason)))
521 (guard (equal lurk-nick (lurk-msg-src msg))))
522 (setq lurk-nick (car (lurk-msg-params msg)))
523 (lurk-display-notice nil "Set nick to " lurk-nick))
526 (let ((old-nick (lurk-msg-src msg))
527 (new-nick (car (lurk-msg-params msg))))
528 (lurk-display-notice nil nick " is now known as " new-nick)
529 (lurk-rename-user nick new-nick)))
532 (let ((nick (lurk-msg-src msg))
533 (channel (car (lurk-msg-params msg)))
534 (text (cadr (lurk-msg-params msg))))
536 ((rx (: "\01VERSION "
537 (let version (* (not "\01")))
539 (lurk-display-notice nil "CTCP version reply from " nick ": " version))
541 (lurk-display-notice nil text)))))
544 (let* ((from (lurk-msg-src msg))
545 (params (lurk-msg-params msg))
547 (text (cadr params)))
550 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
551 (lurk-send-msg (lurk-msg nil nil "NOTICE"
552 (list from (concat "\01VERSION "
555 (lurk-display-notice nil "CTCP version request received from " from))
557 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
558 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
559 (lurk-display-notice "CTCP ping received from " from))
562 (lurk-display-notice "CTCP userinfo request from " from " (no response sent)"))
565 (lurk-display-message from to text)))))
567 (lurk-display-string (lurk-msg->string msg))))))
573 (defun lurk-enter-string (string)
574 (if (string-prefix-p "/" string)
575 (pcase (substring string 1)
576 ((rx (: "CONNECT " (let network (* not-newline))))
577 (lurk-display-notice nil "Attempting to connect to " network "...")
578 (lurk-connect network))
580 ((rx (: "TOPIC " (let new-topic (* not-newline))))
581 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
583 ((rx (: "ME " (let action (* not-newline))))
584 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
585 (list lurk-current-context
586 (concat "\01ACTION " action "\01"))))
587 (lurk-display-action lurk-nick action))
589 ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
590 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
591 (list nick "\01VERSION\01")))
592 (lurk-display-notice nil "CTCP version request sent to " nick))
594 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
595 (if (or lurk-current-context channel)
596 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
598 lurk-current-context)))
599 (lurk-display-error "No current channel to leave.")))
601 ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
602 (lurk-send-msg (lurk-msg nil nil "QUIT"
603 (or quit-msg lurk-default-quit-msg))))
605 ((rx (: "NICK" (* whitespace) string-end))
606 (lurk-display-notice nil "Current nick: " lurk-nick))
608 ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
609 (if (lurk-connected-p)
610 (lurk-send-msg (lurk-msg nil nil "NICK" nick))
611 (setq lurk-nick nick)
612 (lurk-display-notice nil "Set default nick to '" nick "'")))
615 (let to (* (not whitespace)))
617 (let text (* not-newline)))
618 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
619 (lurk-display-message lurk-nick to text))
621 ((rx (: (let cmd-str (+ (not whitespace)))
622 (opt (: " " (let params-str (* not-newline))))))
623 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
625 (split-string params-str)
628 (unless (string-empty-p string)
629 (if lurk-current-context
631 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
634 (lurk-display-message lurk-nick lurk-current-context string))
635 (lurk-display-error "No current context.")))))
638 "Enter current contents of line after prompt."
640 (with-current-buffer "*lurk*"
641 (let ((line (buffer-substring lurk-input-marker (point-max))))
642 (let ((inhibit-read-only t))
643 (delete-region lurk-input-marker (point-max)))
644 (lurk-enter-string line))))
650 (defvar lurk-mode-map
651 (let ((map (make-sparse-keymap)))
652 (define-key map (kbd "RET") 'lurk-enter)
653 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
654 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
657 (define-derived-mode lurk-mode text-mode "lurk"
658 "Major mode for LURK.")
660 (when (fboundp 'evil-set-initial-state)
661 (evil-set-initial-state 'lurk-mode 'insert))
663 ;;; Main start procedure
667 "Switch to *lurk* buffer."
669 (if (get-buffer "*lurk*")
670 (switch-to-buffer "*lurk*")
671 (switch-to-buffer "*lurk*"))
678 ;;; lurk.el ends here