move-hook patch
Hi, I've hacked up a patch to mutt-1.5.20 that creates a 'move-hook'.
It lets the user specify 'mbox' destinations per message (instead a
single mbox set by 'set mbox'). Think of it as a mbox-hook that matches
messages, not mailboxes.
Let me know if this is useful for others. If this is the case, I'll
update the documentation accordingly, thus creating a 'proper' and
complete patch.
Regards,
Rodolfo Lima
PS: This was partially inspired by the patch created by Haakon Riiser,
http://marc.info/?l=mutt-users&m=112521569325952&w=2
diff -ur mutt-1.5.20.orig/hook.c mutt-1.5.20/hook.c
--- mutt-1.5.20.orig/hook.c 2009-01-13 12:47:49.000000000 -0200
+++ mutt-1.5.20/hook.c 2009-07-12 17:43:51.000000000 -0300
@@ -70,7 +70,7 @@
goto error;
}
- mutt_extract_token (&command, s, (data & (M_FOLDERHOOK | M_SENDHOOK |
M_SEND2HOOK | M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
+ mutt_extract_token (&command, s, (data & (M_FOLDERHOOK | M_SENDHOOK |
M_SEND2HOOK | M_ACCOUNTHOOK | M_REPLYHOOK | M_MOVEHOOK)) ? M_TOKEN_SPACE : 0);
if (!command.data)
{
@@ -99,8 +99,8 @@
char tmp[HUGE_STRING];
/* At this stage remain only message-hooks, reply-hooks, send-hooks,
- * send2-hooks, save-hooks, and fcc-hooks: All those allowing full
- * patterns. If given a simple regexp, we expand $default_hook.
+ * send2-hooks, save-hooks, fcc-hooks, and move-hooks: All those
allowing
+ * full patterns. If given a simple regexp, we expand $default_hook.
*/
strfcpy (tmp, pattern.data, sizeof (tmp));
mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
@@ -109,7 +109,7 @@
pattern.data = safe_strdup (tmp);
}
- if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK))
+ if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK | M_MOVEHOOK))
{
strfcpy (path, command.data, sizeof (path));
mutt_expand_path (path, sizeof (path));
@@ -154,7 +154,7 @@
break;
}
- if (data & (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK |
M_MESSAGEHOOK | M_REPLYHOOK))
+ if (data & (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK |
M_MESSAGEHOOK | M_REPLYHOOK | M_MOVEHOOK))
{
if ((pat = mutt_pattern_comp (pattern.data,
(data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ? 0 : M_FULL_MSG,
@@ -356,7 +356,7 @@
current_hook_type = 0;
}
-static int
+int
mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT *ctx, HEADER
*hdr)
{
HOOK *hook;
diff -ur mutt-1.5.20.orig/init.h mutt-1.5.20/init.h
--- mutt-1.5.20.orig/init.h 2009-06-13 18:35:21.000000000 -0300
+++ mutt-1.5.20/init.h 2009-07-12 17:45:05.000000000 -0300
@@ -3473,6 +3473,7 @@
{ "unmailboxes", mutt_parse_mailboxes, M_UNMAILBOXES },
{ "message-hook", mutt_parse_hook, M_MESSAGEHOOK },
{ "mbox-hook", mutt_parse_hook, M_MBOXHOOK },
+ { "move-hook", mutt_parse_hook, M_MOVEHOOK },
{ "mime_lookup", parse_list, UL &MimeLookupList },
{ "unmime_lookup", parse_unlist, UL &MimeLookupList },
{ "mono", mutt_parse_mono, 0 },
diff -ur mutt-1.5.20.orig/mutt.h mutt-1.5.20/mutt.h
--- mutt-1.5.20.orig/mutt.h 2009-06-12 19:15:42.000000000 -0300
+++ mutt-1.5.20/mutt.h 2009-07-12 17:58:20.000000000 -0300
@@ -135,6 +135,7 @@
/* types for mutt_add_hook() */
#define M_FOLDERHOOK 1
+#define M_MOVEHOOK (1<<20)
#define M_MBOXHOOK (1<<1)
#define M_SENDHOOK (1<<2)
#define M_FCCHOOK (1<<3)
diff -ur mutt-1.5.20.orig/mx.c mutt-1.5.20/mx.c
--- mutt-1.5.20.orig/mx.c 2009-06-11 01:29:41.000000000 -0300
+++ mutt-1.5.20/mx.c 2009-07-12 18:18:54.000000000 -0300
@@ -773,6 +773,8 @@
return rc;
}
+int mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT *ctx, HEADER
*hdr);
+
/* save changes and close mailbox */
int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
{
@@ -895,17 +897,31 @@
else /* use regular append-copy mode */
#endif
{
- if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL)
- {
- ctx->closing = 0;
- return -1;
- }
+ char move_hook_mbox[_POSIX_PATH_MAX];
+ char *mbox_ptr = mbox;
for (i = 0; i < ctx->msgcount; i++)
{
+ if(mutt_addr_hook(move_hook_mbox, sizeof(move_hook_mbox),
+ M_MOVEHOOK, ctx, ctx->hdrs[i]) == 0)
+ {
+ mbox_ptr = move_hook_mbox;
+ }
+ else
+ mbox_ptr = mbox;
+
if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
- && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
- {
+ && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
+ {
+ if (!ctx->quiet)
+ mutt_message (_("Moving read message to %s..."), mbox_ptr);
+
+ if (mx_open_mailbox (mbox_ptr, M_APPEND, &f) == NULL)
+ {
+ ctx->closing = 0;
+ return -1;
+ }
+
if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
0)
{
mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
@@ -916,12 +932,10 @@
ctx->closing = 0;
return -1;
}
+ mx_close_mailbox (&f, NULL);
}
}
-
- mx_close_mailbox (&f, NULL);
}
-
}
else if (!ctx->changed && ctx->deleted == 0)
{