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 "localhost"
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.")
72 (defvar lirc-response "")
74 (defun lirc-filter (proc string)
75 (dolist (line (split-string (concat lirc-response string) "\n"))
76 (if (string-suffix-p "\r" line)
77 (lirc-eval-msg-string (string-trim line))
78 (setq lirc-response line))))
80 (defun lirc-get-process ()
81 (let ((proc (get-process "lirc")))
82 (if (and proc (eq (process-status proc) 'open))
84 (make-network-process :name "lirc"
94 (defun lirc-as-string (obj)
96 (with-output-to-string (princ obj))
99 (defun lirc-msg (tags src cmd &rest params)
100 (list (lirc-as-string tags)
102 (upcase (lirc-as-string cmd))
103 (mapcar #'lirc-as-string
104 (if (and params (listp (elt params 0)))
108 (defun lirc-msg-tags (msg) (elt msg 0))
109 (defun lirc-msg-src (msg) (elt msg 1))
110 (defun lirc-msg-cmd (msg) (elt msg 2))
111 (defun lirc-msg-params (msg) (elt msg 3))
112 (defun lirc-msg-trail (msg)
113 (let ((params (lirc-msg-params msg)))
115 (elt params (- (length params) 1)))))
117 (defvar lirc-msg-regex
119 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
121 (opt (: ":" (: (group (* (not (any space "!" "@"))))
122 (* (not (any space)))))
124 (group (: (* (not whitespace))))
126 (opt (group (+ not-newline))))
127 "Regex used to parse IRC messages.
128 Note that this regex is incomplete. Noteably, we discard the non-nick
129 portion of the source component of the message, as LIRC doesn't use this.")
131 (defun lirc-string->msg (string)
132 (if (string-match lirc-msg-regex string)
133 (let* ((tags (match-string 1 string))
134 (src (match-string 2 string))
135 (cmd (upcase (match-string 3 string)))
136 (params-str (match-string 4 string))
139 (let* ((idx (cl-search ":" params-str))
140 (l (split-string (string-trim (substring params-str 0 idx))))
141 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
144 (apply #'lirc-msg (append (list tags src cmd) params)))
145 (error "Failed to parse string " string)))
147 (defun lirc--filtered-join (&rest args)
148 (string-join (seq-filter (lambda (el) el) args) " "))
150 (defun lirc-msg->string (msg)
151 (let ((tags (lirc-msg-tags msg))
152 (src (lirc-msg-src msg))
153 (cmd (lirc-msg-cmd msg))
154 (params (lirc-msg-params msg)))
156 (if tags (concat "@" tags) nil)
157 (if src (concat ":" src) nil)
159 (if (> (length params) 1)
160 (string-join (seq-take params (- (length params) 1)) " ")
162 (if (> (length params) 0)
163 (concat ":" (elt params (- (length params) 1)))
170 (defvar lirc-current-channel nil)
171 (defvar lirc-channel-list nil)
173 (defun lirc-add-channel (channel-name)
174 (add-to-list 'lirc-channel-list
175 (list channel-name)))
177 (defun lirc-del-channel (channel-name)
178 (setq lirc-channel-list
179 (assoc-delete-all channel-name lirc-channel-list)))
181 (defun lirc-get-channel-users (channel-name)
182 (cdr (assoc channel-name lirc-channel-list)))
184 (defun lirc-set-channel-users (channel-name users)
185 (setcdr (assoc channel-name lirc-channel-list) users))
187 (defun lirc-add-channel-users (channel-name &rest users)
188 (let ((current-users (lirc-get-channel-users channel-name)))
189 (lirc-set-channel-users channel-name (append users current-users))))
191 (defun lirc-del-channel-users (channel-name &rest users)
192 (let ((current-users (lirc-get-channel-users channel-name)))
193 (lirc-channel-set-users channel-name
194 (cl-set-difference current-users users :test #'equal))))
200 (defun lirc-display-string (string)
201 (with-current-buffer (get-buffer-create "*lirc*")
203 (goto-char lirc-prompt-marker)
204 (let ((inhibit-read-only t))
205 (insert-before-markers (propertize (concat string "\n") 'read-only t))))))
207 (defun lirc-connect ()
208 (lirc-send-msg (lirc-msg nil nil "USER" lirc-user-name 0 "*" lirc-full-name))
209 (lirc-send-msg (lirc-msg nil nil "NICK" lirc-nick)))
211 (defun lirc-send-msg (msg)
212 (let ((proc (lirc-get-process)))
213 (process-send-string proc (concat (lirc-msg->string msg) "\r\n"))))
215 (defun lirc-render-prompt ()
216 (with-current-buffer "*lirc*"
217 (set-marker-insertion-type lirc-prompt-marker nil)
218 (set-marker-insertion-type lirc-input-marker t)
220 (let ((inhibit-read-only t))
221 (delete-region lirc-prompt-marker lirc-input-marker)
222 (goto-char lirc-prompt-marker)
224 (propertize (if lirc-current-channel
229 (propertize lirc-prompt-string
232 'rear-nonsticky t)))))
233 (set-marker-insertion-type lirc-input-marker nil))
235 (defvar lirc-prompt-marker nil
236 "Marker for prompt position in LIRC buffer.")
238 (defvar lirc-input-marker nil
239 "Marker for prompt position in LIRC buffer.")
241 (defun lirc-setup-buffer ()
242 (with-current-buffer (get-buffer-create "*lirc*")
243 (if (markerp lirc-prompt-marker)
244 (set-marker lirc-prompt-marker (point-max))
245 (setq lirc-prompt-marker (point-max-marker)))
246 (if (markerp lirc-input-marker)
247 (set-marker lirc-input-marker (point-max))
248 (setq lirc-input-marker (point-max-marker)))
250 (goto-char (point-max))
253 ;;; Message evaluation
256 (defun lirc-eval-msg-string (string)
257 (lirc-display-string string)
258 (let* ((msg (lirc-string->msg string)))
259 (pcase (lirc-msg-cmd msg)
262 (lirc-msg nil nil "PONG" (lirc-msg-params msg))))
264 ;; (lirc-display-string (string-join (cdr (lirc-msg-params msg)) " ")))
267 (let* ((params (lirc-msg-params msg))
268 (channel (elt params 2))
269 (names (split-string (elt params 3))))
270 (apply #'lirc-add-channel-users (cons channel names))))
273 (guard (equal lirc-nick (lirc-msg-src msg))))
274 (let ((channel (car (lirc-msg-params msg))))
275 (setq lirc-current-channel channel)
276 (lirc-add-channel channel)
277 (lirc-render-prompt)))
280 (let ((channel (car (lirc-msg-params msg)))
281 (nick (lirc-msg-src msg)))
282 (lirc-add-channel-users channel nick)))
285 (guard (equal lirc-nick (lirc-msg-src msg))))
286 (setq lirc-current-channel nil)
287 (lirc-del-channel (car (lirc-msg-params msg))))
290 (let ((channel (car (lirc-msg-params msg)))
291 (nick (lirc-msg-src msg)))
292 (lirc-del-channel-users channel nick)))
295 (guard (equal lirc-nick (lirc-msg-src msg))))
296 (setq lirc-nick (car (lirc-msg-params msg)))
297 (lirc-display-string (concat "*** Set nick to " lirc-nick)))
300 (let ((nick (lirc-msg-src msg))
301 (channel (car (lirc-msg-params msg)))
302 (text (cadr (lirc-msg-params msg))))
303 (lirc-display-string (concat channel " <" nick "> " text))))
305 (lirc-display-string (lirc-msg->string msg))))))
311 (defun lirc-enter-string (string)
312 (if (string-prefix-p "/" string)
313 (pcase (substring string 1)
314 ((rx (: (let cmd-str (+ (not space))))
317 (let params-str (+ anything)))))
318 (lirc-send-msg (lirc-msg nil nil
321 (split-string params-str)
323 (lirc-send-msg (lirc-msg nil nil "PRIVMSG" lirc-current-channel string))))
326 "Enter current contents of line after prompt."
328 (with-current-buffer "*lirc*"
330 (buffer-substring lirc-input-marker (point-max)))
331 (let ((inhibit-read-only t))
332 (delete-region lirc-input-marker (point-max)))))
339 (defvar lirc-mode-map
340 (let ((map (make-sparse-keymap)))
341 (define-key map (kbd "RET") 'lirc-enter)
344 (define-derived-mode lirc-mode text-mode "lirc"
345 "Major mode for LIRC.")
347 (when (fboundp 'evil-set-initial-state)
348 (evil-set-initial-state 'lirc-mode 'insert))
350 ;;; Main start procedure
354 "Switch to *lirc* buffer."
356 (if (get-buffer "*lirc*")
357 (switch-to-buffer "*lirc*")
358 (switch-to-buffer "*lirc*"))
366 ;;; lirc.el ends here