| 95 error = 1; |
95 error = 1; |
| 96 break; |
96 break; |
| 97 } |
97 } |
| 98 |
98 |
| 99 JMAPMailBox *mbox = cxZalloc(a, sizeof(JMAPMailBox)); |
99 JMAPMailBox *mbox = cxZalloc(a, sizeof(JMAPMailBox)); |
| |
100 mbox->session = sn; |
| 100 mbox->id = cx_strdup_a(a, id->string).ptr; |
101 mbox->id = cx_strdup_a(a, id->string).ptr; |
| 101 if(cxJsonIsString(name)) { |
102 if(cxJsonIsString(name)) { |
| 102 mbox->name = cx_strdup_a(a, name->string).ptr; |
103 mbox->name = cx_strdup_a(a, name->string).ptr; |
| 103 } |
104 } |
| 104 if(cxJsonIsString(parent_id)) { |
105 if(cxJsonIsString(parent_id)) { |
| 117 return NULL; |
118 return NULL; |
| 118 } else { |
119 } else { |
| 119 return list; |
120 return list; |
| 120 } |
121 } |
| 121 } |
122 } |
| |
123 |
| |
124 CxList* jmap_mailbox_get_mails(JMAPMailBox *mailbox) { |
| |
125 JMAPSession *sn = mailbox->session; |
| |
126 const CxAllocator *a = sn->mp->allocator; |
| |
127 |
| |
128 CxBuffer *request = jm_email_get_request(cx_str(mailbox->id)); |
| |
129 //printf("%.*s\n", (int)request->size, request->space); |
| |
130 |
| |
131 CxJsonValue *response = jm_do_api_request(sn, request); |
| |
132 cxBufferFree(request); |
| |
133 if(!response) { |
| |
134 return NULL; |
| |
135 } |
| |
136 |
| |
137 CxJsonValue *mail_list = NULL; |
| |
138 if(cxJsonIsObject(response)) { |
| |
139 CxJsonValue *method_responses = cxJsonObjGet(response, "methodResponses"); |
| |
140 if(cxJsonIsArray(method_responses) && method_responses->array.size > 1) { |
| |
141 CxJsonValue *method_response1 = method_responses->array.data[1]; |
| |
142 if(cxJsonIsArray(method_response1) && method_response1->array.size > 1) { |
| |
143 CxJsonValue *method = method_response1->array.data[0]; |
| |
144 if(cxJsonIsString(method) && !cx_strcmp(method->string, "Email/get")) { |
| |
145 CxJsonValue *mail_get_response_obj = method_response1->array.data[1]; |
| |
146 if(cxJsonIsObject(mail_get_response_obj)) { |
| |
147 mail_list = cxJsonObjGet(mail_get_response_obj, "list"); |
| |
148 } |
| |
149 } |
| |
150 } |
| |
151 } |
| |
152 } |
| |
153 |
| |
154 if(!mail_list) { |
| |
155 cxJsonValueFree(response); |
| |
156 return NULL; |
| |
157 } |
| |
158 |
| |
159 CxList *list = cxArrayListCreate(a, CX_STORE_POINTERS, 16); |
| |
160 list->collection.simple_destructor = (cx_destructor_func)jmap_mailbox_free; |
| |
161 int error = 0; |
| |
162 for(size_t i=0;i<mail_list->array.size;i++) { |
| |
163 CxJsonValue *mail = mail_list->array.data[i]; |
| |
164 if(!cxJsonIsObject(mail)) { |
| |
165 error = 1; |
| |
166 break; |
| |
167 } |
| |
168 |
| |
169 CxJsonValue *id = cxJsonObjGet(mail, "id"); |
| |
170 CxJsonValue *subject = cxJsonObjGet(mail, "subject"); |
| |
171 |
| |
172 if(!cxJsonIsString(id)) { |
| |
173 error = 1; |
| |
174 break; |
| |
175 } |
| |
176 |
| |
177 JMAPMail *email = cxZalloc(a, sizeof(JMAPMail)); |
| |
178 email->session = sn; |
| |
179 email->id = cx_strdup_a(a, id->string).ptr; |
| |
180 if(cxJsonIsString(subject)) { |
| |
181 email->subject = cx_strdup_a(a, subject->string).ptr; |
| |
182 } |
| |
183 |
| |
184 cxListAdd(list, email); |
| |
185 } |
| |
186 |
| |
187 cxJsonValueFree(response); |
| |
188 if(error) { |
| |
189 cxListFree(list); |
| |
190 return NULL; |
| |
191 } else { |
| |
192 return list; |
| |
193 } |
| |
194 } |