libidav/jmap_mail.c

branch
dav-2
changeset 928
57167625bfdd
parent 927
0bcf3d69b37a
--- a/libidav/jmap_mail.c	Fri Jul 10 17:56:21 2026 +0200
+++ b/libidav/jmap_mail.c	Sat Jul 11 20:57:23 2026 +0200
@@ -97,6 +97,7 @@
         }
         
         JMAPMailBox *mbox = cxZalloc(a, sizeof(JMAPMailBox));
+        mbox->session = sn;
         mbox->id = cx_strdup_a(a, id->string).ptr;
         if(cxJsonIsString(name)) {
             mbox->name = cx_strdup_a(a, name->string).ptr;
@@ -119,3 +120,75 @@
         return list;
     }
 }
+
+CxList* jmap_mailbox_get_mails(JMAPMailBox *mailbox) {
+    JMAPSession *sn = mailbox->session;
+    const CxAllocator *a = sn->mp->allocator;
+    
+    CxBuffer *request = jm_email_get_request(cx_str(mailbox->id));
+    //printf("%.*s\n", (int)request->size, request->space);
+    
+    CxJsonValue *response = jm_do_api_request(sn, request);
+    cxBufferFree(request);
+    if(!response) {
+        return NULL;
+    }
+    
+    CxJsonValue *mail_list = NULL;
+    if(cxJsonIsObject(response)) {
+        CxJsonValue *method_responses = cxJsonObjGet(response, "methodResponses");
+        if(cxJsonIsArray(method_responses) && method_responses->array.size > 1) {
+            CxJsonValue *method_response1 = method_responses->array.data[1];
+            if(cxJsonIsArray(method_response1) && method_response1->array.size > 1) {
+                CxJsonValue *method = method_response1->array.data[0];
+                if(cxJsonIsString(method) && !cx_strcmp(method->string, "Email/get")) {
+                    CxJsonValue *mail_get_response_obj = method_response1->array.data[1];
+                    if(cxJsonIsObject(mail_get_response_obj)) {
+                        mail_list = cxJsonObjGet(mail_get_response_obj, "list");
+                    }
+                }
+            }
+        }
+    }
+    
+    if(!mail_list) {
+        cxJsonValueFree(response);
+        return NULL;
+    }
+    
+    CxList *list = cxArrayListCreate(a, CX_STORE_POINTERS, 16);
+    list->collection.simple_destructor = (cx_destructor_func)jmap_mailbox_free;
+    int error = 0;
+    for(size_t i=0;i<mail_list->array.size;i++) {
+        CxJsonValue *mail = mail_list->array.data[i];
+        if(!cxJsonIsObject(mail)) {
+            error = 1;
+            break;
+        }
+        
+        CxJsonValue *id = cxJsonObjGet(mail, "id");
+        CxJsonValue *subject = cxJsonObjGet(mail, "subject");
+        
+        if(!cxJsonIsString(id)) {
+            error = 1;
+            break;
+        }
+        
+        JMAPMail *email = cxZalloc(a, sizeof(JMAPMail));
+        email->session = sn;
+        email->id = cx_strdup_a(a, id->string).ptr;
+        if(cxJsonIsString(subject)) {
+            email->subject = cx_strdup_a(a, subject->string).ptr;
+        }
+        
+        cxListAdd(list, email);
+    }
+    
+    cxJsonValueFree(response);
+    if(error) {
+        cxListFree(list);
+        return NULL;
+    } else {
+        return list;
+    }
+}
\ No newline at end of file

mercurial