aboutsummaryrefslogtreecommitdiff
path: root/bin/mailcfg
blob: ea53a3189ca6287118a34d43bdff4836406903db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/sh
# shellcheck disable=1090,2153
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  mailcfg ACTION
		  mailcfg -h
	EOF
}

help() {
	cat <<-'EOF'

		Options:
		  -h, --help    show this message

		  ACTION        one of:
		                - mbsync
		                - msmtp
		                - notmuchcfg
		                - notmuchhook
		                - alot


		Emit the generated configuration file for the chosen email
		program.  Get the configuration files from
		$XDG_CONFIG_HOME/mailcfg/*.env, where every *.env file is a
		shell script that defines the variables used in this program:
		- $NAME
		- $LABEL
		- $IMAP
		- $SMTP
		- $ADDR

		One of the files also needs to define:
		- $DEFAULT_NAME
		- $DEFAULT_ADDR

		An example of such file could be "30-andre@work.com.env":

		  #!/bin/sh
		  set -eu


		  NAME='André!'
		  LABEL='Work'
		  IMAP='imap.work.com'
		  SMTP='smtp.work.com'
		  ADDR='andre@work.com'


		Examples:

		  Get the alot configuration file:

		    $ mailcfg alot
	EOF
}


for flag; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

while getopts 'h' flag; do
	case "$flag" in
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

ACTION="${1:-}"
eval "$(assert-arg "$ACTION" 'ACTION')"

CFGDIR="${XDG_CONFIG_HOME:-$HOME/.config}/mailcfg"


mbsync() {
	cat <<-'EOF'
		SyncState *
		Create Both
		Expunge Both
		Remove Both
		Sync All
	EOF

	for env in "$CFGDIR"/*.env; do
		. "$env"
		cat <<-EOF


			## $LABEL

			IMAPAccount $LABEL
			Host $IMAP
			User $ADDR
			PassCmd "pass show $ADDR"
			SSLType IMAPS

			IMAPStore ${LABEL}Remote
			Account   $LABEL

			MaildirStore ${LABEL}Local
			Path  ~/Maildir/$LABEL/
			Inbox ~/Maildir/$LABEL/INBOX
			SubFolders Verbatim

			Channel ${LABEL}Folders
			Far    :${LABEL}Remote:
			Near   :${LABEL}Local:
			Patterns *

			Group   $LABEL
			Channel ${LABEL}Folders
		EOF
	done
}

msmtp() {
	cat <<-'EOF'
		defaults
		 auth on
		 tls on
		 port 587
		 syslog on
		 logfile ~/.local/var/log/msmtp
	EOF

	for env in "$CFGDIR"/*.env; do
		. "$env"
		cat <<-EOF

			account $LABEL
			 host $SMTP
			 from $ADDR
			 user $ADDR
			 passwordeval pass show $ADDR
		EOF
	done

	cat <<-EOF

		account default : $DEFAULT_LABEL_LC
	EOF
}

notmuchcfg() {
	for env in "$CFGDIR"/*.env; do
		. "$env"
	done

	cat <<-EOF
		[user]
		name = $DEFAULT_NAME
		primary_email = $DEFAULT_ADDR
	EOF

	printf 'other_email = '
	for env in "$CFGDIR"/*.env; do
		. "$env"
		if [ "$ADDR" = "$DEFAULT_ADDR" ]; then
			continue
		fi
		echo "$ADDR"
	done | paste -sd';'

	cat <<-'EOF'

		[new]
		tags = new;
		ignore = .mbsyncstate;.uidvalidity

		[search]
		exclude_tags = deleted;spam

		[maildir]
		synchronize_flags = true
	EOF
}

notmuchhook() {
	LABELS=''
	for env in "$CFGDIR"/*.env; do
		. "$env"
		if [ -z "$LABELS" ]; then
			LABELS="$LABEL"
		else
			LABELS="$LABELS $LABEL"
		fi
	done
	sed "s|@DIRS@|$LABELS|g" "$CFGDIR"/post-new
}

alot() {
	cat <<-'EOF'
		attachment_prefix = "~/Downloads/"

		[bindings]
		  i = toggletags inbox
		  I = search folder:/INBOX/ AND NOT tag:killed AND NOT tag:archive
	EOF
	echo "
		z archive
		s spam
		u unread
		r keep
		t track
	" | while read -r l; do
		if [ -z "$l" ]; then
			continue
		fi
		LC="$( echo "$l" | cut -d' ' -f1)"
		TAG="$(echo "$l" | cut -d' ' -f2)"
		UC="$(echo "$LC" | tr '[:lower:]' '[:upper:]')"
		cat <<-EOF
			  $LC = toggletags $TAG
			  $UC = search tag:$TAG AND NOT tag:killed
		EOF
	done

	cat <<-'EOF'
		  M = search folder:/lists/ AND NOT tag:killed
		  m = compose --tags inbox
		  [[thread]]
		    v = pipeto urlscan 2>/dev/null
		    V = pipeto 'gpg -d | less'
		    r = reply --all
		    R = reply
		    ' ' = fold; untag unread; move next unfolded
		    P = pipeto 'git am'

		[accounts]
	EOF

	for env in "$CFGDIR"/*.env; do
		. "$env"
		cat <<-EOF
			  [[$LABEL_LC]]
			    realname = $NAME
			    address = $ADDR
			    sendmail_command = msmtpq --account=$LABEL_LC -t
			    sent_box = maildir://~/Maildir/$LABEL/Sent
			    draft_box = maildir://~/Maildir/$LABEL/Drafts
			    gpg_key = 5BDAE9B8B2F6C6BCBB0D6CE581F90EC3CD356060
		EOF
	done
}


case "$ACTION" in
	mbsync|msmtp|notmuchcfg|notmuchhook|alot)
		"$1"
		;;
	*)
		printf 'Unsupported ACTION: "%s".\n\n' "$ACTION" >&2
		usage >&2
		exit 2
		;;
esac