Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/dash
- : ${_o_allnet:=no}
- : ${_o_append:=no}
- : ${_o_asksub:=yes}
- : ${_o_askbcc:=no}
- : ${_o_askcc:=no}
- : ${_o_autoprint:=no}
- : ${_o_bang:=no}
- : ${_o_cmd:=no}
- : ${_o_crt:=no}
- : ${_o_debug:=no}
- : ${_o_dot:=no}
- : ${_o_escape:="~"}
- : ${_o_folder:=no}
- : ${_o_header:=yes}
- : ${_o_ignore:=no}
- : ${_o_ignoreeof:=no}
- : ${_o_indentprefix:=" "}
- : ${_o_keep:=no}
- : ${_o_keepsave:=no}
- : ${_o_metoo:=no}
- : ${_o_onehop:=no}
- : ${_o_outfolder:=no}
- : ${_o_page:=no}
- : ${_o_prompt:="? "}
- : ${_o_quiet:=no}
- : ${_o_record:=no}
- : ${_o_save:=yes}
- : ${_o_screen:=}
- : ${_o_sendwait:=no}
- : ${_o_showto:=no}
- : ${_o_sign:=no}
- : ${_o_Sign:=no}
- : ${_o_toplines:=5}
- : ${EDITOR:=vim}
- : ${LISTER:=ls}
- : ${PAGER:=less}
- : ${VISUAL:=vi}
- : ${edit=}
- : ${MAILSEQ:=.edmailseq}
- : ${RLWRAP:=$(command -v rlwrap)}
- export MAILSEQ
- [ -n "${RLWRAP}" ] && \
- readln() { printf '%s' "$(rlwrap ${1:+"-S$1"} -o cat)"; } || \
- readln() { printf '%s' "$(printf '%s' "${1}"; read -r line)"; }
- usage() {
- cat <<EOF
- EOF
- }
- usage_tildecmd() {
- cat <<EOF
- Tilde commands:
- ${_o_escape}[bct] name... Add names to BCC, CC or TO.
- ${_o_escape}d Read dead.letter file into message.
- ${_o_escape}[ev] Invoke EDITOR or VISUAL text editor.
- ${_o_escape}[FfMm] msg Include messages. [Mm] indented. [FM] with headers.
- ${_o_escape}p Print current headers and message.
- ${_o_escape}[r<] file Read the file into the message.
- ${_o_escape}w file Write message onto the named file.
- ${_o_escape}s subject Set the subject.
- ${_o_escape}! command Execute a shell command.
- ${_o_escape}| command Pipe the message through command.
- ${_o_escape}[:_] mail cmd Execute the given mail command.
- ${_o_escape}q Abort and save message into dead.letter file.
- ${_o_escape}x Abort without saving the message.
- ${_o_escape}${_o_escape} text Insert text into message prefaced by a single ${_o_escape}.
- ${_o_escape}. Simulate end of file on input.\n"
- EOF
- }
- draft() {
- draft=$(mktemp "/tmp/draft.XXXXXX")
- {
- printf 'to: %s\n' "${1:-$(readln "To: ")}"
- isset "$_o_asksub" && \
- printf 'subject: %s\n' "$(readln "Subject: ")"
- isset "$_o_askcc" && \
- printf 'cc: %s\n' "$(readln "Cc: ")"
- isset "$_o_askbcc" && \
- printf 'bcc: %s\n' "$(readln "Bcc: ")"
- isset "$_o_from" && \
- printf 'from: %s\n' "$_o_from"
- printf '\n'
- } >"$draft"
- }
- tildecmd() {
- cmd="${1#~*}"; shift
- set -- "$cmd" "$@"
- case "${cmd%%*~}" in
- b|c|t)
- shift
- case "${cmd%%*~}" in
- b) h="bcc" ;;
- c) h="cc" ;;
- t) h="to" ;;
- esac
- hdrapp "$draft" "$h" "$@"
- ;;
- d) cat dead.letter >"$draft" ;;
- e|v)
- [ "${cmd#~*}" = "e" ] && ed="${EDITOR}" || ed="${VISUAL}"
- "$ed" "$draft"
- ;;
- F|f|M|m)
- mshow -R "$2" >>"$draft"
- ;;
- p)
- printf "%s\nMessage contains:\n" '-------'
- mshow "$draft"
- ;;
- r|"<") cat "$2" >>"$draft" ;;
- w) cp "$draft" "$2" ;;
- s) shift; hdrset "$draft" "subject" "$@" ;;
- !*) bang "$@" ;;
- "|") (cat "$draft" | "$@") >"$draft" ;;
- ":"|_) mailcmd "$@" ;;
- .) eot ;;
- q) eot ;;
- x) eot 1 ;;
- a) isset "$_o_sign" && printf '%s\n' "$_o_sign" >>"$draft" ;;
- A) isset "$_o_Sign" && printf '%s\n' "$_o_Sign" >>"$draft" ;;
- i) shift; printf '%s\n' "$@" >>"$draft";;
- ~) printf '%s\n' "$@" >>"$draft";;
- *) usage_tildecmd ;;
- esac
- }
- edit() {
- edit=1
- while [ -n "$edit" ] && line=$(readln); do
- case "$line" in
- .) isset "$_o_dot" && eot && continue ;;
- "$_o_escape"*) tildecmd $line; continue ;;
- esac
- printf '%s\n' "$line" >>"$draft"
- done
- }
- eot() {
- edit=
- [ -z "$draft" ] && return
- [ "$#" -eq 0 ] && cp "$draft" dead.letter
- rm "$draft"
- }
- setopt() {
- s="$@"; k=${s%=*}; v=${s#*=}
- [ -z "$k" ] && set | sed -n 's/^_o_//p' && return
- case "$k" in no*) k=${k#*no}; v="no" ;; esac
- [ "$k" = "ask" ] && k="asksub"
- [ "$k" = "$v" ] && v=""
- eval "_o_$k=\"$v\""
- }
- hdrset() {
- f=$1; shift
- h=$1; shift
- s=$@
- sed -i "/^$h:/c\\$h: $s" "$f"
- }
- hdrapp() {
- f=$1; shift
- h=$1; shift
- s=$@
- [ -n "$(mhdr -h "$h" "$f")" ] && s=", $s"
- sed -i "/^$h:/ s/$/${s}/" "$f"
- }
- isset() {
- [ -n "$1" -a "$1" != "no" ]
- }
- bang() {
- eval "$(printf '%s' "$@" | cut -c 2-)"
- printf '!\n'
- }
- mailcmd() {
- cmd=$1
- case "$cmd" in
- "#"*) ;;
- !*) bang "$@" ;;
- ex|x) exit 1 ;;
- file|"fi"|fold|folder) _o_folder=$2; mailcmd inc ;;
- inc) isset "$_o_folder" && mlist $_o_folder | mseq -S ;;
- m|mail) draft "$2"; edit ;;
- se|set) shift; setopt "$@" ;;
- q|quit) exit 1 ;;
- *)
- case "$cmd" in
- f) cmd="from" ;;
- h) cmd="headers" ;;
- to) cmd="top" ;;
- esac
- e=$(mhdr -h "$cmd" ./cmd-tab)
- [ -z "$e" ] && printf 'unknwon cmd\n' && return
- shift
- eval "$e"
- ;;
- esac
- }
- interactive() {
- isset "$_o_header" && mailcmd "headers"
- while line=$(readln "$_o_prompt"); do
- [ -z "$line" ] && continue
- mailcmd $line
- done
- }
- while read -r line; do
- mailcmd $line
- done <<EOF
- set folder=/home/duncan/mail/personal
- inc
- EOF
- interactive
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement