]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Code cleanup: Moved some functions to doveadm-util.[ch]
authorTimo Sirainen <tss@iki.fi>
Fri, 23 Jul 2010 14:55:27 +0000 (15:55 +0100)
committerTimo Sirainen <tss@iki.fi>
Fri, 23 Jul 2010 14:55:27 +0000 (15:55 +0100)
src/doveadm/Makefile.am
src/doveadm/doveadm-mail.h
src/doveadm/doveadm-util.c [new file with mode: 0644]
src/doveadm/doveadm-util.h [new file with mode: 0644]
src/doveadm/doveadm.c
src/doveadm/doveadm.h

index 1f3bef9171b0acebb8503209dc378144dc0e5c01..bcce0fae212adaabcd707081f30810b67e9c45e2 100644 (file)
@@ -74,6 +74,7 @@ doveadm_SOURCES = \
        doveadm-print-table.c \
        doveadm-pw.c \
        doveadm-settings.c \
+       doveadm-util.c \
        doveadm-who.c
 
 noinst_HEADERS = \
@@ -85,4 +86,5 @@ noinst_HEADERS = \
        doveadm-print.h \
        doveadm-print-private.h \
        doveadm-settings.h \
+       doveadm-util.h \
        doveadm-who.h
index 2093fad0b7eee54678474144ac822e69c83e2bbb..9ac2f8083255b460c5b7be64cb560eb7b560653e 100644 (file)
@@ -2,7 +2,7 @@
 #define DOVEADM_MAIL_H
 
 #include <stdio.h>
-#include "doveadm.h"
+#include "doveadm-util.h"
 #include "module-context.h"
 
 struct mailbox;
diff --git a/src/doveadm/doveadm-util.c b/src/doveadm/doveadm-util.c
new file mode 100644 (file)
index 0000000..60f3baf
--- /dev/null
@@ -0,0 +1,147 @@
+/* Copyright (c) 2009-2010 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "network.h"
+#include "master-service.h"
+#include "module-dir.h"
+#include "doveadm-settings.h"
+#include "doveadm-mail.h"
+#include "doveadm-util.h"
+
+#include <time.h>
+#include <dirent.h>
+#include <sys/stat.h>
+
+bool doveadm_verbose = FALSE, doveadm_debug = FALSE;
+static struct module *modules = NULL;
+
+void doveadm_load_modules(void)
+{
+       struct module_dir_load_settings mod_set;
+
+       /* some doveadm plugins have dependencies to mail plugins. we can load
+          only those whose dependencies have been loaded earlier, the rest are
+          ignored. */
+       memset(&mod_set, 0, sizeof(mod_set));
+       mod_set.version = master_service_get_version_string(master_service);
+       mod_set.require_init_funcs = TRUE;
+       mod_set.debug = doveadm_debug;
+       mod_set.ignore_dlopen_errors = !doveadm_debug;
+
+       modules = module_dir_load_missing(modules, DOVEADM_MODULEDIR,
+                                         NULL, &mod_set);
+       module_dir_init(modules);
+}
+
+void doveadm_unload_modules(void)
+{
+       module_dir_unload(&modules);
+}
+
+bool doveadm_has_unloaded_plugin(const char *name)
+{
+       struct module *module;
+       DIR *dir;
+       struct dirent *d;
+       const char *plugin_name;
+       unsigned int name_len = strlen(name);
+       bool found = FALSE;
+
+       /* first check that it's not actually loaded */
+       for (module = modules; module != NULL; module = module->next) {
+               if (strcmp(module_get_plugin_name(module), name) == 0)
+                       return FALSE;
+       }
+
+       dir = opendir(DOVEADM_MODULEDIR);
+       if (dir == NULL)
+               return FALSE;
+
+       while ((d = readdir(dir)) != NULL) {
+               plugin_name = module_file_get_name(d->d_name);
+               if (strncmp(plugin_name, "doveadm_", 8) == 0)
+                       plugin_name += 8;
+
+               if (strncmp(plugin_name, name, name_len) == 0 &&
+                   (plugin_name[name_len] == '\0' ||
+                    strcmp(plugin_name + name_len, "_plugin") == 0)) {
+                       found = TRUE;
+                       break;
+               }
+       }
+       (void)closedir(dir);
+       return found;
+}
+
+const char *unixdate2str(time_t timestamp)
+{
+       static char buf[64];
+       struct tm *tm;
+
+       tm = localtime(&timestamp);
+       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
+       return buf;
+}
+
+const char *doveadm_plugin_getenv(const char *name)
+{
+       const char *const *envs;
+       unsigned int i, count;
+
+       if (!array_is_created(&doveadm_settings->plugin_envs))
+               return NULL;
+
+       envs = array_get(&doveadm_settings->plugin_envs, &count);
+       for (i = 0; i < count; i += 2) {
+               if (strcmp(envs[i], name) == 0)
+                       return envs[i+1];
+       }
+       return NULL;
+}
+
+static bool
+parse_hostport(const char *str, const char **host_r, unsigned int *port_r)
+{
+       const char *p;
+
+       /* host:port */
+       p = strrchr(str, ':');
+       if (p == NULL || str_to_uint(p+1, port_r) < 0)
+               return FALSE;
+       *host_r = t_strdup_until(str, p);
+
+       /* there is any '/' character (unlikely to be found from host names),
+          assume ':' is part of a file path */
+       if (strchr(str, '/') != NULL)
+               return FALSE;
+       return TRUE;
+}
+
+int doveadm_connect(const char *path)
+{
+       struct stat st;
+       const char *host;
+       struct ip_addr *ips;
+       unsigned int port, ips_count;
+       int fd, ret;
+
+       if (parse_hostport(path, &host, &port) && stat(path, &st) < 0) {
+               /* it's a host:port, connect via TCP */
+               ret = net_gethostbyname(host, &ips, &ips_count);
+               if (ret != 0) {
+                       i_fatal("Lookup of host %s failed: %s",
+                               host, net_gethosterror(ret));
+               }
+               fd = net_connect_ip_blocking(&ips[0], port, NULL);
+               if (fd == -1) {
+                       i_fatal("connect(%s:%u) failed: %m",
+                               net_ip2addr(&ips[0]), port);
+               }
+       } else {
+               fd = net_connect_unix(path);
+               if (fd == -1)
+                       i_fatal("net_connect_unix(%s) failed: %m", path);
+       }
+       return fd;
+}
diff --git a/src/doveadm/doveadm-util.h b/src/doveadm/doveadm-util.h
new file mode 100644 (file)
index 0000000..f66c3ff
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef DOVEADM_UTIL_H
+#define DOVEADM_UTIL_H
+
+#define DOVEADM_SERVER_PROTOCOL_VERSION_MAJOR 1
+
+extern bool doveadm_verbose, doveadm_debug;
+
+const char *unixdate2str(time_t timestamp);
+const char *doveadm_plugin_getenv(const char *name);
+int doveadm_connect(const char *path);
+
+void doveadm_load_modules(void);
+void doveadm_unload_modules(void);
+bool doveadm_has_unloaded_plugin(const char *name);
+
+#endif
index b1245c33dd66e1b98be566dd0b223a4bbfea8c23..584840b7de02a381a095451438eafd86040d08b7 100644 (file)
@@ -5,25 +5,16 @@
 #include "str.h"
 #include "env-util.h"
 #include "execv-const.h"
-#include "network.h"
-#include "module-dir.h"
 #include "master-service.h"
 #include "master-service-settings.h"
-#include "doveadm-print.h"
+#include "doveadm-print-private.h"
 #include "doveadm-mail.h"
 #include "doveadm-settings.h"
 #include "doveadm.h"
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <dirent.h>
-#include <time.h>
-#include <sys/stat.h>
 
-bool doveadm_verbose = FALSE, doveadm_debug = FALSE;
-
-static struct module *modules = NULL;
 static ARRAY_DEFINE(doveadm_cmds, struct doveadm_cmd);
 
 void doveadm_register_cmd(const struct doveadm_cmd *cmd)
@@ -123,126 +114,6 @@ void help(const struct doveadm_cmd *cmd)
        help_to(cmd, stdout);
 }
 
-const char *unixdate2str(time_t timestamp)
-{
-       static char buf[64];
-       struct tm *tm;
-
-       tm = localtime(&timestamp);
-       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
-       return buf;
-}
-
-const char *doveadm_plugin_getenv(const char *name)
-{
-       const char *const *envs;
-       unsigned int i, count;
-
-       if (!array_is_created(&doveadm_settings->plugin_envs))
-               return NULL;
-
-       envs = array_get(&doveadm_settings->plugin_envs, &count);
-       for (i = 0; i < count; i += 2) {
-               if (strcmp(envs[i], name) == 0)
-                       return envs[i+1];
-       }
-       return NULL;
-}
-
-static bool
-parse_hostport(const char *str, const char **host_r, unsigned int *port_r)
-{
-       const char *p;
-
-       /* host:port */
-       p = strrchr(str, ':');
-       if (p == NULL || str_to_uint(p+1, port_r) < 0)
-               return FALSE;
-       *host_r = t_strdup_until(str, p);
-
-       /* there is any '/' character (unlikely to be found from host names),
-          assume ':' is part of a file path */
-       if (strchr(str, '/') != NULL)
-               return FALSE;
-       return TRUE;
-}
-
-int doveadm_connect(const char *path)
-{
-       struct stat st;
-       const char *host;
-       struct ip_addr *ips;
-       unsigned int port, ips_count;
-       int fd, ret;
-
-       if (parse_hostport(path, &host, &port) && stat(path, &st) < 0) {
-               /* it's a host:port, connect via TCP */
-               ret = net_gethostbyname(host, &ips, &ips_count);
-               if (ret != 0) {
-                       i_fatal("Lookup of host %s failed: %s",
-                               host, net_gethosterror(ret));
-               }
-               fd = net_connect_ip_blocking(&ips[0], port, NULL);
-               if (fd == -1) {
-                       i_fatal("connect(%s:%u) failed: %m",
-                               net_ip2addr(&ips[0]), port);
-               }
-       } else {
-               fd = net_connect_unix(path);
-               if (fd == -1)
-                       i_fatal("net_connect_unix(%s) failed: %m", path);
-       }
-       return fd;
-}
-
-static bool doveadm_has_subcommands(const char *cmd_name)
-{
-       const struct doveadm_cmd *cmd;
-       unsigned int len = strlen(cmd_name);
-
-       array_foreach(&doveadm_cmds, cmd) {
-               if (strncmp(cmd->name, cmd_name, len) == 0 &&
-                   cmd->name[len] == ' ')
-                       return TRUE;
-       }
-       return doveadm_mail_has_subcommands(cmd_name);
-}
-
-static bool doveadm_has_unloaded_plugin(const char *name)
-{
-       struct module *module;
-       DIR *dir;
-       struct dirent *d;
-       const char *plugin_name;
-       unsigned int name_len = strlen(name);
-       bool found = FALSE;
-
-       /* first check that it's not actually loaded */
-       for (module = modules; module != NULL; module = module->next) {
-               if (strcmp(module_get_plugin_name(module), name) == 0)
-                       return FALSE;
-       }
-
-       dir = opendir(DOVEADM_MODULEDIR);
-       if (dir == NULL)
-               return FALSE;
-
-       while ((d = readdir(dir)) != NULL) {
-               plugin_name = module_file_get_name(d->d_name);
-               if (strncmp(plugin_name, "doveadm_", 8) == 0)
-                       plugin_name += 8;
-
-               if (strncmp(plugin_name, name, name_len) == 0 &&
-                   (plugin_name[name_len] == '\0' ||
-                    strcmp(plugin_name + name_len, "_plugin") == 0)) {
-                       found = TRUE;
-                       break;
-               }
-       }
-       (void)closedir(dir);
-       return found;
-}
-
 static void cmd_help(int argc ATTR_UNUSED, char *argv[])
 {
        const char *man_argv[3];
@@ -326,22 +197,17 @@ static bool doveadm_try_run(const char *cmd_name, int argc, char *argv[])
        return FALSE;
 }
 
-static void doveadm_load_modules(void)
+static bool doveadm_has_subcommands(const char *cmd_name)
 {
-       struct module_dir_load_settings mod_set;
-
-       /* some doveadm plugins have dependencies to mail plugins. we can load
-          only those whose dependencies have been loaded earlier, the rest are
-          ignored. */
-       memset(&mod_set, 0, sizeof(mod_set));
-       mod_set.version = master_service_get_version_string(master_service);
-       mod_set.require_init_funcs = TRUE;
-       mod_set.debug = doveadm_debug;
-       mod_set.ignore_dlopen_errors = !doveadm_debug;
-
-       modules = module_dir_load_missing(modules, DOVEADM_MODULEDIR,
-                                         NULL, &mod_set);
-       module_dir_init(modules);
+       const struct doveadm_cmd *cmd;
+       unsigned int len = strlen(cmd_name);
+
+       array_foreach(&doveadm_cmds, cmd) {
+               if (strncmp(cmd->name, cmd_name, len) == 0 &&
+                   cmd->name[len] == ' ')
+                       return TRUE;
+       }
+       return doveadm_mail_has_subcommands(cmd_name);
 }
 
 static void doveadm_read_settings(void)
@@ -472,7 +338,7 @@ int main(int argc, char *argv[])
 
        if (!quick_init) {
                doveadm_mail_deinit();
-               module_dir_unload(&modules);
+               doveadm_unload_modules();
                doveadm_print_deinit();
        }
        array_free(&doveadm_cmds);
index d097b005ce691f6e1dceb132258bfab37ca331d3..a2fa22a1b8d104aa74e9da11b2a5974e2dd014d1 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef DOVEADM_H
 #define DOVEADM_H
 
+#include "doveadm-util.h"
 #include "doveadm-settings.h"
 
 #define USAGE_CMDNAME_FMT "  %-12s"
@@ -24,16 +25,10 @@ extern struct doveadm_cmd doveadm_cmd_penalty;
 extern struct doveadm_cmd doveadm_cmd_kick;
 extern struct doveadm_cmd doveadm_cmd_mailbox_mutf7;
 
-extern bool doveadm_verbose, doveadm_debug;
-
 void doveadm_register_cmd(const struct doveadm_cmd *cmd);
 
 void usage(void) ATTR_NORETURN;
 void help(const struct doveadm_cmd *cmd) ATTR_NORETURN;
-
-const char *unixdate2str(time_t timestamp);
-const char *doveadm_plugin_getenv(const char *name);
-int doveadm_connect(const char *path);
 void doveadm_master_send_signal(int signo);
 
 void doveadm_register_director_commands(void);