+def __write_mbox(from_addr, msg):
+ """Write an mbox like file to the standard output
+ """
+ r = re.compile('^From ', re.M)
+ msg = r.sub('>\g<0>', msg)
+
+ print 'From %s %s' % (from_addr, datetime.datetime.today().ctime())
+ print msg
+ print
+
+def __build_address_headers(tmpl, options, extra_cc = []):
+ """Build the address headers and check existing headers in the
+ template.
+ """
+ def csv(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)
+ if r.search(tmpl):
+ tmpl = r.sub('\g<0>, ' + addr, tmpl, 1)
+ h = ''
+ else:
+ h = header + ': ' + addr
+
+ return tmpl, h
+
+ headers = ''
+ to_addr = ''
+ 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:
+ cc_addr = csv(options.cc + extra_cc)
+ elif extra_cc:
+ cc_addr = csv(extra_cc)
+ if options.bcc:
+ bcc_addr = csv(options.bcc + [autobcc])
+ elif autobcc:
+ bcc_addr = autobcc
+
+ # replace existing headers
+ if to_addr:
+ tmpl, h = replace_header('To', to_addr, tmpl)
+ if h:
+ headers += h + '\n'
+ if cc_addr:
+ tmpl, h = replace_header('Cc', cc_addr, tmpl)
+ if h:
+ headers += h + '\n'
+ if bcc_addr:
+ tmpl, h = replace_header('Bcc', bcc_addr, tmpl)
+ if h:
+ headers += h + '\n'
+
+ return tmpl, headers
+
+def __get_signers_list(msg):
+ """Return the address list generated from signed-off-by and
+ acked-by lines in the message.
+ """
+ addr_list = []
+
+ r = re.compile('^(signed-off-by|acked-by):\s+(.+)$', re.I)
+ for line in msg.split('\n'):
+ m = r.match(line)
+ if m:
+ addr_list.append(m.expand('\g<2>'))
+
+ return addr_list
+
+def __build_extra_headers():
+ """Build extra headers like content-type etc.
+ """
+ headers = 'Content-Type: text/plain; charset=utf-8; format=fixed\n'
+ headers += 'Content-Transfer-Encoding: 8bit\n'
+ headers += 'User-Agent: StGIT/%s\n' % version.version
+
+ 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