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 font-lock-preprocessor-face))
59 "Face used for Lirc text.")
61 (defface lirc-your-nick
62 '((t :inherit font-lock-constant-face))
63 "Face used for highlighting your nick.")
66 '((t :inherit org-level-2))
67 "Face used for the prompt.")
70 '((t :inherit org-list-dt))
71 "Face used for the channel name in the prompt.")
76 (defvar lirc-version "Lirc v0.1")
82 (defvar lirc-response "")
84 (defun lirc-filter (proc string)
85 (dolist (line (split-string (concat lirc-response string) "\n"))
86 (if (string-suffix-p "\r" line)
87 (lirc-eval-msg-string (string-trim line))
88 (setq lirc-response line))))
90 (defun lirc-get-process ()
91 (let ((proc (get-process "lirc")))
92 (if (and proc (eq (process-status proc) 'open))
94 (make-network-process :name "lirc"
104 (defun lirc-as-string (obj)
106 (with-output-to-string (princ obj))
109 (defun lirc-msg (tags src cmd &rest params)
110 (list (lirc-as-string tags)
112 (upcase (lirc-as-string cmd))
113 (mapcar #'lirc-as-string
114 (if (and params (listp (elt params 0)))
118 (defun lirc-msg-tags (msg) (elt msg 0))
119 (defun lirc-msg-src (msg) (elt msg 1))
120 (defun lirc-msg-cmd (msg) (elt msg 2))
121 (defun lirc-msg-params (msg) (elt msg 3))
122 (defun lirc-msg-trail (msg)
123 (let ((params (lirc-msg-params msg)))
125 (elt params (- (length params) 1)))))
127 (defvar lirc-msg-regex
129 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
131 (opt (: ":" (: (group (* (not (any space "!" "@"))))
132 (* (not (any space)))))
134 (group (: (* (not whitespace))))
136 (opt (group (+ not-newline))))
137 "Regex used to parse IRC messages.
138 Note that this regex is incomplete. Noteably, we discard the non-nick
139 portion of the source component of the message, as LIRC doesn't use this.")
141 (defun lirc-string->msg (string)
142 (if (string-match lirc-msg-regex string)
143 (let* ((tags (match-string 1 string))
144 (src (match-string 2 string))
145 (cmd (upcase (match-string 3 string)))
146 (params-str (match-string 4 string))
149 (let* ((idx (cl-search ":" params-str))
150 (l (split-string (string-trim (substring params-str 0 idx))))
151 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
154 (apply #'lirc-msg (append (list tags src cmd) params)))
155 (error "Failed to parse string " string)))
157 (defun lirc--filtered-join (&rest args)
158 (string-join (seq-filter (lambda (el) el) args) " "))
160 (defun lirc-msg->string (msg)
161 (let ((tags (lirc-msg-tags msg))
162 (src (lirc-msg-src msg))
163 (cmd (lirc-msg-cmd msg))
164 (params (lirc-msg-params msg)))
166 (if tags (concat "@" tags) nil)
167 (if src (concat ":" src) nil)
169 (if (> (length params) 1)
170 (string-join (seq-take params (- (length params) 1)) " ")
172 (if (> (length params) 0)
173 (concat ":" (elt params (- (length params) 1)))
180 (defvar lirc-current-channel nil)
181 (defvar lirc-channel-list nil)
183 (defun lirc-add-channel (channel-name)
184 (add-to-list 'lirc-channel-list
185 (list channel-name)))
187 (defun lirc-del-channel (channel-name)
188 (setq lirc-channel-list
189 (assoc-delete-all channel-name lirc-channel-list)))
191 (defun lirc-get-channel-users (channel-name)
192 (cdr (assoc channel-name lirc-channel-list)))
194 (defun lirc-set-channel-users (channel-name users)
195 (setcdr (assoc channel-name lirc-channel-list) users))
197 (defun lirc-add-channel-users (channel-name &rest users)
198 (let ((current-users (lirc-get-channel-users channel-name)))
199 (lirc-set-channel-users channel-name (append users current-users))))
201 (defun lirc-del-channel-users (channel-name &rest users)
202 (let ((current-users (lirc-get-channel-users channel-name)))
203 (lirc-set-channel-users channel-name
204 (cl-set-difference current-users users :test #'equal))))
210 (defun lirc-display-string (&rest strings)
211 (with-current-buffer (get-buffer-create "*lirc*")
213 (goto-char lirc-prompt-marker)
214 (let ((inhibit-read-only t))
215 (insert-before-markers
216 (propertize (concat (format-time-string "%H:%M") " ")
219 (propertize (concat (apply #'concat strings) "\n")
222 (defun lirc-connect ()
223 (lirc-send-msg (lirc-msg nil nil "USER" lirc-user-name 0 "*" lirc-full-name))
224 (lirc-send-msg (lirc-msg nil nil "NICK" lirc-nick)))
226 (defun lirc-send-msg (msg)
227 (let ((proc (lirc-get-process)))
228 (process-send-string proc (concat (lirc-msg->string msg) "\r\n"))))
230 (defun lirc-render-prompt ()
231 (with-current-buffer "*lirc*"
232 (set-marker-insertion-type lirc-prompt-marker nil)
233 (set-marker-insertion-type lirc-input-marker t)
235 (let ((inhibit-read-only t))
236 (delete-region lirc-prompt-marker lirc-input-marker)
237 (goto-char lirc-prompt-marker)
239 (propertize (if lirc-current-channel
244 (propertize lirc-prompt-string
247 'rear-nonsticky t)))))
248 (set-marker-insertion-type lirc-input-marker nil))
250 (defvar lirc-prompt-marker nil
251 "Marker for prompt position in LIRC buffer.")
253 (defvar lirc-input-marker nil
254 "Marker for prompt position in LIRC buffer.")
256 (defun lirc-setup-buffer ()
257 (with-current-buffer (get-buffer-create "*lirc*")
258 (if (markerp lirc-prompt-marker)
259 (set-marker lirc-prompt-marker (point-max))
260 (setq lirc-prompt-marker (point-max-marker)))
261 (if (markerp lirc-input-marker)
262 (set-marker lirc-input-marker (point-max))
263 (setq lirc-input-marker (point-max-marker)))
265 (goto-char (point-max))
269 ;;; Output formatting
272 (defun lirc-display-action (channel nick action)
273 (lirc-display-string (concat " * "
274 (propertize (concat nick " " action)
277 (defun lirc-display-message (channel nick message)
280 (propertize (concat channel " ")
282 (propertize (concat "<" nick "> ")
284 (if (equal nick lirc-nick)
287 (propertize message 'face 'lirc-text))))
289 (defun lirc-display-notice (&rest notices)
290 (lirc-display-string "*** " (apply #'concat notices)))
292 ;;; Message evaluation
295 (defun lirc-eval-msg-string (string)
296 ;; (lirc-display-string string)
297 (let* ((msg (lirc-string->msg string)))
298 (pcase (lirc-msg-cmd msg)
301 (lirc-msg nil nil "PONG" (lirc-msg-params msg))))
304 (let* ((params (lirc-msg-params msg))
305 (channel (elt params 2))
306 (names (split-string (elt params 3))))
307 (apply #'lirc-add-channel-users (cons channel names))))
309 ((rx (= 3 (any digit)))
310 (lirc-display-notice (mapconcat 'identity (cdr (lirc-msg-params msg)) " ")))
313 (guard (equal lirc-nick (lirc-msg-src msg))))
314 (let ((channel (car (lirc-msg-params msg))))
315 (setq lirc-current-channel channel)
316 (lirc-add-channel channel)
317 (lirc-render-prompt)))
320 (let ((channel (car (lirc-msg-params msg)))
321 (nick (lirc-msg-src msg)))
322 (lirc-add-channel-users channel nick)))
325 (guard (equal lirc-nick (lirc-msg-src msg))))
326 (setq lirc-current-channel nil)
327 (lirc-del-channel (car (lirc-msg-params msg)))
328 (lirc-render-prompt))
331 (let ((channel (car (lirc-msg-params msg)))
332 (nick (lirc-msg-src msg)))
333 (lirc-del-channel-users channel nick)
334 (lirc-display-notice nick " left channel " channel)))
337 (let ((nick (lirc-msg-src msg))
338 (reason (mapconcat 'identity (lirc-msg-params msg) " ")))
339 (lirc-del-users nick)
340 (lirc-display-notice nick " quit: " reason)))
343 (guard (equal lirc-nick (lirc-msg-src msg))))
344 (setq lirc-nick (car (lirc-msg-params msg)))
345 (lirc-display-notice "Set nick to " lirc-nick))
348 (let ((nick (lirc-msg-src msg))
349 (channel (car (lirc-msg-params msg)))
350 (text (cadr (lirc-msg-params msg))))
352 ((rx (: "\01VERSION "
353 (let version (* (not "\01")))
355 (lirc-display-notice "CTCP version reply from " nick ": " version)))))
358 (let ((nick (lirc-msg-src msg))
359 (channel (car (lirc-msg-params msg)))
360 (text (cadr (lirc-msg-params msg))))
363 (let action (* (not "\01")))
365 (lirc-display-action nick action))
368 (let ((version-string (concat lirc-version " - running on GNU Emacs " emacs-version)))
369 (lirc-send-msg (lirc-msg nil nil "NOTICE"
370 (list nick (concat "\01VERSION "
373 (lirc-display-notice "CTCP version request received from " nick))
375 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
376 (lirc-send-msg (lirc-msg nil nil "NOTICE" (list nick ping)))
377 (lirc-display-notice "CTCP ping received from " nick))
380 (lirc-display-notice "CTCP userinfo request from " nick " (no response sent)"))
383 (lirc-display-message channel nick text)))))
385 (lirc-display-string (lirc-msg->string msg))))))
391 (defun lirc-enter-string (string)
392 (if (string-prefix-p "/" string)
393 (pcase (substring string 1)
394 ((rx (: "TOPIC" " " (let new-topic (* not-newline))))
395 (lirc-send-msg (lirc-msg nil nil "TOPIC" lirc-current-channel new-topic)))
397 ((rx (: "me" " " (let action (* not-newline))))
398 (lirc-send-msg (lirc-msg nil nil "PRIVMSG"
399 (list lirc-current-channel
400 (concat "\01ACTION " action "\01"))))
401 (lirc-display-action lirc-nick action))
403 ((rx (: "VERSION" " " (let nick (* (not whitespace)))))
404 (lirc-send-msg (lirc-msg nil nil "PRIVMSG"
405 (list nick "\01VERSION\01")))
406 (lirc-display-notice "CTCP version request sent to " nick))
409 (lirc-send-msg (lirc-msg nil nil "PART" lirc-current-channel)))
411 ((rx (: (let cmd-str (+ (not whitespace)))
412 (opt (: " " (let params-str (* not-newline))))))
413 (lirc-send-msg (lirc-msg nil nil (upcase cmd-str)
415 (split-string params-str)
418 (unless (string-empty-p string)
419 (lirc-send-msg (lirc-msg nil nil "PRIVMSG" lirc-current-channel string))
420 (lirc-display-message lirc-current-channel lirc-nick string))))
423 "Enter current contents of line after prompt."
425 (with-current-buffer "*lirc*"
427 (buffer-substring lirc-input-marker (point-max)))
428 (let ((inhibit-read-only t))
429 (delete-region lirc-input-marker (point-max)))))
436 (defvar lirc-mode-map
437 (let ((map (make-sparse-keymap)))
438 (define-key map (kbd "RET") 'lirc-enter)
441 (define-derived-mode lirc-mode text-mode "lirc"
442 "Major mode for LIRC.")
444 (when (fboundp 'evil-set-initial-state)
445 (evil-set-initial-state 'lirc-mode 'insert))
447 ;;; Main start procedure
451 "Switch to *lirc* buffer."
453 (if (get-buffer "*lirc*")
454 (switch-to-buffer "*lirc*")
455 (switch-to-buffer "*lirc*"))
463 ;;; lirc.el ends here