Commit 5d2d4888 5d2d48884172f2d05844a8863ca12c1f47878468 by Sergey Poznyakoff

UID SEARCH support

1 parent f0e16cef
...@@ -157,6 +157,7 @@ struct parsebuf ...@@ -157,6 +157,7 @@ struct parsebuf
157 char tokbuf[MAXTOKEN+1]; /* Token buffer for short tokens */ 157 char tokbuf[MAXTOKEN+1]; /* Token buffer for short tokens */
158 158
159 char *arg; /* Rest of command line to be parsed */ 159 char *arg; /* Rest of command line to be parsed */
160 int isuid; /* UIDs instead of msgnos are required */
160 char *err_mesg; /* Error message if a parse error occured */ 161 char *err_mesg; /* Error message if a parse error occured */
161 struct mem_chain *alloc; /* Chain of objects allocated during parsing */ 162 struct mem_chain *alloc; /* Chain of objects allocated during parsing */
162 163
...@@ -190,49 +191,77 @@ static void do_search __P((struct parsebuf *pb)); ...@@ -190,49 +191,77 @@ static void do_search __P((struct parsebuf *pb));
190 int 191 int
191 imap4d_search (struct imap4d_command *command, char *arg) 192 imap4d_search (struct imap4d_command *command, char *arg)
192 { 193 {
193 char *sp ; 194 int rc;
194 char *str; 195 char buffer[64];
195 struct parsebuf parsebuf;
196 196
197 if (! (command->states & state)) 197 if (! (command->states & state))
198 return util_finish (command, RESP_BAD, "Wrong state"); 198 return util_finish (command, RESP_BAD, "Wrong state");
199 199
200 rc = imap4d_search0 (arg, 0, buffer, sizeof buffer);
201 return util_finish (command, rc, buffer);
202 }
203
204 int
205 imap4d_search0 (char *arg, int isuid, char *replybuf, size_t replysize)
206 {
207 char *sp ;
208 char *str;
209 struct parsebuf parsebuf;
210
200 memset (&parsebuf, 0, sizeof(parsebuf)); 211 memset (&parsebuf, 0, sizeof(parsebuf));
201 parsebuf.arg = arg; 212 parsebuf.arg = arg;
202 parsebuf.err_mesg = NULL; 213 parsebuf.err_mesg = NULL;
203 parsebuf.alloc = NULL; 214 parsebuf.alloc = NULL;
215 parsebuf.isuid = isuid;
204 216
205 if (!parse_gettoken (&parsebuf, 0)) 217 if (!parse_gettoken (&parsebuf, 0))
206 return util_finish (command, RESP_BAD, "Too few args"); 218 {
219 snprintf (replybuf, replysize, "Too few args");
220 return RESP_BAD;
221 }
207 222
208 if (strcasecmp (parsebuf.token, "CHARSET") == 0) 223 if (strcasecmp (parsebuf.token, "CHARSET") == 0)
209 { 224 {
210 if (!parse_gettoken (&parsebuf, 0)) 225 if (!parse_gettoken (&parsebuf, 0))
211 return util_finish (command, RESP_BAD, "Too few args"); 226 {
227 snprintf (replybuf, replysize, "Too few args");
228 return RESP_BAD;
229 }
212 230
213 /* Currently only ASCII is supported */ 231 /* Currently only ASCII is supported */
214 if (strcmp (parsebuf.token, "US-ASCII")) 232 if (strcmp (parsebuf.token, "US-ASCII"))
215 return util_finish (command, RESP_NO, "Charset not supported"); 233 {
234 snprintf (replybuf, replysize, "Charset not supported");
235 return RESP_NO;
236 }
216 237
217 if (!parse_gettoken (&parsebuf, 0)) 238 if (!parse_gettoken (&parsebuf, 0))
218 return util_finish (command, RESP_BAD, "Too few args"); 239 {
240 snprintf (replybuf, replysize, "Too few args");
241 return RESP_BAD;
242 }
243
219 } 244 }
220 245
221 /* Compile the expression */ 246 /* Compile the expression */
222 if (parse_search_key_list (&parsebuf)) 247 if (parse_search_key_list (&parsebuf))
223 { 248 {
224 parse_free_mem (&parsebuf); 249 parse_free_mem (&parsebuf);
225 return util_finish (command, RESP_BAD, "%s (near %s)", 250 snprintf (replybuf, replysize, "%s (near %s)",
226 parsebuf.err_mesg, 251 parsebuf.err_mesg,
227 *parsebuf.arg ? parsebuf.arg : "end"); 252 *parsebuf.arg ? parsebuf.arg : "end");
253 return RESP_BAD;
228 } 254 }
255
229 put_code (&parsebuf, NULL); 256 put_code (&parsebuf, NULL);
230 257
231 /* Execute compiled expression */ 258 /* Execute compiled expression */
232 do_search (&parsebuf); 259 do_search (&parsebuf);
233 260
234 parse_free_mem (&parsebuf); 261 parse_free_mem (&parsebuf);
235 return util_finish (command, RESP_OK, "Completed"); 262
263 snprintf (replybuf, replysize, "Completed");
264 return RESP_OK;
236 } 265 }
237 266
238 /* For each message from the mailbox execute the query from `pb' and 267 /* For each message from the mailbox execute the query from `pb' and
...@@ -251,7 +280,16 @@ do_search (struct parsebuf *pb) ...@@ -251,7 +280,16 @@ do_search (struct parsebuf *pb)
251 { 280 {
252 if (mailbox_get_message (mbox, pb->msgno, &pb->msg) == 0 281 if (mailbox_get_message (mbox, pb->msgno, &pb->msg) == 0
253 && search_run (pb)) 282 && search_run (pb))
283 {
284 if (pb->isuid)
285 {
286 size_t uid;
287 message_get_uid (pb->msg, &uid);
288 util_send (" %d", uid);
289 }
290 else
254 util_send (" %d", pb->msgno); 291 util_send (" %d", pb->msgno);
292 }
255 } 293 }
256 util_send ("\r\n"); 294 util_send ("\r\n");
257 } 295 }
......
...@@ -48,6 +48,10 @@ imap4d_uid (struct imap4d_command *command, char *arg) ...@@ -48,6 +48,10 @@ imap4d_uid (struct imap4d_command *command, char *arg)
48 { 48 {
49 rc = imap4d_store0 (sp, 1, buffer, sizeof buffer); 49 rc = imap4d_store0 (sp, 1, buffer, sizeof buffer);
50 } 50 }
51 else if (strcasecmp (cmd, "SEARCH") == 0)
52 {
53 rc = imap4d_search0 (sp, 1, buffer, sizeof buffer);
54 }
51 else 55 else
52 { 56 {
53 snprintf (buffer, sizeof buffer, "Error uknown uid command"); 57 snprintf (buffer, sizeof buffer, "Error uknown uid command");
......