xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Protocol.h (revision 7993ddfabaa0069192062289b4fb18c0034cdf76)
1 /*
2  * Copyright 2001-2013, Haiku Inc. All Rights Reserved.
3  * Copyright 2001-2002 Dr. Zoidberg Enterprises. All rights reserved.
4  * Copyright 2010 Clemens Zeidler. All rights reserved.
5  *
6  * Distributed under the terms of the MIT License.
7  */
8 #ifndef PROTOCOL_H
9 #define PROTOCOL_H
10 
11 
12 #include <map>
13 
14 #include <BufferedDataIO.h>
15 #include <ObjectList.h>
16 #include <OS.h>
17 #include <SecureSocket.h>
18 #include <String.h>
19 
20 #include "Commands.h"
21 
22 
23 #define xEOF    236
24 const bigtime_t kIMAP4ClientTimeout = 1000000 * 60; // 60 sec
25 
26 
27 namespace IMAP {
28 
29 
30 class Command;
31 class Handler;
32 
33 
34 typedef BObjectList<Handler> HandlerList;
35 typedef std::map<int32, Command*> CommandIDMap;
36 
37 
38 class FolderEntry {
39 public:
40 	FolderEntry()
41 		:
42 		subscribed(false)
43 	{
44 	}
45 
46 	BString	folder;
47 	bool	subscribed;
48 };
49 typedef std::vector<FolderEntry> FolderList;
50 
51 
52 class Protocol {
53 public:
54 								Protocol();
55 								~Protocol();
56 
57 			status_t			Connect(const BNetworkAddress& address,
58 									const char* username, const char* password,
59 									bool useSSL = true);
60 			status_t			Disconnect();
61 			bool				IsConnected();
62 
63 			bool				AddHandler(Handler& handler);
64 			void				RemoveHandler(Handler& handler);
65 
66 			// Some convenience methods
67 			status_t			GetFolders(FolderList& folders,
68 									BString& separator);
69 			status_t			GetSubscribedFolders(StringList& folders,
70 									BString& separator);
71 			status_t			SubscribeFolder(const char* folder);
72 			status_t			UnsubscribeFolder(const char* folder);
73 			status_t			GetQuota(uint64& used, uint64& total);
74 
75 			status_t			SendCommand(const char* command);
76 			status_t			SendCommand(int32 id, const char* command);
77 			ssize_t				SendData(const char* buffer, uint32 length);
78 
79 			status_t			ProcessCommand(Command& command,
80 									bigtime_t timeout = kIMAP4ClientTimeout);
81 
82 			ArgumentList&		Capabilities() { return fCapabilities; }
83 			const ArgumentList&	Capabilities() const { return fCapabilities; }
84 
85 			const BString&		CommandError() { return fCommandError; }
86 
87 protected:
88 			status_t			HandleResponse(Command* command,
89 									bigtime_t timeout = kIMAP4ClientTimeout,
90 									bool disconnectOnTimeout = true);
91 			int32				NextCommandID();
92 
93 private:
94 			status_t			_Disconnect();
95 			status_t			_GetAllFolders(StringList& folders);
96 			void				_ParseCapabilities(
97 									const ArgumentList& arguments);
98 
99 protected:
100 			BSocket* 			fSocket;
101 			BBufferedDataIO*	fBufferedSocket;
102 
103 			HandlerList			fHandlerList;
104 			ArgumentList		fCapabilities;
105 
106 private:
107 			int32				fCommandID;
108 			CommandIDMap		fOngoingCommands;
109 			BString				fCommandError;
110 
111 			bool				fIsConnected;
112 };
113 
114 
115 }	// namespace IMAP
116 
117 
118 #endif // PROTOCOL_H
119