131b7ccb42e44cd9c818c03ae643a7375287c1af
[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 'lirc)
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 "localhost"
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     (save-excursion
189       (let ((inhibit-read-only t))
190         (delete-region lirc-prompt-marker lirc-input-marker)
191         (goto-char lirc-prompt-marker)
192         (insert
193          (propertize (if lirc-current-channel
194                          lirc-current-channel
195                        "[no channel]")
196                      'face 'lirc-channel
197                      'read-only t)
198          (propertize lirc-prompt-string
199                      'face 'lirc-prompt
200                      'read-only t
201                      'rear-nonsticky t)))))
202   (set-marker-insertion-type lirc-input-marker nil))
203   
204 (defvar lirc-prompt-marker nil
205   "Marker for prompt position in LIRC buffer.")
206
207 (defvar lirc-input-marker nil
208   "Marker for prompt position in LIRC buffer.")
209
210 (defun lirc-setup-buffer ()
211   (with-current-buffer (get-buffer-create "*lirc*")
212     (if (markerp lirc-prompt-marker)
213         (set-marker lirc-prompt-marker (point-max))
214       (setq lirc-prompt-marker (point-max-marker)))
215     (if (markerp lirc-input-marker)
216         (set-marker lirc-input-marker (point-max))
217       (setq lirc-input-marker (point-max-marker)))
218     (lirc-render-prompt)
219     (goto-char (point-max))
220     (recenter -1)))
221
222 ;;; Message evaluation
223 ;;
224
225 (defun lirc-eval-msg-string (string)
226   (let* ((msg (lirc-string->msg string)))
227     (pcase (lirc-msg-cmd msg)
228      ("PING"
229       (lirc-send-msg
230        (lirc-msg nil nil "PONG" (lirc-msg-params msg))))
231      ;; ((rx (= 3 digit))
232      ;;  (lirc-display-string (string-join (cdr (lirc-msg-params msg)) " ")))
233      ((and "JOIN" (let (rx (: (literal lirc-nick) "!" (* anychar))) (lirc-msg-src msg)))
234       (let ((channel (car (lirc-msg-params msg))))
235         (setq lirc-current-channel channel)
236         (add-to-list 'lirc-channel-list channel)
237         (lirc-render-prompt)))
238      (_
239       (lirc-display-string (lirc-msg->string msg))))))
240
241
242 ;;; Command entering
243 ;;
244
245 (defun lirc-enter-string (string)
246   (cond ((string-prefix-p "/" string)
247          (let ((cmd-str (substring string 1)))
248            (lirc-send-msg (lirc-msg nil nil cmd-str))))
249         (t
250          (error "Unknown command" string))))
251
252 (defun lirc-enter ()
253   "Enter current contents of line after prompt."
254   (interactive)
255   (with-current-buffer "*lirc*"
256     (lirc-enter-string
257      (buffer-substring lirc-input-marker (point-max)))
258     (let ((inhibit-read-only t))
259       (delete-region lirc-input-marker (point-max)))))
260
261
262
263 ;;; Mode
264 ;;
265
266 (defvar lirc-mode-map
267   (let ((map (make-sparse-keymap)))
268     (define-key map (kbd "RET") 'lirc-enter)
269     map))
270
271 (define-derived-mode lirc-mode text-mode "lirc"
272   "Major mode for LIRC.")
273
274 (when (fboundp 'evil-set-initial-state)
275   (evil-set-initial-state 'lirc-mode 'insert))
276
277 ;;; Main start procedure
278 ;;
279
280 (defun lirc ()
281   "Switch to *lirc* buffer."
282   (interactive)
283   (if (get-buffer "*lirc*")
284       (switch-to-buffer "*lirc*")
285     (switch-to-buffer "*lirc*"))
286   (lirc-mode)
287   (lirc-setup-buffer)
288   (lirc-connect)
289   "Started LIRC.")
290
291
292
293 ;;; lirc.el ends here