You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

trash-folder.patch 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. From: Cedric Duval <cedricduval@free.fr>
  2. Date: Thu, 27 Feb 2014 12:27:41 +0100
  3. Subject: trash-folder
  4. With this patch, if the trash variable is set to a path (unset by default), the
  5. deleted mails will be moved to a trash folder instead of being irremediably
  6. purged when syncing the mailbox.
  7. For instance, set trash="~/Mail/trash" will cause every deleted mail to go to
  8. this folder.
  9. Note that the append to the trash folder doesn't occur until the resync is
  10. done. This allows you to change your mind and undo deletes, and thus the moves
  11. to the trash folder are unnecessary.
  12. Notes
  13. * You might also want to have a look at the purge message feature below
  14. which is related to this patch.
  15. * IMAP is now supported. To retain the previous behavior, add this to your
  16. muttrc:
  17. folder-hook ^imap:// 'unset trash'
  18. FAQ
  19. Every once in a while, someone asks what are the advantages of this patch over
  20. a macro based solution. Here's an attempt to answer this question:
  21. * The folder history doesn't clutter up with unwanted trash entries.
  22. * Delayed move to the trash allows to change one's mind.
  23. * No need to treat the case of "normal folders" and trash folders
  24. separately with folder-hooks, and to create two sets of macros (one for
  25. the index, one for the pager).
  26. * Works not only with delete-message, but also with every deletion
  27. functions like delete-pattern, delete-thread or delete-subthread.
  28. To sum up, it's more integrated and transparent to the user.
  29. * Patch last synced with upstream:
  30. - Date: 2007-02-15
  31. - File: http://cedricduval.free.fr/mutt/patches/download/patch-1.5.5.1.cd.trash_folder.3.4
  32. * Changes made:
  33. - Updated to 1.5.13:
  34. - structure of _mutt_save_message changed (commands.c)
  35. - context of option (OPTCONFIRMAPPEND) changed (muttlib.c)
  36. - Fixed indentation of "appended" in mutt.h.
  37. Signed-off-by: Matteo F. Vescovi <mfvescovi@gmail.com>
  38. Gbp-Pq: Topic features
  39. ---
  40. commands.c | 1 +
  41. flags.c | 19 +++++++++++++++++-
  42. globals.h | 1 +
  43. imap/message.c | 2 ++
  44. init.h | 10 ++++++++++
  45. mutt.h | 3 +++
  46. muttlib.c | 4 +++-
  47. mx.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  48. postpone.c | 3 +++
  49. 9 files changed, 103 insertions(+), 2 deletions(-)
  50. diff --git a/commands.c b/commands.c
  51. index 6b23e39..300d95c 100644
  52. --- a/commands.c
  53. +++ b/commands.c
  54. @@ -720,6 +720,7 @@ int _mutt_save_message (HEADER *h, CONTEXT *ctx, int delete, int decode, int dec
  55. if (option (OPTDELETEUNTAG))
  56. mutt_set_flag (Context, h, M_TAG, 0);
  57. }
  58. + mutt_set_flag (Context, h, M_APPENDED, 1);
  59. return 0;
  60. }
  61. diff --git a/flags.c b/flags.c
  62. index f0f3d81..dfa6a50 100644
  63. --- a/flags.c
  64. +++ b/flags.c
  65. @@ -65,7 +65,13 @@ void _mutt_set_flag (CONTEXT *ctx, HEADER *h, int flag, int bf, int upd_ctx)
  66. {
  67. h->deleted = 0;
  68. update = 1;
  69. - if (upd_ctx) ctx->deleted--;
  70. + if (upd_ctx)
  71. + {
  72. + ctx->deleted--;
  73. + if (h->appended)
  74. + ctx->appended--;
  75. + }
  76. + h->appended = 0; /* when undeleting, also reset the appended flag */
  77. #ifdef USE_IMAP
  78. /* see my comment above */
  79. if (ctx->magic == M_IMAP)
  80. @@ -87,6 +93,17 @@ void _mutt_set_flag (CONTEXT *ctx, HEADER *h, int flag, int bf, int upd_ctx)
  81. }
  82. break;
  83. + case M_APPENDED:
  84. + if (bf)
  85. + {
  86. + if (!h->appended)
  87. + {
  88. + h->appended = 1;
  89. + if (upd_ctx) ctx->appended++;
  90. + }
  91. + }
  92. + break;
  93. +
  94. case M_NEW:
  95. if (!mutt_bit_isset(ctx->rights,M_ACL_SEEN))
  96. diff --git a/globals.h b/globals.h
  97. index 584cd0c..a7e2304 100644
  98. --- a/globals.h
  99. +++ b/globals.h
  100. @@ -139,6 +139,7 @@ WHERE char *StChars;
  101. WHERE char *Status;
  102. WHERE char *Tempdir;
  103. WHERE char *Tochars;
  104. +WHERE char *TrashPath;
  105. WHERE char *Username;
  106. WHERE char *Visual;
  107. WHERE char *XtermTitle;
  108. diff --git a/imap/message.c b/imap/message.c
  109. index 38da127..3c9822f 100644
  110. --- a/imap/message.c
  111. +++ b/imap/message.c
  112. @@ -876,6 +876,7 @@ int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
  113. if (ctx->hdrs[n]->tagged)
  114. {
  115. mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
  116. + mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
  117. if (option (OPTDELETEUNTAG))
  118. mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
  119. }
  120. @@ -883,6 +884,7 @@ int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
  121. else
  122. {
  123. mutt_set_flag (ctx, h, M_DELETE, 1);
  124. + mutt_set_flag (ctx, h, M_APPENDED, 1);
  125. if (option (OPTDELETEUNTAG))
  126. mutt_set_flag (ctx, h, M_TAG, 0);
  127. }
  128. diff --git a/init.h b/init.h
  129. index cf8e75a..d4a882b 100644
  130. --- a/init.h
  131. +++ b/init.h
  132. @@ -3241,6 +3241,16 @@ struct option_t MuttVars[] = {
  133. ** by \fIyou\fP. The sixth character is used to indicate when a mail
  134. ** was sent to a mailing-list you subscribe to.
  135. */
  136. + { "trash", DT_PATH, R_NONE, UL &TrashPath, 0 },
  137. + /*
  138. + ** .pp
  139. + ** If set, this variable specifies the path of the trash folder where the
  140. + ** mails marked for deletion will be moved, instead of being irremediably
  141. + ** purged.
  142. + ** .pp
  143. + ** NOTE: When you delete a message in the trash folder, it is really
  144. + ** deleted, so that you have a way to clean the trash.
  145. + */
  146. #ifdef USE_SOCKET
  147. { "tunnel", DT_STR, R_NONE, UL &Tunnel, UL 0 },
  148. /*
  149. diff --git a/mutt.h b/mutt.h
  150. index 1d7e177..0b879b9 100644
  151. --- a/mutt.h
  152. +++ b/mutt.h
  153. @@ -185,6 +185,7 @@ enum
  154. M_DELETE,
  155. M_UNDELETE,
  156. M_DELETED,
  157. + M_APPENDED,
  158. M_FLAG,
  159. M_TAG,
  160. M_UNTAG,
  161. @@ -709,6 +710,7 @@ typedef struct header
  162. unsigned int mime : 1; /* has a MIME-Version header? */
  163. unsigned int flagged : 1; /* marked important? */
  164. unsigned int tagged : 1;
  165. + unsigned int appended : 1; /* has been saved */
  166. unsigned int deleted : 1;
  167. unsigned int changed : 1;
  168. unsigned int attach_del : 1; /* has an attachment marked for deletion */
  169. @@ -881,6 +883,7 @@ typedef struct _context
  170. int new; /* how many new messages? */
  171. int unread; /* how many unread messages? */
  172. int deleted; /* how many deleted messages */
  173. + int appended; /* how many saved messages? */
  174. int flagged; /* how many flagged messages */
  175. int msgnotreadyet; /* which msg "new" in pager, -1 if none */
  176. diff --git a/muttlib.c b/muttlib.c
  177. index 8005e9c..0425d45 100644
  178. --- a/muttlib.c
  179. +++ b/muttlib.c
  180. @@ -1510,7 +1510,9 @@ int mutt_save_confirm (const char *s, struct stat *st)
  181. if (magic > 0 && !mx_access (s, W_OK))
  182. {
  183. - if (option (OPTCONFIRMAPPEND))
  184. + if (option (OPTCONFIRMAPPEND) &&
  185. + (!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
  186. + /* if we're appending to the trash, there's no point in asking */
  187. {
  188. snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
  189. if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
  190. diff --git a/mx.c b/mx.c
  191. index f599f6c..f7fd01f 100644
  192. --- a/mx.c
  193. +++ b/mx.c
  194. @@ -776,6 +776,53 @@ static int sync_mailbox (CONTEXT *ctx, int *index_hint)
  195. return rc;
  196. }
  197. +/* move deleted mails to the trash folder */
  198. +static int trash_append (CONTEXT *ctx)
  199. +{
  200. + CONTEXT *ctx_trash;
  201. + int i = 0;
  202. + struct stat st, stc;
  203. +
  204. + if (!TrashPath || !ctx->deleted ||
  205. + (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
  206. + return 0;
  207. +
  208. + for (;i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
  209. + ctx->hdrs[i]->appended); i++);
  210. + if (i == ctx->msgcount)
  211. + return 0; /* nothing to be done */
  212. +
  213. + if (mutt_save_confirm (TrashPath, &st) != 0)
  214. + {
  215. + mutt_error _("message(s) not deleted");
  216. + return -1;
  217. + }
  218. +
  219. + if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
  220. + && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
  221. + return 0; /* we are in the trash folder: simple sync */
  222. +
  223. + if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL)
  224. + {
  225. + for (i = 0 ; i < ctx->msgcount ; i++)
  226. + if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
  227. + && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1)
  228. + {
  229. + mx_close_mailbox (ctx_trash, NULL);
  230. + return -1;
  231. + }
  232. +
  233. + mx_close_mailbox (ctx_trash, NULL);
  234. + }
  235. + else
  236. + {
  237. + mutt_error _("Can't open trash folder");
  238. + return -1;
  239. + }
  240. +
  241. + return 0;
  242. +}
  243. +
  244. /* save changes and close mailbox */
  245. int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
  246. {
  247. @@ -912,6 +959,7 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
  248. if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
  249. {
  250. mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
  251. + mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
  252. }
  253. else
  254. {
  255. @@ -936,6 +984,14 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
  256. return 0;
  257. }
  258. + /* copy mails to the trash before expunging */
  259. + if (purge && ctx->deleted && mutt_strcmp(ctx->path, TrashPath))
  260. + if (trash_append (ctx) != 0)
  261. + {
  262. + ctx->closing = 0;
  263. + return -1;
  264. + }
  265. +
  266. #ifdef USE_IMAP
  267. /* allow IMAP to preserve the deleted flag across sessions */
  268. if (ctx->magic == M_IMAP)
  269. @@ -1133,6 +1189,12 @@ int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
  270. msgcount = ctx->msgcount;
  271. deleted = ctx->deleted;
  272. + if (purge && ctx->deleted && mutt_strcmp(ctx->path, TrashPath))
  273. + {
  274. + if (trash_append (ctx) == -1)
  275. + return -1;
  276. + }
  277. +
  278. #ifdef USE_IMAP
  279. if (ctx->magic == M_IMAP)
  280. rc = imap_sync_mailbox (ctx, purge, index_hint);
  281. diff --git a/postpone.c b/postpone.c
  282. index 801ef10..21e96e6 100644
  283. --- a/postpone.c
  284. +++ b/postpone.c
  285. @@ -277,6 +277,9 @@ int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size
  286. /* finished with this message, so delete it. */
  287. mutt_set_flag (PostContext, h, M_DELETE, 1);
  288. + /* and consider it saved, so that it won't be moved to the trash folder */
  289. + mutt_set_flag (PostContext, h, M_APPENDED, 1);
  290. +
  291. /* update the count for the status display */
  292. PostCount = PostContext->msgcount - PostContext->deleted;