+static InternetAddressList *get_to(unused(notmuch_message_t *message),
+ GMimeMessage *mime_message)
+{
+ return g_mime_message_get_recipients (mime_message,
+ GMIME_RECIPIENT_TYPE_TO);
+}
+
+static InternetAddressList *get_cc(unused(notmuch_message_t *message),
+ GMimeMessage *mime_message)
+{
+ return g_mime_message_get_recipients (mime_message,
+ GMIME_RECIPIENT_TYPE_CC);
+}
+
+static InternetAddressList *get_bcc(unused(notmuch_message_t *message),
+ GMimeMessage *mime_message)
+{
+ return g_mime_message_get_recipients (mime_message,
+ GMIME_RECIPIENT_TYPE_BCC);
+}
+
+/* Augment the recipients of 'reply' from the "Reply-to:", "From:",
+ * "To:", "Cc:", and "Bcc:" headers of 'message'.
+ *
+ * If 'reply_all' is true, use sender and all recipients, otherwise
+ * scan the headers for the first that contains something other than
+ * the user's addresses and add the recipients from this header
+ * (typically this would be reply-to-sender, but also handles reply to
+ * user's own message in a sensible way).
+ *
+ * If any of the user's addresses were found in these headers, the
+ * first of these returned, otherwise NULL is returned.
+ */
+static const char *
+add_recipients_from_message (GMimeMessage *reply,
+ notmuch_config_t *config,
+ notmuch_message_t *message,
+ GMimeMessage *mime_message,
+ notmuch_bool_t reply_all)
+{
+ struct {
+ InternetAddressList * (*get_header)(notmuch_message_t *message,
+ GMimeMessage *mime_message);
+ GMimeRecipientType recipient_type;
+ } reply_to_map[] = {
+ { get_sender, GMIME_RECIPIENT_TYPE_TO },
+ { get_to, GMIME_RECIPIENT_TYPE_TO },
+ { get_cc, GMIME_RECIPIENT_TYPE_CC },
+ { get_bcc, GMIME_RECIPIENT_TYPE_BCC },
+ };
+ const char *from_addr = NULL;
+ unsigned int i;
+ unsigned int n = 0;
+
+ for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
+ InternetAddressList *recipients;
+
+ recipients = reply_to_map[i].get_header (message, mime_message);
+
+ n += scan_address_list (recipients, config, reply,
+ reply_to_map[i].recipient_type, &from_addr);
+
+ if (!reply_all && n) {
+ /* Stop adding new recipients in reply-to-sender mode if
+ * we have added some recipient(s) above.
+ *
+ * This also handles the case of user replying to his own
+ * message, where reply-to/from is not a recipient. In
+ * this case there may be more than one recipient even if
+ * not replying to all.
+ */
+ reply = NULL;
+
+ /* From address and some recipients are enough, bail out. */
+ if (from_addr)
+ break;