Read-only behaviour is now correct.
[lurk.git] / lirc.el
1 ;;; lirc.el --- Lambdalabs 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 "localhost"
47   "Default server.")
48 (defcustom lirc-port 6667
49   "Default port.")
50
51 (defcustom lirc-prompt-string "prompt> "
52   "Prompt.")
53
54 ;;; Faces
55 ;;
56
57 (defface lirc-prompt
58   '((t :inherit font-lock-string-face))
59   "Face used for the prompt.")
60
61 ;;; Global variables
62 ;;
63
64 (defvar lirc-current-channel nil)
65 (defvar lirc-channel-list nil)
66
67 (defvar lirc-response "")
68
69
70 ;;; Network process
71 ;;
72
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))))
78
79 (defun lirc-get-process ()
80   (let ((proc (get-process "lirc")))
81     (if (and proc (eq (process-status proc) 'open))
82         proc
83       (make-network-process :name "lirc"
84                             :host lirc-host
85                             :service lirc-port
86                             :filter #'lirc-filter
87                             :nowait t
88                             :buffer "*lirc*"))))
89
90 ;;; Messages
91 ;;
92
93 (defun lirc-as-string (obj)
94   (if obj
95       (with-output-to-string (princ obj))
96     nil))
97
98 (defun lirc-msg (tags src cmd &rest params)
99   (list (lirc-as-string tags)
100         (lirc-as-string src)
101         (upcase (lirc-as-string cmd))
102         (mapcar #'lirc-as-string
103                 (if (and params (listp (elt params 0)))
104                     (elt params 0)
105                   params))))
106
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)))
113     (if params
114         (elt params (- (length params) 1)))))
115
116 (defvar lirc-msg-regex
117   (rx
118    (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
119         (* whitespace))
120    (opt (: ":" (group (* (not (or "\n" "\r" " ")))))
121         (* whitespace))
122    (group (: (* (not whitespace))))
123    (* whitespace)
124    (opt (group (+ not-newline)))))
125
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))
132              (params
133               (if params-str
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)))
137                     (append l r))
138                 nil)))
139         (apply #'lirc-msg (append (list tags src cmd) params)))
140     (error "Failed to parse string " string)))
141
142 (defun lirc--filtered-join (&rest args)
143   (string-join (seq-filter (lambda (el) el) args) " "))
144
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)))
150     (lirc--filtered-join
151      (if tags (concat "@" tags) nil)
152      (if src (concat ":" src) nil)
153      cmd
154      (if (> (length params) 1)
155          (string-join (seq-take params (- (length params) 1)) " ")
156        nil)
157      (if (> (length params) 0)
158          (concat ":" (elt params (- (length params) 1)))
159        nil))))
160
161
162 ;;; Buffer
163 ;;
164
165 (defun lirc-display-string (string)
166   (with-current-buffer (get-buffer-create "*lirc*")
167     (save-excursion
168       (goto-char lirc-prompt-marker)
169       (let ((inhibit-read-only t))
170         (insert-before-markers (propertize (concat string "\n") 'read-only t))))))
171
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)))
175
176 (defun lirc-send-msg (msg)
177   (let ((proc (lirc-get-process)))
178     (process-send-string proc (concat (lirc-msg->string msg) "\r\n"))))
179
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))
186     (save-excursion
187       (goto-char lirc-prompt-marker)
188       (let ((inhibit-read-only t))
189         (insert (propertize lirc-prompt-string
190                             'face 'lirc-prompt
191                             'read-only t
192                             'rear-nonsticky t))))))
193   
194 (defvar lirc-prompt-marker nil
195   "Marker for prompt position in LIRC buffer.")
196
197 (defvar lirc-input-marker nil
198   "Marker for prompt position in LIRC buffer.")
199
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)))
208     (lirc-render-prompt)
209     (goto-char (point-max))
210     (recenter -1)))
211
212 ;;; Message evaluation
213 ;;
214
215 (defun lirc-eval-msg-string (string)
216   (let* ((msg (lirc-string->msg string)))
217     (cond
218      ((equal (lirc-msg-cmd msg) "PING")
219       (lirc-send-msg
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)) " ")))
223      (t
224       (lirc-display-string (lirc-msg->string msg))))))
225
226
227 ;;; Mode
228 ;;
229
230 (defvar lirc-mode-map
231   (let ((map (make-sparse-keymap)))
232     map))
233
234 (define-derived-mode lirc-mode text-mode "lirc"
235   "Major mode for LIRC.")
236
237 (when (fboundp 'evil-set-initial-state)
238   (evil-set-initial-state 'lirc-mode 'insert))
239
240 ;;; Main start procedure
241 ;;
242
243 (defun lirc ()
244   "Switch to *lirc* buffer."
245   (interactive)
246   (if (get-buffer "*lirc*")
247       (switch-to-buffer "*lirc*")
248     (switch-to-buffer "*lirc*"))
249   (lirc-mode)
250   (lirc-setup-buffer)
251   (lirc-connect)
252   "Started LIRC.")
253
254
255
256 ;;; lirc.el ends here