help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: test for network connection location


From: Sébastien Vauban
Subject: Re: test for network connection location
Date: Tue, 15 Apr 2008 10:30:13 +0200
User-agent: Gnus/5.110007 (No Gnus v0.7) Emacs/23.0.60 (gnu/linux)

Alan,

> I haven't been able to think of a way to put a test in my .vm
> file that would distinguish between when my laptop is
> connected to the internet while at home, versus being
> connected while at work. In both cases I am interested in
> fetching mail from the IMAP server at work, but I'd like to
> configure three variables in .vm somewhat differently--
> depending on how the laptop is connected.

This works for me, with Gnus.

I am testing the IP address (both under Windows *and* Linux, as
I'm using both), which is sufficient for my need:

--8<---------------cut here---------------start------------->8---
;; managing multiple SMTP servers
(defun my-get-domain-name ()
  "Determine the current domain name."
  (interactive)
  (save-excursion
    (let ((my-domain-name)
          (my-ip-address))
      (if running-ms-windows
          (progn
            (setq my-domain-name
                  (progn (ipconfig)
                         (set-buffer "*Ipconfig*")
                         (goto-char (point-min))
                         (sleep-for 2)
                         (if (re-search-forward
                              "Connection-specific DNS Suffix.*: 
\\([a-zA-Z0-9].*\\)"
                              nil t)
                             (match-string 1)
                           "*unknown*")))
            (kill-buffer "*Ipconfig*"))
        (progn
          (let ((my-ifconfig (shell-command-to-string "ifconfig -a")))
            (when (string-match "inet addr:\\([0-9]+.[0-9]+.[0-9]+.[0-9]+ \\)"
                                my-ifconfig)
              (setq my-ip-address (match-string 1 my-ifconfig))
              (message my-ip-address)
              (setq my-domain-name
                    (cond
                     ((string-match "^192.168." my-ip-address)
                      "domain1.fr")
                     ((string-match "^10.10.1" my-ip-address)
                      "domain2.com")
                     ((string-match "^169." my-ip-address)
                      "domain3.net")
                     (t
                      "*unknown*")))))))
      my-domain-name)))

(defun my-change-smtp-server ()
  "Change the SMTP server according to the current domain name."
  (save-excursion
    (let* ((domain (my-get-domain-name)))
      (setq smtpmail-smtp-server
            ;; where am I?
            (cond ((string= "domain1.fr" domain)
                    "out.domain1.fr")
                  ((string= "domain2.com" domain)
                   "mail.domain2.com")
                  ((string= "domain3.net" domain)
                   "localhost")
                  (t
                   (error (concat "Don't know which mail server to use for `"
                                  domain "' domain")))))
      (message "Domain name is `%s', setting `smtpmail-smtp-server' to `%s'"
               domain smtpmail-smtp-server))))

(add-hook 'message-send-hook 'my-change-smtp-server)
--8<---------------cut here---------------end--------------->8---

Any comment is welcome!

Best regards,
  Seb

-- 
Sébastien Vauban


reply via email to

[Prev in Thread] Current Thread [Next in Thread]