summary refs log tree commit diff
path: root/imap.h
blob: c6d96f55921ab4090c53fde2ffc39de3c4261a73 (plain) (blame)
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
/* Copyright (C) 2020  C. McEnroe <june@causal.agency>
 *
 * 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 3 of the License, 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, see <https://www.gnu.org/licenses/>.
 *
 * Additional permission under GNU GPL version 3 section 7:
 *
 * If you modify this Program, or any covered work, by linking or
 * combining it with OpenSSL (or a modified version of that library),
 * containing parts covered by the terms of the OpenSSL License and the
 * original SSLeay license, the licensors of this Program grant you
 * additional permission to convey the resulting work. Corresponding
 * Source for a non-source form of such a combination shall include the
 * source code for the parts of OpenSSL used as well as that of the
 * covered work.
 */

#ifndef IMAP_H
#define IMAP_H

#include <err.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>

#define ENUM_ATOM \
	X(AtomNil, "NIL") \
	X(AtomUntagged, "*") \
	X(AtomContinue, "+") \
	X(AtomOk, "OK") \
	X(AtomNo, "NO") \
	X(AtomBad, "BAD") \
	X(AtomPreauth, "PREAUTH") \
	X(AtomBye, "BYE") \
	X(AtomSearch, "SEARCH") \
	X(AtomFetch, "FETCH") \
	X(AtomBody, "BODY") \
	X(AtomDot, ".") \
	X(AtomHeader, "HEADER") \
	X(AtomText, "TEXT")

enum Atom {
#define X(id, str) id,
	ENUM_ATOM
#undef X
	AtomCap = 1024,
};

extern const char *Atoms[AtomCap];

static inline enum Atom atomn(const char *str, size_t len) {
	enum Atom i;
	for (i = 0; i < AtomCap; ++i) {
		if (!Atoms[i]) break;
		if (strlen(Atoms[i]) != len) continue;
		if (!strncasecmp(Atoms[i], str, len)) return i;
	}
	if (i == AtomCap) errx(EX_SOFTWARE, "atom capacity exceeded");
	Atoms[i] = strndup(str, len);
	if (!Atoms[i]) err(EX_OSERR, "strndup");
	return i;
}

static inline enum Atom atom(const char *str) {
	return atomn(str, strlen(str));
}

struct Data {
	enum Type {
		Atom,
		Number,
		String,
		List,
	} type;
	union {
		enum Atom atom;
		uint32_t number;
		char *string;
		struct List {
			size_t cap;
			size_t len;
			struct Data *ptr;
		} list;
	};
};

static inline struct Data dataCheck(struct Data data, enum Type type) {
	const char *Types[] = { "atom", "number", "string", "list" };
	if (data.type != type) {
		errx(
			EX_PROTOCOL, "expected %s, found %s",
			Types[type], Types[data.type]
		);
	}
	return data;
}

static inline struct Data dataTake(struct Data *from) {
	struct Data take = *from;
	from->type = Atom;
	from->atom = AtomNil;
	return take;
}

static inline void listPush(struct List *list, struct Data data) {
	if (list->len == list->cap) {
		list->cap = (list->cap ? list->cap * 2 : 4);
		list->ptr = realloc(list->ptr, sizeof(*list->ptr) * list->cap);
		if (!list->ptr) err(EX_OSERR, "realloc");
	}
	list->ptr[list->len++] = data;
}

static inline void listFlatten(struct List *flat, struct List nested) {
	for (size_t i = 0; i < nested.len; ++i) {
		if (nested.ptr[i].type == List) {
			listFlatten(flat, nested.ptr[i].list);
		} else {
			listPush(flat, nested.ptr[i]);
		}
	}
}

static inline void dataFree(struct Data data);

static inline void listFree(struct List list) {
	for (size_t i = 0; i < list.len; ++i) {
		dataFree(list.ptr[i]);
	}
	free(list.ptr);
}

static inline void dataFree(struct Data data) {
	if (data.type == String) free(data.string);
	if (data.type == List) listFree(data.list);
}

struct Resp {
	enum Atom tag;
	uint32_t number;
	enum Atom resp;
	struct List code;
	struct List data;
	const char *text;
};

static inline void respFree(struct Resp resp) {
	listFree(resp.code);
	listFree(resp.data);
}

extern bool imapVerbose;

struct IMAP {
	int sock;
	FILE *r;
	FILE *w;
	size_t cap;
	char *buf;
	char *ptr;
};

struct IMAP imapOpen(const char *host, const char *port);
struct Resp imapResp(struct IMAP *imap);

#endif /* IMAP_H */