Allow hand-editing of patches before sending
[stgit] / stgit / commands / mail.py
index 34504e6..2b28564 100644 (file)
@@ -36,10 +36,10 @@ generated from the template file passed as argument to '--template'
 can either be added to the template file or passed via the
 corresponding command line options.
 
-A preamble e-mail can be sent using the '--cover' and/or '--edit'
-options. The first allows the user to specify a file to be used as a
-template. The latter option will invoke the editor on the specified
-file (defaulting to '.git/covermail.tmpl' or
+A preamble e-mail can be sent using the '--cover' and/or
+'--edit-cover' options. The first allows the user to specify a file to
+be used as a template. The latter option will invoke the editor on the
+specified file (defaulting to '.git/covermail.tmpl' or
 '~/.stgit/templates/covermail.tmpl' or
 '/usr/share/stgit/templates/covermail.tmpl').
 
@@ -65,6 +65,7 @@ the following variables:
   %(diffstat)s     - diff statistics
   %(date)s         - current date/time
   %(version)s      - ' version' string passed on the command line (or empty)
+  %(prefix)s       - 'prefix ' string passed on the command line
   %(patchnr)s      - patch number
   %(totalnr)s      - total number of patches to be sent
   %(number)s       - empty if only one patch is sent or ' patchnr/totalnr'
@@ -98,13 +99,18 @@ options = [make_option('-a', '--all',
                        action = 'store_true'),
            make_option('-v', '--version', metavar = 'VERSION',
                        help = 'add VERSION to the [PATCH ...] prefix'),
+           make_option('--prefix', metavar = 'PREFIX',
+                       help = 'add PREFIX to the [... PATCH ...] prefix'),
            make_option('-t', '--template', metavar = 'FILE',
                        help = 'use FILE as the message template'),
            make_option('-c', '--cover', metavar = 'FILE',
                        help = 'send FILE as the cover message'),
-           make_option('-e', '--edit',
+           make_option('-e', '--edit-cover',
                        help = 'edit the cover message before sending',
                        action = 'store_true'),
+           make_option('-E', '--edit-patches',
+                       help = 'edit each patch before sending',
+                       action = 'store_true'),
            make_option('-s', '--sleep', type = 'int', metavar = 'SECONDS',
                        help = 'sleep for SECONDS between e-mails sending'),
            make_option('--refid',
@@ -193,7 +199,15 @@ def __build_address_headers(tmpl, options, extra_cc = []):
     template.
     """
     def csv(lst):
-        return reduce(lambda x, y: x + ', ' + y, lst)
+        s = ''
+        for i in lst:
+            if not i:
+                continue
+            if s:
+                s += ', ' + i
+            else:
+                s = i
+        return s
 
     def replace_header(header, addr, tmpl):
         r = re.compile('^' + header + ':\s+.+$', re.I | re.M)
@@ -210,6 +224,11 @@ def __build_address_headers(tmpl, options, extra_cc = []):
     cc_addr = ''
     bcc_addr = ''
 
+    if config.has_option('stgit', 'autobcc'):
+        autobcc = config.get('stgit', 'autobcc')
+    else:
+        autobcc = ''
+
     if options.to:
         to_addr = csv(options.to)
     if options.cc:
@@ -217,7 +236,9 @@ def __build_address_headers(tmpl, options, extra_cc = []):
     elif extra_cc:
         cc_addr = csv(extra_cc)
     if options.bcc:
-        bcc_addr = csv(options.bcc)
+        bcc_addr = csv(options.bcc + [autobcc])
+    elif autobcc:
+        bcc_addr = autobcc
 
     # replace existing headers
     if to_addr:
@@ -258,6 +279,34 @@ def __build_extra_headers():
 
     return headers
 
+def edit_message(msg):
+    fname = '.stgitmail.txt'
+
+    # create the initial file
+    f = file(fname, 'w')
+    f.write(msg)
+    f.close()
+
+    # the editor
+    if config.has_option('stgit', 'editor'):
+        editor = config.get('stgit', 'editor')
+    elif 'EDITOR' in os.environ:
+        editor = os.environ['EDITOR']
+    else:
+        editor = 'vi'
+    editor += ' %s' % fname
+
+    print 'Invoking the editor: "%s"...' % editor,
+    sys.stdout.flush()
+    print 'done (exit code: %d)' % os.system(editor)
+
+    # read the message back
+    f = file(fname)
+    msg = f.read()
+    f.close()
+
+    return msg
+
 def __build_cover(tmpl, total_nr, msg_id, options):
     """Build the cover message (series description) to be sent via SMTP
     """
@@ -277,6 +326,11 @@ def __build_cover(tmpl, total_nr, msg_id, options):
     else:
         version_str = ''
 
+    if options.prefix:
+        prefix_str = options.prefix + ' '
+    else:
+        prefix_str = ''
+        
     total_nr_str = str(total_nr)
     patch_nr_str = '0'.zfill(len(total_nr_str))
     if total_nr > 1:
@@ -288,6 +342,7 @@ def __build_cover(tmpl, total_nr, msg_id, options):
                  'endofheaders': headers_end,
                  'date':         email.Utils.formatdate(localtime = True),
                  'version':      version_str,
+                 'prefix':      prefix_str,
                  'patchnr':      patch_nr_str,
                  'totalnr':      total_nr_str,
                  'number':       number_str}
@@ -301,31 +356,8 @@ def __build_cover(tmpl, total_nr, msg_id, options):
         raise CmdException, 'Only "%(name)s" variables are ' \
               'supported in the patch template'
 
-    if options.edit:
-        fname = '.stgitmail.txt'
-
-        # create the initial file
-        f = file(fname, 'w+')
-        f.write(msg)
-        f.close()
-
-        # the editor
-        if config.has_option('stgit', 'editor'):
-            editor = config.get('stgit', 'editor')
-        elif 'EDITOR' in os.environ:
-            editor = os.environ['EDITOR']
-        else:
-            editor = 'vi'
-        editor += ' %s' % fname
-
-        print 'Invoking the editor: "%s"...' % editor,
-        sys.stdout.flush()
-        print 'done (exit code: %d)' % os.system(editor)
-
-        # read the message back
-        f = file(fname)
-        msg = f.read()
-        f.close()
+    if options.edit_cover:
+        msg = edit_message(msg)
 
     return msg.strip('\n')
 
@@ -362,6 +394,11 @@ def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id, options):
     else:
         version_str = ''
 
+    if options.prefix:
+        prefix_str = options.prefix + ' '
+    else:
+        prefix_str = ''
+        
     total_nr_str = str(total_nr)
     patch_nr_str = str(patch_nr).zfill(len(total_nr_str))
     if total_nr > 1:
@@ -380,6 +417,7 @@ def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id, options):
                                               rev2 = git_id('%s//top' % patch)),
                  'date':         email.Utils.formatdate(localtime = True),
                  'version':      version_str,
+                 'prefix':       prefix_str,
                  'patchnr':      patch_nr_str,
                  'totalnr':      total_nr_str,
                  'number':       number_str,
@@ -401,6 +439,9 @@ def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id, options):
         raise CmdException, 'Only "%(name)s" variables are ' \
               'supported in the patch template'
 
+    if options.edit_patches:
+        msg = edit_message(msg)
+
     return msg.strip('\n')
 
 def func(parser, options, args):
@@ -451,7 +492,7 @@ def func(parser, options, args):
         sleep = config.getint('stgit', 'smtpdelay')
 
     # send the cover message (if any)
-    if options.cover or options.edit:
+    if options.cover or options.edit_cover:
         # find the template file
         if options.cover:
             tmpl = file(options.cover).read()