"Default full name.")
(defcustom lirc-user-name "plugd"
"Default user name.")
-(defcustom lirc-host "irc.libera.chat"
- "Default server.")
-(defcustom lirc-port 6697
- "Default port.")
+
+(defcustom lirc-networks
+ '(("libera" "irc.libera.chat" 6697)
+ ("freenode" "chat.freenode.net" 6697)
+ ("local" "localhost" 6697))
+ "IRC networks.")
(defcustom lirc-prompt-string "> "
"Prompt.")
(defface lirc-faded
'((t :inherit font-lock-preprocessor-face))
- "Face used for Lirc text.")
+ "Face used for faded Lirc text.")
(defface lirc-bold
'((t :inherit font-lock-function-name-face))
- "Face used for Lirc text.")
+ "Face used for bold Lirc text.")
+
+(defface lirc-error
+ '((t :inherit font-lock-regexp-grouping-construct))
+ "Face used for Lirc error text.")
;;; Global variables
;;
"!" 'face 'lirc-bold)
(propertize
"-" 'face 'lirc-faded)))
-
+
+(defvar lirc-error-prompt
+ (propertize "!!!" 'face 'lirc-error))
;;; Network process
;;
(lirc-eval-msg-string (string-trim line))
(setq lirc-response line))))
-(defun lirc-get-process ()
+
+(defun lirc-start-process (network)
+ (let* ((row (assoc network lirc-networks))
+ (host (elt row 1))
+ (port (elt row 2)))
+ (make-network-process :name "lirc"
+ :host host
+ :service port
+ :filter #'lirc-filter
+ :nowait nil
+ :tls-parameters (cons 'gnutls-x509pki
+ (gnutls-boot-parameters
+ :type 'gnutls-x509pki
+ :hostname host))
+ :buffer "*lirc*")))
+
+
+(defun lirc-connect (network)
+ (setq lirc-channel-list nil)
+ (setq lirc-current-channel nil)
+ (lirc-start-process network)
+ (lirc-send-msg (lirc-msg nil nil "USER" lirc-user-name 0 "*" lirc-full-name))
+ (lirc-send-msg (lirc-msg nil nil "NICK" lirc-nick)))
+
+(defun lirc-send-msg (msg)
(let ((proc (get-process "lirc")))
(if (and proc (eq (process-status proc) 'open))
- proc
- (make-network-process :name "lirc"
- :host lirc-host
- :service lirc-port
- :filter #'lirc-filter
- :nowait nil
- :tls-parameters (cons 'gnutls-x509pki
- (gnutls-boot-parameters
- :type 'gnutls-x509pki
- :hostname lirc-host))
- :buffer "*lirc*"))))
+ (process-send-string proc (concat (lirc-msg->string msg) "\r\n"))
+ (lirc-display-error "No server connection established.")
+ (error "No server connection established"))))
;;; Messages
;;
nil))))
-;;; Channels
+;;; Channels and users
;;
(defvar lirc-current-channel nil)
-(defvar lirc-channel-list nil)
-
-(defun lirc-add-channel (channel-name)
- (add-to-list 'lirc-channel-list
- (list channel-name)))
-(defun lirc-del-channel (channel-name)
- (setq lirc-channel-list
- (assoc-delete-all channel-name lirc-channel-list)))
-
-(defun lirc-get-channel-users (channel-name)
- (cdr (assoc channel-name lirc-channel-list)))
+(defun lirc-channel (name next prev users)
+ (list name prev next users))
+
+(defun lirc-get-channel-name (channel)
+ (elt channel 0))
+(defun lirc-get-channel-next (channel)
+ (elt channel 1))
+(defun lirc-get-channel-prev (channel)
+ (elt channel 2))
+(defun lirc-get-channel-users (channel)
+ (elt channel 3))
+
+(defun lirc-set-channel-name (channel name)
+ (setf (elt channel 0) name))
+(defun lirc-set-channel-next (channel next)
+ (setf (elt channel 1) next))
+(defun lirc-set-channel-prev (channel prev)
+ (setf (elt channel 2) prev))
+(defun lirc-set-channel-users (channel users)
+ (setf (elt channel 3) users))
+
+(defun lirc-add-channel (new-channel)
+ (if lirc-current-channel
+ (let* ((prev lirc-current-channel)
+ (next (lirc-get-channel-next lirc-current-channel)))
+ (lirc-set-channel-next new-channel prev)
+ (lirc-set-channel-prev new-channel next))
+ (lirc-set-channel-next new-channel new-channel)
+ (lirc-set-channel-prev new-channel new-channel))
+ (setq lirc-current-channel new-channel))
+
+(defun lirc-del-channel (channel)
+ (let ((prev (lirc-get-channel-prev channel))
+ (next (lirc-get-channel-next channel)))
+ (if (and prev next)
+ (if (eq prev next)
+ (progn
+ (lirc-set-channel-next prev nil)
+ (lirc-set-channel-prev prev nil)
+ (setq lirc-current-channel prev))
+ (lirc-set-channel-next prev next)
+ (lirc-set-channel-prev next prev)
+ (if (eq channel lirc-current-channel)
+ (setq lirc-current-channel prev)))
+ (setq lirc-current-channel nil))))
+
+(defun lirc-channel-do (proc)
+ (if lirc-current-channel
+ (let ((channel lirc-current-channel))
+ (funcall proc lirc-current-channel)
+ (while (not (eq (lirc-get-channel-next channel) lirc-current-channel))
+ (setq channel (lirc-get-channel-next channel))
+ (funcall proc channel)))))
+
+(defun lirc-get-channel-with-name (channel-name)
+ (if lirc-current-channel
+ (let ((channel lirc-current-channel))
+ (while (and (not (equal (lirc-get-channel-name channel) channel-name))
+ (not (eq (lirc-get-channel-next channel) lirc-current-channel)))
+ (setq channel (lirc-get-channel-next channel)))
+ (if (equal (lirc-get-channel-name channel) channel-name)
+ channel
+ nil))
+ nil))
-(defun lirc-set-channel-users (channel-name users)
- (setcdr (assoc channel-name lirc-channel-list) users))
+(defun lirc-add-channel-with-name (channel-name)
+ (lirc-add-channel (lirc-channel channel-name nil nil nil)))
-(defun lirc-add-channel-users (channel-name &rest users)
- (let ((current-users (lirc-get-channel-users channel-name)))
- (lirc-set-channel-users channel-name (append users current-users))))
+(defun lirc-add-channel-users (channel &rest users)
+ (lirc-set-channel-users channel (append users (lirc-get-channel-users channel))))
-(defun lirc-del-channel-users (channel-name &rest users)
- (let ((current-users (lirc-get-channel-users channel-name)))
- (lirc-set-channel-users channel-name
- (cl-set-difference current-users users :test #'equal))))
+(defun lirc-del-channel-users (channel &rest users)
+ (lirc-set-channel-users channel (cl-set-difference (lirc-get-channel-users channel) users)))
(defun lirc-del-users (&rest users)
- (dolist (channel lirc-channel-list)
- (apply #'lirc-del-channel-users (cons (car channel) users))))
+ (lirc-channel-do
+ (lambda (channel)
+ (lirc-set-channel-users
+ channel
+ (cl-set-difference
+ (lirc-get-channel-users channel)
+ users
+ :test #'equal)))))
(defun lirc-rename-user (old-nick new-nick)
- (dolist (channel lirc-channel-list)
- (let ((channel-name (car channel))
- (channel-users (cdr channel)))
- (when (memq old-nick channel-users)
- (lirc-del-channel-users old-nick)
- (lirc-add-channel-users new-nick)))))
+ (lirc-channel-do
+ (lambda (channel)
+ (lirc-set-channel-users
+ (cons new-nick (delete old-nick (lirc-get-channel-users channel)))))))
+(defun lirc-cycle-channels ()
+ (interactive)
+ (if lirc-current-channel
+ (progn
+ (setq lirc-current-channel (lirc-get-channel-next lirc-current-channel))
+ (lirc-render-prompt))
+ (lirc-display-error "No channels joined.")))
;;; Buffer
;;
(with-current-buffer (get-buffer-create "*lirc*")
(save-excursion
(goto-char lirc-prompt-marker)
- (let ((inhibit-read-only t))
+ (let ((inhibit-read-only t)
+ (old-pos (marker-position lirc-prompt-marker))
+ (adaptive-fill-regexp (rx (= 6 anychar))))
(insert-before-markers
(propertize (concat (format-time-string "%H:%M") " ")
'face 'lirc-text
'read-only t)
(propertize (concat (apply #'concat strings) "\n")
- 'read-only t))))))
-
-(defun lirc-connect ()
- (lirc-send-msg (lirc-msg nil nil "USER" lirc-user-name 0 "*" lirc-full-name))
- (lirc-send-msg (lirc-msg nil nil "NICK" lirc-nick)))
-
-(defun lirc-send-msg (msg)
- (let ((proc (lirc-get-process)))
- (process-send-string proc (concat (lirc-msg->string msg) "\r\n"))))
+ 'read-only t))
+ (fill-region old-pos lirc-prompt-marker)))))
(defun lirc-render-prompt ()
(with-current-buffer "*lirc*"
- (set-marker-insertion-type lirc-prompt-marker nil)
- (set-marker-insertion-type lirc-input-marker t)
(save-excursion
+ (set-marker-insertion-type lirc-prompt-marker nil)
+ (set-marker-insertion-type lirc-input-marker t)
(let ((inhibit-read-only t))
(delete-region lirc-prompt-marker lirc-input-marker)
(goto-char lirc-prompt-marker)
(insert
(propertize (if lirc-current-channel
- lirc-current-channel
+ (lirc-get-channel-name lirc-current-channel)
"[no channel]")
'face 'lirc-channel
'read-only t)
(propertize lirc-prompt-string
'face 'lirc-prompt
'read-only t
- 'rear-nonsticky t)))))
- (set-marker-insertion-type lirc-input-marker nil))
+ 'rear-nonsticky t)))
+ (set-marker-insertion-type lirc-input-marker nil))))
(defvar lirc-prompt-marker nil
"Marker for prompt position in LIRC buffer.")
;;; Output formatting
;;
-(defun lirc-display-action (channel nick action)
- (lirc-display-string (concat " * "
+(defun lirc-display-action (channel-name nick action)
+ (lirc-display-string (concat channel-name
+ " * "
(propertize (concat nick " " action)
'face 'lirc-text))))
+(defun lirc-display-private-message (target nick message)
+ (lirc-display-string
+ (concat
+ (propertize
+ (concat "[P]"
+ (if (equal target lirc-nick)
+ "<- "
+ "-> "))
+ 'face 'lirc-bold)
+ (propertize (concat "<" nick "> " message) 'face 'lirc-text))))
+
+
(defun lirc-display-message (channel nick message)
(lirc-display-string
(concat
- (propertize (concat channel " ")
+ (propertize (concat (lirc-get-channel-name channel) " ")
'face 'lirc-text)
(propertize (concat "<" nick "> ")
'face
(defun lirc-display-notice (&rest notices)
(lirc-display-string lirc-notice-prompt " " (apply #'concat notices)))
+(defun lirc-display-error (&rest messages)
+ (lirc-display-string lirc-error-prompt " "
+ (propertize (apply #'concat messages)
+ 'face 'lirc-error)))
+
;;; Message evaluation
;;
(pcase (lirc-msg-cmd msg)
("PING"
(lirc-send-msg
- (lirc-msg nil nil "PONG" (lirc-msg-params msg))))
+ (lirc-msg nil nil "PONG" (lirc-msg-params msg)))
+ (lirc-display-notice "ping-pong"))
("353" ; NAMEREPLY
(let* ((params (lirc-msg-params msg))
- (channel (elt params 2))
+ (channel-name (elt params 2))
(names (split-string (elt params 3))))
- (apply #'lirc-add-channel-users (cons channel names))))
+ (apply #'lirc-add-channel-users
+ (cons (lirc-get-channel-with-name channel-name) names))))
("366" ; ENDOFNAMES
(lirc-display-notice
(lirc-as-string (length (lirc-get-channel-users lirc-current-channel)))
- " users in " lirc-current-channel))
+ " users in " (lirc-get-channel-name lirc-current-channel)))
((rx (= 3 (any digit)))
(lirc-display-notice (mapconcat 'identity (cdr (lirc-msg-params msg)) " ")))
((and "JOIN"
(guard (equal lirc-nick (lirc-msg-src msg))))
- (let ((channel (car (lirc-msg-params msg))))
- (setq lirc-current-channel channel)
- (lirc-add-channel channel)
+ (let ((channel-name (car (lirc-msg-params msg))))
+ (lirc-add-channel-with-name channel-name)
+ (lirc-display-notice "Joining channel " channel-name)
(lirc-render-prompt)))
("JOIN"
- (let ((channel (car (lirc-msg-params msg)))
+ (let ((channel-name (car (lirc-msg-params msg)))
(nick (lirc-msg-src msg)))
- (lirc-add-channel-users channel nick)
- (lirc-display-notice nick " joined channel " channel)))
+ (lirc-add-channel-users (lirc-get-channel-with-name channel-name) nick)
+ (lirc-display-notice nick " joined channel " channel-name)))
((and "PART"
(guard (equal lirc-nick (lirc-msg-src msg))))
- (setq lirc-current-channel nil)
- (lirc-del-channel (car (lirc-msg-params msg)))
- (lirc-render-prompt))
+ (let ((channel-name (car (lirc-msg-params msg))))
+ (lirc-display-notice "Left channel " channel-name)
+ (lirc-del-channel (lirc-get-channel-with-name channel-name))
+ (lirc-render-prompt)))
("PART"
- (let ((channel (car (lirc-msg-params msg)))
+ (let ((channel-name (car (lirc-msg-params msg)))
(nick (lirc-msg-src msg)))
- (lirc-del-channel-users channel nick)
- (lirc-display-notice nick " left channel " channel)))
+ (lirc-del-channel-users (lirc-get-channel channel-name) nick)
+ (lirc-display-notice nick " left channel " channel-name)))
("QUIT"
(let ((nick (lirc-msg-src msg))
("PRIVMSG"
(let ((nick (lirc-msg-src msg))
- (channel (car (lirc-msg-params msg)))
+ (channel (lirc-get-channel-with-name (car (lirc-msg-params msg))))
(text (cadr (lirc-msg-params msg))))
(pcase text
((rx (: "\01ACTION "
(let action (* (not "\01")))
"\01"))
- (lirc-display-action nick action))
+ (lirc-display-action channel nick action))
("\01VERSION\01"
(let ((version-string (concat lirc-version " - running on GNU Emacs " emacs-version)))
(lirc-display-notice "CTCP userinfo request from " nick " (no response sent)"))
(_
- (lirc-display-message channel nick text)))))
+ (if channel
+ (lirc-display-message channel nick text)
+ (lirc-display-private-message (car (lirc-msg-params msg)) nick text))))))
(_
(lirc-display-string (lirc-msg->string msg))))))
(defun lirc-enter-string (string)
(if (string-prefix-p "/" string)
(pcase (substring string 1)
- ((rx (: "TOPIC" " " (let new-topic (* not-newline))))
+ ((rx (: "CONNECT " (let network (* not-newline))))
+ (lirc-display-notice "Connecting to " network "...")
+ (lirc-connect network))
+
+ ((rx (: "TOPIC " (let new-topic (* not-newline))))
(lirc-send-msg (lirc-msg nil nil "TOPIC" lirc-current-channel new-topic)))
- ((rx (: "me" " " (let action (* not-newline))))
+ ((rx (: "ME " (let action (* not-newline))))
(lirc-send-msg (lirc-msg nil nil "PRIVMSG"
(list lirc-current-channel
(concat "\01ACTION " action "\01"))))
(list nick "\01VERSION\01")))
(lirc-display-notice "CTCP version request sent to " nick))
- ((rx "PART")
- (lirc-send-msg (lirc-msg nil nil "PART" lirc-current-channel)))
+ ((rx "PART" (opt (: " " (let channel (* not-newline)))))
+ (if (or lirc-current-channel channel)
+ (lirc-send-msg (lirc-msg nil nil "PART" (if channel
+ channel
+ lirc-current-channel)))
+ (lirc-display-error "No current channel to leave.")))
((rx "MSG "
(let target (* (not whitespace)))
" "
(let text (* not-newline)))
(lirc-send-msg (lirc-msg nil nil "PRIVMSG" target text))
- (lirc-display-message target lirc-nick text))
+ (lirc-display-private-message target lirc-nick text))
((rx (: (let cmd-str (+ (not whitespace)))
(opt (: " " (let params-str (* not-newline))))))
nil)))))
(unless (string-empty-p string)
- (lirc-send-msg (lirc-msg nil nil "PRIVMSG" lirc-current-channel string))
- (lirc-display-message lirc-current-channel lirc-nick string))))
+ (if lirc-current-channel
+ (progn
+ (lirc-send-msg (lirc-msg nil nil "PRIVMSG"
+ (lirc-get-channel-name lirc-current-channel)
+ string))
+ (lirc-display-message lirc-current-channel lirc-nick string))
+ (lirc-display-error "No current channel.")))))
(defun lirc-enter ()
"Enter current contents of line after prompt."
(defvar lirc-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'lirc-enter)
+ (define-key map (kbd "<C-tab>") 'lirc-cycle-channels)
map))
(define-derived-mode lirc-mode text-mode "lirc"
(switch-to-buffer "*lirc*"))
(lirc-mode)
(lirc-setup-buffer)
- (lirc-connect)
"Started LIRC.")