+const static_str_t *query_field_for_id(const query_t *query, postlicyd_token id)
+{
+ switch (id) {
+#define CASE(Up, Low) \
+ case PTK_ ## Up: return &query->Low;
+ CASE(HELO_NAME, helo_name)
+ CASE(QUEUE_ID, queue_id)
+ CASE(SENDER, sender)
+ CASE(SENDER_DOMAIN, sender_domain)
+ CASE(RECIPIENT, recipient)
+ CASE(RECIPIENT_DOMAIN, recipient_domain)
+ CASE(RECIPIENT_COUNT, recipient_count)
+ CASE(CLIENT_ADDRESS, client_address)
+ CASE(CLIENT_NAME, client_name)
+ CASE(REVERSE_CLIENT_NAME, reverse_client_name)
+ CASE(INSTANCE, instance)
+ CASE(SASL_METHOD, sasl_method)
+ CASE(SASL_USERNAME, sasl_username)
+ CASE(SASL_SENDER, sasl_sender)
+ CASE(SIZE, size)
+ CASE(CCERT_SUBJECT, ccert_subject)
+ CASE(CCERT_ISSUER, ccert_issuer)
+ CASE(CCERT_FINGERPRINT, ccert_fingerprint)
+ CASE(ENCRYPTION_PROTOCOL, encryption_protocol)
+ CASE(ENCRYPTION_CIPHER, encryption_cipher)
+ CASE(ENCRYPTION_KEYSIZE, encryption_keysize)
+ CASE(ETRN_DOMAIN, etrn_domain)
+ CASE(STRESS, stress)
+#undef CASE
+ case PTK_PROTOCOL_NAME:
+ return query->esmtp ? &static_ESMTP : &static_SMTP;
+
+ case PTK_PROTOCOL_STATE:
+ return &smtp_state_names[query->state];
+
+ default: return NULL;
+ }
+}
+
+const static_str_t *query_field_for_name(const query_t *query, const char *name)
+{
+ postlicyd_token id = policy_tokenize(name, strlen(name));
+ if (id == PTK_UNKNOWN) {
+ warn("unknown query field %s", name);
+ return NULL;
+ }
+ return query_field_for_id(query, id);
+}
+
+ssize_t query_format(char *dest, size_t len, const char *fmt, const query_t *query)
+{
+ size_t written = 0;
+ size_t pos = 0;
+
+#define WRITE(Src, Len) \
+ do { \
+ size_t __len = (Len); \
+ if (written < len) { \
+ size_t __to_write = MIN(len - written - 1, __len); \
+ memcpy(dest + written, (Src), __to_write); \
+ written += __to_write; \
+ } \
+ pos += __len; \
+ } while (0)
+ while (*fmt != '\0') {
+ const char *next_format = strchr(fmt, '$');
+ while (next_format != NULL && next_format[1] != '{') {
+ next_format = strchr(next_format + 1, '$');
+ }
+ if (next_format == NULL) {
+ next_format = fmt + m_strlen(fmt);
+ }
+ WRITE(fmt, next_format - fmt);
+ fmt = next_format;
+ if (*fmt != '\0') {
+ fmt += 2;
+ next_format = strchr(fmt, '}');
+ if (next_format == NULL) {
+ return -1;
+ }
+
+ postlicyd_token tok = policy_tokenize(fmt, next_format - fmt);
+ if (tok == PTK_UNKNOWN) {
+ warn("unknown field name \"%.*s\"", (int)(next_format - fmt), fmt);
+ }
+ const static_str_t *field = query == NULL ? NULL
+ : query_field_for_id(query, tok);
+ if (field == NULL) {
+ WRITE("(null)", 6);
+ } else {
+ WRITE(field->str, field->len);
+ }
+ fmt = next_format + 1;
+ }
+ }
+
+ if (written > 0 && len > 0) {
+ dest[written] = '\0';
+ }
+ return pos;
+}
+
+bool query_format_buffer(buffer_t *buf, const char *fmt, const query_t *query)
+{
+ buffer_ensure(buf, m_strlen(fmt) + 64);
+
+ ssize_t size = array_free_space(*buf);
+ ssize_t format_size = query_format(array_end(*buf),
+ size, fmt, query);
+ if (format_size == -1) {
+ return false;
+ } else if (format_size > size) {
+ buffer_ensure(buf, format_size + 1);
+ query_format(array_end(*buf),
+ array_free_space(*buf),
+ fmt, query);
+ array_len(*buf) += format_size;
+ } else {
+ array_len(*buf) += format_size;
+ }
+ return true;
+}