imap-quota-plugin.c revision d12d1accd6060eee70375f1e6122425a159114e4
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2005-2011 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char *imap_quota_plugin_version = DOVECOT_VERSION;
8887bf3757d51d73887dd20b1db3334d867d3817Timo Sirainenstatic void (*next_hook_client_created)(struct client **client);
8887bf3757d51d73887dd20b1db3334d867d3817Timo Sirainenstatic const char *
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainenimap_quota_root_get_name(struct mail_user *user, struct mail_user *owner,
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen return t_strdup_printf("%s%c%s", owner->username,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenquota_reply_write(string_t *str, struct mail_user *user,
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct mail_user *owner, struct quota_root *root)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int i;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen name = imap_quota_root_get_name(user, owner, root);
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen ret = quota_get_resource(root, "", *list, &value, &limit);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (unsigned long long)value,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (unsigned long long)limit);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append(str, "* BAD Internal quota calculation error\r\n");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic bool cmd_getquotaroot(struct client_command_context *cmd)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct quota_user *quser = QUOTA_USER_CONTEXT(client->user);
8aacc9e7c84f8376822823ec98c2f551d4919b2eTimo Sirainen /* <mailbox> */
8aacc9e7c84f8376822823ec98c2f551d4919b2eTimo Sirainen if (!client_read_string_args(cmd, 1, &mailbox))
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen ns = client_find_namespace(cmd, mailbox, &storage_name, &status);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ns->owner != NULL && ns->owner != client->user &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_send_tagline(cmd, "NO Not showing other users' quota.");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen client_fail_mailbox_name_status(cmd, mailbox, NULL, status);
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen box = mailbox_alloc(ns->list, storage_name, MAILBOX_FLAG_READONLY |
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen /* build QUOTAROOT reply and QUOTA reply for all quota roots */
6bc98d3898c475ba7615ba2b016e5142c8b2c09fTimo Sirainen imap_quote_append_string(quotaroot_reply, mailbox, FALSE);
6bc98d3898c475ba7615ba2b016e5142c8b2c09fTimo Sirainen while ((root = quota_root_iter_next(iter)) != NULL) {
6bc98d3898c475ba7615ba2b016e5142c8b2c09fTimo Sirainen name = imap_quota_root_get_name(client->user, ns->owner, root);
6bc98d3898c475ba7615ba2b016e5142c8b2c09fTimo Sirainen imap_quote_append_string(quotaroot_reply, name, FALSE);
6bc98d3898c475ba7615ba2b016e5142c8b2c09fTimo Sirainen quota_reply_write(quota_reply, client->user, ns->owner, root);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* send replies */
26ff8f8a4867bf8e9551a27a2de8c12cd138b065Timo Sirainen client_send_line(client, str_c(quotaroot_reply));
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen o_stream_send(client->output, str_data(quota_reply),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_send_tagline(cmd, "OK Getquotaroot completed.");
020a39a395d2adb768e0179631b37bc78ecd9471Timo Sirainenstatic bool cmd_getquota(struct client_command_context *cmd)
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen const char *root_name, *p;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen /* <quota root> */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!client_read_string_args(cmd, 1, &root_name))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen root = quota_root_lookup(cmd->client->user, root_name);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (root == NULL && cmd->client->user->admin) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* we're an admin. see if there's a quota root for another
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainen client_send_tagline(cmd, "NO Quota root doesn't exist.");
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen quota_reply_write(quota_reply, cmd->client->user, owner, root);
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen o_stream_send(cmd->client->output, str_data(quota_reply),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_send_tagline(cmd, "OK Getquota completed.");
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenstatic bool cmd_setquota(struct client_command_context *cmd)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *root_name, *name, *value_str, *error;
df4018ae2f0a95be602f724ca70df7e0e3bd6a7dTimo Sirainen /* <quota root> <resource limits> */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!imap_arg_get_astring(&args[0], &root_name) ||
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen client_send_command_error(cmd, "Invalid arguments.");
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainen root = quota_root_lookup(cmd->client->user, root_name);
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainen client_send_tagline(cmd, "NO Quota root doesn't exist.");
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen for (; !IMAP_ARG_IS_EOL(list_args); list_args += 2) {
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen if (!imap_arg_get_atom(&list_args[0], &name) ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen !imap_arg_get_atom(&list_args[1], &value_str) ||
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen client_send_command_error(cmd, "Invalid arguments.");
48136ae5a0eb49daa44e343553f3688a500307e2Timo Sirainen if (quota_set_resource(root, name, value, &error) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_send_tagline(cmd, "OK Setquota completed.");
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainenstatic void imap_quota_client_created(struct client **client)
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen if (mail_user_is_plugin_loaded((*client)->user, imap_quota_module))
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen str_append((*client)->capability_string, " QUOTA");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid imap_quota_plugin_init(struct module *module)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen command_register("GETQUOTAROOT", cmd_getquotaroot, 0);
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen command_register("GETQUOTA", cmd_getquota, 0);
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen command_register("SETQUOTA", cmd_setquota, 0);
d9de52132072d80b8c268094b879c0ef5a108db3Timo Sirainen imap_client_created_hook_set(imap_quota_client_created);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen imap_client_created_hook_set(next_hook_client_created);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenconst char *imap_quota_plugin_dependencies[] = { "quota", NULL };
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char imap_quota_plugin_binary_dependency[] = "imap";