Commit a736b7a9 a736b7a9a775db4b82806a6214deefc3ac7a4dd4 by Sergey Poznyakoff

imap client: redo capability and id support via list mappers.

* libproto/imap/capability.c (mu_imap_capability): Use mu_list_map to
create the capability list.
* libproto/imap/id.c (_id_convert): Likewise for ID pairs.
1 parent e4a76054
...@@ -44,6 +44,27 @@ capa_comp (const void *item, const void *value) ...@@ -44,6 +44,27 @@ capa_comp (const void *item, const void *value)
44 return !(*capa == 0 || *capa == '='); 44 return !(*capa == 0 || *capa == '=');
45 } 45 }
46 46
47 static int
48 _map_capa (void **itmv, size_t itmc, void *call_data)
49 {
50 int *n = call_data;
51 struct imap_list_element *elt = itmv[0];
52
53 if (elt->type != imap_eltype_string)
54 return MU_LIST_MAP_STOP;
55 if (*n == 0)
56 {
57 ++*n;
58 if (strcmp (elt->v.string, "CAPABILITY") == 0)
59 return MU_LIST_MAP_SKIP;
60 else
61 return MU_LIST_MAP_STOP;
62 }
63 itmv[0] = elt->v.string;
64 elt->v.string = NULL;
65 return MU_LIST_MAP_OK;
66 }
67
47 int 68 int
48 mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter) 69 mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter)
49 { 70 {
...@@ -100,28 +121,9 @@ mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter) ...@@ -100,28 +121,9 @@ mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter)
100 if (mu_list_get (imap->untagged_resp, 0, (void*)&elt) == 0) 121 if (mu_list_get (imap->untagged_resp, 0, (void*)&elt) == 0)
101 { 122 {
102 /* Top-level elements are always of imap_eltype_list type. */ 123 /* Top-level elements are always of imap_eltype_list type. */
103 mu_iterator_t itr; 124 int n = 0;
104 125 status = mu_list_map (elt->v.list, _map_capa, &n, 1,
105 mu_list_get_iterator (elt->v.list, &itr); 126 &imap->capa);
106 mu_iterator_first (itr);
107 if (mu_iterator_is_done (itr))
108 return MU_ERR_PARSE;
109 mu_iterator_current (itr, (void **) &elt);
110 if (elt->type == imap_eltype_string &&
111 strcmp (elt->v.string, "CAPABILITY") == 0)
112 {
113 for (mu_iterator_next (itr); !mu_iterator_is_done (itr);
114 mu_iterator_next (itr))
115 {
116 mu_iterator_current (itr, (void **) &elt);
117 if (elt->type == imap_eltype_string)
118 {
119 mu_list_append (imap->capa, elt->v.string);
120 elt->v.string = NULL;
121 }
122 }
123 }
124 mu_iterator_destroy (&itr);
125 } 127 }
126 if (piter) 128 if (piter)
127 status = mu_list_get_iterator (imap->capa, piter); 129 status = mu_list_get_iterator (imap->capa, piter);
......
...@@ -34,6 +34,20 @@ _id_free (void *data) ...@@ -34,6 +34,20 @@ _id_free (void *data)
34 free (s); 34 free (s);
35 } 35 }
36 36
37 static int
38 _id_mapper (void **itmv, size_t itmc, void *call_data)
39 {
40 int rc;
41 mu_assoc_t assoc = call_data;
42 struct imap_list_element *key = itmv[0], *val = itmv[1];
43 if (key->type != imap_eltype_string || val->type != imap_eltype_string)
44 return MU_ERR_FAILURE;
45 rc = mu_assoc_install (assoc, key->v.string, &val->v.string);
46 if (rc == 0)
47 val->v.string = NULL;
48 return rc;
49 }
50
37 struct id_convert_state 51 struct id_convert_state
38 { 52 {
39 int item; 53 int item;
...@@ -61,32 +75,7 @@ _id_convert (void *item, void *data) ...@@ -61,32 +75,7 @@ _id_convert (void *item, void *data)
61 75
62 case 1: 76 case 1:
63 if (elt->type == imap_eltype_list) 77 if (elt->type == imap_eltype_list)
64 { 78 mu_list_gmap (elt->v.list, _id_mapper, 2, stp->assoc);
65 mu_iterator_t itr;
66
67 mu_list_get_iterator (elt->v.list, &itr);
68 for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
69 mu_iterator_next (itr))
70 {
71 char *key, *val;
72 mu_iterator_current (itr, (void **) &elt);
73
74 if (elt->type != imap_eltype_string)
75 break;
76 key = elt->v.string;
77 elt->v.string = NULL;
78
79 mu_iterator_next (itr);
80 if (mu_iterator_is_done (itr))
81 break;
82 mu_iterator_current (itr, (void **) &elt);
83 if (elt->type != imap_eltype_string)
84 break;
85 val = elt->v.string;
86 elt->v.string = NULL;
87 mu_assoc_install (stp->assoc, key, &val);
88 }
89 }
90 } 79 }
91 return 1; 80 return 1;
92 } 81 }
......