Sample code for 30+ languages & platforms
Zig

IMAP Get Quota Root

Sends the GETQUOTAROOT command to the IMAP server to get the quota root for a given mailbox. This also provides quota information for the given mailbox.

The GetQuotaRoot method is available in Chilkat starting at v9.5.0.58, and is only available for IMAP servers that support the QUOTA extension.

The information is returned in JSON format. This example demonstrates how to parse the JSON to get the desired information.

Typically, the quota root for the Inbox is the empty string.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    // Use TLS
    imap.setSsl(true);
    imap.setPort(993);
    imap.connect("MY-IMAP-DOMAIN") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Authenticate
    imap.login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // First check to see if the IMAP server supports the QUOTA extension.
    const caps = imap.capability(alloc) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    if (!imap.hasCapability("QUOTA", caps)) {
        std.debug.print("IMAP server does not support the QUOTA extension.\n", .{});
        return;
    }

    const json_str = imap.getQuotaRoot(alloc, "Inbox") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // The JSON string will look something like this:
    // {"QUOTAROOT":{"mailbox":"Inbox","root":""},"QUOTA":{"root":"","resource":"STORAGE","used":22216,"max":15728640}}
    std.debug.print("{s}\n", .{json_str});

    // This is easy to parse...
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(json_str) catch {};

    // Get the quota root for Inbox, which is an empty string in the case above.
    const quota_root = try json.stringOf(alloc, "QUOTAROOT.root");
    std.debug.print("Quota Root for Inbox: [{s}]\n", .{quota_root});

    // Now get the resource name (which can be STORAGE or MESSAGE) and the used/max values.
    // Most servers have STORAGE quotas, which are for total capacity.  For example, GMail's
    // STORAGE quota is 15GB (at the time of writing this example).
    // A MESSAGE quota sets a limit on the number of messages.
    const resource_name = try json.stringOf(alloc, "QUOTA.resource");
    const quota_used = json.intOf("QUOTA.used");
    const quota_max = json.intOf("QUOTA.max");

    std.debug.print("Resource: {s}\n", .{resource_name});
    std.debug.print("Quota Used (in KB): {d}\n", .{quota_used});
    std.debug.print("Quota Max (in KB): {d}\n", .{quota_max});

    // Disconnect from the IMAP server.
    try imap.disconnect();
}