static int list_cal_cb(char *name,
		       int matchlen __attribute__((unused)),
		       int maycreate __attribute__((unused)),
		       void *rock)
{
    struct list_cal_rock *lrock = (struct list_cal_rock *) rock;
    struct transaction_t *txn = lrock->txn;
    struct buf *body = &txn->resp_body.payload;
    const char *base_path = txn->req_tgt.path;
    unsigned *level = lrock->level;
    static size_t inboxlen = 0;
    static size_t outboxlen = 0;
    static size_t defaultlen = 0;
    char *shortname;
    mbentry_t *mbentry = NULL;
    size_t len;
    int r, rights, any_rights = 0, isdefault = 0;
    static const char *displayname_annot =
	ANNOT_NS "<" XML_NS_DAV ">displayname";
    struct buf displayname = BUF_INITIALIZER;

    if (!inboxlen) inboxlen = strlen(SCHED_INBOX) - 1;
    if (!outboxlen) outboxlen = strlen(SCHED_OUTBOX) - 1;
    if (!defaultlen) defaultlen = strlen(SCHED_DEFAULT) - 1;

    shortname = strrchr(name, '.') + 1;
    len = strlen(shortname);

    /* Don't list scheduling Inbox/Outbox */
    if ((len == inboxlen && !strncmp(shortname, SCHED_INBOX, inboxlen)) ||
	(len == outboxlen && !strncmp(shortname, SCHED_OUTBOX, outboxlen)))
	goto done;

    /* Don't list deleted mailboxes */
    if (mboxname_isdeletedmailbox(name, 0)) goto done;

    /* Lookup the mailbox and make sure its readable */
    r = http_mlookup(name, &mbentry, NULL);
    if (r || !((rights = cyrus_acl_myrights(httpd_authstate, mbentry->acl)) & ACL_READ))
	goto done;

    /* Is this the default calendar? */
    if (len == defaultlen && !strncmp(shortname, SCHED_DEFAULT, defaultlen)) {
	isdefault = 1;
    }

    /* Send a body chunk once in a while */
    if (buf_len(body) > PROT_BUFSIZE) {
	write_body(0, txn, buf_cstring(body), buf_len(body));
	buf_reset(body);
    }

    /* Lookup DAV:displayname */
    r = annotatemore_lookupmask(name, displayname_annot, httpd_userid, &displayname);
    /* fall back to the last part of the mailbox name */
    if (r || !displayname.len) buf_setcstr(&displayname, shortname);

    /* Add available calendar with link */
    buf_printf_markup(body, (*level)++, "<tr>");
    buf_printf_markup(body, *level, "<td>%s%s%s", isdefault ? "<b>" : "",
		      buf_cstring(&displayname), isdefault ? "</b>" : "");

    buf_printf_markup(body, *level,
		      "<td><a href=\"webcal://%s%s%s\">Subscribe</a></td>",
		      lrock->host, base_path, shortname);

    buf_printf_markup(body, *level, "<td><a href=\"%s%s\">Download</a></td>",
		      base_path, shortname);

    if (!isdefault && (rights & DACL_RMCOL)) {
	buf_printf_markup(body, *level,
			  "<td><a href=\"%s%s?action=delete\">Delete</a></td>",
			  base_path, shortname);
    }
    else buf_printf_markup(body, *level, "<td></td>");

    if (mbentry->acl) {
	struct auth_state *auth_anyone = auth_newstate("anyone");

	any_rights = cyrus_acl_myrights(auth_anyone, mbentry->acl);
	auth_freestate(auth_anyone);
    }

    buf_printf_markup(body, *level,
		      "<td><input type=checkbox%s%s onclick=\""
		      "httpGet('%s%s?action=setacl&amp;share=' + this.checked)"
		      "\">Public</td>",
		      !(rights & DACL_ADMIN) ? " disabled" : "",
		      (any_rights & DACL_READ) == ACL_READ ? " checked" : "",
		      base_path, shortname);

    buf_printf_markup(body, --(*level), "</tr>");

done:
    buf_free(&displayname);
    mboxlist_entry_free(&mbentry);

    return 0;
}