| |
1 /* |
| |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| |
3 * |
| |
4 * Copyright 2026 Olaf Wintermann. All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright |
| |
10 * notice, this list of conditions and the following disclaimer. |
| |
11 * |
| |
12 * 2. Redistributions in binary form must reproduce the above copyright |
| |
13 * notice, this list of conditions and the following disclaimer in the |
| |
14 * documentation and/or other materials provided with the distribution. |
| |
15 * |
| |
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
26 * POSSIBILITY OF SUCH DAMAGE. |
| |
27 */ |
| |
28 |
| |
29 #include "jmap_mail.h" |
| |
30 #include "jmap_methods.h" |
| |
31 |
| |
32 void jmap_mailbox_free(JMAPMailBox *mailbox) { |
| |
33 const CxAllocator *a = mailbox->session->mp->allocator; |
| |
34 cxFree(a, mailbox->id); |
| |
35 cxFree(a, mailbox->parent_id); |
| |
36 cxFree(a, mailbox->name); |
| |
37 cxFree(a, mailbox->role); |
| |
38 cxFree(a, mailbox); |
| |
39 } |
| |
40 |
| |
41 CxList* jmap_mailbox_get_list(JMAPSession *sn) { |
| |
42 if(!sn->primary_accounts.mail) { |
| |
43 return NULL; |
| |
44 } |
| |
45 const CxAllocator *a = sn->mp->allocator; |
| |
46 |
| |
47 CxBuffer *request = jm_mailbox_get_request(cx_str(sn->primary_accounts.mail)); |
| |
48 //printf("%.*s\n", (int)request->size, request->space); |
| |
49 |
| |
50 CxJsonValue *response = jm_do_api_request(sn, request); |
| |
51 cxBufferFree(request); |
| |
52 if(!response) { |
| |
53 return NULL; |
| |
54 } |
| |
55 |
| |
56 CxJsonValue *mailbox_list = NULL; |
| |
57 // check response and get mailbox response value |
| |
58 if(cxJsonIsObject(response)) { |
| |
59 CxJsonValue *method_responses = cxJsonObjGet(response, "methodResponses"); |
| |
60 if(cxJsonIsArray(method_responses) && method_responses->array.size > 0) { |
| |
61 CxJsonValue *method_response0 = method_responses->array.data[0]; |
| |
62 if(cxJsonIsArray(method_response0) && method_response0->array.size > 1) { |
| |
63 CxJsonValue *method = method_response0->array.data[0]; |
| |
64 if(cxJsonIsString(method) && !cx_strcmp(method->string, "Mailbox/get")) { |
| |
65 CxJsonValue *mailbox_response_obj = method_response0->array.data[1]; |
| |
66 if(cxJsonIsObject(mailbox_response_obj)) { |
| |
67 mailbox_list = cxJsonObjGet(mailbox_response_obj, "list"); |
| |
68 } |
| |
69 } |
| |
70 } |
| |
71 } |
| |
72 } |
| |
73 |
| |
74 if(!mailbox_list) { |
| |
75 cxJsonValueFree(response); |
| |
76 return NULL; |
| |
77 } |
| |
78 |
| |
79 CxList *list = cxArrayListCreate(a, CX_STORE_POINTERS, 16); |
| |
80 list->collection.simple_destructor = (cx_destructor_func)jmap_mailbox_free; |
| |
81 int error = 0; |
| |
82 for(size_t i=0;i<mailbox_list->array.size;i++) { |
| |
83 CxJsonValue *mailbox = mailbox_list->array.data[i]; |
| |
84 if(!cxJsonIsObject(mailbox)) { |
| |
85 error = 1; |
| |
86 break; |
| |
87 } |
| |
88 |
| |
89 CxJsonValue *id = cxJsonObjGet(mailbox, "id"); |
| |
90 CxJsonValue *name = cxJsonObjGet(mailbox, "name"); |
| |
91 CxJsonValue *parent_id = cxJsonObjGet(mailbox, "parentId"); |
| |
92 CxJsonValue *role = cxJsonObjGet(mailbox, "role"); |
| |
93 |
| |
94 if(!cxJsonIsString(id)) { |
| |
95 error = 1; |
| |
96 break; |
| |
97 } |
| |
98 |
| |
99 JMAPMailBox *mbox = cxZalloc(a, sizeof(JMAPMailBox)); |
| |
100 mbox->id = cx_strdup_a(a, id->string).ptr; |
| |
101 if(cxJsonIsString(name)) { |
| |
102 mbox->name = cx_strdup_a(a, name->string).ptr; |
| |
103 } |
| |
104 if(cxJsonIsString(parent_id)) { |
| |
105 mbox->parent_id = cx_strdup_a(a, parent_id->string).ptr; |
| |
106 } |
| |
107 if(cxJsonIsString(role)) { |
| |
108 mbox->role = cx_strdup_a(a, role->string).ptr; |
| |
109 } |
| |
110 |
| |
111 cxListAdd(list, mbox); |
| |
112 } |
| |
113 |
| |
114 cxJsonValueFree(response); |
| |
115 if(error) { |
| |
116 cxListFree(list); |
| |
117 return NULL; |
| |
118 } else { |
| |
119 return list; |
| |
120 } |
| |
121 } |