[PATCH 4 of 7] Mail: add IMAP ID command response settings to customize server response

Filipe da Silva fdasilvayy at gmail.com
Tue Jan 14 11:54:21 UTC 2014


# HG changeset patch
# User Filipe da Silva <fdasilvayy at gmail.com>
# Date 1389700251 -3600
#      Tue Jan 14 12:50:51 2014 +0100
# Node ID 2d3ff21b5373a83dec32759062c4e04a14567c6e
# Parent  56df02d0dad9e7746fed311c88787fcb3ea902d7
Mail: add IMAP ID command response settings to customize server response.

diff -r 56df02d0dad9 -r 2d3ff21b5373 src/mail/ngx_mail_imap_handler.c
--- a/src/mail/ngx_mail_imap_handler.c	Tue Jan 14 12:50:41 2014 +0100
+++ b/src/mail/ngx_mail_imap_handler.c	Tue Jan 14 12:50:51 2014 +0100
@@ -304,9 +304,10 @@
 static ngx_int_t
 ngx_mail_imap_id(ngx_mail_session_t *s, ngx_connection_t *c)
 {
-    ngx_str_t   *arg;
+    ngx_str_t   *arg, server_id;
     size_t       size, i;
     u_char      *p, *data;
+    ngx_mail_imap_srv_conf_t  *iscf;
 
     arg = s->args.elts;
     if (s->args.nelts < 1 || arg[0].len == 0) {
@@ -346,11 +347,24 @@
     }
 
     ngx_log_debug2(NGX_LOG_DEBUG_MAIL, c->log, 0,
-                   "imap client ID:\"%V%V\"",
-                    &s->tag, &s->imap_client_id);
+                   "imap client ID:\"%V%V\"", &s->tag, &s->imap_client_id);
 
     // Prepare server response to ID command
-    s->text = ngx_mail_imap_server_id_nil;
+    iscf = ngx_mail_get_module_srv_conf(s, ngx_mail_imap_module);
+    if (iscf->server_id.len > 0) {
+        server_id = iscf->server_id;
+
+    } else {
+        s->text = ngx_mail_imap_server_id_nil;
+        server_id.len = 0;
+    }
+
+    if (server_id.len >= 2) {
+        s->text = server_id;
+        server_id.len -= 2; // remove CRLF from log
+        ngx_log_debug(NGX_LOG_DEBUG_MAIL, c->log, 0,
+                      "imap server ID:\"%V\"", &server_id);
+    }
 
     return NGX_OK;
 }
diff -r 56df02d0dad9 -r 2d3ff21b5373 src/mail/ngx_mail_imap_module.c
--- a/src/mail/ngx_mail_imap_module.c	Tue Jan 14 12:50:41 2014 +0100
+++ b/src/mail/ngx_mail_imap_module.c	Tue Jan 14 12:50:51 2014 +0100
@@ -80,6 +80,13 @@
       offsetof(ngx_mail_imap_srv_conf_t, auth_methods),
       &ngx_mail_imap_auth_methods },
 
+    { ngx_string("imap_server_id"),
+      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_2MORE,
+      ngx_mail_capabilities,
+      NGX_MAIL_SRV_CONF_OFFSET,
+      offsetof(ngx_mail_imap_srv_conf_t, server_ids),
+      NULL },
+
       ngx_null_command
 };
 
@@ -129,6 +136,11 @@
         return NULL;
     }
 
+    if (ngx_array_init(&iscf->server_ids, cf->pool, 8, sizeof(ngx_str_t))
+        != NGX_OK)
+    {
+        return NULL;
+    }
     return iscf;
 }
 
@@ -154,6 +166,11 @@
                                |NGX_MAIL_AUTH_PLAIN_ENABLED));
 
 
+    if (conf->server_ids.nelts == 0) {
+        conf->server_ids = prev->server_ids;
+    }
+
+
     if (conf->capabilities.nelts == 0) {
         conf->capabilities = prev->capabilities;
     }
@@ -168,6 +185,13 @@
 
             *c = *d;
         }
+    } else if (conf->server_ids.nelts > 0) {
+        c = ngx_array_push(&conf->capabilities);
+        if (c == NULL) {
+            return NGX_CONF_ERROR;
+        }
+        // Push ID to initial capabilities
+        *c = ngx_mail_imap_default_capabilities[0];
     }
 
     size = sizeof("* CAPABILITY" CRLF) - 1;
@@ -250,5 +274,44 @@
                    sizeof(" STARTTLS LOGINDISABLED") - 1);
     *p++ = CR; *p = LF;
 
+
+    if (conf->server_ids.nelts % 2 != 0) {
+        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+           "odd item count(%ui) of key/value pairs declared",
+            conf->server_ids.nelts );
+        return NGX_CONF_ERROR;
+    }
+
+    if (conf->server_ids.nelts > 0) {
+        size = sizeof("* ID (" CRLF) - 1;
+
+        c = conf->server_ids.elts;
+        for (i = 0; i < conf->server_ids.nelts; i++) {
+            size += 1 + c[i].len + 2;
+        }
+
+        p = ngx_pnalloc(cf->pool, size);
+        if (p == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        conf->server_id.len = size;
+        conf->server_id.data = p;
+
+        p = ngx_cpymem(p, "* ID (", sizeof("* ID (") - 1);
+
+        *p++ = '"';
+        p = ngx_cpymem(p, c[0].data, c[0].len);
+        *p++ = '"';
+        for (i = 1; i < conf->server_ids.nelts; i++) {
+            *p++ = ' ';
+            *p++ = '"';
+            p = ngx_cpymem(p, c[i].data, c[i].len);
+            *p++ = '"';
+        }
+        *p++ = ')';
+        *p++ = CR; *p = LF;
+    }
+
     return NGX_CONF_OK;
 }
diff -r 56df02d0dad9 -r 2d3ff21b5373 src/mail/ngx_mail_imap_module.h
--- a/src/mail/ngx_mail_imap_module.h	Tue Jan 14 12:50:41 2014 +0100
+++ b/src/mail/ngx_mail_imap_module.h	Tue Jan 14 12:50:51 2014 +0100
@@ -20,10 +20,12 @@
     ngx_str_t    capability;
     ngx_str_t    starttls_capability;
     ngx_str_t    starttls_only_capability;
+    ngx_str_t    server_id;
 
     ngx_uint_t   auth_methods;
 
     ngx_array_t  capabilities;
+    ngx_array_t  server_ids;
 } ngx_mail_imap_srv_conf_t;
 
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 003-ImapID_ServerReponseSettings.diff
Type: text/x-patch
Size: 4851 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20140114/c8f8027a/attachment-0001.bin>


More information about the nginx-devel mailing list