imap-quota-plugin.c revision 08d6658a4e2ec8104cd1307f6baa75fdb07a24f8
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainenconst char *imap_quota_plugin_version = PACKAGE_VERSION;
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainenstatic void (*next_hook_client_created)(struct client **client);
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainenstatic const char *
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainenimap_quota_root_get_name(struct mail_user *user, struct mail_user *owner,
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen return t_strdup_printf("%s%c%s", owner->username,
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainenquota_send(struct client_command_context *cmd, struct mail_user *owner,
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen unsigned int i;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen name = imap_quota_root_get_name(cmd->client->user, owner, root);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen ret = quota_get_resource(root, "", *list, &value, &limit);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen (unsigned long long)value,
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen (unsigned long long)limit);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen } else if (ret < 0) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen "* BAD Internal quota calculation error");
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainenstatic bool cmd_getquotaroot(struct client_command_context *cmd)
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen struct quota_user *quser = QUOTA_USER_CONTEXT(client->user);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen /* <mailbox> */
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (!client_read_string_args(cmd, 1, &mailbox))
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen box = mailbox_open(&storage, mailbox, NULL, (MAILBOX_OPEN_READONLY |
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (ns->owner != NULL && ns->owner != client->user &&
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen client_send_tagline(cmd, "NO Not showing other users' quota.");
380dbb60ae291cbe39d1f710284562ca9167150bTimo Sirainen /* send QUOTAROOT reply */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen imap_quote_append_string(str, orig_mailbox, FALSE);
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen while ((root = quota_root_iter_next(iter)) != NULL) {
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen name = imap_quota_root_get_name(client->user, ns->owner, root);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen /* send QUOTA reply for each quotaroot */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen while ((root = quota_root_iter_next(iter)) != NULL)
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen client_send_tagline(cmd, "OK Getquotaroot completed.");
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic bool cmd_getquota(struct client_command_context *cmd)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen const char *root_name, *p;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen /* <quota root> */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (!client_read_string_args(cmd, 1, &root_name))
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen root = quota_root_lookup(cmd->client->user, root_name);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (root == NULL && cmd->client->user->admin) {
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen /* we're an admin. see if there's a quota root for another
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen client_send_tagline(cmd, "NO Quota root doesn't exist.");
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen client_send_tagline(cmd, "OK Getquota completed.");
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic bool cmd_setquota(struct client_command_context *cmd)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen /* <quota root> <resource limits> */
50e20db49f29917fe9adcf1b56b11badf28bd0e4Timo Sirainen if (args[1].type != IMAP_ARG_LIST || root_name == NULL) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen client_send_command_error(cmd, "Invalid arguments.");
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen root = quota_root_lookup(cmd->client->user, root_name);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen client_send_tagline(cmd, "NO Quota root doesn't exist.");
c6f894e1522f7b0b6068c228900914073c145175Timo Sirainen if (name == NULL || arg[1].type != IMAP_ARG_ATOM ||
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen client_send_command_error(cmd, "Invalid arguments.");
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen value = strtoull(IMAP_ARG_STR_NONULL(&arg[1]), NULL, 10);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (quota_set_resource(root, name, value, &error) < 0) {
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen client_send_tagline(cmd, "OK Setquota completed.");
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void imap_quota_client_created(struct client **client)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen str_append((*client)->capability_string, " QUOTA");
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen command_register("GETQUOTAROOT", cmd_getquotaroot, 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen command_register("GETQUOTA", cmd_getquota, 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen command_register("SETQUOTA", cmd_setquota, 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen next_hook_client_created = hook_client_created;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen hook_client_created = imap_quota_client_created;