xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Commands.h (revision 90c3b9bf9fe633e4c6a59d42c11c3f523183c553)
1 /*
2  * Copyright 2010-2013, Haiku Inc. All Rights Reserved.
3  * Copyright 2010 Clemens Zeidler. All rights reserved.
4  *
5  * Distributed under the terms of the MIT License.
6  */
7 #ifndef COMMANDS_H
8 #define COMMANDS_H
9 
10 
11 #include <vector>
12 
13 #include "Response.h"
14 
15 
16 typedef std::vector<BString> StringList;
17 
18 
19 namespace IMAP {
20 
21 
22 struct MessageEntry {
23 	MessageEntry()
24 		:
25 		uid(0),
26 		flags(0)
27 	{
28 	}
29 
30 	uint32	uid;
31 	uint32	flags;
32 	uint32	size;
33 };
34 typedef std::vector<MessageEntry> MessageEntryList;
35 
36 typedef std::vector<uint32> MessageUIDList;
37 
38 enum MessageFlags {
39 	kSeen				= 0x01,
40 	kAnswered			= 0x02,
41 	kFlagged			= 0x04,
42 	kDeleted			= 0x08,
43 	kDraft				= 0x10,
44 	// \Recent doesn't really have any useful meaning, so we just ignore it
45 
46 	kServerFlagsMask	= 0x0000ffff
47 };
48 
49 
50 class Handler {
51 public:
52 								Handler();
53 	virtual						~Handler();
54 
55 	virtual	bool				HandleUntagged(Response& response) = 0;
56 };
57 
58 
59 class Command {
60 public:
61 	virtual						~Command();
62 
63 	virtual	BString				CommandString() = 0;
64 	virtual	status_t			HandleTagged(Response& response);
65 };
66 
67 
68 class RawCommand : public Command {
69 public:
70 								RawCommand(const BString& command);
71 
72 	virtual	BString				CommandString();
73 
74 private:
75 			BString				fCommand;
76 };
77 
78 
79 class LoginCommand : public Command, public Handler {
80 public:
81 								LoginCommand(const char* user,
82 									const char* password);
83 
84 	virtual	BString				CommandString();
85 	virtual	bool				HandleUntagged(Response& response);
86 
87 			const ArgumentList&	Capabilities() const { return fCapabilities; }
88 
89 private:
90 			const char*			fUser;
91 			const char*			fPassword;
92 			ArgumentList		fCapabilities;
93 };
94 
95 
96 class SelectCommand : public Command, public Handler {
97 public:
98 								SelectCommand();
99 								SelectCommand(const char* mailboxName);
100 
101 	virtual	BString				CommandString();
102 	virtual	bool				HandleUntagged(Response& response);
103 
104 			void				SetTo(const char* mailboxName);
105 			uint32				NextUID() { return fNextUID; }
106 			uint32				UIDValidity() { return fUIDValidity; }
107 
108 private:
109 			BString				fMailboxName;
110 			uint32				fNextUID;
111 			uint32				fUIDValidity;
112 };
113 
114 
115 class CapabilityHandler : public Command, public Handler {
116 public:
117 	virtual	BString				CommandString();
118 	virtual	bool				HandleUntagged(Response& response);
119 
120 			const ArgumentList&	Capabilities() const { return fCapabilities; }
121 
122 private:
123 			ArgumentList		fCapabilities;
124 };
125 
126 
127 class FetchMessageEntriesCommand : public Command, public Handler {
128 public:
129 								FetchMessageEntriesCommand(
130 									MessageEntryList& entries, uint32 from,
131 									uint32 to, bool uids);
132 
133 			BString				CommandString();
134 	virtual	bool				HandleUntagged(Response& response);
135 
136 private:
137 			MessageEntryList&	fEntries;
138 			uint32				fFrom;
139 			uint32				fTo;
140 			bool				fUIDs;
141 };
142 
143 
144 enum FetchFlags {
145 	kFetchHeader	= 0x01,
146 	kFetchBody		= 0x02,
147 	kFetchAll		= kFetchHeader | kFetchBody,
148 	kFetchFlags		= 0x04,
149 };
150 
151 
152 class FetchListener {
153 public:
154 	virtual	bool				FetchData(uint32 fetchFlags, BDataIO& stream,
155 									size_t& length) = 0;
156 	virtual void				FetchedData(uint32 fetchFlags, uint32 uid,
157 									uint32 flags) = 0;
158 };
159 
160 
161 class FetchCommand : public Command, public Handler,
162 	public LiteralHandler {
163 public:
164 								FetchCommand(uint32 from, uint32 to,
165 									uint32 fetchFlags);
166 								FetchCommand(MessageUIDList& uids,
167 									size_t max, uint32 fetchFlags);
168 
169 			void				SetListener(FetchListener* listener);
170 			FetchListener*		Listener() const { return fListener; }
171 
172 	virtual	BString				CommandString();
173 	virtual	bool				HandleUntagged(Response& response);
174 	virtual bool				HandleLiteral(Response& response,
175 									ArgumentList& arguments, BDataIO& stream,
176 									size_t& length);
177 
178 private:
179 			BString				fSequence;
180 			uint32				fFlags;
181 			FetchListener*		fListener;
182 };
183 
184 
185 class SetFlagsCommand : public Command, public Handler {
186 public:
187 								SetFlagsCommand(uint32 uid, uint32 flags);
188 
189 	virtual	BString				CommandString();
190 	virtual	bool				HandleUntagged(Response& response);
191 
192 private:
193 			uint32				fUID;
194 			uint32				fFlags;
195 };
196 
197 
198 #if 0
199 class AppendCommand : public IMAPMailboxCommand {
200 public:
201 								AppendCommand(IMAPMailbox& mailbox,
202 									BPositionIO& message, off_t size,
203 									int32 flags, time_t time);
204 
205 			BString				CommandString();
206 			bool				HandleUntagged(const BString& response);
207 
208 private:
209 			BPositionIO&		fMessageData;
210 			off_t				fDataSize;
211 			int32				fFlags;
212 			time_t				fTime;
213 };
214 #endif
215 
216 
217 class ExistsListener {
218 public:
219 	virtual	void				MessageExistsReceived(uint32 count) = 0;
220 };
221 
222 
223 class ExistsHandler : public Handler {
224 public:
225 								ExistsHandler();
226 
227 			void				SetListener(ExistsListener* listener);
228 			ExistsListener*		Listener() const { return fListener; }
229 
230 	virtual	bool				HandleUntagged(Response& response);
231 
232 private:
233 			ExistsListener*		fListener;
234 };
235 
236 
237 /*! Just send a expunge command to delete kDeleted flagged messages. The
238 	response is handled by the unsolicited ExpungeHandler which is installed
239 	all the time.
240 */
241 class ExpungeCommand : public Command {
242 public:
243 								ExpungeCommand();
244 
245 			BString				CommandString();
246 };
247 
248 
249 class ExpungeListener {
250 public:
251 	virtual	void				MessageExpungeReceived(uint32 index) = 0;
252 };
253 
254 
255 class ExpungeHandler : public Handler {
256 public:
257 								ExpungeHandler();
258 
259 			void				SetListener(ExpungeListener* listener);
260 			ExpungeListener*	Listener() const { return fListener; }
261 
262 	virtual	bool				HandleUntagged(Response& response);
263 
264 private:
265 			ExpungeListener*	fListener;
266 };
267 
268 
269 #if 0
270 class FlagsHandler : public Handler {
271 public:
272 								FlagsHandler(IMAPMailbox& mailbox);
273 
274 			bool				HandleUntagged(const BString& response);
275 };
276 #endif
277 
278 
279 class ListCommand : public Command, public Handler {
280 public:
281 								ListCommand(const char* prefix,
282 									bool subscribedOnly);
283 
284 	virtual	BString				CommandString();
285 	virtual	bool				HandleUntagged(Response& response);
286 
287 	const	StringList&			FolderList();
288 			const BString&		Separator() { return fSeparator; }
289 
290 private:
291 			const char*			_Command() const;
292 
293 private:
294 			RFC3501Encoding		fEncoding;
295 			const char*			fPrefix;
296 			StringList			fFolders;
297 			BString				fSeparator;
298 			bool				fSubscribedOnly;
299 };
300 
301 
302 class SubscribeCommand : public Command {
303 public:
304 								SubscribeCommand(const char* mailboxName);
305 
306 			BString				CommandString();
307 
308 private:
309 			BString				fMailboxName;
310 };
311 
312 
313 class UnsubscribeCommand : public Command {
314 public:
315 								UnsubscribeCommand(const char* mailboxName);
316 
317 			BString				CommandString();
318 
319 private:
320 			BString				fMailboxName;
321 };
322 
323 
324 class GetQuotaCommand : public Command, public Handler {
325 public:
326 								GetQuotaCommand(
327 									const char* mailboxName = "INBOX");
328 
329 			BString				CommandString();
330 			bool				HandleUntagged(Response& response);
331 
332 			uint64				UsedStorage();
333 			uint64				TotalStorage();
334 private:
335 			BString				fMailboxName;
336 
337 			uint64				fUsedStorage;
338 			uint64				fTotalStorage;
339 };
340 
341 
342 }	// namespace IMAP
343 
344 
345 #endif // COMMANDS_H
346