+ (pcase (lirc-msg-cmd msg)
+ ("PING"
+ (lirc-send-msg
+ (lirc-msg nil nil "PONG" (lirc-msg-params msg)))
+ (lirc-display-notice "ping-pong"))
+
+ ("353" ; NAMEREPLY
+ (let* ((params (lirc-msg-params msg))
+ (channel-name (elt params 2))
+ (names (split-string (elt params 3))))
+ (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-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-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-name (car (lirc-msg-params msg)))
+ (nick (lirc-msg-src msg)))
+ (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))))
+ (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-name (car (lirc-msg-params msg)))
+ (nick (lirc-msg-src msg)))
+ (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))
+ (reason (mapconcat 'identity (lirc-msg-params msg) " ")))
+ (lirc-del-users nick)
+ (lirc-display-notice nick " quit: " reason)))
+
+ ((and "NICK"
+ (guard (equal lirc-nick (lirc-msg-src msg))))
+ (setq lirc-nick (car (lirc-msg-params msg)))
+ (lirc-display-notice "Set nick to " lirc-nick))
+
+ ("NICK"
+ (let ((old-nick (lirc-msg-src msg))
+ (new-nick (car (lirc-msg-params msg))))
+ (lirc-display-notice nick " is now known as " new-nick)
+ (lirc-rename-user nick new-nick)))
+
+ ("NOTICE"
+ (let ((nick (lirc-msg-src msg))
+ (channel (car (lirc-msg-params msg)))
+ (text (cadr (lirc-msg-params msg))))
+ (pcase text
+ ((rx (: "\01VERSION "
+ (let version (* (not "\01")))
+ "\01"))
+ (lirc-display-notice "CTCP version reply from " nick ": " version))
+ (_
+ (lirc-display-notice text)))))
+
+ ("PRIVMSG"
+ (let ((nick (lirc-msg-src 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 channel nick action))
+
+ ("\01VERSION\01"
+ (let ((version-string (concat lirc-version " - running on GNU Emacs " emacs-version)))
+ (lirc-send-msg (lirc-msg nil nil "NOTICE"
+ (list nick (concat "\01VERSION "
+ version-string
+ "\01")))))
+ (lirc-display-notice "CTCP version request received from " nick))
+
+ ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
+ (lirc-send-msg (lirc-msg nil nil "NOTICE" (list nick ping)))
+ (lirc-display-notice "CTCP ping received from " nick))
+
+ ("\01USERINFO\01"
+ (lirc-display-notice "CTCP userinfo request from " nick " (no response sent)"))
+
+ (_
+ (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))))))
+
+
+;;; Command entering
+;;
+
+(defun lirc-enter-string (string)
+ (if (string-prefix-p "/" string)
+ (pcase (substring string 1)
+ ((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))))
+ (lirc-send-msg (lirc-msg nil nil "PRIVMSG"
+ (list lirc-current-channel
+ (concat "\01ACTION " action "\01"))))
+ (lirc-display-action lirc-nick action))
+
+ ((rx (: "VERSION" " " (let nick (* (not whitespace)))))
+ (lirc-send-msg (lirc-msg nil nil "PRIVMSG"
+ (list nick "\01VERSION\01")))
+ (lirc-display-notice "CTCP version request sent to " nick))
+
+ ((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-private-message target lirc-nick text))
+
+ ((rx (: (let cmd-str (+ (not whitespace)))
+ (opt (: " " (let params-str (* not-newline))))))
+ (lirc-send-msg (lirc-msg nil nil (upcase cmd-str)
+ (if params-str
+ (split-string params-str)
+ nil)))))
+
+ (unless (string-empty-p 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."
+ (interactive)
+ (with-current-buffer "*lirc*"
+ (lirc-enter-string
+ (buffer-substring lirc-input-marker (point-max)))
+ (let ((inhibit-read-only t))
+ (delete-region lirc-input-marker (point-max)))))
+