Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. #!/bin/dash
  2.  
  3. : ${_o_allnet:=no}
  4. : ${_o_append:=no}
  5. : ${_o_asksub:=yes}
  6. : ${_o_askbcc:=no}
  7. : ${_o_askcc:=no}
  8. : ${_o_autoprint:=no}
  9. : ${_o_bang:=no}
  10. : ${_o_cmd:=no}
  11. : ${_o_crt:=no}
  12. : ${_o_debug:=no}
  13. : ${_o_dot:=no}
  14. : ${_o_escape:="~"}
  15. : ${_o_folder:=no}
  16. : ${_o_header:=yes}
  17. : ${_o_ignore:=no}
  18. : ${_o_ignoreeof:=no}
  19. : ${_o_indentprefix:=" "}
  20. : ${_o_keep:=no}
  21. : ${_o_keepsave:=no}
  22. : ${_o_metoo:=no}
  23. : ${_o_onehop:=no}
  24. : ${_o_outfolder:=no}
  25. : ${_o_page:=no}
  26. : ${_o_prompt:="? "}
  27. : ${_o_quiet:=no}
  28. : ${_o_record:=no}
  29. : ${_o_save:=yes}
  30. : ${_o_screen:=}
  31. : ${_o_sendwait:=no}
  32. : ${_o_showto:=no}
  33. : ${_o_sign:=no}
  34. : ${_o_Sign:=no}
  35. : ${_o_toplines:=5}
  36.  
  37. : ${EDITOR:=vim}
  38. : ${LISTER:=ls}
  39. : ${PAGER:=less}
  40. : ${VISUAL:=vi}
  41.  
  42. : ${edit=}
  43.  
  44. : ${MAILSEQ:=.edmailseq}
  45. : ${RLWRAP:=$(command -v rlwrap)}
  46.  
  47. export MAILSEQ
  48.  
  49. [ -n "${RLWRAP}" ] && \
  50. readln() { printf '%s' "$(rlwrap ${1:+"-S$1"} -o cat)"; } || \
  51. readln() { printf '%s' "$(printf '%s' "${1}"; read -r line)"; }
  52.  
  53. usage() {
  54. cat <<EOF
  55. EOF
  56. }
  57.  
  58. usage_tildecmd() {
  59. cat <<EOF
  60. Tilde commands:
  61. ${_o_escape}[bct] name... Add names to BCC, CC or TO.
  62. ${_o_escape}d Read dead.letter file into message.
  63. ${_o_escape}[ev] Invoke EDITOR or VISUAL text editor.
  64. ${_o_escape}[FfMm] msg Include messages. [Mm] indented. [FM] with headers.
  65. ${_o_escape}p Print current headers and message.
  66. ${_o_escape}[r<] file Read the file into the message.
  67. ${_o_escape}w file Write message onto the named file.
  68. ${_o_escape}s subject Set the subject.
  69. ${_o_escape}! command Execute a shell command.
  70. ${_o_escape}| command Pipe the message through command.
  71. ${_o_escape}[:_] mail cmd Execute the given mail command.
  72. ${_o_escape}q Abort and save message into dead.letter file.
  73. ${_o_escape}x Abort without saving the message.
  74. ${_o_escape}${_o_escape} text Insert text into message prefaced by a single ${_o_escape}.
  75. ${_o_escape}. Simulate end of file on input.\n"
  76. EOF
  77. }
  78.  
  79. draft() {
  80. draft=$(mktemp "/tmp/draft.XXXXXX")
  81. {
  82. printf 'to: %s\n' "${1:-$(readln "To: ")}"
  83. isset "$_o_asksub" && \
  84. printf 'subject: %s\n' "$(readln "Subject: ")"
  85. isset "$_o_askcc" && \
  86. printf 'cc: %s\n' "$(readln "Cc: ")"
  87. isset "$_o_askbcc" && \
  88. printf 'bcc: %s\n' "$(readln "Bcc: ")"
  89. isset "$_o_from" && \
  90. printf 'from: %s\n' "$_o_from"
  91. printf '\n'
  92. } >"$draft"
  93. }
  94.  
  95. tildecmd() {
  96. cmd="${1#~*}"; shift
  97. set -- "$cmd" "$@"
  98. case "${cmd%%*~}" in
  99. b|c|t)
  100. shift
  101. case "${cmd%%*~}" in
  102. b) h="bcc" ;;
  103. c) h="cc" ;;
  104. t) h="to" ;;
  105. esac
  106. hdrapp "$draft" "$h" "$@"
  107. ;;
  108. d) cat dead.letter >"$draft" ;;
  109. e|v)
  110. [ "${cmd#~*}" = "e" ] && ed="${EDITOR}" || ed="${VISUAL}"
  111. "$ed" "$draft"
  112. ;;
  113. F|f|M|m)
  114. mshow -R "$2" >>"$draft"
  115. ;;
  116. p)
  117. printf "%s\nMessage contains:\n" '-------'
  118. mshow "$draft"
  119. ;;
  120. r|"<") cat "$2" >>"$draft" ;;
  121. w) cp "$draft" "$2" ;;
  122. s) shift; hdrset "$draft" "subject" "$@" ;;
  123. !*) bang "$@" ;;
  124. "|") (cat "$draft" | "$@") >"$draft" ;;
  125. ":"|_) mailcmd "$@" ;;
  126. .) eot ;;
  127. q) eot ;;
  128. x) eot 1 ;;
  129. a) isset "$_o_sign" && printf '%s\n' "$_o_sign" >>"$draft" ;;
  130. A) isset "$_o_Sign" && printf '%s\n' "$_o_Sign" >>"$draft" ;;
  131. i) shift; printf '%s\n' "$@" >>"$draft";;
  132. ~) printf '%s\n' "$@" >>"$draft";;
  133. *) usage_tildecmd ;;
  134. esac
  135. }
  136.  
  137. edit() {
  138. edit=1
  139. while [ -n "$edit" ] && line=$(readln); do
  140. case "$line" in
  141. .) isset "$_o_dot" && eot && continue ;;
  142. "$_o_escape"*) tildecmd $line; continue ;;
  143. esac
  144. printf '%s\n' "$line" >>"$draft"
  145. done
  146. }
  147.  
  148. eot() {
  149. edit=
  150. [ -z "$draft" ] && return
  151. [ "$#" -eq 0 ] && cp "$draft" dead.letter
  152. rm "$draft"
  153. }
  154.  
  155. setopt() {
  156. s="$@"; k=${s%=*}; v=${s#*=}
  157. [ -z "$k" ] && set | sed -n 's/^_o_//p' && return
  158. case "$k" in no*) k=${k#*no}; v="no" ;; esac
  159. [ "$k" = "ask" ] && k="asksub"
  160. [ "$k" = "$v" ] && v=""
  161. eval "_o_$k=\"$v\""
  162. }
  163.  
  164. hdrset() {
  165. f=$1; shift
  166. h=$1; shift
  167. s=$@
  168. sed -i "/^$h:/c\\$h: $s" "$f"
  169. }
  170.  
  171. hdrapp() {
  172. f=$1; shift
  173. h=$1; shift
  174. s=$@
  175. [ -n "$(mhdr -h "$h" "$f")" ] && s=", $s"
  176. sed -i "/^$h:/ s/$/${s}/" "$f"
  177. }
  178.  
  179. isset() {
  180. [ -n "$1" -a "$1" != "no" ]
  181. }
  182.  
  183. bang() {
  184. eval "$(printf '%s' "$@" | cut -c 2-)"
  185. printf '!\n'
  186. }
  187.  
  188. mailcmd() {
  189. cmd=$1
  190. case "$cmd" in
  191. "#"*) ;;
  192. !*) bang "$@" ;;
  193. ex|x) exit 1 ;;
  194. file|"fi"|fold|folder) _o_folder=$2; mailcmd inc ;;
  195. inc) isset "$_o_folder" && mlist $_o_folder | mseq -S ;;
  196. m|mail) draft "$2"; edit ;;
  197. se|set) shift; setopt "$@" ;;
  198. q|quit) exit 1 ;;
  199. *)
  200. case "$cmd" in
  201. f) cmd="from" ;;
  202. h) cmd="headers" ;;
  203. to) cmd="top" ;;
  204. esac
  205. e=$(mhdr -h "$cmd" ./cmd-tab)
  206. [ -z "$e" ] && printf 'unknwon cmd\n' && return
  207. shift
  208. eval "$e"
  209. ;;
  210. esac
  211. }
  212.  
  213. interactive() {
  214. isset "$_o_header" && mailcmd "headers"
  215. while line=$(readln "$_o_prompt"); do
  216. [ -z "$line" ] && continue
  217. mailcmd $line
  218. done
  219. }
  220.  
  221. while read -r line; do
  222. mailcmd $line
  223. done <<EOF
  224. set folder=/home/duncan/mail/personal
  225. inc
  226. EOF
  227.  
  228. interactive
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement