summaryrefslogtreecommitdiffstats
path: root/src/imap-login/imap-login-commands.c
blob: 8d4878d142649793ed83d63e0789eb2a8b1ad7a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */

#include "login-common.h"
#include "array.h"
#include "imap-login-commands.h"

static ARRAY(struct imap_login_command *) imap_login_commands;
static pool_t imap_login_commands_pool;

struct imap_login_command *imap_login_command_lookup(const char *name)
{
	struct imap_login_command *cmd;

	array_foreach_elem(&imap_login_commands, cmd) {
		if (strcasecmp(cmd->name, name) == 0)
			return cmd;
	}
	return NULL;
}

void imap_login_commands_register(const struct imap_login_command *commands,
				  unsigned int count)
{
	struct imap_login_command *cmd;
	unsigned int i;

	for (i = 0; i < count; i++) {
		cmd = p_new(imap_login_commands_pool, struct imap_login_command, 1);
		cmd->name = p_strdup(imap_login_commands_pool, commands[i].name);
		cmd->func = commands[i].func;
		array_push_back(&imap_login_commands, &cmd);
	}
}

static void
imap_login_command_unregister(const struct imap_login_command *unreg_cmd)
{
	struct imap_login_command *const *cmdp;

	array_foreach(&imap_login_commands, cmdp) {
		if ((*cmdp)->func == unreg_cmd->func &&
		    strcmp((*cmdp)->name, unreg_cmd->name) == 0) {
			array_delete(&imap_login_commands,
				array_foreach_idx(&imap_login_commands, cmdp), 1);
			return;
		}
	}
	i_panic("imap_login_command_unregister: Command '%s' not found", unreg_cmd->name);
}

void imap_login_commands_unregister(const struct imap_login_command *commands,
				    unsigned int count)
{
	unsigned int i;

	for (i = 0; i < count; i++)
		imap_login_command_unregister(&commands[i]);
}

void imap_login_commands_init(void)
{
	imap_login_commands_pool =
		pool_alloconly_create("imap login commands", 128);
	p_array_init(&imap_login_commands, imap_login_commands_pool, 8);
}

void imap_login_commands_deinit(void)
{
	pool_unref(&imap_login_commands_pool);
}