summarylogtreecommitdiffstats
path: root/trash-folder.patch
blob: ce47b0c30e5d7078083655960bf2f41ceb68231d (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
From: Cedric Duval <cedricduval@free.fr>
Date: Thu, 27 Feb 2014 12:27:41 +0100
Subject: trash-folder

With this patch, if the trash variable is set to a path (unset by default), the
deleted mails will be moved to a trash folder instead of being irremediably
purged when syncing the mailbox.

For instance, set trash="~/Mail/trash" will cause every deleted mail to go to
this folder.

Note that the append to the trash folder doesn't occur until the resync is
done. This allows you to change your mind and undo deletes, and thus the moves
to the trash folder are unnecessary.

Notes

    * You might also want to have a look at the purge message feature below
      which is related to this patch.
    * IMAP is now supported. To retain the previous behavior, add this to your
      muttrc:
      folder-hook ^imap:// 'unset trash'

FAQ

Every once in a while, someone asks what are the advantages of this patch over
a macro based solution. Here's an attempt to answer this question:

    * The folder history doesn't clutter up with unwanted trash entries.
    * Delayed move to the trash allows to change one's mind.
    * No need to treat the case of "normal folders" and trash folders
      separately with folder-hooks, and to create two sets of macros (one for
      the index, one for the pager).
    * Works not only with delete-message, but also with every deletion
      functions like delete-pattern, delete-thread or delete-subthread.

To sum up, it's more integrated and transparent to the user.

* Patch last synced with upstream:
  - Date: 2007-02-15
  - File: http://cedricduval.free.fr/mutt/patches/download/patch-1.5.5.1.cd.trash_folder.3.4

* Changes made:
  - Updated to 1.5.13:
    - structure of _mutt_save_message changed (commands.c)
    - context of option (OPTCONFIRMAPPEND) changed (muttlib.c)
  - Fixed indentation of "appended" in mutt.h.

Signed-off-by: Matteo F. Vescovi <mfvescovi@gmail.com>

Gbp-Pq: Topic features
---
 commands.c     |  1 +
 flags.c        | 19 +++++++++++++++++-
 globals.h      |  1 +
 imap/message.c |  2 ++
 init.h         | 10 ++++++++++
 mutt.h         |  3 +++
 muttlib.c      |  4 +++-
 mx.c           | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 postpone.c     |  3 +++
 9 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/commands.c b/commands.c
index 5dbd100..7fd014b 100644
--- a/commands.c
+++ b/commands.c
@@ -720,6 +720,7 @@ int _mutt_save_message (HEADER *h, CONTEXT *ctx, int delete, int decode, int dec
     if (option (OPTDELETEUNTAG))
       mutt_set_flag (Context, h, M_TAG, 0);
   }
+  mutt_set_flag (Context, h, M_APPENDED, 1);
   
   return 0;
 }
diff --git a/flags.c b/flags.c
index f0f3d81..dfa6a50 100644
--- a/flags.c
+++ b/flags.c
@@ -65,7 +65,13 @@ void _mutt_set_flag (CONTEXT *ctx, HEADER *h, int flag, int bf, int upd_ctx)
       {
 	h->deleted = 0;
         update = 1;
-	if (upd_ctx) ctx->deleted--;
+	if (upd_ctx)
+	{
+	  ctx->deleted--;
+	  if (h->appended)
+	    ctx->appended--;
+	}
+	h->appended = 0; /* when undeleting, also reset the appended flag */
 #ifdef USE_IMAP
         /* see my comment above */
 	if (ctx->magic == M_IMAP) 
@@ -87,6 +93,17 @@ void _mutt_set_flag (CONTEXT *ctx, HEADER *h, int flag, int bf, int upd_ctx)
       }
       break;
 
+    case M_APPENDED:
+      if (bf)
+      {
+	if (!h->appended)
+	{
+	  h->appended = 1;
+	  if (upd_ctx) ctx->appended++;
+	}
+      }
+      break;
+
     case M_NEW:
 
       if (!mutt_bit_isset(ctx->rights,M_ACL_SEEN))
diff --git a/globals.h b/globals.h
index e77030c..6a1b8da 100644
--- a/globals.h
+++ b/globals.h
@@ -144,6 +144,7 @@ WHERE char *Tochars;
 WHERE char *TSStatusFormat;
 WHERE char *TSIconFormat;
 WHERE short TSSupported;
+WHERE char *TrashPath;
 WHERE char *Username;
 WHERE char *Visual;
 
diff --git a/imap/message.c b/imap/message.c
index 3877381..039fda6 100644
--- a/imap/message.c
+++ b/imap/message.c
@@ -884,6 +884,7 @@ int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
         if (ctx->hdrs[n]->tagged)
         {
           mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
+	  mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
           if (option (OPTDELETEUNTAG))
             mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
         }
@@ -891,6 +892,7 @@ int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
     else
     {
       mutt_set_flag (ctx, h, M_DELETE, 1);
+      mutt_set_flag (ctx, h, M_APPENDED, 1);
       if (option (OPTDELETEUNTAG))
         mutt_set_flag (ctx, h, M_TAG, 0);
     }
diff --git a/init.h b/init.h
index 6b49341..d3206f9 100644
--- a/init.h
+++ b/init.h
@@ -3341,6 +3341,16 @@ struct option_t MuttVars[] = {
   ** provided that ``$$ts_enabled'' has been set. This string is identical in
   ** formatting to the one used by ``$$status_format''.
   */
+  { "trash",		DT_PATH, R_NONE, UL &TrashPath, 0 },
+  /*
+  ** .pp
+  ** If set, this variable specifies the path of the trash folder where the
+  ** mails marked for deletion will be moved, instead of being irremediably
+  ** purged.
+  ** .pp
+  ** NOTE: When you delete a message in the trash folder, it is really
+  ** deleted, so that you have a way to clean the trash.
+  */
 #ifdef USE_SOCKET
   { "tunnel",            DT_STR, R_NONE, UL &Tunnel, UL 0 },
   /*
diff --git a/mutt.h b/mutt.h
index f8565fa..29bb6c2 100644
--- a/mutt.h
+++ b/mutt.h
@@ -185,6 +185,7 @@ enum
   M_DELETE,
   M_UNDELETE,
   M_DELETED,
+  M_APPENDED,
   M_FLAG,
   M_TAG,
   M_UNTAG,
@@ -713,6 +714,7 @@ typedef struct header
   unsigned int mime : 1;    		/* has a MIME-Version header? */
   unsigned int flagged : 1; 		/* marked important? */
   unsigned int tagged : 1;
+  unsigned int appended : 1;		/* has been saved */
   unsigned int deleted : 1;
   unsigned int changed : 1;
   unsigned int attach_del : 1; 		/* has an attachment marked for deletion */
@@ -885,6 +887,7 @@ typedef struct _context
   int new;			/* how many new messages? */
   int unread;			/* how many unread messages? */
   int deleted;			/* how many deleted messages */
+  int appended;                 /* how many saved messages? */
   int flagged;			/* how many flagged messages */
   int msgnotreadyet;		/* which msg "new" in pager, -1 if none */
 
diff --git a/muttlib.c b/muttlib.c
index 02067cc..0fd9766 100644
--- a/muttlib.c
+++ b/muttlib.c
@@ -1505,7 +1505,9 @@ int mutt_save_confirm (const char *s, struct stat *st)
 
   if (magic > 0 && !mx_access (s, W_OK))
   {
-    if (option (OPTCONFIRMAPPEND))
+    if (option (OPTCONFIRMAPPEND) &&
+	(!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
+      /* if we're appending to the trash, there's no point in asking */
     {
       snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
       if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
diff --git a/mx.c b/mx.c
index 4c5cb07..c0a6d30 100644
--- a/mx.c
+++ b/mx.c
@@ -776,6 +776,53 @@ static int sync_mailbox (CONTEXT *ctx, int *index_hint)
   return rc;
 }
 
+/* move deleted mails to the trash folder */
+static int trash_append (CONTEXT *ctx)
+{
+    CONTEXT *ctx_trash;
+    int i = 0;
+    struct stat st, stc;
+
+    if (!TrashPath || !ctx->deleted ||
+	(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
+      return 0;
+
+    for (;i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
+				ctx->hdrs[i]->appended); i++);
+    if (i == ctx->msgcount)
+      return 0; /* nothing to be done */
+
+    if (mutt_save_confirm (TrashPath, &st) != 0)
+    {
+      mutt_error _("message(s) not deleted");
+      return -1;
+    }
+
+    if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
+	&& stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
+      return 0;  /* we are in the trash folder: simple sync */
+
+    if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL)
+    {
+      for (i = 0 ; i < ctx->msgcount ; i++)
+	if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
+	    && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1)
+	  {
+	    mx_close_mailbox (ctx_trash, NULL);
+	    return -1;
+	  }
+
+      mx_close_mailbox (ctx_trash, NULL);
+    }
+    else
+    {
+      mutt_error _("Can't open trash folder");
+      return -1;
+    }
+
+    return 0;
+}
+
 /* save changes and close mailbox */
 int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
 {
@@ -912,6 +959,7 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
 	  if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
 	  {
 	    mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
+	    mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
 	  }
 	  else
 	  {
@@ -936,6 +984,14 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
     return 0;
   }
   
+  /* copy mails to the trash before expunging */
+  if (purge && ctx->deleted && mutt_strcmp(ctx->path, TrashPath))
+    if (trash_append (ctx) != 0)
+    {
+      ctx->closing = 0;
+      return -1;
+    }
+
 #ifdef USE_IMAP
   /* allow IMAP to preserve the deleted flag across sessions */
   if (ctx->magic == M_IMAP)
@@ -1133,6 +1189,12 @@ int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
   msgcount = ctx->msgcount;
   deleted = ctx->deleted;
 
+  if (purge && ctx->deleted && mutt_strcmp(ctx->path, TrashPath))
+  {
+    if (trash_append (ctx) == -1)
+      return -1;
+  }
+
 #ifdef USE_IMAP
   if (ctx->magic == M_IMAP)
     rc = imap_sync_mailbox (ctx, purge, index_hint);
diff --git a/postpone.c b/postpone.c
index a703161..7a4cbb1 100644
--- a/postpone.c
+++ b/postpone.c
@@ -277,6 +277,9 @@ int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size
   /* finished with this message, so delete it. */
   mutt_set_flag (PostContext, h, M_DELETE, 1);
 
+  /* and consider it saved, so that it won't be moved to the trash folder */
+  mutt_set_flag (PostContext, h, M_APPENDED, 1);
+
   /* update the count for the status display */
   PostCount = PostContext->msgcount - PostContext->deleted;