1 ;;; lirc.el --- Lightweight irc client -*- 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/>.
37 "Lightweight IRC client."
40 (defcustom lirc-nick "plugd"
42 (defcustom lirc-full-name "plugd"
44 (defcustom lirc-user-name "plugd"
46 (defcustom lirc-host "irc.libera.chat"
48 (defcustom lirc-port 6667
51 (defcustom lirc-prompt-string "> "
58 '((t :inherit org-level-2))
59 "Face used for the prompt.")
62 '((t :inherit org-list-dt))
63 "Face used for the channel name in the prompt.")
68 (defvar lirc-current-channel nil)
69 (defvar lirc-channel-list nil)
71 (defvar lirc-response "")
77 (defun lirc-filter (proc string)
78 (dolist (line (split-string (concat lirc-response string) "\n"))
79 (if (string-suffix-p "\r" line)
80 (lirc-eval-msg-string (string-trim line))
81 (setq lirc-response line))))
83 (defun lirc-get-process ()
84 (let ((proc (get-process "lirc")))
85 (if (and proc (eq (process-status proc) 'open))
87 (make-network-process :name "lirc"
97 (defun lirc-as-string (obj)
99 (with-output-to-string (princ obj))
102 (defun lirc-msg (tags src cmd &rest params)
103 (list (lirc-as-string tags)
105 (upcase (lirc-as-string cmd))
106 (mapcar #'lirc-as-string
107 (if (and params (listp (elt params 0)))
111 (defun lirc-msg-tags (msg) (elt msg 0))
112 (defun lirc-msg-src (msg) (elt msg 1))
113 (defun lirc-msg-cmd (msg) (elt msg 2))
114 (defun lirc-msg-params (msg) (elt msg 3))
115 (defun lirc-msg-trail (msg)
116 (let ((params (lirc-msg-params msg)))
118 (elt params (- (length params) 1)))))
120 (defvar lirc-msg-regex
122 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
124 (opt (: ":" (group (* (not (or "\n" "\r" " ")))))
126 (group (: (* (not whitespace))))
128 (opt (group (+ not-newline)))))
130 (defun lirc-string->msg (string)
131 (if (string-match lirc-msg-regex string)
132 (let* ((tags (match-string 1 string))
133 (src (match-string 2 string))
134 (cmd (upcase (match-string 3 string)))
135 (params-str (match-string 4 string))
138 (let* ((idx (cl-search ":" params-str))
139 (l (split-string (string-trim (substring params-str 0 idx))))
140 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
143 (apply #'lirc-msg (append (list tags src cmd) params)))
144 (error "Failed to parse string " string)))
146 (defun lirc--filtered-join (&rest args)
147 (string-join (seq-filter (lambda (el) el) args) " "))
149 (defun lirc-msg->string (msg)
150 (let ((tags (lirc-msg-tags msg))
151 (src (lirc-msg-src msg))
152 (cmd (lirc-msg-cmd msg))
153 (params (lirc-msg-params msg)))
155 (if tags (concat "@" tags) nil)
156 (if src (concat ":" src) nil)
158 (if (> (length params) 1)
159 (string-join (seq-take params (- (length params) 1)) " ")
161 (if (> (length params) 0)
162 (concat ":" (elt params (- (length params) 1)))
169 (defun lirc-display-string (string)
170 (with-current-buffer (get-buffer-create "*lirc*")
172 (goto-char lirc-prompt-marker)
173 (let ((inhibit-read-only t))
174 (insert-before-markers (propertize (concat string "\n") 'read-only t))))))
176 (defun lirc-connect ()
177 (lirc-send-msg (lirc-msg nil nil "USER" lirc-user-name 0 "*" lirc-full-name))
178 (lirc-send-msg (lirc-msg nil nil "NICK" lirc-nick)))
180 (defun lirc-send-msg (msg)
181 (let ((proc (lirc-get-process)))
182 (process-send-string proc (concat (lirc-msg->string msg) "\r\n"))))
184 (defun lirc-render-prompt ()
185 (with-current-buffer "*lirc*"
186 (set-marker-insertion-type lirc-prompt-marker nil)
187 (set-marker-insertion-type lirc-input-marker t)
188 (let ((inhibit-read-only t))
189 (delete-region lirc-prompt-marker lirc-input-marker))
191 (goto-char lirc-prompt-marker)
192 (let ((inhibit-read-only t))
194 (propertize (if lirc-current-channel
199 (propertize lirc-prompt-string
202 'rear-nonsticky t))))
203 (set-marker-insertion-type lirc-input-marker nil)))
205 (defvar lirc-prompt-marker nil
206 "Marker for prompt position in LIRC buffer.")
208 (defvar lirc-input-marker nil
209 "Marker for prompt position in LIRC buffer.")
211 (defun lirc-setup-buffer ()
212 (with-current-buffer (get-buffer-create "*lirc*")
213 (if (markerp lirc-prompt-marker)
214 (set-marker lirc-prompt-marker (point-max))
215 (setq lirc-prompt-marker (point-max-marker)))
216 (if (markerp lirc-input-marker)
217 (set-marker lirc-input-marker (point-max))
218 (setq lirc-input-marker (point-max-marker)))
220 (goto-char (point-max))
223 ;;; Message evaluation
226 (defun lirc-eval-msg-string (string)
227 (let* ((msg (lirc-string->msg string)))
229 ((equal (lirc-msg-cmd msg) "PING")
231 (lirc-msg nil nil "PONG" (lirc-msg-params msg))))
232 ((string-match (rx (= 3 digit)) (lirc-msg-cmd msg))
233 (lirc-display-string (string-join (cdr (lirc-msg-params msg)) " ")))
235 (lirc-display-string (lirc-msg->string msg))))))
241 (defun lirc-enter-string (string)
242 (cond ((string-prefix-p "/" string)
243 (let ((cmd-str (substring string 1)))
244 (lirc-send-msg (lirc-msg nil nil cmd-str))))
246 (error "Unknown command" string))))
249 "Enter current contents of line after prompt."
251 (with-current-buffer "*lirc*"
253 (buffer-substring lirc-input-marker (point-max)))
254 (let ((inhibit-read-only t))
255 (delete-region lirc-input-marker (point-max)))))
262 (defvar lirc-mode-map
263 (let ((map (make-sparse-keymap)))
264 (define-key map (kbd "RET") 'lirc-enter)
267 (define-derived-mode lirc-mode text-mode "lirc"
268 "Major mode for LIRC.")
270 (when (fboundp 'evil-set-initial-state)
271 (evil-set-initial-state 'lirc-mode 'insert))
273 ;;; Main start procedure
277 "Switch to *lirc* buffer."
279 (if (get-buffer "*lirc*")
280 (switch-to-buffer "*lirc*")
281 (switch-to-buffer "*lirc*"))
289 ;;; lirc.el ends here