1 ;;; lirc.el --- Lambdalabs 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 "localhost"
48 (defcustom lirc-port 6667
51 (defcustom lirc-prompt-string "prompt> "
58 '((t :inherit font-lock-string-face))
59 "Face used for the prompt.")
64 (defvar lirc-current-channel nil)
65 (defvar lirc-channel-list nil)
67 (defvar lirc-response "")
73 (defun lirc-filter (proc string)
74 (dolist (line (split-string (concat lirc-response string) "\n"))
75 (if (string-suffix-p "\r" line)
76 (lirc-eval-msg-string (string-trim line))
77 (setq lirc-response line))))
79 (defun lirc-get-process ()
80 (let ((proc (get-process "lirc")))
81 (if (and proc (eq (process-status proc) 'open))
83 (make-network-process :name "lirc"
93 (defun lirc-as-string (obj)
95 (with-output-to-string (princ obj))
98 (defun lirc-msg (tags src cmd &rest params)
99 (list (lirc-as-string tags)
101 (upcase (lirc-as-string cmd))
102 (mapcar #'lirc-as-string
103 (if (and params (listp (elt params 0)))
107 (defun lirc-msg-tags (msg) (elt msg 0))
108 (defun lirc-msg-src (msg) (elt msg 1))
109 (defun lirc-msg-cmd (msg) (elt msg 2))
110 (defun lirc-msg-params (msg) (elt msg 3))
111 (defun lirc-msg-trail (msg)
112 (let ((params (lirc-msg-params msg)))
114 (elt params (- (length params) 1)))))
116 (defvar lirc-msg-regex
118 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
120 (opt (: ":" (group (* (not (or "\n" "\r" " ")))))
122 (group (: (* (not whitespace))))
124 (opt (group (+ not-newline)))))
126 (defun lirc-string->msg (string)
127 (if (string-match lirc-msg-regex string)
128 (let* ((tags (match-string 1 string))
129 (src (match-string 2 string))
130 (cmd (upcase (match-string 3 string)))
131 (params-str (match-string 4 string))
134 (let* ((idx (cl-search ":" params-str))
135 (l (split-string (string-trim (substring params-str 0 idx))))
136 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
139 (apply #'lirc-msg (append (list tags src cmd) params)))
140 (error "Failed to parse string " string)))
142 (defun lirc--filtered-join (&rest args)
143 (string-join (seq-filter (lambda (el) el) args) " "))
145 (defun lirc-msg->string (msg)
146 (let ((tags (lirc-msg-tags msg))
147 (src (lirc-msg-src msg))
148 (cmd (lirc-msg-cmd msg))
149 (params (lirc-msg-params msg)))
151 (if tags (concat "@" tags) nil)
152 (if src (concat ":" src) nil)
154 (if (> (length params) 1)
155 (string-join (seq-take params (- (length params) 1)) " ")
157 (if (> (length params) 0)
158 (concat ":" (elt params (- (length params) 1)))
165 (defun lirc-display-string (string)
166 (with-current-buffer (get-buffer-create "*lirc*")
168 (goto-char lirc-prompt-marker)
169 (let ((inhibit-read-only t))
170 (insert-before-markers (propertize (concat string "\n") 'read-only t))))))
172 (defun lirc-connect ()
173 (lirc-send-msg (lirc-msg nil nil "USER" lirc-user-name 0 "*" lirc-full-name))
174 (lirc-send-msg (lirc-msg nil nil "NICK" lirc-nick)))
176 (defun lirc-send-msg (msg)
177 (let ((proc (lirc-get-process)))
178 (process-send-string proc (concat (lirc-msg->string msg) "\r\n"))))
180 (defun lirc-render-prompt ()
181 (with-current-buffer "*lirc*"
182 (set-marker-insertion-type lirc-prompt-marker nil)
183 (set-marker-insertion-type lirc-input-marker t)
184 (let ((inhibit-read-only t))
185 (delete-region lirc-prompt-marker lirc-input-marker))
187 (goto-char lirc-prompt-marker)
188 (let ((inhibit-read-only t))
189 (insert (propertize lirc-prompt-string
192 'rear-nonsticky t))))))
194 (defvar lirc-prompt-marker nil
195 "Marker for prompt position in LIRC buffer.")
197 (defvar lirc-input-marker nil
198 "Marker for prompt position in LIRC buffer.")
200 (defun lirc-setup-buffer ()
201 (with-current-buffer (get-buffer-create "*lirc*")
202 (if (markerp lirc-prompt-marker)
203 (set-marker lirc-prompt-marker (point-max))
204 (setq lirc-prompt-marker (point-max-marker)))
205 (if (markerp lirc-input-marker)
206 (set-marker lirc-input-marker (point-max))
207 (setq lirc-input-marker (point-max-marker)))
209 (goto-char (point-max))
212 ;;; Message evaluation
215 (defun lirc-eval-msg-string (string)
216 (let* ((msg (lirc-string->msg string)))
218 ((equal (lirc-msg-cmd msg) "PING")
220 (lirc-msg nil nil "PONG" (lirc-msg-params msg))))
221 ((string-match (rx (= 3 digit)) (lirc-msg-cmd msg))
222 (lirc-display-string (string-join (cdr (lirc-msg-params msg)) " ")))
224 (lirc-display-string (lirc-msg->string msg))))))
230 (defvar lirc-mode-map
231 (let ((map (make-sparse-keymap)))
234 (define-derived-mode lirc-mode text-mode "lirc"
235 "Major mode for LIRC.")
237 (when (fboundp 'evil-set-initial-state)
238 (evil-set-initial-state 'lirc-mode 'insert))
240 ;;; Main start procedure
244 "Switch to *lirc* buffer."
246 (if (get-buffer "*lirc*")
247 (switch-to-buffer "*lirc*")
248 (switch-to-buffer "*lirc*"))
256 ;;; lirc.el ends here