blob: 53ca2dd1b19efe18d18b03d715bb5303cd499f2a [file]
# SPDX-License-Identifier: GPL-2.0
import tempfile
import _hkml
import _hkml_list_cache
import hkml_mail_note
import hkml_send
import hkml_write
def format_reply_subject(mail):
subject = mail.subject
if subject and subject.split()[0].lower() != 're:':
subject = 'Re: %s' % subject
return subject
def notes_added(draft_body_lines, mail):
notes = hkml_mail_note.get_notes_for(mail.get_msgid())
if notes is None:
return draft_body_lines
line_notes = notes.line_notes
notes_added_lines = []
for line_nr, line in enumerate(draft_body_lines):
notes_added_lines.append(line)
# we added two new lines at the beginning of the body.
orig_line_nr = line_nr - 2
if orig_line_nr in line_notes:
notes_added_lines.append('')
for note in line_notes[orig_line_nr]:
notes_added_lines.append('HKML NOTE: %s' % note.text)
notes_added_lines.append('')
return notes_added_lines
def format_reply(mail, attach_file, body_lines=None, subject=None):
if subject is None:
subject = format_reply_subject(mail)
in_reply_to = mail.get_msgid()
cc = [x for x in [mail.get_field('to'), mail.get_field('cc')] if x]
to = [mail.get_from()]
if body_lines is None:
body_lines = []
date = mail.get_field('date')
if date and to[0]:
body_lines.append('On %s %s wrote:' % (date, to[0]))
body_lines.append('')
body = mail.get_body()
for line in body.split('\n'):
body_lines.append('> %s' % line)
body_lines = notes_added(body_lines, mail)
body = '\n'.join(body_lines)
return hkml_write.format_mbox(subject, in_reply_to, to, cc, body,
from_=None, draft_mail=None,
attach_files=attach_file)
def reply(mail, attach_files, format_only, cursor_row=0):
reply_mbox_str = format_reply(mail, attach_files)
if format_only:
print(reply_mbox_str)
return
if cursor_row != 0:
nr_header_rows = 0
for line in reply_mbox_str.split('\n'):
nr_header_rows += 1
if line == '':
break
# add three for the context line, the blank line after it, and because
# vim counts rows from 1.
cursor_row += nr_header_rows + 3
fd, reply_tmp_path = tempfile.mkstemp(prefix='hkml_reply_')
with open(reply_tmp_path, 'w') as f:
f.write(reply_mbox_str)
err = hkml_write.open_editor(
reply_tmp_path, cursor_row=cursor_row, is_reply=True)
if err is not None:
print(err)
exit(1)
hkml_send.send_mail(reply_tmp_path, get_confirm=True, erase_mbox=True,
orig_draft_subject=None)
def main(args):
if args.mail.isdigit():
mail = _hkml_list_cache.get_mail(int(args.mail))
elif args.mail == 'clipboard':
mails, err = _hkml.read_mails_from_clipboard()
if err != None:
print('reading mails in clipboard failed: %s' % err)
exit(1)
if len(mails) != 1:
print('multiple mails in clipboard')
exit(1)
mail = mails[0]
else:
print('unsupported <mail> (%s)' % args.mail)
if mail is None:
print('mail is not cached')
exit(1)
reply(mail, args.attach, args.format_only)
def set_argparser(parser):
parser.description = 'reply to a mail'
parser.add_argument(
'mail', metavar='<mail>',
help=' '.join(
['The mail to reply to.',
'Could be index on the list, or \'clipboard\'']))
hkml_write.add_common_arguments(parser)