xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Protocol.h (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2001-2015, 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 
25 
26 const bigtime_t kIMAP4ClientTimeout = 1000000 * 60;
27 	// 60 seconds
28 
29 
30 namespace IMAP {
31 
32 
33 class Command;
34 class Handler;
35 
36 
37 typedef BObjectList<Handler> HandlerList;
38 typedef std::map<int32, Command*> CommandIDMap;
39 
40 
41 class FolderEntry {
42 public:
43 	FolderEntry()
44 		:
45 		subscribed(false)
46 	{
47 	}
48 
49 	BString	folder;
50 	bool	subscribed;
51 };
52 typedef std::vector<FolderEntry> FolderList;
53 
54 
55 class Protocol {
56 public:
57 								Protocol();
58 								~Protocol();
59 
60 			status_t			Connect(const BNetworkAddress& address,
61 									const char* username, const char* password,
62 									bool useSSL = true);
63 			status_t			Disconnect();
64 			bool				IsConnected();
65 
66 			bool				AddHandler(Handler& handler);
67 			void				RemoveHandler(Handler& handler);
68 
69 			// Some convenience methods
70 			status_t			GetFolders(FolderList& folders,
71 									BString& separator);
72 			status_t			GetSubscribedFolders(BStringList& folders,
73 									BString& separator);
74 			status_t			SubscribeFolder(const char* folder);
75 			status_t			UnsubscribeFolder(const char* folder);
76 			status_t			GetQuota(uint64& used, uint64& total);
77 
78 			status_t			SendCommand(const char* command);
79 			status_t			SendCommand(int32 id, const char* command);
80 			ssize_t				SendData(const char* buffer, uint32 length);
81 
82 			status_t			ProcessCommand(Command& command,
83 									bigtime_t timeout = kIMAP4ClientTimeout);
84 
85 			ArgumentList&		Capabilities() { return fCapabilities; }
86 			const ArgumentList&	Capabilities() const { return fCapabilities; }
87 
88 			const BString&		CommandError() { return fCommandError; }
89 
90 protected:
91 			status_t			HandleResponse(Command* command,
92 									bigtime_t timeout = kIMAP4ClientTimeout,
93 									bool disconnectOnTimeout = true);
94 			int32				NextCommandID();
95 
96 private:
97 			status_t			_Disconnect();
98 			status_t			_GetAllFolders(BStringList& folders);
99 			void				_ParseCapabilities(
100 									const ArgumentList& arguments);
101 
102 protected:
103 			BSocket* 			fSocket;
104 			BBufferedDataIO*	fBufferedSocket;
105 
106 			HandlerList			fHandlerList;
107 			ArgumentList		fCapabilities;
108 
109 private:
110 			int32				fCommandID;
111 			CommandIDMap		fOngoingCommands;
112 			BString				fCommandError;
113 
114 			bool				fIsConnected;
115 };
116 
117 
118 }	// namespace IMAP
119 
120 
121 #endif // PROTOCOL_H
122