Commands are removed from prompt after sending.
[lurk.git] / lirc.el
1 ;;; lirc.el --- Lightweight irc client  -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2021 Tim Vaughan
4
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 14 June 2021
7 ;; Version: 1.0
8 ;; Keywords: network
9 ;; Homepage: http://thelambdalab.xyz/erc
10 ;; Package-Requires: ((emacs "26"))
11
12 ;; This file is not part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (provide 'lerc)
32
33 ;;; Customizations
34 ;;
35
36 (defgroup lirc nil
37   "Lightweight IRC client."
38   :group 'network)
39
40 (defcustom lirc-nick "plugd"
41   "Default nick.")
42 (defcustom lirc-full-name "plugd"
43   "Default full name.")
44 (defcustom lirc-user-name "plugd"
45   "Default user name.")
46 (defcustom lirc-host "irc.libera.chat"
47   "Default server.")
48 (defcustom lirc-port 6667
49   "Default port.")
50
51 (defcustom lirc-prompt-string "> "
52   "Prompt.")
53
54 ;;; Faces
55 ;;
56
57 (defface lirc-prompt
58   '((t :inherit org-level-2))
59   "Face used for the prompt.")
60
61 (defface lirc-channel
62   '((t :inherit org-list-dt))
63   "Face used for the channel name in the prompt.")
64
65 ;;; Global variables
66 ;;
67
68 (defvar lirc-current-channel nil)
69 (defvar lirc-channel-list nil)
70
71 (defvar lirc-response "")
72
73
74 ;;; Network process
75 ;;
76
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))))
82
83 (defun lirc-get-process ()
84   (let ((proc (get-process "lirc")))
85     (if (and proc (eq (process-status proc) 'open))
86         proc
87       (make-network-process :name "lirc"
88                             :host lirc-host
89                             :service lirc-port
90                             :filter #'lirc-filter
91                             :nowait t
92                             :buffer "*lirc*"))))
93
94 ;;; Messages
95 ;;
96
97 (defun lirc-as-string (obj)
98   (if obj
99       (with-output-to-string (princ obj))
100     nil))
101
102 (defun lirc-msg (tags src cmd &rest params)
103   (list (lirc-as-string tags)
104         (lirc-as-string src)
105         (upcase (lirc-as-string cmd))
106         (mapcar #'lirc-as-string
107                 (if (and params (listp (elt params 0)))
108                     (elt params 0)
109                   params))))
110
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)))
117     (if params
118         (elt params (- (length params) 1)))))
119
120 (defvar lirc-msg-regex
121   (rx
122    (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
123         (* whitespace))
124    (opt (: ":" (group (* (not (or "\n" "\r" " ")))))
125         (* whitespace))
126    (group (: (* (not whitespace))))
127    (* whitespace)
128    (opt (group (+ not-newline)))))
129
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))
136              (params
137               (if params-str
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)))
141                     (append l r))
142                 nil)))
143         (apply #'lirc-msg (append (list tags src cmd) params)))
144     (error "Failed to parse string " string)))
145
146 (defun lirc--filtered-join (&rest args)
147   (string-join (seq-filter (lambda (el) el) args) " "))
148
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)))
154     (lirc--filtered-join
155      (if tags (concat "@" tags) nil)
156      (if src (concat ":" src) nil)
157      cmd
158      (if (> (length params) 1)
159          (string-join (seq-take params (- (length params) 1)) " ")
160        nil)
161      (if (> (length params) 0)
162          (concat ":" (elt params (- (length params) 1)))
163        nil))))
164
165
166 ;;; Buffer
167 ;;
168
169 (defun lirc-display-string (string)
170   (with-current-buffer (get-buffer-create "*lirc*")
171     (save-excursion
172       (goto-char lirc-prompt-marker)
173       (let ((inhibit-read-only t))
174         (insert-before-markers (propertize (concat string "\n") 'read-only t))))))
175
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)))
179
180 (defun lirc-send-msg (msg)
181   (let ((proc (lirc-get-process)))
182     (process-send-string proc (concat (lirc-msg->string msg) "\r\n"))))
183
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))
190     (save-excursion
191       (goto-char lirc-prompt-marker)
192       (let ((inhibit-read-only t))
193         (insert
194          (propertize (if lirc-current-channel
195                          lirc-current-channel
196                        "[no channel]")
197                      'face 'lirc-channel
198                      'read-only t)
199          (propertize lirc-prompt-string
200                      'face 'lirc-prompt
201                      'read-only t
202                      'rear-nonsticky t))))
203     (set-marker-insertion-type lirc-input-marker nil)))
204   
205 (defvar lirc-prompt-marker nil
206   "Marker for prompt position in LIRC buffer.")
207
208 (defvar lirc-input-marker nil
209   "Marker for prompt position in LIRC buffer.")
210
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)))
219     (lirc-render-prompt)
220     (goto-char (point-max))
221     (recenter -1)))
222
223 ;;; Message evaluation
224 ;;
225
226 (defun lirc-eval-msg-string (string)
227   (let* ((msg (lirc-string->msg string)))
228     (cond
229      ((equal (lirc-msg-cmd msg) "PING")
230       (lirc-send-msg
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)) " ")))
234      (t
235       (lirc-display-string (lirc-msg->string msg))))))
236
237
238 ;;; Command entering
239 ;;
240
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))))
245         (t
246          (error "Unknown command" string))))
247
248 (defun lirc-enter ()
249   "Enter current contents of line after prompt."
250   (interactive)
251   (with-current-buffer "*lirc*"
252     (lirc-enter-string
253      (buffer-substring lirc-input-marker (point-max)))
254     (let ((inhibit-read-only t))
255       (delete-region lirc-input-marker (point-max)))))
256
257
258
259 ;;; Mode
260 ;;
261
262 (defvar lirc-mode-map
263   (let ((map (make-sparse-keymap)))
264     (define-key map (kbd "RET") 'lirc-enter)
265     map))
266
267 (define-derived-mode lirc-mode text-mode "lirc"
268   "Major mode for LIRC.")
269
270 (when (fboundp 'evil-set-initial-state)
271   (evil-set-initial-state 'lirc-mode 'insert))
272
273 ;;; Main start procedure
274 ;;
275
276 (defun lirc ()
277   "Switch to *lirc* buffer."
278   (interactive)
279   (if (get-buffer "*lirc*")
280       (switch-to-buffer "*lirc*")
281     (switch-to-buffer "*lirc*"))
282   (lirc-mode)
283   (lirc-setup-buffer)
284   (lirc-connect)
285   "Started LIRC.")
286
287
288
289 ;;; lirc.el ends here