static int mailbox_update_email_alarms(struct mailbox *mailbox,
                                       const struct index_record *old,
                                       const struct index_record *new)
{
    int r = 0;

    if (!(mailbox->i.options & OPT_IMAP_HAS_ALARMS))
        return 0;

    /* never have alarms on deleted mailboxes */
    if (mboxname_isdeletedmailbox(mailbox_name(mailbox), NULL))
        return 0;

    /* remove associated alarms if deleted */
    if (!new || new->internal_flags & FLAG_INTERNAL_EXPUNGED) {
        r = caldav_alarm_delete_record(mailbox_name(mailbox), new->uid);
    }

    /* remove associated alarms if canceled or final */
    else if (mbtype_isa(mailbox_mbtype(mailbox)) == MBTYPE_JMAPSUBMIT &&
             (new->system_flags & (FLAG_FLAGGED | FLAG_ANSWERED))) {
        r = caldav_alarm_delete_record(mailbox_name(mailbox), new->uid);
    }

    /* touch or create otherwise */
    else if (old && (old->uid == new->uid)) {
        r = caldav_alarm_touch_record(mailbox, new, 0);
    }
    else {
        message_t *m = message_new_from_record(mailbox, new);
        struct buf buf = BUF_INITIALIZER;
        json_t *submission = NULL;

        /* Parse the submission object from the header field */
        r = message_get_field(m, JMAP_SUBMISSION_HDR, MESSAGE_RAW, &buf);
        if (!r) {
            json_error_t jerr;
            submission = json_loadb(buf_base(&buf), buf_len(&buf),
                                    JSON_DISABLE_EOF_CHECK, &jerr);

            r = caldav_alarm_add_record(mailbox, new, submission);
            if (submission) json_decref(submission);
        }

        if (m) message_unref(&m);
        buf_free(&buf);
    }

    return r;
}
static int mailbox_update_email_alarms(struct mailbox *mailbox,
                                       const struct index_record *old,
                                       const struct index_record *new)
{
    int r = 0;

    if (!(mailbox->i.options & OPT_IMAP_HAS_ALARMS))
        return 0;

     
    if (mboxname_isdeletedmailbox(mailbox_name(mailbox), NULL))
        return 0;

     
    if (!new || new->internal_flags & FLAG_INTERNAL_EXPUNGED) {
        r = caldav_alarm_delete_record(mailbox_name(mailbox), new->uid);
    }

     
    else if (mbtype_isa(mailbox_mbtype(mailbox)) == MBTYPE_JMAPSUBMIT &&
             (new->system_flags & (FLAG_FLAGGED | FLAG_ANSWERED))) {
        r = caldav_alarm_delete_record(mailbox_name(mailbox), new->uid);
    }

     
    else if (old && (old->uid == new->uid)) {
        r = caldav_alarm_touch_record(mailbox, new, 0);
    }
    else {
        message_t *m = message_new_from_record(mailbox, new);
        struct buf buf = BUF_INITIALIZER;
        json_t *submission = NULL;

         
        r = message_get_field(m, JMAP_SUBMISSION_HDR, MESSAGE_RAW, &buf);
        if (!r) {
            json_error_t jerr;
            submission = json_loadb(buf_base(&buf), buf_len(&buf),
                                    JSON_DISABLE_EOF_CHECK, &jerr);

            r = caldav_alarm_add_record(mailbox, new, submission);
            if (submission) json_decref(submission);
        }

        if (m) message_unref(&m);
        buf_free(&buf);
    }

    return r;
}