annotate src/imap/cmd-sort.c @ 2708:f1e9f3ec8135 HEAD

Buffer API change: we no longer support limited sized buffers where writes past limit wouldn't kill the process. They weren't used hardly anywhere, they could have hidden bugs and the code for handling them was too complex. This also changed base64 and hex-binary APIs.
author Timo Sirainen <tss@iki.fi>
date Fri, 08 Oct 2004 20:51:47 +0300
parents e1616067df5c
children 61abed5f7864
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /* Copyright (C) 2002 Timo Sirainen */
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "common.h"
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents: 764
diff changeset
4 #include "buffer.h"
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "commands.h"
988
8028c4dcf38f mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents: 924
diff changeset
6 #include "imap-search.h"
8028c4dcf38f mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents: 924
diff changeset
7 #include "imap-sort.h"
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
9 struct sort_name {
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
10 enum mail_sort_type type;
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 const char *name;
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
12 };
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
14 static struct sort_name sort_names[] = {
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 { MAIL_SORT_ARRIVAL, "arrival" },
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 { MAIL_SORT_CC, "cc" },
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 { MAIL_SORT_DATE, "date" },
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 { MAIL_SORT_FROM, "from" },
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 { MAIL_SORT_SIZE, "size" },
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 { MAIL_SORT_SUBJECT, "subject" },
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21 { MAIL_SORT_TO, "to" },
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 { MAIL_SORT_REVERSE, "reverse" },
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 { MAIL_SORT_END, NULL }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 };
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
27 static enum mail_sort_type *
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
28 get_sort_program(struct client *client, struct imap_arg *args)
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
30 enum mail_sort_type type;
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
31 buffer_t *buf;
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 int i;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33
1012
b2d9219c60af Give nice error message for "SORT ()"
Timo Sirainen <tss@iki.fi>
parents: 988
diff changeset
34 if (args->type == IMAP_ARG_EOL) {
b2d9219c60af Give nice error message for "SORT ()"
Timo Sirainen <tss@iki.fi>
parents: 988
diff changeset
35 /* empyty list */
b2d9219c60af Give nice error message for "SORT ()"
Timo Sirainen <tss@iki.fi>
parents: 988
diff changeset
36 client_send_command_error(client, "Empty sort program.");
b2d9219c60af Give nice error message for "SORT ()"
Timo Sirainen <tss@iki.fi>
parents: 988
diff changeset
37 return NULL;
b2d9219c60af Give nice error message for "SORT ()"
Timo Sirainen <tss@iki.fi>
parents: 988
diff changeset
38 }
b2d9219c60af Give nice error message for "SORT ()"
Timo Sirainen <tss@iki.fi>
parents: 988
diff changeset
39
1782
2f3d906d99d8 data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents: 1654
diff changeset
40 buf = buffer_create_dynamic(pool_datastack_create(),
2708
f1e9f3ec8135 Buffer API change: we no longer support limited sized buffers where
Timo Sirainen <tss@iki.fi>
parents: 2427
diff changeset
41 32 * sizeof(enum mail_sort_type));
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 while (args->type == IMAP_ARG_ATOM || args->type == IMAP_ARG_STRING) {
877
7935347f54f1 Don't access ImapArg's union members directly - too easy to mess up. Fixes a
Timo Sirainen <tss@iki.fi>
parents: 825
diff changeset
44 const char *arg = IMAP_ARG_STR(args);
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46 for (i = 0; sort_names[i].type != MAIL_SORT_END; i++) {
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 if (strcasecmp(arg, sort_names[i].name) == 0)
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 break;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 if (sort_names[i].type == MAIL_SORT_END) {
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 client_send_command_error(client, t_strconcat(
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 "Unknown sort argument: ", arg, NULL));
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 return NULL;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
57 buffer_append(buf, &sort_names[i].type,
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
58 sizeof(enum mail_sort_type));
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 args++;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents: 764
diff changeset
62 type = MAIL_SORT_END;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents: 764
diff changeset
63 buffer_append(buf, &type, sizeof(type));
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 if (args->type != IMAP_ARG_EOL) {
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 client_send_command_error(client,
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 "Invalid sort list argument.");
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 return NULL;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents: 764
diff changeset
71 return buffer_free_without_data(buf);
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
74 int cmd_sort(struct client *client)
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
76 struct mail_search_arg *sargs;
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
77 enum mail_sort_type *sorting;
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
78 struct imap_arg *args;
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 int args_count;
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
80 pool_t pool;
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 const char *error, *charset;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 args_count = imap_parser_read_args(client->parser, 0, 0, &args);
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 if (args_count == -2)
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 return FALSE;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 if (args_count < 3) {
768
8b3518bb327e Limited max. command argument elements to 128. Added more verbose error
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
88 client_send_command_error(client, args_count < 0 ? NULL :
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89 "Missing or invalid arguments.");
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 return TRUE;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93 if (!client_verify_open_mailbox(client))
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94 return TRUE;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 /* sort program */
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97 if (args->type != IMAP_ARG_LIST) {
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 client_send_command_error(client, "Invalid sort argument.");
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99 return TRUE;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101
877
7935347f54f1 Don't access ImapArg's union members directly - too easy to mess up. Fixes a
Timo Sirainen <tss@iki.fi>
parents: 825
diff changeset
102 sorting = get_sort_program(client, IMAP_ARG_LIST(args)->args);
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103 if (sorting == NULL)
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104 return TRUE;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 args++;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107 /* charset */
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 if (args->type != IMAP_ARG_ATOM && args->type != IMAP_ARG_STRING) {
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 client_send_command_error(client,
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 "Invalid charset argument.");
924
4f697dde0fca THREAD=REFERENCES implementation. Doesn't crash, but I'm not sure how
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
111 return TRUE;
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 }
877
7935347f54f1 Don't access ImapArg's union members directly - too easy to mess up. Fixes a
Timo Sirainen <tss@iki.fi>
parents: 825
diff changeset
113 charset = IMAP_ARG_STR(args);
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 args++;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115
924
4f697dde0fca THREAD=REFERENCES implementation. Doesn't crash, but I'm not sure how
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
116 pool = pool_alloconly_create("mail_search_args", 2048);
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
118 sargs = imap_search_args_build(pool, client->mailbox, args, &error);
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 if (sargs == NULL) {
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 /* error in search arguments */
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121 client_send_tagline(client, t_strconcat("NO ", error, NULL));
2023
1659cfc67eaf return value fixes
Timo Sirainen <tss@iki.fi>
parents: 1915
diff changeset
122 } else if (imap_sort(client, charset, sargs, sorting) == 0) {
2427
e1616067df5c Syncing works now too without buffering everything. Also fixed handling
Timo Sirainen <tss@iki.fi>
parents: 2023
diff changeset
123 pool_unref(pool);
e1616067df5c Syncing works now too without buffering everything. Also fixed handling
Timo Sirainen <tss@iki.fi>
parents: 2023
diff changeset
124 return cmd_sync(client, MAILBOX_SYNC_FLAG_FAST |
e1616067df5c Syncing works now too without buffering everything. Also fixed handling
Timo Sirainen <tss@iki.fi>
parents: 2023
diff changeset
125 (client->cmd_uid ?
e1616067df5c Syncing works now too without buffering everything. Also fixed handling
Timo Sirainen <tss@iki.fi>
parents: 2023
diff changeset
126 0 : MAILBOX_SYNC_FLAG_NO_EXPUNGES),
e1616067df5c Syncing works now too without buffering everything. Also fixed handling
Timo Sirainen <tss@iki.fi>
parents: 2023
diff changeset
127 "OK Sort completed.");
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128 } else {
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
129 client_send_storage_error(client,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
130 mailbox_get_storage(client->mailbox));
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 pool_unref(pool);
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 return TRUE;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135 }