comparison src/director/doveadm-connection.c @ 22534:30549d635210

director: Cleanup - Move all doveadm commands into an array.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Mon, 14 Aug 2017 10:01:01 +0300
parents 3c4348ffe0d6
children 2805abc195e1
comparison
equal deleted inserted replaced
22533:062f94c547ce 22534:30549d635210
54 static struct director_reset_cmd *reset_cmds = NULL; 54 static struct director_reset_cmd *reset_cmds = NULL;
55 55
56 static void doveadm_connection_set_io(struct doveadm_connection *conn); 56 static void doveadm_connection_set_io(struct doveadm_connection *conn);
57 static void doveadm_connection_deinit(struct doveadm_connection **_conn); 57 static void doveadm_connection_deinit(struct doveadm_connection **_conn);
58 58
59 static void doveadm_cmd_host_list(struct doveadm_connection *conn) 59 static int doveadm_cmd_host_list(struct doveadm_connection *conn,
60 const char *const *args ATTR_UNUSED)
60 { 61 {
61 struct mail_host *const *hostp; 62 struct mail_host *const *hostp;
62 string_t *str = t_str_new(1024); 63 string_t *str = t_str_new(1024);
63 64
64 array_foreach(mail_hosts_get(conn->dir->mail_hosts), hostp) { 65 array_foreach(mail_hosts_get(conn->dir->mail_hosts), hostp) {
70 (long)(*hostp)->last_updown_change); 71 (long)(*hostp)->last_updown_change);
71 str_append_c(str, '\n'); 72 str_append_c(str, '\n');
72 } 73 }
73 str_append_c(str, '\n'); 74 str_append_c(str, '\n');
74 o_stream_nsend(conn->output, str_data(str), str_len(str)); 75 o_stream_nsend(conn->output, str_data(str), str_len(str));
75 } 76 return 1;
76 77 }
77 static void doveadm_cmd_host_list_removed(struct doveadm_connection *conn) 78
79 static int doveadm_cmd_host_list_removed(struct doveadm_connection *conn,
80 const char *const *args ATTR_UNUSED)
78 { 81 {
79 struct mail_host_list *orig_hosts_list; 82 struct mail_host_list *orig_hosts_list;
80 struct mail_host *const *orig_hosts, *const *cur_hosts; 83 struct mail_host *const *orig_hosts, *const *cur_hosts;
81 unsigned int i, j, orig_hosts_count, cur_hosts_count; 84 unsigned int i, j, orig_hosts_count, cur_hosts_count;
82 string_t *str = t_str_new(1024); 85 string_t *str = t_str_new(1024);
110 str_printfa(str, "%s\n", net_ip2addr(&orig_hosts[i]->ip)); 113 str_printfa(str, "%s\n", net_ip2addr(&orig_hosts[i]->ip));
111 str_append_c(str, '\n'); 114 str_append_c(str, '\n');
112 o_stream_nsend(conn->output, str_data(str), str_len(str)); 115 o_stream_nsend(conn->output, str_data(str), str_len(str));
113 116
114 mail_hosts_deinit(&orig_hosts_list); 117 mail_hosts_deinit(&orig_hosts_list);
118 return 1;
115 } 119 }
116 120
117 static void doveadm_director_append_status(struct director *dir, string_t *str) 121 static void doveadm_director_append_status(struct director *dir, string_t *str)
118 { 122 {
119 if (!dir->ring_handshaked) 123 if (!dir->ring_handshaked)
165 169
166 if (conn != NULL) 170 if (conn != NULL)
167 doveadm_director_connection_append_status(conn, str); 171 doveadm_director_connection_append_status(conn, str);
168 } 172 }
169 173
170 static void doveadm_cmd_director_list(struct doveadm_connection *conn) 174 static int doveadm_cmd_director_list(struct doveadm_connection *conn,
175 const char *const *args ATTR_UNUSED)
171 { 176 {
172 struct director *dir = conn->dir; 177 struct director *dir = conn->dir;
173 struct director_host *const *hostp; 178 struct director_host *const *hostp;
174 string_t *str = t_str_new(1024); 179 string_t *str = t_str_new(1024);
175 const char *type; 180 const char *type;
206 doveadm_director_host_append_status(dir, host, str); 211 doveadm_director_host_append_status(dir, host, str);
207 str_append_c(str, '\n'); 212 str_append_c(str, '\n');
208 } 213 }
209 str_append_c(str, '\n'); 214 str_append_c(str, '\n');
210 o_stream_nsend(conn->output, str_data(str), str_len(str)); 215 o_stream_nsend(conn->output, str_data(str), str_len(str));
216 return 1;
211 } 217 }
212 218
213 static int 219 static int
214 doveadm_cmd_director_add(struct doveadm_connection *conn, 220 doveadm_cmd_director_add(struct doveadm_connection *conn,
215 const char *const *args) 221 const char *const *args)
356 director_update_host(conn->dir, conn->dir->self_host, 362 director_update_host(conn->dir, conn->dir->self_host,
357 NULL, host); 363 NULL, host);
358 } 364 }
359 o_stream_nsend(conn->output, "OK\n", 3); 365 o_stream_nsend(conn->output, "OK\n", 3);
360 return 1; 366 return 1;
367 }
368
369 static int
370 doveadm_cmd_host_up(struct doveadm_connection *conn,
371 const char *const *args)
372 {
373 return doveadm_cmd_host_updown(conn, FALSE, args);
374 }
375
376 static int
377 doveadm_cmd_host_down(struct doveadm_connection *conn,
378 const char *const *args)
379 {
380 return doveadm_cmd_host_updown(conn, TRUE, args);
361 } 381 }
362 382
363 static int 383 static int
364 doveadm_cmd_host_remove(struct doveadm_connection *conn, 384 doveadm_cmd_host_remove(struct doveadm_connection *conn,
365 const char *const *args) 385 const char *const *args)
715 args[0], args[1]); 735 args[0], args[1]);
716 o_stream_nsend(conn->output, "OK\n", 3); 736 o_stream_nsend(conn->output, "OK\n", 3);
717 return 1; 737 return 1;
718 } 738 }
719 739
740 struct {
741 const char *name;
742 int (*cmd)(struct doveadm_connection *conn, const char *const *args);
743 } doveadm_director_commands[] = {
744 { "HOST-LIST", doveadm_cmd_host_list },
745 { "HOST-LIST-REMOVED", doveadm_cmd_host_list_removed },
746 { "DIRECTOR-LIST", doveadm_cmd_director_list },
747 { "DIRECTOR-ADD", doveadm_cmd_director_add },
748 { "DIRECTOR-REMOVE", doveadm_cmd_director_remove },
749 { "HOST-SET", doveadm_cmd_host_set },
750 { "HOST-UPDATE", doveadm_cmd_host_update },
751 { "HOST-UP", doveadm_cmd_host_up },
752 { "HOST-DOWN", doveadm_cmd_host_down },
753 { "HOST-REMOVE", doveadm_cmd_host_remove },
754 { "HOST-FLUSH", doveadm_cmd_host_flush },
755 { "HOST-RESET-USERS", doveadm_cmd_host_reset_users },
756 { "USER-LOOKUP", doveadm_cmd_user_lookup },
757 { "USER-LIST", doveadm_cmd_user_list },
758 { "USER-MOVE", doveadm_cmd_user_move },
759 { "USER-KICK", doveadm_cmd_user_kick },
760 { "USER-KICK-ALT", doveadm_cmd_user_kick_alt },
761 };
762
720 static int 763 static int
721 doveadm_connection_cmd(struct doveadm_connection *conn, const char *line) 764 doveadm_connection_cmd(struct doveadm_connection *conn, const char *line)
722 { 765 {
723 const char *cmd, *const *args; 766 const char *cmd, *const *args;
724 int ret = 1;
725 767
726 args = t_strsplit_tabescaped(line); 768 args = t_strsplit_tabescaped(line);
727 if (args[0] == NULL) { 769 if (args[0] == NULL) {
728 i_error("doveadm sent empty command line"); 770 i_error("doveadm sent empty command line");
729 return -1; 771 return -1;
730 } 772 }
731 cmd = args[0]; 773 cmd = args[0];
732 args++; 774 args++;
733 775
734 if (strcmp(cmd, "HOST-LIST") == 0) 776 for (unsigned int i = 0; i < N_ELEMENTS(doveadm_director_commands); i++) {
735 doveadm_cmd_host_list(conn); 777 if (strcmp(doveadm_director_commands[i].name, cmd) == 0)
736 else if (strcmp(cmd, "HOST-LIST-REMOVED") == 0) 778 return doveadm_director_commands[i].cmd(conn, args);
737 doveadm_cmd_host_list_removed(conn); 779 }
738 else if (strcmp(cmd, "DIRECTOR-LIST") == 0) 780 i_error("doveadm sent unknown command: %s", line);
739 doveadm_cmd_director_list(conn); 781 return -1;
740 else if (strcmp(cmd, "DIRECTOR-ADD") == 0)
741 ret = doveadm_cmd_director_add(conn, args);
742 else if (strcmp(cmd, "DIRECTOR-REMOVE") == 0)
743 ret = doveadm_cmd_director_remove(conn, args);
744 else if (strcmp(cmd, "HOST-SET") == 0)
745 ret = doveadm_cmd_host_set(conn, args);
746 else if (strcmp(cmd, "HOST-UPDATE") == 0)
747 ret = doveadm_cmd_host_update(conn, args);
748 else if (strcmp(cmd, "HOST-UP") == 0)
749 ret = doveadm_cmd_host_updown(conn, FALSE, args);
750 else if (strcmp(cmd, "HOST-DOWN") == 0)
751 ret = doveadm_cmd_host_updown(conn, TRUE, args);
752 else if (strcmp(cmd, "HOST-REMOVE") == 0)
753 ret = doveadm_cmd_host_remove(conn, args);
754 else if (strcmp(cmd, "HOST-FLUSH") == 0)
755 ret = doveadm_cmd_host_flush(conn, args);
756 else if (strcmp(cmd, "HOST-RESET-USERS") == 0)
757 ret = doveadm_cmd_host_reset_users(conn, args);
758 else if (strcmp(cmd, "USER-LOOKUP") == 0)
759 ret = doveadm_cmd_user_lookup(conn, args);
760 else if (strcmp(cmd, "USER-LIST") == 0)
761 ret = doveadm_cmd_user_list(conn, args);
762 else if (strcmp(cmd, "USER-MOVE") == 0)
763 ret = doveadm_cmd_user_move(conn, args);
764 else if (strcmp(cmd, "USER-KICK") == 0)
765 ret = doveadm_cmd_user_kick(conn, args);
766 else if (strcmp(cmd, "USER-KICK-ALT") == 0)
767 ret = doveadm_cmd_user_kick_alt(conn, args);
768 else {
769 i_error("doveadm sent unknown command: %s", line);
770 ret = -1;
771 }
772 return ret;
773 } 782 }
774 783
775 static void doveadm_connection_input(struct doveadm_connection *conn) 784 static void doveadm_connection_input(struct doveadm_connection *conn)
776 { 785 {
777 const char *line; 786 const char *line;