Previous: Defining a new command, Up: Hacking and Tweaking

4.6 Reconnecting after you have lost the connection

If you're chatting from a laptop, then you might be familiar with this problem: When your laptop falls asleep and wakes up later, your IRC client doesn't realize that it has been disconnected. It takes several minutes until the client decides that the connection has in fact been lost. The simple solution is to use M-x rcirc. The problem is that this opens an additional connection, so you'll have two copies of every channel buffer, one dead and one live.

The real answer, therefore, is a /reconnect command:

     (with-eval-after-load 'rcirc
       (defun-rcirc-command reconnect (arg)
         "Reconnect the server process."
         (interactive "i")
         (unless process
           (error "There's no process for this target"))
         (let* ((server (car (process-contact process)))
                (port (process-contact process :service))
                (nick (rcirc-nick process))
                channels query-buffers)
           (dolist (buf (buffer-list))
             (with-current-buffer buf
               (when (eq process (rcirc-buffer-process))
                 (remove-hook 'change-major-mode-hook
                 (if (rcirc-channel-p rcirc-target)
                     (setq channels (cons rcirc-target channels))
                   (setq query-buffers (cons buf query-buffers))))))
           (delete-process process)
           (rcirc-connect server port nick