summaryrefslogtreecommitdiffstats
path: root/imap/storeinfo.c
blob: cc31c311ea1826bdea182fca6f5a8469fd357880 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
** Copyright 1998 - 2003 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#if     HAVE_UNISTD_H
#include        <unistd.h>
#endif
#include	<errno.h>
#include	"imaptoken.h"
#include	"imapscanclient.h"
#include	"imapwrite.h"
#include	"storeinfo.h"
#include	"maildir/maildirquota.h"
#include	"maildir/maildirmisc.h"
#include	"maildir/maildircreate.h"
#include	"maildir/maildiraclt.h"
#include	"outbox.h"

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<ctype.h>
#include	<fcntl.h>
#include	<sys/stat.h>
#if	HAVE_UTIME_H
#include	<utime.h>
#endif
#if TIME_WITH_SYS_TIME
#include	<sys/time.h>
#include	<time.h>
#else
#if HAVE_SYS_TIME_H
#include	<sys/time.h>
#else
#include	<time.h>
#endif
#endif


#if SMAP
extern int smapflag;
#endif

extern char *get_reflagged_filename(const char *fn, struct imapflags *newfl);
extern int is_trash(const char *);
extern int get_flagname(const char *, struct imapflags *);
extern int get_flagsAndKeywords(struct imapflags *flags,
				struct libmail_kwMessage **kwPtr);
extern void get_message_flags( struct imapscanmessageinfo *,
			       char *, struct imapflags *);
extern int reflag_filename(struct imapscanmessageinfo *, struct imapflags *,
	int);
extern void fetchflags(unsigned long);
extern void fetchflags_byuid(unsigned long);
extern FILE *maildir_mkfilename(const char *, struct imapflags *,
				unsigned long, char **, char **);
extern int acl_flags_adjust(const char *access_rights,
			    struct imapflags *flags);

extern struct imapscaninfo current_maildir_info;
extern char *current_mailbox;
extern char *current_mailbox_acl;
extern int fastkeywords();

int storeinfo_init(struct storeinfo *si)
{
struct imaptoken *t=currenttoken();
const char *p;

	if (t->tokentype != IT_ATOM)	return (-1);
	si->plusminus=0;
	si->silent=0;

	p=t->tokenbuf;
	if (*p == '+' || *p == '-')
		si->plusminus= *p++;
	if (strncmp(p, "FLAGS", 5))	return (-1);
	p += 5;
	if (*p)
	{
		if (strcmp(p, ".SILENT"))	return (-1);
		si->silent=1;
	}

	memset(&si->flags, 0, sizeof(si->flags));

	if ((si->keywords=libmail_kwmCreate()) == NULL)
		write_error_exit(0);

	t=nexttoken_noparseliteral();
	if (t->tokentype == IT_LPAREN)
	{
		if (get_flagsAndKeywords(&si->flags, &si->keywords))
		{
			libmail_kwmDestroy(si->keywords);
			si->keywords=NULL;
			return (-1);
		}
		nexttoken();
	}
	else if (t->tokentype == IT_NIL)
		nexttoken();
	else if (t->tokentype == IT_ATOM)
	{
		if (get_flagname(t->tokenbuf, &si->flags))
			libmail_kwmSetName(current_maildir_info
				       .keywordList,
				       si->keywords,
				       t->tokenbuf);
		nexttoken();
	}
	return (0);
}

int do_store(unsigned long n, int byuid, void *voidptr)
{
struct storeinfo *si=(struct storeinfo *)voidptr;
int	fd;
 struct imapflags new_flags, old_flags;
int changedKeywords;
struct libmail_kwMessageEntry *kme;
int kwAllowed=1;

	--n;
	fd=imapscan_openfile(current_mailbox, &current_maildir_info, n);
	if (fd < 0)	return (0);

	changedKeywords=0;
	get_message_flags(current_maildir_info.msgs+n, 0, &new_flags);

	old_flags=new_flags;

	if (current_mailbox_acl)
	{
		if (strchr(current_mailbox_acl, ACL_WRITE[0]) == NULL)
			kwAllowed=0;
	}


	if (si->plusminus == '+')
	{
		if (si->flags.drafts)	new_flags.drafts=1;
		if (si->flags.seen)	new_flags.seen=1;
		if (si->flags.answered)	new_flags.answered=1;
		if (si->flags.deleted)	new_flags.deleted=1;
		if (si->flags.flagged)	new_flags.flagged=1;

		for (kme=si->keywords ? si->keywords->firstEntry:NULL;
		     kme; kme=kme->next)
		{
			int rc;

			if (!kwAllowed)
			{
				current_maildir_info.msgs[n].changedflags=1;
				continue;
			}

			imapscan_createKeyword(&current_maildir_info, n);

			if ((rc=libmail_kwmSet(current_maildir_info.msgs[n]
					       .keywordMsg,
					       kme->libmail_keywordEntryPtr))
			    < 0)
			{
				write_error_exit(0);
				return 0;
			}

			if (rc == 0)
			{
				if (fastkeywords())
					changedKeywords=1;
				current_maildir_info.msgs[n].changedflags=1;
			}
		}
	}
	else if (si->plusminus == '-')
	{
		if (si->flags.drafts)	new_flags.drafts=0;
		if (si->flags.seen)	new_flags.seen=0;
		if (si->flags.answered)	new_flags.answered=0;
		if (si->flags.deleted)	new_flags.deleted=0;
		if (si->flags.flagged)	new_flags.flagged=0;

		if (current_maildir_info.msgs[n].keywordMsg && kwAllowed)
			for (kme=si->keywords ?
				     si->keywords->firstEntry:NULL;
			     kme; kme=kme->next)
			{
				if (!kwAllowed)
				{
					current_maildir_info.msgs[n]
						.changedflags=1;
					continue;
				}

				if (libmail_kwmClear(current_maildir_info.msgs[n]
						 .keywordMsg,
						 kme->libmail_keywordEntryPtr)==0)
				{
					if (fastkeywords())
						changedKeywords=1;
					current_maildir_info.msgs[n]
						.changedflags=1;
				}
			}

		if (current_maildir_info.msgs[n].keywordMsg &&
		    !current_maildir_info.msgs[n].keywordMsg->firstEntry)
		{
			libmail_kwmDestroy(current_maildir_info.msgs[n]
					      .keywordMsg);
			current_maildir_info.msgs[n].keywordMsg=NULL;
		}
	}
	else
	{
		struct libmail_kwMessage *kw;
		struct libmail_kwMessage *si_kw;

		new_flags=si->flags;

		kw=current_maildir_info.msgs[n].keywordMsg;

		if (kw && kw->firstEntry == NULL)
			kw=NULL;

		si_kw=si->keywords;

		if (si_kw && si_kw->firstEntry == NULL)
		{
			si_kw=NULL;
		}

		if ((si_kw && !kw) ||
		    (!si_kw && kw) ||
		    (si_kw && kw && libmail_kwmCmp(si_kw, kw)))
		{
			if (kwAllowed)
			{
				kw=current_maildir_info.msgs[n].keywordMsg;

				if (kw)
					libmail_kwmDestroy(kw);

				current_maildir_info.msgs[n].keywordMsg=NULL;

				if (si->keywords && si->keywords->firstEntry)
				{
					struct libmail_kwMessageEntry *kme;

					kw=imapscan_createKeyword(&current_maildir_info,
								  n);

					for (kme=si->keywords->lastEntry; kme;
					     kme=kme->prev)
						if (libmail_kwmSet(kw,
								   kme->libmail_keywordEntryPtr)
						    < 0)
							write_error_exit(0);
					current_maildir_info.msgs[n].keywordMsg=kw;
				}
			}

			changedKeywords=1;
		}
	}

	if (current_mailbox_acl)
	{
		if (strchr(current_mailbox_acl, ACL_WRITE[0]) == NULL)
		{
			new_flags.drafts=old_flags.drafts;
			new_flags.answered=old_flags.answered;
			new_flags.flagged=old_flags.flagged;
		}

		if (strchr(current_mailbox_acl, ACL_SEEN[0]) == NULL)
		{
			new_flags.seen=old_flags.seen;
		}

		if (strchr(current_mailbox_acl, ACL_DELETEMSGS[0])
		    == NULL)
		{
			new_flags.deleted=old_flags.deleted;
		}
	}

	if (changedKeywords)
	{
		current_maildir_info.msgs[n].changedflags=1;
		if (imapscan_updateKeywords(current_maildir_info.msgs[n]
					    .filename,
					    current_maildir_info.msgs[n]
					    .keywordMsg))
		{
			close(fd);
			return -1;
		}
	}

	if (reflag_filename(current_maildir_info.msgs+n, &new_flags, fd))
	{
		close(fd);
		return (-1);
	}
	close(fd);
	if (si->silent)
		current_maildir_info.msgs[n].changedflags=0;
	else
	{
#if SMAP
		/* SMAP flag notification is handled elsewhere */

		if (!smapflag)
#endif
		{
			if (byuid)
				fetchflags_byuid(n);
			else
				fetchflags(n);
		}
	}

	return (0);
}

static int copy_message(int fd,
			struct do_copy_info *cpy_info,
			struct	imapflags *flags,
			struct libmail_kwMessage *keywords,
			unsigned long old_uid)
{
char	*tmpname;
char	*newname;
FILE	*fp;
struct	stat	stat_buf;
char	buf[BUFSIZ];
struct uidplus_info *new_uidplus_info;

	if (fstat(fd, &stat_buf) < 0
	    || (new_uidplus_info=(struct uidplus_info *)
		malloc(sizeof(struct uidplus_info))) == NULL)
	{
		return (-1);
	}
	memset(new_uidplus_info, 0, sizeof(*new_uidplus_info));

	fp=maildir_mkfilename(cpy_info->mailbox, flags, stat_buf.st_size,
			      &tmpname, &newname);

	if (!fp)
	{
		free(new_uidplus_info);
		return (-1);
	}

	while (stat_buf.st_size)
	{
	int	n=sizeof(buf);

		if (n > stat_buf.st_size)
			n=stat_buf.st_size;

		n=read(fd, buf, n);

		if (n <= 0 || fwrite(buf, 1, n, fp) != n)
		{
			fprintf(stderr,
			"ERR: error copying a message, user=%s, errno=%d\n",
				getenv("AUTHENTICATED"), errno);

			fclose(fp);
			unlink(tmpname);
			free(tmpname);
			free(newname);
			free(new_uidplus_info);
			return (-1);
		}
		stat_buf.st_size -= n;
	}

	if (fflush(fp) || ferror(fp))
	{
		fclose(fp);
		free(tmpname);
		free(newname);
		free(new_uidplus_info);
		return (-1);
	}
	fclose(fp);

	new_uidplus_info->mtime = stat_buf.st_mtime;

	if (check_outbox(tmpname, cpy_info->mailbox))
	{
		unlink(tmpname);
		free(tmpname);
		free(newname);
		free(new_uidplus_info);
		return (-1);
	}

	if (keywords && keywords->firstEntry &&
	    maildir_kwSave(cpy_info->mailbox,
			   strrchr(newname, '/')+1,
			   keywords,
			   &new_uidplus_info->tmpkeywords,
			   &new_uidplus_info->newkeywords, 0))
	{
		unlink(tmpname);
		free(tmpname);
		free(newname);
		free(new_uidplus_info);
		perror("maildir_kwSave");
		return (-1);
	}

	new_uidplus_info->tmpfilename=tmpname;
	new_uidplus_info->curfilename=newname;
	new_uidplus_info->next=NULL;
	new_uidplus_info->old_uid=old_uid;
	*cpy_info->uidplus_tail=new_uidplus_info;
	cpy_info->uidplus_tail=&new_uidplus_info->next;
	return (0);
}

int do_copy_message(unsigned long n, int byuid, void *voidptr)
{
	struct do_copy_info *cpy_info=(struct do_copy_info *)voidptr;
	int	fd;
	struct imapflags new_flags;

	--n;
	fd=imapscan_openfile(current_mailbox, &current_maildir_info, n);
	if (fd < 0)	return (0);
	get_message_flags(current_maildir_info.msgs+n, 0, &new_flags);

	if (copy_message(fd, cpy_info, &new_flags,

			 acl_flags_adjust(cpy_info->acls,
					  &new_flags) ? NULL
			 : current_maildir_info.msgs[n].keywordMsg,
			 current_maildir_info.msgs[n].uid))
	{
		close(fd);
		return (-1);
	}
	close(fd);
	current_maildir_info.msgs[n].copiedflag=1;
	return (0);
}

int do_copy_quota_calc(unsigned long n, int byuid, void *voidptr)
{
struct copyquotainfo *info=(struct copyquotainfo *)voidptr;
const char *filename;
unsigned long nbytes;
struct	stat	stat_buf;
int	fd;
struct imapflags flags;
char *ff;

	--n;

	fd=imapscan_openfile(current_mailbox, &current_maildir_info, n);
	if (fd < 0)	return (0);
	filename=current_maildir_info.msgs[n].filename;

	get_message_flags(&current_maildir_info.msgs[n], NULL, &flags);

	(void)acl_flags_adjust(info->acls, &flags);

	ff=get_reflagged_filename(filename, &flags);

	if (maildirquota_countfile(ff))
	{
		if (maildir_parsequota(ff, &nbytes))
		{
			if (fstat(fd, &stat_buf) < 0)
			{
				close(fd);
				free(ff);
				return (0);
			}
			nbytes=stat_buf.st_size;
		}
		info->nbytes += nbytes;
		info->nfiles += 1;
	}
	close(fd);
	free(ff);
	return (0);
}