summaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'db.c')
-rw-r--r--db.c92
1 files changed, 67 insertions, 25 deletions
diff --git a/db.c b/db.c
index 69a7664e..2c1aa049 100644
--- a/db.c
+++ b/db.c
@@ -2799,18 +2799,22 @@ static int db_findmailbox_owner(const char *name, u64_t owner_idnr,
}
static int mailboxes_by_regex(u64_t user_idnr, int only_subscribed, const char * pattern,
- u64_t ** mailboxes, unsigned int *nr_mailboxes)
+ u64_t ** mailboxes, unsigned int *nr_mailboxes, mailbox_t ** mb)
{
unsigned int i;
u64_t *tmp_mailboxes;
u64_t *all_mailboxes;
char** all_mailbox_names;
u64_t *all_mailbox_owners;
+ u64_t *all_mailbox_nosels;
+ u64_t *all_mailbox_noinfs;
+ u64_t *all_mailbox_children;
u64_t search_user_idnr = user_idnr;
unsigned n_rows;
char *matchname;
const char *spattern;
char *namespace, *username;
+ mailbox_t *tmp_mb;
char query[DEF_QUERYSIZE];
memset(query,0,DEF_QUERYSIZE);
@@ -2852,7 +2856,7 @@ static int mailboxes_by_regex(u64_t user_idnr, int only_subscribed, const char *
if (only_subscribed)
snprintf(query, DEF_QUERYSIZE,
- "SELECT distinct(mbx.name), mbx.mailbox_idnr, mbx.owner_idnr "
+ "SELECT distinct(mbx.name), mbx.mailbox_idnr, mbx.owner_idnr, mbx.no_select, mbx.no_inferiors, mbx.children "
"FROM %smailboxes mbx "
"LEFT JOIN %sacl acl ON mbx.mailbox_idnr = acl.mailbox_id "
"LEFT JOIN %susers usr ON acl.user_id = usr.user_idnr "
@@ -2868,7 +2872,7 @@ static int mailboxes_by_regex(u64_t user_idnr, int only_subscribed, const char *
DBMAIL_ACL_ANYONE_USER);
else
snprintf(query, DEF_QUERYSIZE,
- "SELECT distinct(mbx.name), mbx.mailbox_idnr, mbx.owner_idnr "
+ "SELECT distinct(mbx.name), mbx.mailbox_idnr, mbx.owner_idnr, mbx.no_select, mbx.no_inferiors, mbx.children "
"FROM %smailboxes mbx "
"LEFT JOIN %sacl acl ON mbx.mailbox_idnr = acl.mailbox_id "
"LEFT JOIN %susers usr ON acl.user_id = usr.user_idnr "
@@ -2896,20 +2900,32 @@ static int mailboxes_by_regex(u64_t user_idnr, int only_subscribed, const char *
all_mailboxes = g_new0(u64_t,n_rows);
all_mailbox_names = g_new0(char *,n_rows);
all_mailbox_owners = g_new0(u64_t,n_rows);
+ all_mailbox_nosels = g_new0(u64_t,n_rows);
+ all_mailbox_noinfs = g_new0(u64_t,n_rows);
+ all_mailbox_children = g_new0(u64_t,n_rows);
tmp_mailboxes = g_new0(u64_t,n_rows);
for (i = 0; i < n_rows; i++) {
all_mailbox_names[i] = g_strdup(db_get_result(i, 0));
all_mailboxes[i] = db_get_result_u64(i, 1);
all_mailbox_owners[i] = db_get_result_u64(i, 2);
+ all_mailbox_nosels[i] = db_get_result_u64(i, 3);
+ all_mailbox_noinfs[i] = db_get_result_u64(i, 4);
+ all_mailbox_children[i] = db_get_result_u64(i, 5);
}
db_free_result();
+ tmp_mb = g_new0(mailbox_t,n_rows);
+
for (i = 0; i < n_rows; i++) {
char *mailbox_name;
+ GString *fqname;
u64_t mailbox_idnr = all_mailboxes[i];
u64_t owner_idnr = all_mailbox_owners[i];
+ u64_t no_select = all_mailbox_nosels[i];
+ u64_t no_inferiors = all_mailbox_noinfs[i];
+ u64_t no_children = all_mailbox_children[i];
char *simple_mailbox_name = all_mailbox_names[i];
/* add possible namespace prefix to mailbox_name */
@@ -2918,7 +2934,23 @@ static int mailboxes_by_regex(u64_t user_idnr, int only_subscribed, const char *
if (mailbox_name) {
tmp_mailboxes[*nr_mailboxes] = mailbox_idnr;
(*nr_mailboxes)++;
+
+ fqname = g_string_new(mailbox_name);
+ fqname = g_string_truncate(fqname,IMAP_MAX_MAILBOX_NAMELEN);
+ tmp_mb[i].name = fqname->str;
+ g_string_free(fqname,FALSE);
}
+
+ /* owner_idnr */
+ tmp_mb[i].owner_idnr = owner_idnr;
+
+ /* no_select */
+ tmp_mb[i].no_select=no_select;
+ /* no_inferior */
+ tmp_mb[i].no_inferiors=no_inferiors;
+
+ /* no_children */
+ tmp_mb[i].no_children=no_children?0:1;
g_free(mailbox_name);
g_free(simple_mailbox_name);
@@ -2926,6 +2958,9 @@ static int mailboxes_by_regex(u64_t user_idnr, int only_subscribed, const char *
g_free(all_mailbox_names);
g_free(all_mailboxes);
g_free(all_mailbox_owners);
+ g_free(all_mailbox_nosels);
+ g_free(all_mailbox_noinfs);
+ g_free(all_mailbox_children);
if (*nr_mailboxes == 0) {
/* none exist, none matched */
@@ -2934,18 +2969,19 @@ static int mailboxes_by_regex(u64_t user_idnr, int only_subscribed, const char *
}
*mailboxes = tmp_mailboxes;
+ *mb = tmp_mb;
return DM_EGENERAL;
}
int db_findmailbox_by_regex(u64_t owner_idnr, const char *pattern,
u64_t ** children, unsigned *nchildren,
- int only_subscribed)
+ int only_subscribed, mailbox_t ** mb)
{
*children = NULL;
/* list normal mailboxes */
- if (mailboxes_by_regex(owner_idnr, only_subscribed, pattern, children, nchildren) < 0) {
+ if (mailboxes_by_regex(owner_idnr, only_subscribed, pattern, children, nchildren, mb) < 0) {
TRACE(TRACE_ERROR, "error listing mailboxes");
return DM_EQUERY;
}
@@ -4662,7 +4698,6 @@ int db_getmailbox_list_result(u64_t mailbox_idnr, u64_t user_idnr, mailbox_t * m
{
/* query mailbox for LIST results */
char *mbxname, *name;
- char *mailbox_like;
GString *fqname;
int i=0;
char query[DEF_QUERYSIZE];
@@ -4670,7 +4705,7 @@ int db_getmailbox_list_result(u64_t mailbox_idnr, u64_t user_idnr, mailbox_t * m
snprintf(query, DEF_QUERYSIZE,
- "SELECT owner_idnr, name, no_select, no_inferiors "
+ "SELECT owner_idnr, name, no_select, no_inferiors, children "
"FROM %smailboxes WHERE mailbox_idnr = %llu",
DBPFX, mailbox_idnr);
@@ -4699,26 +4734,9 @@ int db_getmailbox_list_result(u64_t mailbox_idnr, u64_t user_idnr, mailbox_t * m
mb->no_select=db_get_result_bool(0,i++);
/* no_inferior */
mb->no_inferiors=db_get_result_bool(0,i++);
- db_free_result();
/* no_children */
- mailbox_like = db_imap_utf7_like("name", name, "/%");
-
- memset(query,0,DEF_QUERYSIZE);
-
- snprintf(query, DEF_QUERYSIZE,
- "SELECT COUNT(*) AS nr_children "
- "FROM %smailboxes WHERE owner_idnr = %llu "
- "AND %s",
- DBPFX, mb->owner_idnr, mailbox_like);
-
- g_free(mailbox_like);
-
- if (db_query(query) == -1) {
- TRACE(TRACE_ERROR, "db error");
- return DM_EQUERY;
- }
- mb->no_children=db_get_result_u64(0,0)?0:1;
+ mb->no_children=db_get_result_u64(0,i++)?0:1;
g_free(name);
db_free_result();
@@ -5199,3 +5217,27 @@ int db_user_log_login(u64_t user_idnr)
return result;
}
+
+int db_getmailbox_list_by_regex(u64_t owner_idnr, const char *pattern,
+ u64_t ** children, unsigned *nchildren,
+ int only_subscribed, mailbox_t **mb)
+{
+ *children = NULL;
+
+ /* list normal mailboxes */
+ if (mailboxes_by_regex(owner_idnr, only_subscribed, pattern, children, nchildren, mb) < 0) {
+ TRACE(TRACE_ERROR, "error listing mailboxes");
+ return DM_EQUERY;
+ }
+
+ if (*nchildren == 0) {
+ TRACE(TRACE_INFO, "did not find any mailboxes that "
+ "match pattern. returning 0, nchildren = 0");
+ return DM_SUCCESS;
+ }
+
+
+ /* store matches */
+ TRACE(TRACE_INFO, "found [%d] mailboxes", *nchildren);
+ return DM_SUCCESS;
+}