#!/usr/bin/env bash # HEADER #======================================================================= # # email -- Shell script to sync my emails with isync and read # them with mutt. # # SYNOPSIS # email # # DESCRIPTION # Shell script to get my email with `isync` (tool to sync mail # from IMAP servers) and read them width `mutt`. # # EXAMPLES # $ email account # $ email -n account # # POSITIONAL ARGUMENT # mail account # # known as "account" in your isync configuration file # ~/.mbsyncrc # that is in # grep -e "^IMAPAccount" ~/.mbsyncrc | cut -d" " -f2 # and define with a mutt configuration file in # ~/Mail/.config/${MailAccount}.rc # # # OPTIONAL ARGUMENTS # -h, --help show this help message and exit # -l, --list list mail accounts # -n, --new just check for new emails # -N, --allnew just check for new emails for all accounts # -o, --offline offline mode - do not call offlineimap # -r, -R, --readonly read only mode - do not write anything # # DEPENDENCIES # - isync # - mutt # # INSTALLATION # - link file in $PATH (~/bin/) # - add completion script in "~/.bashrc": # _email_completions(){ # local cur prev opts # COMPREPLY=() # cur="${COMP_WORDS[COMP_CWORD]}" # prev="${COMP_WORDS[COMP_CWORD-1]}" # opts=$(grep -e "^IMAPAccount" ~/.mbsyncrc \ # | cut -d" " -f2 | tr -d ']' | xargs )" local -o -n" # if [[ ${cur} == * ]] ; then # COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) # return 0 # fi # } # complete -F _email_completions email # # Copyright (C) 2020 Nicolas Mesnier # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3 or # above as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # #======================================================================= # END_OF_HEADER #----------------------------------------------------------------------- # *** read this header to display help and version #----------------------------------------------------------------------- BOH=$(head -200 ${0} | grep -n "^# HEADER" | cut -f1 -d:) EOH=$(head -200 ${0} | grep -n "^# END_OF_HEADER" | cut -f1 -d:) function Help(){ head -$(($EOH-1)) ${0} | tail -$(($EOH-$BOH-1)) \ | grep -e "^#$" -e "^# " | sed -e "s/^#=*//g" } #----------------------------------------------------------------------- # *** parameters *** #----------------------------------------------------------------------- account_list=$(grep -e "^IMAPAccount" ~/.mbsyncrc | cut -d" " -f2 | xargs ) accounts="@("$(echo ${account_list}| tr ' ' '|')")" #----------------------------------------------------------------------- # *** get options *** #----------------------------------------------------------------------- MailAccount="" muttOptions="" OnlyNew=false offline=false readOnly=false AllNew=false shopt -s extglob while [[ ${1} ]]; do case "${1}" in -h|--help) Help exit 0 ;; -l|--list) echo " *** List of accounts:" echo " "${account_list} exit 0 ;; -n|--new) OnlyNew=true ;; -N|--allnew) AllNew=true ;; -o|--offline) offline=true ;; -r|-R|--readonly) readOnly=true ;; *) if [ -z ${MailAccount} ];then case "${1}" in ${accounts}|local) MailAccount=${1} ;; *) echo "Uknown mail account \"${1}\"." >&2 echo "Must be part of: "${account_list} >&2 exit 1 ;; esac else echo "Mail account \"${MailAccount}\" allready given." >&2 exit 1 fi ;; esac if ! shift; then echo 'Missing parameter argument.' >&2 exit 1 fi done #----------------------------------------------------------------------- # *** run #----------------------------------------------------------------------- if [ "${AllNew}" = true ];then for MailAccount in ${account_list};do echo " *** Processing account "${MailAccount} mbsync -q ${MailAccount} \ && mutt -F ~/Mail/.config/${MailAccount}.rc -Z done else case ${MailAccount} in ${accounts}) if [ "${OnlyNew}" = true ];then mbsync -nd -q ${MailAccount} \ && mutt -F ~/Mail/.config/${MailAccount}.rc -Z elif [ "${offline}" = true ];then mutt -F ~/Mail/.config/${MailAccount}.rc elif [ "${readOnly}" = true ];then mutt -R -F ~/Mail/.config/${MailAccount}.rc else mbsync -q ${MailAccount} \ && mutt -F ~/Mail/.config/${MailAccount}.rc fi ;; local) mutt -F ~/Mail/.config/${MailAccount}.rc ;; esac fi #====================================================================eof # vim: set tw=72 ts=4 sw=4 nu: