+ (lurk-display-notice (lurk-get-context network)
+ (lurk-msg->string msg))))))
+
+
+;;; User-defined responses
+;;
+
+(defun lurk--lists-equal (l1 l2)
+ (if (and l1 l2)
+ (if (or (not (and (car l1) (car l2)))
+ (string-match (car l1) (car l2)))
+ (lurk--lists-equal (cdr l1) (cdr l2))
+ nil)
+ t))
+
+(defun lurk-process-autoreply (network msg autoreply)
+ (let ((matcher (car autoreply))
+ (reply (cadr autoreply)))
+ (let ((target-network (car matcher)))
+ (when (and (or (not target-network)
+ (and (equal network target-network)))
+ (lurk--lists-equal (cdr matcher)
+ (append (list (lurk-msg-src msg)
+ (lurk-msg-cmd msg))
+ (lurk-msg-params msg))))
+ (lurk-send-msg network
+ (lurk-msg nil nil (car reply) (cdr reply)))))))
+
+(defun lurk-process-autoreplies (network msg)
+ (mapc
+ (lambda (autoreply)
+ (lurk-process-autoreply network msg autoreply))
+ lurk-autoreply-table))
+
+
+;;; 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)
+ ("SHOWJOINS" "Toggles display of joins/parts." lurk-command-showjoins lurk-boolean-completions)
+ ("NETWORKS" "List known IRC networks." lurk-command-networks)
+ ("CONNECT" "Connect to an IRC network." lurk-command-connect lurk-network-completions)
+ ("QUIT" "Disconnect from current network." lurk-command-quit)
+ ("JOIN" "Join one or more channels." lurk-command-join)
+ ("PART" "Leave channel." lurk-command-part lurk-channel-completions)
+ ("SWITCHCONTEXT" "Switch current context" lurk-command-switch-context lurk-context-completions)
+ ("NICK" "Change nick." lurk-command-nick)
+ ("LIST" "Display details of one or more channels." lurk-command-list)
+ ("TOPIC" "Set/query topic for current channel." lurk-command-topic)
+ ("USERS" "List nicks of users in current channel." lurk-command-users)
+ ("MSG" "Send private message to user." lurk-command-msg lurk-nick-completions)
+ ("ME" "Display action." lurk-command-me)
+ ("VERSION" "Request version of another user's client via CTCP." lurk-command-version lurk-nick-completions)
+ ("CLEAR" "Clear buffer text." lurk-command-clear lurk-context-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-help-completions ()
+ (mapcar (lambda (row) (car row)) lurk-command-table))
+
+(defun lurk-channel-completions ()
+ (mapcar (lambda (ctx)
+ (lurk-context->string ctx))
+ (seq-filter (lambda (ctx)
+ (not (lurk-network-context-p ctx)))
+ lurk-contexts)))
+
+(defun lurk-context-completions ()
+ (mapcar (lambda (ctx) (lurk-context->string ctx)) lurk-contexts))
+
+(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-showjoins (params)
+ (setq lurk-show-joins
+ (if params
+ (if (equal (upcase (car params)) "ON")
+ t
+ nil)
+ (not lurk-show-joins)))
+ (lurk-display-notice nil "Joins/parts will now be "
+ (if lurk-show-joins "shown" "hidden") "."))
+
+(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 network port &rest _others) row
+ (lurk-display-notice nil "\t" network
+ " [" network
+ " " (number-to-string port) "]")))
+ (lurk-display-notice nil "(Modify the `lurk-networks' variable to add more.)"))
+
+(defun lurk-command-quit (params)
+ (let ((ctx (lurk-current-context)))
+ (if (not ctx)
+ (lurk-display-error "No current network")
+ (let ((quit-msg (if params (string-join params " ") lurk-default-quit-msg)))
+ (lurk-send-msg
+ (lurk-context-network ctx)
+ (lurk-msg nil nil "QUIT" quit-msg))))))
+
+(defun lurk-command-join (params)
+ (if params
+ (let ((network (lurk-context-network (lurk-current-context))))
+ (dolist (channel params)
+ (lurk-send-msg network (lurk-msg nil nil "JOIN" channel))))
+ (lurk-display-notice nil "Usage: /join channel [channel2 ...]")))
+
+(defun lurk-command-part (params)
+ (let ((ctx (cond
+ ((not params) (lurk-current-context))
+ ((seq-contains (car params) "@") (lurk-string->context (car params)))
+ (t (list (lurk-context-network (lurk-current-context)) (car params))))))
+ (let ((network (lurk-context-network ctx))
+ (channel (lurk-context-channel ctx)))
+ (if channel
+ (lurk-send-msg network (lurk-msg nil nil "PART" channel))
+ (lurk-display-error "Specify which channel to leave")))))
+
+(defun lurk-command-switch-context (params)
+ (if (not params)
+ (lurk-display-notice nil "Usage: /switchcontext #channel@network")
+ (let ((ctx (lurk-string->context (car params))))
+ (lurk-switch-to-context ctx)
+ (lurk-highlight-current-context)
+ (lurk-render-prompt)
+ (if lurk-zoomed
+ (lurk-zoom-in (lurk-current-context))))))
+
+(defun lurk-command-nick (params)
+ (if params
+ (let ((new-nick (string-join params " "))
+ (ctx (lurk-current-context)))
+ (if ctx
+ (lurk-send-msg (lurk-context-network ctx)
+ (lurk-msg nil nil "NICK" new-nick))
+ (lurk-display-error "No current connection")))
+ (lurk-display-notice nil "Usage: /nick <new-nick>")))
+
+(defun lurk-command-list (params)
+ (let ((ctx (lurk-current-context)))
+ (if ctx
+ (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.")
+ (let ((network (lurk-context-network ctx)))
+ (if (equal (upcase (car params)) "-YES")
+ (lurk-send-msg network (lurk-msg nil nil "LIST"))
+ (lurk-send-msg network (lurk-msg nil nil "LIST"
+ (car params))))))
+ (lurk-display-error "No current connection"))))
+
+(defun lurk-command-topic (params)
+ (let ((ctx (lurk-current-context)))
+ (if (and ctx (not (lurk-network-context-p ctx)))
+ (let ((network (lurk-context-network ctx))
+ (channel (lurk-context-channel ctx)))
+ (if params
+ (lurk-send-msg network
+ (lurk-msg nil nil "TOPIC" channel
+ (string-join params " ")))
+ (lurk-send-msg network
+ (lurk-msg nil nil "TOPIC" channel))))
+ (lurk-display-notice nil "No current channel."))))
+
+(defun lurk-command-msg (params)
+ (let ((network (lurk-context-network (lurk-current-context))))
+ (if (and params (>= (length params) 2))
+ (let ((to (car params))
+ (text (string-join (cdr params) " ")))
+ (lurk-send-msg network (lurk-msg nil nil "PRIVMSG" to text))
+ (lurk-display-message network
+ (lurk-connection-nick network)
+ to text))
+ (lurk-display-notice nil "Usage: /msg <nick> <message>"))))
+
+(defun lurk-command-me (params)
+ (let* ((ctx (lurk-current-context))
+ (network (lurk-context-network ctx)))
+ (if (and ctx (not (lurk-network-context-p ctx)))
+ (if params
+ (let* ((channel (lurk-context-channel ctx))
+ (my-nick (lurk-connection-nick network))
+ (action (string-join params " "))
+ (ctcp-text (concat "\01ACTION " action "\01")))
+ (lurk-send-msg network
+ (lurk-msg nil nil "PRIVMSG"
+ (list channel ctcp-text)))
+ (lurk-display-action network my-nick channel action))
+ (lurk-display-notice nil "Usage: /me <action>"))
+ (lurk-display-notice nil "No current channel."))))
+
+(defun lurk-command-version (params)
+ (let ((ctx (lurk-current-context)))
+ (if ctx
+ (if params
+ (let ((network (lurk-context-network ctx))
+ (nick (car params)))
+ (lurk-send-msg network
+ (lurk-msg nil nil "PRIVMSG"
+ (list nick "\01VERSION\01")))
+ (lurk-display-notice ctx "CTCP version request sent to "
+ nick " on " network))
+ (lurk-display-notice ctx "Usage: /version <nick>"))
+ (lurk-display-notice nil "No current channel."))))
+
+(defun lurk-command-users (_params)
+ (let ((ctx (lurk-current-context)))
+ (if (and ctx (not (lurk-network-context-p ctx)))
+ (let ((channel (lurk-context-channel ctx))
+ (network (lurk-context-network ctx))
+ (users (lurk-get-context-users ctx)))
+ (lurk-display-notice ctx "Users in " channel " on " network ":")
+ (lurk-display-notice ctx (string-join users " ")))
+ (lurk-display-notice nil "No current channel."))))