'Writes a new message, with the specified uid.
See folder/Base for detail. Note that savemessage() does not
check against dryrun settings, so you need to ensure that
savemessage is never called in a dryrun mode.'

def savemessage(self, uid, content, flags, rtime):

    self.ui.savemessage('maildir', uid, flags, self)
    if (uid < 0):
        return uid
    if (uid in self.messagelist):
        self.savemessageflags(uid, flags)
        return uid
    tmpdir = os.path.join(self.getfullname(), 'tmp')
    message_timestamp = None
    if (self._filename_use_mail_timestamp is not False):
        try:
            message_timestamp = emailutil.get_message_date(content, 'Date')
            if (message_timestamp is None):
                message_timestamp = emailutil.get_message_date(content, 'Delivery-date')
        except Exception as e:
            from email.Parser import Parser
            from offlineimap.ui import getglobalui
            datestr = Parser().parsestr(content, True).get('Date')
            ui = getglobalui()
            ui.warn(('UID    %d    has    invalid    date    %s:    %s\nNot    using    message    timestamp    as    file    prefix' % (uid, datestr, e)))
    messagename = self.new_message_filename(uid, flags, date=message_timestamp)
    tmpname = self.save_to_tmp_file(messagename, content)
    if (self._utime_from_header is True):
        try:
            date = emailutil.get_message_date(content, 'Date')
            if (date is not None):
                os.utime(os.path.join(self.getfullname(), tmpname), (date, date))
        except Exception as e:
            from email.Parser import Parser
            from offlineimap.ui import getglobalui
            datestr = Parser().parsestr(content, True).get('Date')
            ui = getglobalui()
            ui.warn(('UID    %d    has    invalid    date    %s:    %s\nNot    changing    file    modification    time' % (uid, datestr, e)))
    self.messagelist[uid] = self.msglist_item_initializer(uid)
    self.messagelist[uid]['flags'] = flags
    self.messagelist[uid]['filename'] = tmpname
    self.savemessageflags(uid, flags)
    self.ui.debug('maildir', ('savemessage:    returning    uid    %d' % uid))
    return uid
