annotate src/imap/cmd-sort.c @ 924:4f697dde0fca HEAD

THREAD=REFERENCES implementation. Doesn't crash, but I'm not sure how correct replies it produces :)
author Timo Sirainen <tss@iki.fi>
date Wed, 08 Jan 2003 22:49:51 +0200
parents fd8888f6f037
children 8028c4dcf38f
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"
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "mail-search.h"
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "mail-sort.h"
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
34 buf = buffer_create_dynamic(data_stack_pool,
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
35 32 * sizeof(enum mail_sort_type),
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
36 (size_t)-1);
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 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
39 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
40
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 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
42 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
43 break;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 }
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 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
47 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
48 "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
49 return NULL;
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
52 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
53 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
54 args++;
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
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
57 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
58 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
59
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 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
61 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
62 "Invalid sort list argument.");
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 return NULL;
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
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
66 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
67 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
69 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
70 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 877
diff changeset
71 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
72 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
73 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
74 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
75 pool_t pool;
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 const char *error, *charset;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 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
79 if (args_count == -2)
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 return FALSE;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 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
83 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
84 "Missing or invalid arguments.");
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 return TRUE;
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
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 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
89 return TRUE;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 /* sort program */
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92 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
93 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
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
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
97 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
98 if (sorting == NULL)
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 args++;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
102 /* charset */
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103 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
104 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
105 "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
106 return TRUE;
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107 }
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
108 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
109 args++;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110
924
4f697dde0fca THREAD=REFERENCES implementation. Doesn't crash, but I'm not sure how
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
111 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
112
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 sargs = mail_search_args_build(pool, args, &error);
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 if (sargs == NULL) {
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 /* error in search arguments */
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 client_send_tagline(client, t_strconcat("NO ", error, NULL));
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 } else {
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 if (client->mailbox->search(client->mailbox, charset,
924
4f697dde0fca THREAD=REFERENCES implementation. Doesn't crash, but I'm not sure how
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
119 sargs, sorting, MAIL_THREAD_NONE,
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents: 761
diff changeset
120 client->output, client->cmd_uid)) {
761
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121 /* NOTE: syncing is allowed when returning UIDs */
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 if (client->cmd_uid)
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123 client_sync_full(client);
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124 else
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 client_sync_without_expunges(client);
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126 client_send_tagline(client, "OK Search completed.");
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 } else {
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128 client_send_storage_error(client);
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
129 }
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130 }
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 pool_unref(pool);
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 return TRUE;
d3bd41a56309 First implementation of SORT extension. String comparing still not up to
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 }