~anjan/sxmo-userscripts

ref: b50d4c3e8da7a19c26752ce8468e07ce613d0cc2 sxmo-userscripts/mail.sh -rwxr-xr-x 3.5 KiB
b50d4c3eStacy Harper Add musicpd user script 5 months ago
                                                                                
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
#!/usr/bin/env sh

# minimal email client.
# script requires mbsync (isync) and mu
# mbsync is required to send email
# If you have alot of email use 
# format of MAILDIR should be $MAILDIR/ACCOUNT/{INBOX,Archive,etc.}/{cur,new,tmp}


findaccount() {
	MAILNUM="$1"
	MAILPATH="$(mseq -r "$MAILNUM")"
	REL="$(realpath --relative-to "$MAILDIR" "$MAILPATH")"
	ACCOUNT="$(echo "$REL" | cut -f1 -d '/')"
	echo "$ACCOUNT"
}

finddir() {
	ACCOUNT="$1"

	FINDME="$2"

	# Must be defined by user
	case "$FINDME" in
		"inbox")
			[ -d "$MAILDIR"/"$ACCOUNT"/"Inbox" ] && echo "Inbox"
			[ -d "$MAILDIR"/"$ACCOUNT"/"INBOX" ] && echo "INBOX"
			;;
		"archive")
			[ -d "$MAILDIR"/"$ACCOUNT"/"Archive" ] && echo "Archive"
			;;
		"sent")
			[ -d "$MAILDIR"/"$ACCOUNT"/"Sent" ] && echo "Sent"
			[ -d "$MAILDIR"/"$ACCOUNT"/"Sent Items" ] && echo "Sent Items"
			;;
	esac

}

prompt() {
	dmenu -i -fn Terminus-20 -c -l 15 "$@"
}

attachmenthandler() {
	ATTACH="$1"
	MIME="$(echo "$ATTACH" | cut -f 2 -d ":" | cut -f 2 -d " ")"
	ATTACHNUM="$(echo "$ATTACH" | cut -f1 -d':')"

	tmp="$(mktemp '/tmp/mblaze.XXXXXX')"
	mshow -O "$MAIL" "$ATTACHNUM" > "$tmp"
	# replace this with xdg?
	case "$MIME" in
		"application/pdf")
			zathura "$tmp"
			;;
		"text/html")
			firefox "$tmp" 
			;;
	esac
	rm "$tmp"

}

showmsg() {
	incontext="$1"

	if [ $incontext = 1 ]
	then
		st -e mless .
	else
		st -e mless
	fi

	while : ;
	do
		CHOICE="$(printf %b "attachments\ncontext\nopen url\nrefile\nCancel" | prompt )"
		case "$CHOICE" in
			"attachments")
				ATTACH="$(mshow -t . | prompt )"
				attachmenthandler "$ATTACH"
				;;
			"context")
				[ $incontext = 1 ] && showmsg 1 && return
				ACCOUNT="$(findaccount .)"
				SENTDIR="$(finddir "$ACCOUNT" "sent")"
				ARCHIVEDIR="$(finddir "$ACCOUNT" "archive")"
				INBOXDIR="$(finddir "$ACCOUNT" "inbox")"
				mthread -S "$MAILDIR/$ACCOUNT/$SENTDIR" -S "$MAILDIR/$ACCOUNT/$ARCHIVEDIR" -S "$MAILDIR/$ACCOUNT/$INBOXDIR" . | mseq -S
				showmsg 1
				break ;;
			"open url")
				which urlview || echo 'Please install urlview' | prompt
				mshow -O . | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*" | sort -u | prompt | xargs -r firefox
				;;
			"refile")
				ACCOUNT="$(findaccount .)"
				ARCHIVEDIR="$(finddir "$ACCOUNT" "archive")"
				mrefile -v . "$MAILDIR/$ACCOUNT/$ARCHIVEDIR" || echo "error occured" | prompt
				st -e mless .+1
				;;
			*)
				break ;;
		esac

	done

}

mainmenu() {
	account="$(ls -1 "$MAILDIR" | prompt)"
	while : ;
	do
		CHOICE="$(printf %b "Get Mail\nChange Account\nShow Inbox\nShow Archive\nCancel" | prompt -p "$account")"
		case "$CHOICE" in
			"Change Account")
				account="$(ls -1 "$MAILDIR" | prompt)"
				;;
			"Get Mail")
				st -e mbsync -a -c "$MBSYNC_CONFIG"
				;;
			"Show Inbox")
				#mlist "$MAILDIR"/"$account"/"$(finddir "$account" "inbox")" | mthread -r | mseq -S
				mlist "$MAILDIR"/"$account"/"$(finddir "$account" "inbox")" | msort -dr | mseq -S
				showmsg 0
				;;
			"Show Archive")
				#mlist "$MAILDIR"/"$account"/"$(finddir "$account" "archive")" | mthread -r | mseq -S
				mlist "$MAILDIR"/"$account"/"$(finddir "$account" "archive")" | msort -dr | mseq -S
				showmsg 0
				;;
			*)
				kill $$;;
		esac
	done

}

# todo, move to startup script and check if set by user
export MAILDIR=~/.mail
export MBSYNC_CONFIG=~/.config/isync/mbsyncrc
export MBLAZE="$XDG_CONFIG_HOME/mblaze"
export MBLAZE_PAGER="less -R"

which mbsync || echo 'Please install isync' | prompt

[ -f "$MBSYNC_CONFIG" ] || echo 'Please configure isync' | prompt

mkdir -p "$MBLAZE"
mainmenu