imap-login-commands.c revision 2454dfa32c93c20a8522c6ed42fe057baaac9f9a
6ae232055d4d8a97267517c5e50074c2c819941and/* Copyright (c) 2016-2017 Dovecot authors, see the included COPYING file */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include "login-common.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "array.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "imap-login-commands.h"
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic ARRAY(struct imap_login_command *) imap_login_commands;
6ae232055d4d8a97267517c5e50074c2c819941andstatic pool_t imap_login_commands_pool;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstruct imap_login_command *imap_login_command_lookup(const char *name)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and struct imap_login_command *const *cmdp;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and array_foreach(&imap_login_commands, cmdp) {
6ae232055d4d8a97267517c5e50074c2c819941and if (strcasecmp((*cmdp)->name, name) == 0)
6ae232055d4d8a97267517c5e50074c2c819941and return *cmdp;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and return NULL;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andvoid imap_login_commands_register(const struct imap_login_command *commands,
6ae232055d4d8a97267517c5e50074c2c819941and unsigned int count)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and struct imap_login_command *cmd;
4b3a8afbfcea8b265d179a122bf40dfedd1ce280takashi unsigned int i;
11495c9f0bd33e51a25b4d532beadfbcf9b944a3nilgun
6ae232055d4d8a97267517c5e50074c2c819941and for (i = 0; i < count; i++) {
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi cmd = p_new(imap_login_commands_pool, struct imap_login_command, 1);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi cmd->name = p_strdup(imap_login_commands_pool, commands[i].name);
6ae232055d4d8a97267517c5e50074c2c819941and cmd->func = commands[i].func;
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd array_append(&imap_login_commands, &cmd, 1);
282b62d8e9a4edbc2da22ba2d876ec94afc48084nd }
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic void
6ae232055d4d8a97267517c5e50074c2c819941andimap_login_command_unregister(const struct imap_login_command *unreg_cmd)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and struct imap_login_command *const *cmdp;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and array_foreach(&imap_login_commands, cmdp) {
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi if ((*cmdp)->func == unreg_cmd->func &&
6ae232055d4d8a97267517c5e50074c2c819941and strcmp((*cmdp)->name, unreg_cmd->name) == 0) {
6ae232055d4d8a97267517c5e50074c2c819941and array_delete(&imap_login_commands,
6ae232055d4d8a97267517c5e50074c2c819941and array_foreach_idx(&imap_login_commands, cmdp), 1);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi return;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and i_panic("imap_login_command_unregister: Command '%s' not found", unreg_cmd->name);
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andvoid imap_login_commands_unregister(const struct imap_login_command *commands,
6ae232055d4d8a97267517c5e50074c2c819941and unsigned int count)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and unsigned int i;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and for (i = 0; i < count; i++)
6ae232055d4d8a97267517c5e50074c2c819941and imap_login_command_unregister(&commands[i]);
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andvoid imap_login_commands_init(void)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and imap_login_commands_pool =
6ae232055d4d8a97267517c5e50074c2c819941and pool_alloconly_create("imap login commands", 128);
6ae232055d4d8a97267517c5e50074c2c819941and p_array_init(&imap_login_commands, imap_login_commands_pool, 8);
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andvoid imap_login_commands_deinit(void)
6ae232055d4d8a97267517c5e50074c2c819941and{
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi pool_unref(&imap_login_commands_pool);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi}
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi