summaryrefslogtreecommitdiffstats
path: root/src/plugins/quota/doveadm-quota.c
blob: 8a42b221f791625177c4d6aa04122c51c6baef9c (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "module-dir.h"
#include "quota-plugin.h"
#include "quota-private.h"
#include "doveadm-print.h"
#include "doveadm-mail.h"

const char *doveadm_quota_plugin_version = DOVECOT_ABI_VERSION;

void doveadm_quota_plugin_init(struct module *module);
void doveadm_quota_plugin_deinit(void);

static int cmd_quota_get_root(struct quota_root *root)
{
	const char *const *res;
	const char *error;
	uint64_t value, limit;
	enum quota_get_result qret;
	int ret = 0;

	res = quota_root_get_resources(root);
	for (; *res != NULL; res++) {
		qret = quota_get_resource(root, "", *res, &value, &limit, &error);
		doveadm_print(root->set->name);
		doveadm_print(*res);
		if (qret == QUOTA_GET_RESULT_LIMITED) {
			doveadm_print_num(value);
			doveadm_print_num(limit);
			if (limit > 0)
				doveadm_print_num(value*100 / limit);
			else
				doveadm_print("0");
		} else if (qret == QUOTA_GET_RESULT_UNLIMITED) {
			doveadm_print_num(value);
			doveadm_print("-");
			doveadm_print("0");
		} else {
			i_error("Failed to get quota resource %s: %s",
				*res, error);
			doveadm_print("error");
			doveadm_print("error");
			doveadm_print("error");
			ret = -1;
		}
	}
	return ret;
}

static int
cmd_quota_get_run(struct doveadm_mail_cmd_context *ctx,
		  struct mail_user *user)
{
	struct quota_user *quser = QUOTA_USER_CONTEXT(user);
	struct quota_root *const *root;

	if (quser == NULL) {
		i_error("Quota not enabled");
		doveadm_mail_failed_error(ctx, MAIL_ERROR_NOTFOUND);
		return -1;
	}

	int ret = 0;
	array_foreach(&quser->quota->roots, root)
		if (cmd_quota_get_root(*root) < 0)
			ret = -1;
	if (ret < 0)
		doveadm_mail_failed_error(ctx, MAIL_ERROR_TEMP);
	return ret;
}

static void cmd_quota_get_init(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
			       const char *const args[] ATTR_UNUSED)
{
	doveadm_print_header("root", "Quota name", 0);
	doveadm_print_header("type", "Type", 0);
	doveadm_print_header("value", "Value",
			     DOVEADM_PRINT_HEADER_FLAG_RIGHT_JUSTIFY);
	doveadm_print_header("limit", "Limit",
			     DOVEADM_PRINT_HEADER_FLAG_RIGHT_JUSTIFY);
	doveadm_print_header("percent", "%",
			     DOVEADM_PRINT_HEADER_FLAG_RIGHT_JUSTIFY);
}

static struct doveadm_mail_cmd_context *
cmd_quota_get_alloc(void)
{
	struct doveadm_mail_cmd_context *ctx;

	ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
	ctx->v.run = cmd_quota_get_run;
	ctx->v.init = cmd_quota_get_init;
	doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);
	return ctx;
}

static int
cmd_quota_recalc_run(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
		     struct mail_user *user)
{
	struct quota_user *quser = QUOTA_USER_CONTEXT(user);
	struct quota_root *const *root;
	struct quota_transaction_context trans;

	if (quser == NULL) {
		i_error("Quota not enabled");
		doveadm_mail_failed_error(ctx, MAIL_ERROR_NOTFOUND);
		return -1;
	}

	i_zero(&trans);
	trans.quota = quser->quota;
	trans.recalculate = QUOTA_RECALCULATE_FORCED;

	array_foreach(&quser->quota->roots, root) {
		const char *error;
		if ((*root)->backend.v.update(*root, &trans, &error) < 0)
			i_error("Recalculating quota failed: %s", error);
		if ((*root)->backend.v.flush != NULL)
			(*root)->backend.v.flush(*root);
	}
	return 0;
}

static struct doveadm_mail_cmd_context *
cmd_quota_recalc_alloc(void)
{
	struct doveadm_mail_cmd_context *ctx;

	ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
	ctx->v.run = cmd_quota_recalc_run;
	return ctx;
}

static struct doveadm_cmd_ver2 quota_commands[] = {
	{
		.name = "quota get",
		.usage = "",
		.mail_cmd = cmd_quota_get_alloc,
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_MAIL_COMMON
DOVEADM_CMD_PARAMS_END
	},
	{
		.name = "quota recalc",
		.usage = "",
		.mail_cmd = cmd_quota_recalc_alloc,
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_MAIL_COMMON
DOVEADM_CMD_PARAMS_END
	}
};

void doveadm_quota_plugin_init(struct module *module ATTR_UNUSED)
{
	unsigned int i;

	for (i = 0; i < N_ELEMENTS(quota_commands); i++)
		doveadm_cmd_register_ver2(&quota_commands[i]);
}

void doveadm_quota_plugin_deinit(void)
{
}