jaromail

a commandline tool to easily and privately handle your e-mail
git clone git://parazyd.org/jaromail.git
Log | Files | Refs | Submodules | README

accounts (5266B)


      1 #!/usr/bin/env zsh
      2 #
      3 # Jaro Mail, your humble and faithful electronic postman
      4 #
      5 # a tool to easily and privately handle your e-mail communication
      6 #
      7 # Copyleft (C) 2010-2015 Denis Roio <jaromil@dyne.org>
      8 #
      9 # This source  code is free  software; you can redistribute  it and/or
     10 # modify it under the terms of  the GNU Public License as published by
     11 # the Free  Software Foundation; either  version 3 of the  License, or
     12 # (at your option) any later version.
     13 #
     14 # This source code is distributed in  the hope that it will be useful,
     15 # but  WITHOUT ANY  WARRANTY;  without even  the  implied warranty  of
     16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     17 # Please refer to the GNU Public License for more details.
     18 #
     19 # You should have received a copy of the GNU Public License along with
     20 # this source code; if not, write to:
     21 # Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     22 
     23 new_account() {
     24 	fn new_account $*
     25 	acct=$1
     26 	req=(acct)
     27 	ckreq || return 1
     28 
     29 	[[ -r $MAILDIRS/Accounts/$acct ]] && {
     30 		error "account already existing: $MAILDIRS/Accounts/$acct"
     31 		return 1 }
     32 
     33 	isemail $acct || {
     34 		error "new account is not an email: $acct"
     35 		return 1 }
     36 
     37 
     38 	name=${acct%%@*}
     39 	domain=${acct##*@}
     40 	local options
     41 	case $domain in
     42 		gmail.com)
     43 			imap=imap.gmail.com
     44 			smtp=smtp.gmail.com
     45 			auth=plain
     46 			cert=check
     47 			options=keep
     48 			;;
     49 		*)
     50 			imap=mail.${domain}
     51 			smtp=mail.${domain}
     52 			auth=plain
     53 			cert=check
     54 			options=keep
     55 			;;
     56 	esac
     57 	cat <<EOF > $MAILDIRS/Accounts/$acct
     58 # automatically generated by 'jaro new' for account: $acct
     59 # modifications will not be overwritten
     60 name $name
     61 email $acct
     62 login $acct
     63 imap $imap
     64 smtp $smtp
     65 auth $auth
     66 cert $cert
     67 options $options
     68 EOF
     69 	act "account succesfully generated for $acct"
     70 	return 1
     71 }
     72 
     73 # account = type.name es: imap.default or smtp.gmail
     74 # -a defines which account name other than 'default'
     75 # results in the definition of global account variables:
     76 # name login host protocol port auth folders accountopt
     77 read_account() {
     78     typeset -al all
     79     unset name email imap imap_port smtp smtp_port \
     80         host login transport auth cert options folders exclude
     81     # parse arguments
     82 
     83     [[ "$account" = "" ]] && account="default"
     84 
     85     # find the account
     86     func "read_account looking for $account"
     87     acct="$MAILDIRS/Accounts/$account";
     88     { test -r "$acct" } || {
     89         acct="$MAILDIRS/Accounts/$account.txt"
     90         { test -r "$acct" } || {
     91             error "no account found: $acct"
     92             act "Refine your argument using '-a accountname'"
     93             act "Available accounts:"
     94             ls "$MAILDIRS/Accounts/"
     95             return 1
     96         }
     97     }
     98 
     99     ttmp=`awk '
    100 	/^my_hdr/ { next }
    101     /^#/ { next }
    102     /^name/ { printf "name=\""; for(i=2;i<=NF;i++) printf "%s ", $i; printf "\";" }
    103     /^email/ { printf "email=\"%s\";", $2 }
    104     /^imap / { printf "imap=\"%s\";", $2 }
    105     /^smtp / { printf "smtp=\"%s\";", $2 }
    106     /^host / { printf "host=\"%s\";", $2 }
    107     /^port / { printf "port=\"%s\";", $2 }
    108     /^login/ { printf "login=\"%s\";", $2 }
    109     /^transport/ { printf "transport=\"%s\";", $2 }
    110     /^certfile/ { printf "certfile=\"%s\";", $2 }
    111     /^imap_port/ { printf "imap_port=\"%s\";", $2 }
    112     /^smtp_port/ { printf "smtp_port=\"%s\";", $2 }
    113     /^auth/ { printf "auth=\"%s\";", $2 }
    114     /^cert/ { printf "cert=\"%s\";", $2 }
    115     /^password/ { printf "password=\"%s\";", $2 }
    116     /^options/ { printf "accountopt=\""; for(i=2;i<=NF;i++) printf "%s ", $i; printf "\";" }
    117     /^folders/ { printf "folders=("; for(i=2;i<=NF;i++) printf "%s ", $i; printf ");" }
    118     /^exclude/ { printf "exclude=("; for(i=2;i<=NF;i++) printf "%s ", $i; printf ");" }
    119     ' "$acct"`
    120     { test $? = 0 } || {
    121     error "Error parsing account: $acct"
    122     return 1 }
    123 
    124     eval "$ttmp"
    125     # check required fields
    126 
    127     # falling back to old host/port conf directives
    128     imap=${imap:-$host}
    129     host=${host:-$imap}
    130 
    131     smtp=${smtp:-$host}
    132     smtp=${smtp:-$imap}
    133 
    134     imap_port=${imap_port:-$port}
    135     smtp_port=${smtp_port:-$imap_port}
    136 
    137     # fill in defaults
    138     { test -z $name }       && { name="$type" }
    139     { test -z $login }      && { login="$email" } # usually email and login are the same
    140     { test -z $email }      && { email="$login" } # so if one is specified, deduce the other
    141     { test -z $transport }  && { transport=TLS1 }
    142     { test -z $certfile }   && { certfile="" }
    143     { test -z $imap_port }  && { imap_port=993 }
    144     { test -z $smtp_port }  && { smtp_port=587 }
    145 
    146     { test -z $auth }       && { auth=plain }
    147     { test -z $cert }       && { cert=ignore }
    148     { test -z $accountopt } && { accountopt=keep }
    149     # cert and password can be missing
    150 
    151 	act "account in use: $login"
    152     func "name: $name"
    153     func "email: $email"
    154     func "login: $login"
    155 
    156     func "host: $host"
    157     func "port: $port"
    158 
    159     func "imap: $imap"
    160     func "imap port: $imap_port"
    161     func "smtp: $smtp"
    162     func "smtp port: $smtp_port"
    163 
    164     func "trans: $transport"
    165     func "certfile: $certfile"
    166     func "cert: $cert"
    167     func "auth: $auth"
    168     [[ "$password" = "" ]] || func "password: manually set"
    169     func "options: $accountopt"
    170     func "folders: $folders"
    171     func "exclude: $exclude"
    172 	my_hdr=`cat "$acct" | grep '^my_hdr'`
    173 	[[ "$my_hdr" = "" ]] ||
    174 		func "my_hdr:\n$my_hdr"
    175 
    176     return 0
    177 }