]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
var_expand(): Added support for long %{variable} names.
authorTimo Sirainen <tss@iki.fi>
Sat, 13 Dec 2008 16:04:55 +0000 (18:04 +0200)
committerTimo Sirainen <tss@iki.fi>
Sat, 13 Dec 2008 16:04:55 +0000 (18:04 +0200)
--HG--
branch : HEAD

12 files changed:
src/auth/auth-request.c
src/deliver/deliver.c
src/deliver/mail-send.c
src/imap/client.c
src/lib-storage/index/shared/shared-storage.c
src/lib/var-expand.c
src/lib/var-expand.h
src/login-common/client-common.c
src/master/mail-process.c
src/plugins/acl/acl-shared-storage.c
src/pop3/client.c
src/pop3/commands.c

index 1b1325c7cd2e79211ba57642a194c6fbd5dacab3..b32a0db02d113f17a2bd7a17a74b96310dd00fd8 100644 (file)
@@ -1340,22 +1340,22 @@ auth_request_get_var_expand_table(const struct auth_request *auth_request,
                                  auth_request_escape_func_t *escape_func)
 {
        static struct var_expand_table static_tab[] = {
-               { 'u', NULL },
-               { 'n', NULL },
-               { 'd', NULL },
-               { 's', NULL },
-               { 'h', NULL },
-               { 'l', NULL },
-               { 'r', NULL },
-               { 'p', NULL },
-               { 'w', NULL },
-               { '!', NULL },
-               { 'm', NULL },
-               { 'c', NULL },
-               { 'a', NULL },
-               { 'b', NULL },
-               { 'k', NULL },
-               { '\0', NULL }
+               { 'u', NULL, "user" },
+               { 'n', NULL, "username" },
+               { 'd', NULL, "domain" },
+               { 's', NULL, "service" },
+               { 'h', NULL, "home" },
+               { 'l', NULL, "lip" },
+               { 'r', NULL, "rip" },
+               { 'p', NULL, "pid" },
+               { 'w', NULL, "password" },
+               { '!', NULL, NULL },
+               { 'm', NULL, "mech" },
+               { 'c', NULL, "secured" },
+               { 'a', NULL, "lport" },
+               { 'b', NULL, "rport" },
+               { 'k', NULL, "cert" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
 
index dcaab488a853452b27147cd40f4a782ac6ff48f5..60d0cbccab813d094b05ed3ab12c9b24aead4629 100644 (file)
@@ -101,11 +101,11 @@ static const struct var_expand_table *
 get_log_var_expand_table(struct mail *mail, const char *message)
 {
        static struct var_expand_table static_tab[] = {
-               { '$', NULL },
-               { 'm', NULL },
-               { 's', NULL },
-               { 'f', NULL },
-               { '\0', NULL }
+               { '$', NULL, NULL },
+               { 'm', NULL, "msgid" },
+               { 's', NULL, "subject" },
+               { 'f', NULL, "from" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
        unsigned int i;
@@ -504,16 +504,16 @@ static const struct var_expand_table *
 get_var_expand_table(const char *user, const char *home)
 {
        static struct var_expand_table static_tab[] = {
-               { 'u', NULL },
-               { 'n', NULL },
-               { 'd', NULL },
-               { 's', NULL },
-               { 'h', NULL },
-               { 'l', NULL },
-               { 'r', NULL },
-               { 'p', NULL },
-               { 'i', NULL },
-               { '\0', NULL }
+               { 'u', NULL, "user" },
+               { 'n', NULL, "username" },
+               { 'd', NULL, "domain" },
+               { 's', NULL, "service" },
+               { 'h', NULL, "home" },
+               { 'l', NULL, "lip" },
+               { 'r', NULL, "rip" },
+               { 'p', NULL, "pid" },
+               { 'i', NULL, "uid" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
 
index a6a34269512c3f531071d1735bb7efb725f8462b..e767c7b9783a0aeee1cbfb8f33243698fb757d40 100644 (file)
@@ -25,11 +25,11 @@ get_var_expand_table(struct mail *mail, const char *reason,
                     const char *recipient)
 {
        static struct var_expand_table static_tab[] = {
-               { 'n', NULL },
-               { 'r', NULL },
-               { 's', NULL },
-               { 't', NULL },
-               { '\0', NULL }
+               { 'n', NULL, "crlf" },
+               { 'r', NULL, "reason" },
+               { 's', NULL, "subject" },
+               { 't', NULL, "to" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
        const char *subject;
index 073c834cbf0ae323888b156bcca9ff6a14422ec3..e8b67231e593c74e3a611cd9b6515faab4e7345c 100644 (file)
@@ -100,9 +100,9 @@ void client_command_cancel(struct client_command_context **_cmd)
 static const char *client_stats(struct client *client)
 {
        static struct var_expand_table static_tab[] = {
-               { 'i', NULL },
-               { 'o', NULL },
-               { '\0', NULL }
+               { 'i', NULL, "input" },
+               { 'o', NULL, "output" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
        string_t *str;
index 78392b4bdcc06787600c15064fd612e12de21c45..ec115e29945ca7a13bef9cda5a784572087375fb 100644 (file)
@@ -126,11 +126,11 @@ int shared_storage_get_namespace(struct mail_storage *_storage,
        struct shared_storage *storage = (struct shared_storage *)_storage;
        struct mail_user *user = _storage->ns->user;
        static struct var_expand_table static_tab[] = {
-               { 'u', NULL },
-               { 'n', NULL },
-               { 'd', NULL },
-               { 'h', NULL },
-               { '\0', NULL }
+               { 'u', NULL, "user" },
+               { 'n', NULL, "username" },
+               { 'd', NULL, "domain" },
+               { 'h', NULL, "home" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
        struct mail_namespace *ns;
@@ -208,7 +208,7 @@ int shared_storage_get_namespace(struct mail_storage *_storage,
        }
 
        owner = mail_user_init(userdomain);
-       if (!var_has_key(storage->location, 'h'))
+       if (!var_has_key(storage->location, 'h', "home"))
                ret = 1;
        else {
                /* we'll need to look up the user's home directory */
index ed9045110d506d884bc7966b867354ecb82506ca..255dca29e0e5849e00b2e7f068d4b11efccf5fb3 100644 (file)
@@ -12,6 +12,9 @@
 #include <stdlib.h>
 #include <ctype.h>
 
+#define TABLE_LAST(t) \
+       ((t)->key == '\0' && (t)->long_key == NULL)
+
 struct var_expand_context {
        int offset;
        unsigned int width;
@@ -142,7 +145,8 @@ void var_expand(string_t *dest, const char *str,
         struct var_expand_context ctx;
        const char *(*modifier[MAX_MODIFIER_COUNT])
                (const char *, struct var_expand_context *);
-       unsigned int i, modifier_count;
+       const char *end;
+       unsigned int i, len, modifier_count;
 
        memset(&ctx, 0, sizeof(ctx));
        for (; *str != '\0'; str++) {
@@ -209,10 +213,27 @@ void var_expand(string_t *dest, const char *str,
                                break;
 
                        var = NULL;
-                       for (t = table; t->key != '\0'; t++) {
-                               if (t->key == *str) {
-                                       var = t->value != NULL ? t->value : "";
-                                       break;
+                       if (*str == '{' && (end = strchr(str, '}')) != NULL) {
+                               /* %{long_key} */
+                               len = end - (str + 1);
+                               for (t = table; !TABLE_LAST(t); t++) {
+                                       if (t->long_key != NULL &&
+                                           strncmp(t->long_key, str+1,
+                                                   len) == 0 &&
+                                           t->long_key[len] == '\0') {
+                                               var = t->value != NULL ?
+                                                       t->value : "";
+                                               str = end;
+                                               break;
+                                       }
+                               }
+                       } else {
+                               for (t = table; !TABLE_LAST(t); t++) {
+                                       if (t->key == *str) {
+                                               var = t->value != NULL ?
+                                                       t->value : "";
+                                               break;
+                                       }
                                }
                        }
 
@@ -283,13 +304,26 @@ char var_get_key(const char *str)
        return *str;
 }
 
-bool var_has_key(const char *str, char key)
+bool var_has_key(const char *str, char key, const char *long_key)
 {
+       const char *end;
+       char c;
+
        for (; *str != '\0'; str++) {
                if (*str == '%' && str[1] != '\0') {
                        str++;
-                       if (var_get_key(str) == key)
+                       c = var_get_key(str);
+                       if (c == key)
                                return TRUE;
+
+                       if (c == '{' && long_key != NULL &&
+                           (str = strchr(str, '{')) != NULL &&
+                           (end = strchr(++str, '}')) != NULL) {
+                               if (strncmp(str, long_key, end-str) == 0 &&
+                                   long_key[end-str] == '\0')
+                                       return TRUE;
+                               str = end;
+                       }
                }
        }
        return FALSE;
index 3bdb19bf58c951cb4af41f3908812a778db8abb3..1a3e89853f764760cdb3c4376d3a335d0461a05b 100644 (file)
@@ -4,6 +4,7 @@
 struct var_expand_table {
        char key;
        const char *value;
+       const char *long_key;
 };
 
 /* Expand % variables in src and append the string in dest.
@@ -14,8 +15,8 @@ void var_expand(string_t *dest, const char *str,
 /* Returns the actual key character for given string, ie. skip any modifiers
    that are before it. The string should be the data after the '%' character. */
 char var_get_key(const char *str) ATTR_PURE;
-/* Returns TRUE if key variable is used in the string. */
-bool var_has_key(const char *str, char key) ATTR_PURE;
+/* Returns TRUE if key variable is used in the string. long_key may be NULL. */
+bool var_has_key(const char *str, char key, const char *long_key) ATTR_PURE;
 
 const struct var_expand_table *
 var_expand_table_build(char key, const char *value, char key2, ...);
index efb061b59bc5f304d3f2b425d2d521a4e8b4ca8f..432cb6e7dad61f32b040e9ba201cc327ab652189 100644 (file)
@@ -37,21 +37,21 @@ static const struct var_expand_table *
 get_var_expand_table(struct client *client)
 {
        static struct var_expand_table static_tab[] = {
-               { 'u', NULL },
-               { 'n', NULL },
-               { 'd', NULL },
-               { 's', NULL },
-               { 'h', NULL },
-               { 'l', NULL },
-               { 'r', NULL },
-               { 'p', NULL },
-               { 'm', NULL },
-               { 'a', NULL },
-               { 'b', NULL },
-               { 'c', NULL },
-               { 'k', NULL },
-               { 'e', NULL },
-               { '\0', NULL }
+               { 'u', NULL, "user" },
+               { 'n', NULL, "username" },
+               { 'd', NULL, "domain" },
+               { 's', NULL, "service" },
+               { 'h', NULL, "home" },
+               { 'l', NULL, "lip" },
+               { 'r', NULL, "rip" },
+               { 'p', NULL, "pid" },
+               { 'm', NULL, "mech" },
+               { 'a', NULL, "lport" },
+               { 'b', NULL, "rport" },
+               { 'c', NULL, "secured" },
+               { 'k', NULL, "ssl_security" },
+               { 'e', NULL, "mail_pid" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
        unsigned int i;
@@ -112,9 +112,9 @@ static bool have_key(const struct var_expand_table *table, const char *str)
 static void client_syslog_real(struct client *client, const char *msg)
 {
        static struct var_expand_table static_tab[3] = {
-               { 's', NULL },
-               { '$', NULL },
-               { '\0', NULL }
+               { 's', NULL, NULL },
+               { '$', NULL, NULL },
+               { '\0', NULL, NULL }
        };
        const struct var_expand_table *var_expand_table;
        struct var_expand_table *tab;
index 6fdc982df271e627ee30ecdd0dd5ca96d6a27c50..f9a18797cae5ddbdc866f3952f1204a123b2d9f0 100644 (file)
@@ -175,16 +175,16 @@ get_var_expand_table(const char *protocol,
 {
 #define VAR_EXPAND_HOME_IDX 4
        static struct var_expand_table static_tab[] = {
-               { 'u', NULL },
-               { 'n', NULL },
-               { 'd', NULL },
-               { 's', NULL },
-               { 'h', NULL },
-               { 'l', NULL },
-               { 'r', NULL },
-               { 'p', NULL },
-               { 'i', NULL },
-               { '\0', NULL }
+               { 'u', NULL, "user" },
+               { 'n', NULL, "username" },
+               { 'd', NULL, "domain" },
+               { 's', NULL, "service" },
+               { 'h', NULL, "home" },
+               { 'l', NULL, "lip" },
+               { 'r', NULL, "rip" },
+               { 'p', NULL, "pid" },
+               { 'i', NULL, "uid" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
 
@@ -211,7 +211,7 @@ has_missing_used_home(const char *str, const struct var_expand_table *table)
        i_assert(table[VAR_EXPAND_HOME_IDX].key == 'h');
 
        return table[VAR_EXPAND_HOME_IDX].value == NULL &&
-               var_has_key(str, 'h');
+               var_has_key(str, 'h', "home");
 }
 
 static const char *
index c5b6081a5c7ee847070f01ece34d9aa6879cb76f..90530c8ead70ddb77e67496b43bca538a5b21f1c 100644 (file)
@@ -18,10 +18,10 @@ acl_shared_namespace_add(struct mail_user *user,
                         const char *userdomain)
 {
        static struct var_expand_table static_tab[] = {
-               { 'u', NULL },
-               { 'n', NULL },
-               { 'd', NULL },
-               { '\0', NULL }
+               { 'u', NULL, "user" },
+               { 'n', NULL, "username" },
+               { 'd', NULL, "domain" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
        struct mail_namespace *ns;
index 9eea8628a1ec105d8b36d5757ac410780a0e8527..503b69d659b7159e188996064f901cbc459cfcd7 100644 (file)
@@ -208,16 +208,16 @@ struct client *client_create(int fd_in, int fd_out, struct mail_user *user)
 static const char *client_stats(struct client *client)
 {
        static struct var_expand_table static_tab[] = {
-               { 'p', NULL },
-               { 't', NULL },
-               { 'b', NULL },
-               { 'r', NULL },
-               { 'd', NULL },
-               { 'm', NULL },
-               { 's', NULL },
-               { 'i', NULL },
-               { 'o', NULL },
-               { '\0', NULL }
+               { 'p', NULL, "top_bytes" },
+               { 't', NULL, "top_count" },
+               { 'b', NULL, "retr_bytes" },
+               { 'r', NULL, "retr_count" },
+               { 'd', NULL, "deleted_count" },
+               { 'm', NULL, "message_count" },
+               { 's', NULL, "message_bytes" },
+               { 'i', NULL, "input" },
+               { 'o', NULL, "output" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
        string_t *str;
index 976cc227b2f90304a2486dab19ff62e9afc195ec..44c4bad0d61ba04ccfc22676241de5af547e5d6e 100644 (file)
@@ -551,11 +551,11 @@ static void pop3_get_uid(struct cmd_uidl_context *ctx,
 static bool list_uids_iter(struct client *client, struct cmd_uidl_context *ctx)
 {
        static struct var_expand_table static_tab[] = {
-               { 'v', NULL },
-               { 'u', NULL },
-               { 'm', NULL },
-               { 'f', NULL },
-               { '\0', NULL }
+               { 'v', NULL, "uidvalidity" },
+               { 'u', NULL, "uid" },
+               { 'm', NULL, "md5" },
+               { 'f', NULL, "filename" },
+               { '\0', NULL, NULL }
        };
        struct var_expand_table *tab;
        string_t *str;