summaryrefslogtreecommitdiff
path: root/imap4d/store.c
blob: c043a3ece828f0dd63bc72e88dbbcb4e1ff3ed29 (plain)
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
179
180
181
182
183
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2001, 2005, 2007, 2008, 2009, 2010 Free Software
   Foundation, Inc.

   GNU Mailutils 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 3, or (at your option)
   any later version.

   GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#include "imap4d.h"

enum value_type { STORE_SET, STORE_ADD, STORE_UNSET };

struct store_parse_closure
{
  enum value_type how;
  int ack;
  int type;
  int isuid;
  size_t *set;
  int count;
};
  
static int
store_thunk (imap4d_parsebuf_t p)
{
  struct store_parse_closure *pclos = imap4d_parsebuf_data (p);
  char *msgset;
  char *data;
  int status;
  
  msgset = imap4d_parsebuf_next (p, 1);
  data = imap4d_parsebuf_next (p, 1);

  if (*data == '+')
    {
      pclos->how = STORE_ADD;
      data++;
    }
  else if (*data == '-')
    {
      pclos->how = STORE_UNSET;
      data++;
    }
  else
    pclos->how = STORE_SET;
  
  if (mu_c_strcasecmp (data, "FLAGS"))
    imap4d_parsebuf_exit (p, "Bogus data item");
  data = imap4d_parsebuf_next (p, 1);

  if (*data == '.')
    {
      data = imap4d_parsebuf_next (p, 1);
      if (mu_c_strcasecmp (data, "SILENT") == 0)
	{
	  pclos->ack = 0;
	  imap4d_parsebuf_next (p, 1);
	}
      else
	imap4d_parsebuf_exit (p, "Bogus data suffix");
    }

  /* Get the message numbers in set[].  */
  status = util_msgset (msgset, &pclos->set, &pclos->count, pclos->isuid);
  switch (status)
    {
    case 0:
      break;

    case EINVAL:
      /* See RFC 3501, section 6.4.8, and a comment to the equivalent code
	 in fetch.c */
      p->err_text = "Completed";
      return RESP_OK;

    default:
      p->err_text = "Failed to parse message set";
      return RESP_NO;
    }      

  if (p->token[0] != '(')
    imap4d_parsebuf_exit (p, "Syntax error");
  imap4d_parsebuf_next (p, 1);
  
  do
    {
      int t;
      if (!util_attribute_to_type (p->token, &t))
	pclos->type |= t;
    }
  while (imap4d_parsebuf_next (p, 1) && p->token[0] != ')');
  return RESP_OK;
}

int
imap4d_store0 (imap4d_tokbuf_t tok, int isuid, char **ptext)
{
  int rc;
  struct store_parse_closure pclos;

  memset (&pclos, 0, sizeof pclos);
  pclos.ack = 1;
  pclos.isuid = isuid;
  
  rc = imap4d_with_parsebuf (tok,
			     IMAP4_ARG_1 + !!isuid,
			     ".",
			     store_thunk, &pclos,
			     ptext);
  if (rc == RESP_OK)
    {
      size_t i;
      
      for (i = 0; i < pclos.count; i++)
	{
	  mu_message_t msg = NULL;
	  mu_attribute_t attr = NULL;
	  size_t msgno = isuid ? uid_to_msgno (pclos.set[i]) : pclos.set[i];
      
	  if (msgno)
	    {
	      mu_mailbox_get_message (mbox, msgno, &msg);
	      mu_message_get_attribute (msg, &attr);
	      
	      switch (pclos.how)
		{
		case STORE_ADD:
		  mu_attribute_set_flags (attr, pclos.type);
		  break;
		  
		case STORE_UNSET:
		  mu_attribute_unset_flags (attr, pclos.type);
		  break;
      
		case STORE_SET:
		  mu_attribute_unset_flags (attr, 0xffffffff); /* FIXME */
		  mu_attribute_set_flags (attr, pclos.type);
		}
	    }
	  
	  if (pclos.ack)
	    {
	      util_send ("* %lu FETCH (", (unsigned long) msgno);
	      
	      if (isuid)
		util_send ("UID %lu ", (unsigned long) msgno);
	      util_send ("FLAGS (");
	      util_print_flags (attr);
	      util_send ("))\n");
	    }
	  /* Update the flags of uid table.  */
	  imap4d_sync_flags (pclos.set[i]);
	}

      *ptext = "Completed";
    }

  free (pclos.set);
  
  return rc;
}

int
imap4d_store (struct imap4d_command *command, imap4d_tokbuf_t tok)
{
  int rc;
  char *err_text;
  
  rc = imap4d_store0 (tok, 0, &err_text);
  return util_finish (command, rc, "%s", err_text);
}

Return to:

Send suggestions and report system problems to the System administrator.