summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ip/imap4d/store.c
blob: b13c3bb728df9c2778d4a9f37281456de79f1b3a (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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <auth.h>
#include "imap4d.h"

static NamedInt	flagMap[] =
{
	{"\\Seen",	MSeen},
	{"\\Answered",	MAnswered},
	{"\\Flagged",	MFlagged},
	{"\\Deleted",	MDeleted},
	{"\\Draft",	MDraft},
	{"\\Recent",	MRecent},
	{nil,		0}
};

int
storeMsg(Box *box, Msg *m, int uids, void *vst)
{
	Store *st;
	int f, flags;

	USED(uids);

	if(m->expunged)
		return uids;

	st = vst;
	flags = st->flags;

	f = m->flags;
	if(st->sign == '+')
		f |= flags;
	else if(st->sign == '-')
		f &= ~flags;
	else
		f = flags;

	/*
	 * not allowed to change the recent flag
	 */
	f = (f & ~MRecent) | (m->flags & MRecent);
	setFlags(box, m, f);

	if(st->op != STFlagsSilent){
		m->sendFlags = 1;
		box->sendFlags = 1;
	}

	return 1;
}

/*
 * update flags & global flag counts in box
 */
void
setFlags(Box *box, Msg *m, int f)
{
	if(f == m->flags)
		return;

	box->dirtyImp = 1;
	if((f & MRecent) != (m->flags & MRecent)){
		if(f & MRecent)
			box->recent++;
		else
			box->recent--;
	}
	m->flags = f;
}

void
sendFlags(Box *box, int uids)
{
	Msg *m;

	if(!box->sendFlags)
		return;

	box->sendFlags = 0;
	for(m = box->msgs; m != nil; m = m->next){
		if(!m->expunged && m->sendFlags){
			Bprint(&bout, "* %lud FETCH (", m->seq);
			if(uids)
				Bprint(&bout, "uid %lud ", m->uid);
			Bprint(&bout, "FLAGS (");
			writeFlags(&bout, m, 1);
			Bprint(&bout, "))\r\n");
			m->sendFlags = 0;
		}
	}
}

void
writeFlags(Biobuf *b, Msg *m, int recentOk)
{
	char *sep;
	int f;

	sep = "";
	for(f = 0; flagMap[f].name != nil; f++){
		if((m->flags & flagMap[f].v)
		&& (flagMap[f].v != MRecent || recentOk)){
			Bprint(b, "%s%s", sep, flagMap[f].name);
			sep = " ";
		}
	}
}

int
msgSeen(Box *box, Msg *m)
{
	if(m->flags & MSeen)
		return 0;
	m->flags |= MSeen;
	box->sendFlags = 1;
	m->sendFlags = 1;
	box->dirtyImp = 1;
	return 1;
}

ulong
mapFlag(char *name)
{
	return mapInt(flagMap, name);
}