store.c
4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
* Now you're messing with a sumbitch
*/
static int get_attribute_type __P ((const char *, int *));
int
imap4d_store (struct imap4d_command *command, char *arg)
{
int rc;
char buffer[64];
if (! (command->states & state))
return util_finish (command, RESP_BAD, "Wrong state");
rc = imap4d_store0 (arg, 0, buffer, sizeof buffer);
return util_finish (command, rc, buffer);
}
int
imap4d_store0 (char *arg, int isuid, char *resp, size_t resplen)
{
char *msgset;
char *data;
char *sp = NULL;
int status;
int ack = 0;
size_t i, n = 0;
size_t *set = NULL;
enum value_type { STORE_SET, STORE_ADD, STORE_UNSET } how;
msgset = util_getword (arg, &sp);
data = util_getword (NULL, &sp);
if (!msgset || !data || !sp || *sp == '\0')
{
snprintf (resp, resplen, "Too few args");
return RESP_BAD;
}
/* The parsing of the data-item is a little slugish. */
if (strcasecmp (data, "FLAGS") == 0)
{
ack = 1;
how = STORE_SET;
}
else if (strcasecmp (data, "+FLAGS") == 0)
{
ack = 1;
how = STORE_ADD;
}
else if (strcasecmp (data, "-FLAGS") == 0)
{
ack = 1;
how = STORE_UNSET;
}
else if (strcasecmp (data, "FLAGS.SILENT") == 0)
{
ack = 0;
how = STORE_SET;
}
else if (strcasecmp (data, "+FLAGS.SILENT") == 0)
{
ack = 0;
how = STORE_ADD;
}
else if (strcasecmp (data, "-FLAGS.SILENT") == 0)
{
ack = 0;
how = STORE_UNSET;
}
else
{
snprintf (resp, resplen, "Bogus data item");
return RESP_BAD;
}
/* Get the message numbers in set[]. */
status = util_msgset (msgset, &set, &n, isuid);
if (status != 0)
{
snprintf (resp, resplen, "Bogus number set");
return RESP_BAD;
}
for (i = 0; i < n; i++)
{
message_t msg = NULL;
attribute_t attr = NULL;
char *items = strdup (sp); /* Don't use the orignal list. */
char *flags = strdup ("");
int first = 1;
size_t msgno;
char *p = items;
msgno = (isuid) ? uid_to_msgno (set[i]) : set[i];
if (msgno)
{
mailbox_get_message (mbox, msgno, &msg);
message_get_attribute (msg, &attr);
/* Get the fetch command names. */
while (*items && *items != ')')
{
int type = 0;
char item[64] = "";
util_token (item, sizeof (item), &items);
if (get_attribute_type (item, &type))
{
if (how == STORE_ADD )
attribute_set_flags (attr, type);
else if (how == STORE_UNSET )
attribute_unset_flags (attr, type);
else if (how == STORE_SET )
{
if (first)
{
attribute_set_flags (attr, 0);
first = 0;
}
attribute_set_flags (attr, type);
}
flags = realloc (flags, strlen (flags) + strlen (item) + 2);
if (*flags)
strcat (flags, " ");
strcat (flags, item);
}
}
}
if (ack && *flags)
util_out (RESP_NONE, "%d FETCH FLAGS (%s)", msgno, flags);
free (p);
free (flags);
/* Update the flags of uid table. */
imap4d_sync_flags (set[i]);
}
free (set);
snprintf (resp, resplen, "Completed");
return RESP_OK;
}
static int
get_attribute_type (const char *item, int *type)
{
if (strcasecmp (item, "\\Answered") == 0)
*type = MU_ATTRIBUTE_ANSWERED;
else if (strcasecmp (item, "\\Deleted") == 0)
*type = MU_ATTRIBUTE_DELETED;
else if (strcasecmp (item, "\\Draft") == 0)
*type = MU_ATTRIBUTE_DRAFT;
else if (strcasecmp (item, "\\Flagged") == 0)
*type = MU_ATTRIBUTE_FLAGGED;
else if (strcasecmp (item, "\\Recent") == 0)
*type = MU_ATTRIBUTE_RECENT;
else if (strcasecmp (item, "\\Seen") == 0)
*type = MU_ATTRIBUTE_SEEN;
else
return 0;
return 1;
}