#include "protos.h"
/**
- * edit_one_message - Edit an email
+ * edit_or_view_one_message - Edit an email or view it in an external editor
+ * @param edit true if the message should be editable. If false, changes
+ * to the massage (in the editor) will be ignored.
* @param ctx Context
* @param cur Header of email
* @retval 1 Message not modified
* @retval 0 Message edited successfully
* @retval -1 Error
*/
-static int edit_one_message(struct Context *ctx, struct Header *cur)
+static int edit_or_view_one_message(bool edit, struct Context *ctx,
+ struct Header *cur)
{
char tmp[_POSIX_PATH_MAX];
char buff[STRING];
goto bail;
}
- mtime = mutt_decrease_mtime(tmp, &sb);
+ /* remove write permissions */
+ if (!edit)
+ {
+ rc = chmod(tmp, sb.st_mode & ~(S_IWUSR | S_IWGRP | S_IWOTH));
+ if (rc == -1)
+ {
+ mutt_debug(1, "Could not remove write permissions of %s: %s", tmp, strerror(errno));
+ /* Do not bail out here as we are checking afterwards if we should adopt
+ * changes of the temporary file. */
+ }
+ }
+
+ /* Do not reuse the stat sb here as it is outdated. */
+ mtime = mutt_decrease_mtime(tmp, NULL);
mutt_edit_file(NONULL(Editor), tmp);
goto bail;
}
- if (sb.st_mtime == mtime)
+ if (edit && sb.st_mtime == mtime)
{
mutt_message(_("Message not modified!"));
rc = 1;
goto bail;
}
+ if (!edit && sb.st_mtime != mtime)
+ {
+ mutt_message(_("Message of read-only mailbox modified! Ignoring changes."));
+ rc = 1;
+ goto bail;
+ }
+
+ if (!edit)
+ {
+ /* stop processing here and skip right to the end */
+ rc = 1;
+ goto bail;
+ }
+
fp = fopen(tmp, "r");
if (!fp)
{
return rc;
}
-int mutt_edit_message(struct Context *ctx, struct Header *hdr)
+int edit_or_view_message(bool edit, struct Context *ctx, struct Header *hdr)
{
if (hdr)
- return edit_one_message(ctx, hdr);
+ return edit_or_view_one_message(edit, ctx, hdr);
for (int i = 0; i < ctx->msgcount; i++)
{
if (!message_is_tagged(ctx, i))
continue;
- if (edit_one_message(ctx, ctx->hdrs[i]) == -1)
+ if (edit_or_view_one_message(edit, ctx, ctx->hdrs[i]) == -1)
return -1;
}
return 0;
}
+
+int mutt_edit_message(struct Context *ctx, struct Header *hdr)
+{
+ return edit_or_view_message(true, ctx, hdr); /* true means edit */
+}
+
+int mutt_view_message(struct Context *ctx, struct Header *hdr)
+{
+ return edit_or_view_message(false, ctx, hdr); /* false means only view */
+}
int mutt_dump_variables(int hide_sensitive);
int mutt_edit_attachment(struct Body *a);
int mutt_edit_message(struct Context *ctx, struct Header *hdr);
+int mutt_view_message(struct Context *ctx, struct Header *hdr);
int mutt_fetch_recips(struct Envelope *out, struct Envelope *in, int flags);
int mutt_chscmp(const char *s, const char *chs);
#define mutt_is_utf8(a) mutt_chscmp(a, "utf-8")