"Fossies" - the Fresh Open Source Software Archive

Member "msmtp-1.8.5/scripts/set_sendmail/set_sendmail.sh" (2 May 2019, 5087 Bytes) of package /linux/privat/msmtp-1.8.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "set_sendmail.sh": 1.8.4_vs_1.8.5.

    1 #!/usr/bin/env bash
    2 # Created   : Tue 03 Jul 2007 11:54:53 PM EDT
    3 # Modified  : Thu 13 Sep 2007 06:35:56 AM PDT
    4 # Author    : Gautam Iyer <gi1242@users.sourceforge.net>
    5 #
    6 # Selects an msmtp account based on users current network. Type -h for a more
    7 # detailed description.
    8 
    9 #
   10 # User settings
   11 #
   12 
   13 # Default setting used if the domain name can not be found, or if something goes
   14 # wrong.
   15 default='/usr/sbin/sendmail -oem -oi'
   16 # Script uses bash-3.2. Exit if bash version is lower.
   17 if (( BASH_VERSINFO[0] < 3 || BASH_VERSINFO[1] < 2 )); then
   18     echo $default
   19     exit
   20 fi
   21 
   22 # {{{1 Script Functions
   23 function debug()
   24 {
   25     [[ -n $verbose ]] && echo -e "\033[31m$*\033[m" >> /dev/stderr
   26 }
   27 
   28 function print_usage()
   29 {
   30     echo "USAGE:"
   31     echo "    set_sendmail.sh [-v] -c config_file"
   32     echo "    set_sendmail.sh -h"
   33 }
   34 
   35 function print_help()
   36 {
   37     print_usage
   38 
   39     cat << 'EOF'
   40 
   41 DESCRIPTION:
   42 
   43     Selects an msmtp account, based on the current host's domain name. This is
   44     useful for laptop users who move the laptop between networks. Firewall
   45     settings of your ISP may render your default smtp service useless, and force
   46     you to use a different msmtp account. This script first matches
   47     /etc/resolv.conf against a given set of (egrep) regular expressions. If a
   48     match is found, it is used to decide which msmtp account to use.
   49     
   50     If no match is found, this script tries to get the domain name. (NOTE: This
   51     can be time consuming, as it could involve a name server lookup). The domain
   52     name is then matched against a given set of (bash) regular expressions the
   53     msmtp account is selected based on that.
   54 
   55     The output of this script is suitable for use in the users .muttrc. For
   56     instance, this script can be called via
   57 
   58     set sendmail="`~/.mutt/set_sendmail.sh -c ~/.mutt/set_sendmail.conf`"
   59 
   60     from the users ~/.muttrc.
   61 
   62     The patterns, and msmtp accounts should be provided in the config file, and
   63     are "sourced" directly into the script. Use bash compatible syntax, and look
   64     at the example.
   65 EOF
   66     exit
   67 }
   68 
   69 # Check if a host is reachable
   70 function check_host()
   71 {
   72     debug "Checking for host $1"
   73     if [[ $host_pkg == 'bind' ]]; then
   74     host $1 >& /dev/null
   75     elif [[ $host_pkg == 'hostx' ]]; then
   76     hostx -Q $1 >& /dev/null
   77     else
   78     # Fail
   79     return 1
   80     fi
   81 }
   82 
   83 # Function to get the domain name (in variable domainname).
   84 function get_domainname()
   85 {
   86     debug "Getting domain name"
   87 
   88     domainname=
   89     ipaddr=
   90     host_pkg=
   91 
   92     if [[ -n $DNSDOMAIN ]]; then
   93     domainname=$DNSDOMAIN
   94     return
   95     fi
   96 
   97     # We need either 'host' (from bind-tools) or 'hostx' to proceed.
   98     which hostx >& /dev/null && host_pkg='hostx'
   99     [[ -z $host_pkg ]] && which host >& /dev/null && host_pkg='bind'
  100     debug "Using host package '$host_pkg'"
  101 
  102     # See if the internet is up
  103     check_host whatismyip.org || return
  104 
  105     # Try and get domain name from /etc/resolv.conf.
  106     # 2007-07-10: If a vpn connection is active, then it usually adds the
  107     # private search domain as the last field.
  108     domainname=$(grep -E -m1 '^(search|domain)' /etc/resolv.conf |  \
  109             awk '{ print $NF }')
  110     debug "Got domain '$domainname' from /etc/resolv.conf"
  111     [[ -n $domainname ]] && return;
  112 
  113     # Get ip address
  114     #ipaddr=$(/sbin/ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | \
  115     #       cut -d: -f2 | awk '{ print $1}')
  116     #ipaddr=$(w3m -dump whatismyip.org)
  117     ipaddr=$(wget -qO- http://whatismyip.org)
  118     debug "Got IP address '$ipaddr'"
  119     [[ -z $ipaddr ]] && return
  120 
  121     if [[ $host_pkg == bind ]]; then
  122     domainname=$(host $ipaddr | sed -r 's/^.*pointer [^.]+\.(.*)\.$/\1/')
  123     elif [[ $host_pkg == hostx ]]; then
  124     domainname=$(hostx -Q $ipaddr | grep -m1 ^Name | sed -r 's/^[^.]+\.//')
  125     fi
  126     debug "Got domain name '$domainname'"
  127 }
  128 
  129 function do_account()
  130 {
  131     local account=$1
  132 
  133     [[ $account == ssh ]] && ssh_tunnel
  134     echo "$(which msmtp) -a $account"
  135     
  136     exit 0
  137 }
  138 
  139 # {{{1 Main script
  140 while getopts "hvc:" OPT; do
  141     case $OPT in
  142     "c") configFile=${OPTARG};;
  143     "v") verbose=1;;
  144     "h") print_help;;
  145 
  146     "?") print_usage; exit;;
  147     esac
  148 done
  149 
  150 if [[ -z $configFile || ! -r $configFile ]]; then
  151     print_usage
  152     exit
  153 fi
  154 source $configFile
  155 
  156 # If we can't (or shouldn't) msmtp, then print default and exit
  157 if [[ ! $HOSTNAME =~ $run_hosts ]] || ! which msmtp >& /dev/null; then
  158     echo $default
  159     exit
  160 fi
  161 
  162 # See if we can decide on the account based on /etc/resolv.conf
  163 debug "Getting account from /etc/resolv.conf"
  164 for ((i=0; i<${#accounts[*]}; i++ )); do
  165     if [[ -n ${resolv_regexp[$i]} ]]; then
  166     regexp=${resolv_regexp[$i]}
  167     if [[ ${regexp:0:1} != '!' ]]; then
  168         grep -E -q $regexp /etc/resolv.conf && do_account ${accounts[$i]}
  169     else
  170         grep -E -q ${regexp:1} /etc/resolv.conf || do_account ${accounts[$i]}
  171     fi
  172     fi
  173 done
  174 
  175 # Get the domain name
  176 get_domainname
  177 #echo "ipaddr=$ipaddr, domainname=$domainname."
  178 
  179 # Decide which account to use
  180 debug "Getting account from domainname ($domainname)"
  181 for ((i=0; i<${#accounts[*]}; i++ )); do
  182     if [[ $domainname =~ ${dom_regexp[$i]} ]]; then
  183     do_account ${accounts[$i]}
  184     fi
  185 done