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/erc
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"
43 (defcustom lurk-full-name "plugd"
45 (defcustom lurk-user-name "plugd"
48 (defcustom lurk-networks
49 '(("libera" "irc.libera.chat" 6697)
50 ("freenode" "chat.freenode.net" 6697)
51 ("local" "localhost" 6697))
54 (defcustom lurk-allow-ipv6 nil
55 "Set to non-nil to allow use of IPv6.")
61 '((t :inherit font-lock-preprocessor-face))
62 "Face used for Lurk text.")
64 (defface lurk-your-nick
65 '((t :inherit font-lock-constant-face))
66 "Face used for highlighting your nick.")
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
96 "-" 'face 'lurk-faded)
100 "-" 'face 'lurk-faded)))
102 (defvar lurk-error-prefix
103 (propertize "!!!" 'face 'lurk-error))
106 (defvar lurk-prompt-string
107 (propertize "> " 'face 'lurk-prompt))
113 (defvar lurk-response "")
115 (defun lurk-filter (proc string)
116 (dolist (line (split-string (concat lurk-response string) "\n"))
117 (if (string-suffix-p "\r" line)
118 (lurk-eval-msg-string (string-trim line))
119 (setq lurk-response line))))
121 (defun lurk-sentinel (proc string)
122 (unless (equal "open" (string-trim string))
123 (lurk-display-error "Disconnected from server.")
124 (clrhash lurk-contexts)
125 (setq lurk-current-context nil)
127 (cancel-timer lurk-ping-timer)))
129 (defun lurk-start-process (network)
130 (let* ((row (assoc network lurk-networks))
133 (flags (seq-drop row 3)))
134 (make-network-process :name "lurk"
137 :family (if lurk-allow-ipv6 nil 'ipv4)
138 :filter #'lurk-filter
139 :sentinel #'lurk-sentinel
141 :tls-parameters (if (memq :notls flags)
143 (cons 'gnutls-x509pki
144 (gnutls-boot-parameters
145 :type 'gnutls-x509pki
149 (defvar lurk-ping-timer nil)
150 (defvar lurk-ping-period 60)
152 (defun lurk-ping-function ()
153 (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
154 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
156 (defun lurk-connect (network)
157 (if (get-process "lurk")
158 (lurk-display-error "Already connected. Disconnect first.")
159 (if (not (assoc network lurk-networks))
160 (lurk-display-error "Network '" network "' is unknown.")
161 (clrhash lurk-contexts)
162 (setq lurk-current-context nil)
163 (lurk-start-process network)
164 (lurk-send-msg (lurk-msg nil nil "USER" lurk-user-name 0 "*" lurk-full-name))
165 (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
166 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
169 (defun lurk-send-msg (msg)
170 (let ((proc (get-process "lurk")))
171 (if (and proc (eq (process-status proc) 'open))
172 (process-send-string proc (concat (lurk-msg->string msg) "\r\n"))
173 (lurk-display-error "No server connection established.")
174 (error "No server connection established"))))
180 (defun lurk--as-string (obj)
182 (with-output-to-string (princ obj))
185 (defun lurk-msg (tags src cmd &rest params)
186 (list (lurk--as-string tags)
187 (lurk--as-string src)
188 (upcase (lurk--as-string cmd))
189 (mapcar #'lurk--as-string
190 (if (and params (listp (elt params 0)))
194 (defun lurk-msg-tags (msg) (elt msg 0))
195 (defun lurk-msg-src (msg) (elt msg 1))
196 (defun lurk-msg-cmd (msg) (elt msg 2))
197 (defun lurk-msg-params (msg) (elt msg 3))
198 (defun lurk-msg-trail (msg)
199 (let ((params (lurk-msg-params msg)))
201 (elt params (- (length params) 1)))))
203 (defvar lurk-msg-regex
205 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
207 (opt (: ":" (: (group (* (not (any space "!" "@"))))
208 (* (not (any space)))))
210 (group (: (* (not whitespace))))
212 (opt (group (+ not-newline))))
213 "Regex used to parse IRC messages.
214 Note that this regex is incomplete. Noteably, we discard the non-nick
215 portion of the source component of the message, as LURK doesn't use this.")
217 (defun lurk-string->msg (string)
218 (if (string-match lurk-msg-regex string)
219 (let* ((tags (match-string 1 string))
220 (src (match-string 2 string))
221 (cmd (upcase (match-string 3 string)))
222 (params-str (match-string 4 string))
225 (let* ((idx (cl-search ":" params-str))
226 (l (split-string (string-trim (substring params-str 0 idx))))
227 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
230 (apply #'lurk-msg (append (list tags src cmd) params)))
231 (error "Failed to parse string " string)))
233 (defun lurk--filtered-join (&rest args)
234 (string-join (seq-filter (lambda (el) el) args) " "))
236 (defun lurk-msg->string (msg)
237 (let ((tags (lurk-msg-tags msg))
238 (src (lurk-msg-src msg))
239 (cmd (lurk-msg-cmd msg))
240 (params (lurk-msg-params msg)))
242 (if tags (concat "@" tags) nil)
243 (if src (concat ":" src) nil)
245 (if (> (length params) 1)
246 (string-join (seq-take params (- (length params) 1)) " ")
248 (if (> (length params) 0)
249 (concat ":" (elt params (- (length params) 1)))
253 ;;; Contexts and users
256 (defvar lurk-context-table
257 '((channel lurk-display-channel-message)
258 (nick lurk-display-private-message)
259 (host lurk-diaplay-server-message)))
261 (defvar lurk-current-context nil)
262 (defvar lurk-contexts (make-hash-table :test #'equal))
264 (defun lurk-add-context (name)
265 (puthash name nil lurk-contexts))
267 (defun lurk-del-context (name)
268 (remhash name lurk-contexts))
270 (defun lurk-get-context-users (name)
271 (gethash name lurk-contexts))
273 (defun lurk-add-context-users (context users)
276 (gethash context lurk-contexts))
279 (defun lurk-del-context-user (context user)
281 (remove user (gethash context lurk-contexts))
284 (defun lurk-del-user (user)
285 (dolist (context (lurk-get-context-list))
286 (lurk-del-context-user context user)))
288 (defun lurk-get-context-type (name)
290 ((string-prefix-p "#" name) 'channel)
291 ((string-match-p (rx (or "." "localhost")) name) 'host)
294 (defun lurk-get-context-list ()
296 (maphash (lambda (key val)
297 (cl-pushnew key res))
301 (defun lurk-get-next-context (&optional prev)
302 (if lurk-current-context
303 (let* ((context-list (if prev
304 (reverse (lurk-get-context-list))
305 (lurk-get-context-list)))
306 (context-list* (member lurk-current-context context-list)))
307 (if (> (length context-list*) 1)
312 (defun lurk-cycle-contexts (&optional rev)
313 (if lurk-current-context
315 (setq lurk-current-context (lurk-get-next-context rev))
316 (lurk-render-prompt))
317 (lurk-display-error "No channels joined.")))
319 (defun lurk-cycle-contexts-forward ()
321 (lurk-cycle-contexts))
323 (defun lurk-cycle-contexts-reverse ()
325 (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 (if (markerp lurk-prompt-marker)
384 (set-marker lurk-prompt-marker (point-max))
385 (setq lurk-prompt-marker (point-max-marker)))
386 (if (markerp lurk-input-marker)
387 (set-marker lurk-input-marker (point-max))
388 (setq lurk-input-marker (point-max-marker)))
389 (goto-char (point-max))
390 (lurk-render-prompt)))
393 ;;; Output formatting
396 (defun lurk-display-message (from to text)
397 (let* ((to-type (lurk-get-context-type to))
398 (display-fun (cadr (assoc to-type lurk-context-table))))
399 (funcall display-fun from to text)))
401 (defun lurk-display-channel-message (from to text)
403 (propertize (concat to
409 (defun lurk-display-action (channel-name nick action)
410 (lurk-display-string (concat channel-name
412 (propertize (concat nick " " action)
415 (defun lurk-display-private-message (from to text)
419 (concat "[" from " -> " to "] "
424 (defun lurk-display-notice (&rest notices)
425 (lurk-display-string lurk-notice-prefix " " (apply #'concat notices)))
427 (defun lurk-display-error (&rest messages)
428 (lurk-display-string lurk-error-prefix " "
429 (propertize (apply #'concat messages)
432 ;;; Message evaluation
435 (defun lurk-eval-msg-string (string)
436 ;; (lurk-display-string string)
437 (let* ((msg (lurk-string->msg string)))
438 (pcase (lurk-msg-cmd msg)
441 (lurk-msg nil nil "PONG" (lurk-msg-params msg)))
442 (lurk-display-notice "ping-pong"))
445 (let* ((params (lurk-msg-params msg))
446 (channel (elt params 2))
447 (names (split-string (elt params 3))))
448 (lurk-add-context-users channel names)))
452 (lurk--as-string (length (lurk-get-context-users lurk-current-context)))
453 " users in " lurk-current-context))
455 ((rx (= 3 (any digit)))
456 (lurk-display-notice (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
459 (guard (equal lurk-nick (lurk-msg-src msg))))
460 (let ((channel-name (car (lurk-msg-params msg))))
461 (lurk-add-context channel-name)
462 (setq lurk-current-context channel-name)
463 (lurk-display-notice "Joining channel " channel-name)
464 (lurk-render-prompt)))
467 (let ((channel-name (car (lurk-msg-params msg)))
468 (nick (lurk-msg-src msg)))
469 (lurk-add-context-users channel-name (list nick))
470 (lurk-display-notice nick " joined channel " channel-name)))
473 (guard (equal lurk-nick (lurk-msg-src msg))))
474 (let ((channel-name (car (lurk-msg-params msg))))
475 (lurk-display-notice "Left channel " channel-name)
476 (lurk-del-context channel-name)
477 (if (equal channel-name lurk-current-context)
478 (setq lurk-current-context (lurk-get-next-context)))
479 (lurk-render-prompt)))
482 (let ((channel-name (car (lurk-msg-params msg)))
483 (nick (lurk-msg-src msg)))
484 (lurk-del-context-user channel-name nick)
485 (lurk-display-notice nick " left channel " channel-name)))
488 (let ((nick (lurk-msg-src msg))
489 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
491 (lurk-display-notice nick " quit: " reason)))
494 (guard (equal lurk-nick (lurk-msg-src msg))))
495 (setq lurk-nick (car (lurk-msg-params msg)))
496 (lurk-display-notice "Set nick to " lurk-nick))
499 (let ((old-nick (lurk-msg-src msg))
500 (new-nick (car (lurk-msg-params msg))))
501 (lurk-display-notice nick " is now known as " new-nick)
502 (lurk-rename-user nick new-nick)))
505 (let ((nick (lurk-msg-src msg))
506 (channel (car (lurk-msg-params msg)))
507 (text (cadr (lurk-msg-params msg))))
509 ((rx (: "\01VERSION "
510 (let version (* (not "\01")))
512 (lurk-display-notice "CTCP version reply from " nick ": " version))
514 (lurk-display-notice text)))))
517 (let* ((from (lurk-msg-src msg))
518 (params (lurk-msg-params msg))
520 (text (cadr params)))
523 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
524 (lurk-send-msg (lurk-msg nil nil "NOTICE"
525 (list from (concat "\01VERSION "
528 (lurk-display-notice "CTCP version request received from " from))
530 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
531 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
532 (lurk-display-notice "CTCP ping received from " from))
535 (lurk-display-notice "CTCP userinfo request from " from " (no response sent)"))
538 (lurk-display-message from to text)))))
540 (lurk-display-string (lurk-msg->string msg))))))
546 (defun lurk-enter-string (string)
547 (if (string-prefix-p "/" string)
548 (pcase (substring string 1)
549 ((rx (: "CONNECT " (let network (* not-newline))))
550 (lurk-display-notice "Attempting to connect to " network "...")
551 (lurk-connect network))
553 ((rx (: "TOPIC " (let new-topic (* not-newline))))
554 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
556 ((rx (: "ME " (let action (* not-newline))))
557 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
558 (list lurk-current-context
559 (concat "\01ACTION " action "\01"))))
560 (lurk-display-action lurk-nick action))
562 ((rx (: "VERSION" " " (let nick (* (not whitespace)))))
563 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
564 (list nick "\01VERSION\01")))
565 (lurk-display-notice "CTCP version request sent to " nick))
567 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
568 (if (or lurk-current-context channel)
569 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
571 lurk-current-context)))
572 (lurk-display-error "No current channel to leave.")))
575 (let to (* (not whitespace)))
577 (let text (* not-newline)))
578 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
579 (lurk-display-message lurk-nick to text))
581 ((rx (: (let cmd-str (+ (not whitespace)))
582 (opt (: " " (let params-str (* not-newline))))))
583 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
585 (split-string params-str)
588 (unless (string-empty-p string)
589 (if lurk-current-context
591 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
594 (lurk-display-message lurk-nick lurk-current-context string))
595 (lurk-display-error "No current context.")))))
598 "Enter current contents of line after prompt."
600 (with-current-buffer "*lurk*"
602 (buffer-substring lurk-input-marker (point-max)))
603 (let ((inhibit-read-only t))
604 (delete-region lurk-input-marker (point-max)))))
610 (defvar lurk-mode-map
611 (let ((map (make-sparse-keymap)))
612 (define-key map (kbd "RET") 'lurk-enter)
613 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
614 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
617 (define-derived-mode lurk-mode text-mode "lurk"
618 "Major mode for LURK.")
620 (when (fboundp 'evil-set-initial-state)
621 (evil-set-initial-state 'lurk-mode 'insert))
623 ;;; Main start procedure
627 "Switch to *lurk* buffer."
629 (if (get-buffer "*lurk*")
630 (switch-to-buffer "*lurk*")
631 (switch-to-buffer "*lurk*"))
638 ;;; lurk.el ends here