#!/bin/sh # Copyright (C) 2019-2021 June McEnroe # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # 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 . set -u idle= apply= headers= verbose= host=$(git config fetchemail.imapServer) port=$(git config fetchemail.imapServerPort) user=$(git config fetchemail.imapUser) pass=$(git config fetchemail.imapPass) mailbox=$(git config fetchemail.imapMailbox) subject=$(git config fetchemail.subject) from=$(git config fetchemail.from) to=$(git config fetchemail.to) cc=$(git config fetchemail.cc) unseen=$(git config --type bool fetchemail.unseen) seen=$(git config --type bool fetchemail.markSeen) move=$(git config fetchemail.moveTo) [ "${unseen}" = 'false' ] && unseen= [ "${seen}" = 'false' ] && seen= : ${subject:='[PATCH'} OPTS_SPEC="\ git fetch-email [] -- C,cc= fetch patches with matching Cc headers F,from= fetch patches with matching From headers H,headers fetch only patch headers M,move-to= move patches to mailbox S,subject= fetch patches with matching Subject headers T,to= fetch patches with matching To headers U,unseen fetch unseen patches a,apply apply patches with git-am h,host=! connect to IMAP on host i,idle wait for matching patches m,mailbox=! fetch patches from mailbox p,port=! connect to IMAP on port s,mark-seen mark matching patches as seen u,user=! log in to IMAP as user v,verbose log IMAP protocol to standard error " eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)" while [ $# -gt 0 ]; do opt=$1 shift case "${opt}" in (-C) cc=$1; shift;; (-F) from=$1; shift;; (-H) headers=yes;; (-M) move=$1; shift;; (-S) subject=$1; shift;; (-T) to=$1; shift;; (-U) unseen=true;; (-a) apply=yes;; (-h) host=$1; shift;; (-i) idle=yes;; (-m) mailbox=$1; shift;; (-p) port=$1; shift;; (-s) seen=true; shift;; (-u) user=$1; shift;; (-v) verbose=yes;; (--no-apply) apply=;; (--no-cc) cc=;; (--no-from) from=;; (--no-headers) headers=;; (--no-idle) idle=;; (--no-mark-seen) seen=;; (--no-move-to) move=;; (--no-subject) subject=;; (--no-to) to=;; (--no-unseen) unseen=;; (--no-verbose) verbose=;; (--) break;; esac done if [ -z "${user}" ]; then echo "${0}: username required" >&2 exit 1 fi description() { cat <<-EOF protocol=imaps host=${host:-${user#*@}} username=${user%@*} ${pass:+password=${pass}} EOF } if [ -z "${pass}" ]; then pass=$(description | git credential fill | grep '^password=') pass=${pass#*=} fi fetch() { printf '%s' "${pass}" | imbox -w \ ${verbose:+-v} ${idle:+-i} ${headers:+-H} ${unseen:+-U} ${seen:+-s} \ ${host:+-h "${host}"} \ ${port:+-p "${port}"} \ ${mailbox:+-m "${mailbox}"} \ ${subject:+-S "${subject}"} \ ${from:+-F "${from}"} \ ${to:+-T "${to}"} \ ${cc:+-C "${cc}"} \ ${move:+-M "${move}"} \ ${user} status=$? if [ $status -ne 78 ]; then description | git credential approve else description | git credential reject fi return $status } if [ -n "${apply}" ]; then applySeen=$seen applyMove=$move seen= move= fetch | git am --patch-format=mboxrd "$@" status=$? if [ $status -eq 0 ] && [ -n "${applySeen}${applyMove}" ]; then seen=$applySeen move=$applyMove fetch >/dev/null fi exit $status else fetch fi