+
+;;; Commands
+;;
+
+(defvar lurk-command-table
+ '(("DEBUG" "Toggle debug mode on/off." lurk-command-debug lurk-boolean-completions)
+ ("HEADER" "Toggle display of header." lurk-command-header lurk-boolean-completions)
+ ("CONNECT" "Connect to an IRC network." lurk-command-connect lurk-network-completions)
+ ("NETWORKS" "List known IRC networks." lurk-command-networks)
+ ("TOPIC" "Set topic for current channel." lurk-command-topic)
+ ("ME" "Display action." lurk-command-me)
+ ("VERSION" "Request version of another user's client via CTCP." lurk-command-version)
+ ("PART" "Leave channel." lurk-command-part lurk-context-completions)
+ ("QUIT" "Disconnect from current network." lurk-command-quit)
+ ("NICK" "Change nick." lurk-command-nick)
+ ("LIST" "Display details of one or more channels." lurk-command-list)
+ ("MSG" "Send private message to user." lurk-command-msg lurk-nick-completions)
+ ("HELP" "Display help on client commands." lurk-command-help lurk-help-completions))
+ "Table of commands explicitly supported by Lurk.")
+
+(defun lurk-boolean-completions ()
+ '("on" "off"))
+
+(defun lurk-network-completions ()
+ (mapcar (lambda (row) (car row)) lurk-networks))
+
+(defun lurk-nick-completions ()
+ (lurk-get-context-users lurk-current-context))
+
+(defun lurk-context-completions ()
+ (lurk-get-context-list))
+
+(defun lurk-help-completions ()
+ (mapcar (lambda (row) (car row)) lurk-command-table))
+
+(defun lurk-command-help (params)
+ (if params
+ (let* ((cmd-str (upcase (car params)))
+ (row (assoc cmd-str lurk-command-table #'equal)))
+ (if row
+ (progn
+ (lurk-display-notice nil "Help for \x02" cmd-str "\x02:")
+ (lurk-display-notice nil " " (elt row 1)))
+ (lurk-display-notice nil "No such (client-interpreted) command.")))
+ (lurk-display-notice nil "Client-interpreted commands:")
+ (dolist (row lurk-command-table)
+ (lurk-display-notice nil " \x02" (elt row 0) "\x02: " (elt row 1)))
+ (lurk-display-notice nil "Use /HELP COMMAND to display information about a specific command.")))
+
+(defun lurk-command-debug (params)
+ (setq lurk-debug
+ (if params
+ (if (equal (upcase (car params)) "ON")
+ t
+ nil)
+ (not lurk-debug)))
+ (lurk-display-notice nil "Debug mode now " (if lurk-debug "on" "off") "."))
+
+(defun lurk-command-header (params)
+ (if
+ (if params
+ (equal (upcase (car params)) "ON")
+ (not header-line-format))
+ (progn
+ (lurk-setup-header)
+ (lurk-display-notice nil "Header enabled."))
+ (setq-local header-line-format nil)
+ (lurk-display-notice nil "Header disabled.")))
+
+(defun lurk-command-connect (params)
+ (if params
+ (let ((network (car params)))
+ (lurk-display-notice nil "Attempting to connect to " network "...")
+ (lurk-connect network))
+ (lurk-display-notice nil "Usage: /connect <network>")))
+
+(defun lurk-command-networks (params)
+ (lurk-display-notice nil "Currently-known networks:")
+ (dolist (row lurk-networks)
+ (seq-let (network server port &rest others) row
+ (lurk-display-notice nil "\t" network
+ " [" server
+ " " (number-to-string port) "]")))
+ (lurk-display-notice nil "(Modify the `lurk-networks' variable to add more.)"))
+
+(defun lurk-command-part (params)
+ (let ((channel (if params (car params) lurk-current-context)))
+ (if channel
+ (lurk-send-msg (lurk-msg nil nil "PART" channel))
+ (lurk-display-error "No current channel to leave."))))
+
+(defun lurk-command-version (params)
+ (if params
+ (let ((nick (car params)))
+ (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
+ (list nick "\01VERSION\01")))
+ (lurk-display-notice nil "CTCP version request sent to " nick))
+ (lurk-display-notice nil "Usage: /version <nick>")))
+
+(defun lurk-command-quit (params)
+ (let ((quit-msg (if params (string-join params " ") lurk-default-quit-msg)))
+ (lurk-send-msg (lurk-msg nil nil "QUIT" quit-msg))))
+
+(defun lurk-command-nick (params)
+ (let ((new-nick (if params (string-join params " ") nil)))
+ (if new-nick
+ (if (lurk-connected-p)
+ (lurk-send-msg (lurk-msg nil nil "NICK" new-nick))
+ (setq lurk-nick nick)
+ (lurk-display-notice nil "Set default nick to '" nick "'."))
+ (lurk-display-notice nil "Current nick: " lurk-nick))))
+
+(defun lurk-command-me (params)
+ (if lurk-current-context
+ (if params
+ (let* ((action (string-join params " "))
+ (ctcp-text (concat "\01ACTION " action "\01")))
+ (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
+ (list lurk-current-context ctcp-text)))
+ (lurk-display-action lurk-nick lurk-current-context action))
+ (lurk-display-notice nil "Usage: /me <action>"))
+ (lurk-display-notice nil "No current channel.")))
+
+(defun lurk-command-list (params)
+ (if (not params)
+ (lurk-display-notice nil "This command can generate lots of output. Use `/LIST -yes' if you really want this, or `/LIST <channel_regexp>' to reduce the output.")
+ (if (equal (upcase (car params)) "-YES")
+ (lurk-send-msg (lurk-msg nil nil "LIST"))
+ (lurk-send-msg (lurk-msg nil nil "LIST" (car params))))))
+
+(defun lurk-command-topic (params)
+ (if lurk-current-context
+ (if params
+ (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context (string-join params " ")))
+ (lurk-display-notice nil "Usage: /topic <new topic>"))
+ (lurk-display-notice nil "No current channel.")))
+
+(defun lurk-command-msg (params)
+ (if (and params (>= (length params) 2))
+ (let ((to (car params))
+ (text (string-join (cdr params) " ")))
+ (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
+ (lurk-display-message lurk-nick to text))
+ (lurk-display-notice nil "Usage: /msg <nick> <message>")))
+
+