1 ;;; lirc.el --- Lambdalabs 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 "> "
60 (defvar lirc-current-channel nil)
61 (defvar lirc-channel-list nil)
63 (defvar lirc-response "")
65 (defun lirc-filter (proc string)
66 (dolist (line (split-string (concat lirc-response string) "\n"))
67 (if (string-suffix-p "\r" line)
68 (lirc-process-msg-string (string-trim line))
69 (setq lirc-response line))))
71 (defun lirc-get-process ()
72 (let ((proc (get-process "lirc")))
73 (if (and proc (eq (process-status proc) 'open))
75 (make-network-process :name "lirc"
82 (defun lirc-as-string (obj)
84 (with-output-to-string (princ obj))
87 (defun lirc-msg (tags src cmd &rest params)
88 (list (lirc-as-string tags)
90 (upcase (lirc-as-string cmd))
91 (mapcar #'lirc-as-string
92 (if (and params (listp (elt params 0)))
96 (defun lirc-msg-tags (msg) (elt msg 0))
97 (defun lirc-msg-src (msg) (elt msg 1))
98 (defun lirc-msg-cmd (msg) (elt msg 2))
99 (defun lirc-msg-params (msg) (elt msg 3))
100 (defun lirc-msg-trail (msg)
101 (let ((params (lirc-msg-params msg)))
103 (elt params (- (length params) 1)))))
105 (defvar lirc-msg-regex
107 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
109 (opt (: ":" (group (* (not (or "\n" "\r" " ")))))
111 (group (: (* (not whitespace))))
113 (opt (group (+ not-newline)))))
115 (defun lirc-string->msg (string)
116 (if (string-match lirc-msg-regex string)
117 (let* ((tags (match-string 1 string))
118 (src (match-string 2 string))
119 (cmd (upcase (match-string 3 string)))
120 (params-str (match-string 4 string))
123 (let* ((idx (cl-search ":" params-str))
124 (l (split-string (string-trim (substring params-str 0 idx))))
125 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
128 (apply #'lirc-msg (append (list tags src cmd) params)))
129 (error "Failed to parse string " string)))
131 (defun lirc--filtered-join (&rest args)
132 (string-join (seq-filter (lambda (el) el) args) " "))
134 (defun lirc-msg->string (msg)
135 (let ((tags (lirc-msg-tags msg))
136 (src (lirc-msg-src msg))
137 (cmd (lirc-msg-cmd msg))
138 (params (lirc-msg-params msg)))
140 (if tags (concat "@" tags) nil)
141 (if src (concat ":" src) nil)
143 (if (> (length params) 1)
144 (string-join (seq-take params (- (length params) 1)) " ")
146 (if (> (length params) 0)
147 (concat ":" (elt params (- (length params) 1)))
150 (defun lirc-display-string (string)
151 (with-current-buffer "*lirc*"
152 (let ((inhibit-read-only t))
154 (goto-char (point-max))
155 (insert (propertize (concat string "\n") 'read-only t))))))
157 (defun lirc-process-msg-string (string)
158 (let* ((msg (lirc-string->msg string)))
160 ((equal (lirc-msg-cmd msg) "PING")
162 (lirc-msg nil nil "PONG" (lirc-msg-params msg))))
163 ((string-match (rx (= 3 digit)) (lirc-msg-cmd msg))
164 (lirc-display-string (string-join (cdr (lirc-msg-params msg)) " ")))
166 (lirc-display-string (lirc-msg->string msg))))))
168 (defun lirc-connect ()
169 (lirc-send-msg (lirc-msg nil nil "USER" lirc-user-name 0 "*" lirc-full-name))
170 (lirc-send-msg (lirc-msg nil nil "NICK" lirc-nick)))
172 (defun lirc-send-msg (msg)
173 (let ((proc (lirc-get-process)))
174 (process-send-string proc (concat (lirc-msg->string msg) "\r\n"))))
178 "Switch to *lirc* buffer."
180 (pop-to-buffer-same-window "*lirc*")
184 (define-derived-mode lirc-mode text-mode "lirc"
185 "Major mode for LIRC.")
188 ;;; lirc.el ends here
189 :bs-mbpr348.d.ethz.ch