-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfish.fish
251 lines (227 loc) · 7.16 KB
/
mfish.fish
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
#!/usr/bin/env fish
# =============================================================================
# mfish - A plugin for managing email workflows with mblaze and Fish shell
#
# Copyright (C) 2025 Xavier Maillard <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# =============================================================================
# Fonction interne pour récupérer une variable dans $MBLAZE/profile
function __mfish_get_profile_var
set -l var_name $argv[1]
set -l profile_file "$MBLAZE/profile"
if test -f $profile_file
mhdr -h $var_name $profile_file 2>/dev/null
else
echo ""
end
end
# Main function
function mfish
set -l prompt_default "mfish"
set -l maildir_default "~/mail/Reception"
set -l maildir $maildir_default
set -l profile_file "$MBLAZE/profile"
switch $argv[1]
case -q
mfish_prompt $maildir
case -1
set -e argv[1]
if test (count $argv) -gt 0
switch $argv[1]
case fetch
mfish_fetch $argv[2..-1]
case index
mfish_index
case ls
mfish_ls $argv[2..-1]
case cd
mfish_cd $argv[2..-1]
case show
mfish_show $argv[2..-1]
case grep
mfish_grep $argv[2..-1]
case rm
mfish_rm $argv[2..-1]
case spam
mfish_spam $argv[2..-1]
case help
mfish_help
case '*'
echo "Unknown command: $argv[1]. Type 'help' for a list of commands."
end
else
echo "Error: No command provided."
return 1
end
case fetch
mfish_fetch $argv[2..-1]
case '*'
if tmux has-session -t mfish 2>/dev/null
tmux attach -t mfish
else
tmux new-session -d -s mfish
mfish_ls $maildir_default | tmux send-keys -t mfish "less" Enter
tmux attach -t mfish
end
end
end
# Prompt interactif
function mfish_prompt
set -l maildir $argv[1]
while true
echo -n "mfish:$maildir> "
read -l cmd args
switch $cmd
case fetch
mfish_fetch $args
case index
mfish_index
case ls
mfish_ls $args
case cd
mfish_cd $args
case show
mfish_show $args
case grep
mfish_grep $args
case rm
mfish_rm $args
case spam
mfish_spam $args
case cur
mfish_cur $args
case new
mfish_new $args
case help
mfish_help
case quit
mfish_quit
case ''
continue
case \d+
mfish_show $cmd
case '*'
echo "Unknown command: $cmd. Type 'help' for a list of commands."
end
end
end
# Commande fetch
function mfish_fetch
set -l fetch_cmd (__mfish_get_profile_var MailFetchCommand)
set -l fetch_sleep (__mfish_get_profile_var MailFetchSleep)
set -q fetch_cmd; or set fetch_cmd "fdm -q"
set -q fetch_sleep; or set fetch_sleep 30
if contains -- "--daemon" $argv
while true
echo "INFO: fetching new mails..."
eval $fetch_cmd
echo "INFO: indexing new mails..."
mfish_index
echo "INFO: Sleeping... $fetch_sleep seconds"
sleep $fetch_sleep
end
else
echo "INFO: fetching new mails..."
eval $fetch_cmd
echo "INFO: indexing new mails..."
mfish_index
echo "INFO: exiting"
end
end
# Commande index
function mfish_index
set -l index_cmd (__mfish_get_profile_var "IndexCommand")
eval (or $index_cmd "mscan $maildir_default")
end
# Commande cd
function mfish_cd
set -l target_dir (or $argv[1] $maildir_default)
if test -d $target_dir
set maildir $target_dir
else
echo "Error: Directory not found."
end
end
# Commande ls
function mfish_ls
set -l target_dir (or $argv[1] $maildir)
#mlist $target_dir | msort -dr | head -25 | mthread | mseq -S | mscan
mlist ~/mail/Reception | msort -dr | head -25 | mthread | mseq -S | mscan
end
# Commande show
function mfish_show
set -l message_id (or $argv[1] "last")
if test $message_id = "last"
set message_id (mscan -last)
end
tmux split-window -h "mshow $maildir/$message_id"
end
# Commande grep (recherche dans les mails)
function mfish_grep
mgrep $argv
end
# Commande rm (supprimer un mail)
function mfish_rm
mrm $argv
end
# Commande spam (signaler un spam)
function mfish_spam
mspam $argv
end
# Commande cur (changer vers le répertoire cur)
function mfish_cur
set -l target_dir "$maildir/cur"
if test -d $target_dir
mfish_cd $target_dir
mfish_ls
else
echo "Error: Directory 'cur' not found."
end
end
# Commande new (changer vers le répertoire new)
function mfish_new
set -l target_dir "$maildir/new"
if test -d $target_dir
mfish_cd $target_dir
mfish_ls
else
echo "Error: Directory 'new' not found."
end
end
# Commande quit (quitter la REPL)
function mfish_quit
echo "Exiting mfish REPL. Goodbye!"
exit
end
# Commande help
function mfish_help
echo "Commands available in mfish:"
echo " fetch [--daemon] Fetch mail"
echo " index Index current maildir"
echo " ls [MAILDIR] List messages in a maildir"
echo " cd [MAILDIR] Change maildir"
echo " show [MESSAGE_ID] Show a message"
echo " grep [PATTERN] Search in maildir"
echo " rm [MESSAGE_ID] Delete a message"
echo " spam [MESSAGE_ID] Mark a message as spam"
echo " cur Switch to 'cur' directory"
echo " new Switch to 'new' directory"
echo " help Show this help message"
end