<<< Date Index >>>     <<< Thread Index >>>

[patch] Remind about attachments before sending



Hi,
while graphical mail clients allow the user to attach attachments while he's
writing the email, mutt requires you to do so after you've finished writing.
So I guess I'm not the only one who forgets he's attachments once in a while.

Attached (yes, it is) patch from Bernard Blackham has been bit rotting in the
Debian bugtracking system for some time now. It checks for user definable
keywords in the mail body (the default is "attach") and prompts the user if
he really wants to send the mail if it doesn't carry an attachment. 

The patch has been sent to this list once, but didn't seem to have caught
attention. I've resynced the patch against current CVS, please consider
inclusion.

Cheers,
        Moritz
diff -Naur mutt.orig/globals.h mutt/globals.h
--- mutt.orig/globals.h 2004-08-30 22:05:40.000000000 +0200
+++ mutt/globals.h      2004-11-26 13:20:05.000000000 +0100
@@ -32,6 +32,7 @@
 
 WHERE char *AliasFile;
 WHERE char *AliasFmt;
+WHERE char *AttachKeyword;
 WHERE char *AttachSep;
 WHERE char *Attribution;
 WHERE char *AttachFormat;
diff -Naur mutt.orig/init.h mutt/init.h
--- mutt.orig/init.h    2005-01-24 18:32:54.000000000 +0100
+++ mutt/init.h 2005-01-24 18:30:50.000000000 +0100
@@ -102,6 +102,14 @@
 
 struct option_t MuttVars[] = {
   /*++*/
+  { "abort_noattach",  DT_QUAD, R_NONE, OPT_ATTACH, M_ASKYES },
+  /*
+  ** .pp
+  ** If set to \fIyes\fP, when composing messages containing the word
+  ** specified by $attach_keyword (default is "attach") and no attachments 
+  ** are given, composition will be aborted. If set to \fIno\fP, composing
+  ** messages as such will never be aborted.
+  */
   { "abort_nosubject", DT_QUAD, R_NONE, OPT_SUBJECT, M_ASKYES },
   /*
   ** .pp
@@ -210,6 +218,13 @@
   ** .dt %|X .dd pad to the end of the line with character "X"
   ** .de
   */
+  { "attach_keyword",  DT_STR,  R_NONE, UL &AttachKeyword, UL "attach" },
+  /*
+  ** .pp
+  ** If $abort_attach is not set to no, then the body of the message
+  ** will be scanned for this keyword, and if found, you will be prompted
+  ** if there are no attachments. This is case insensitive.
+  */
   { "attach_sep",      DT_STR,  R_NONE, UL &AttachSep, UL "\n" },
   /*
   ** .pp
diff -Naur mutt.orig/mutt.h mutt/mutt.h
--- mutt.orig/mutt.h    2005-01-24 18:32:54.000000000 +0100
+++ mutt/mutt.h 2005-01-24 18:30:50.000000000 +0100
@@ -272,6 +272,7 @@
 enum
 {
   OPT_ABORT,
+  OPT_ATTACH,
   OPT_BOUNCE,
   OPT_COPY,
   OPT_DELETE,
diff -Naur mutt.orig/send.c mutt/send.c
--- mutt.orig/send.c    2005-01-24 18:32:54.000000000 +0100
+++ mutt/send.c 2005-01-24 18:30:51.000000000 +0100
@@ -1034,6 +1034,34 @@
 }
 
 int
+mutt_search_attach_keyword(char* filename)
+{
+  /* searches for the magic keyword "attach" within a file */
+  int found = 0;
+  char* inputline = malloc(1024);
+  char* lowerKeyword = malloc(strlen(AttachKeyword)+1);
+  FILE *attf = fopen(filename, "r");
+  int i;
+  for (i=0; i <= strlen(AttachKeyword); i++) {
+    lowerKeyword[i] = tolower(AttachKeyword[i]);
+  }
+  while (!feof(attf)) {
+    fgets(inputline, 1024, attf);
+    for (i=0; i < strlen(inputline); i++) {
+      inputline[i] = tolower(inputline[i]);
+    }
+    if (strstr(inputline, lowerKeyword)) {
+        found = 1;
+        break;
+    }
+  }
+  free(inputline);
+  free(lowerKeyword);
+  fclose(attf);
+  return found;
+}
+
+int
 ci_send_message (int flags,            /* send mode */
                 HEADER *msg,           /* template to use for new message */
                 char *tempfile,        /* file specified by -i or -H */
@@ -1496,6 +1524,21 @@
     goto main_loop;
   }
 
+  if (mutt_search_attach_keyword(msg->content->filename) && 
+         !msg->content->next && 
+         query_quadoption(OPT_ATTACH, _("No attachments, cancel sending?")) != 
M_NO)
+  {
+    /* if the abort is automatic, print an error message */
+    if (quadoption (OPT_ATTACH) == M_YES) {
+         char errorstr[512];
+         if (snprintf(errorstr, 512, 
+                       "Message contains magic keyword \"%s\", but no 
attachments. Not sending.", AttachKeyword)==-1)
+               errorstr[511] = 0; // terminate if need be. our string shouldnt 
be this long.
+      mutt_error _(errorstr);
+    }
+    goto main_loop;
+  }
+
   if (msg->content->next)
     msg->content = mutt_make_multipart (msg->content);