xref: /haiku/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/Protocol.h (revision 9192d4dcf36ca64087e0702be1ef464c1e49d208)
1 /*
2  * Copyright 2001-2011, 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<Command> CommandList;
35 typedef BObjectList<Handler> HandlerList;
36 typedef std::map<int32, Command*> CommandIDMap;
37 
38 
39 class FolderEntry {
40 public:
41 	FolderEntry()
42 		:
43 		subscribed(false)
44 	{
45 	}
46 
47 	BString	folder;
48 	bool	subscribed;
49 };
50 typedef std::vector<FolderEntry> FolderList;
51 
52 
53 class Protocol {
54 public:
55 								Protocol();
56 								~Protocol();
57 
58 			status_t			Connect(const BNetworkAddress& address,
59 									const char* username, const char* password,
60 									bool useSSL = true);
61 			status_t			Disconnect();
62 			bool				IsConnected();
63 
64 			status_t			SendCommand(const char* command);
65 			status_t			SendCommand(int32 id, const char* command);
66 			ssize_t				SendData(const char* buffer, uint32 length);
67 
68 			// Some convenience methods
69 			status_t			GetFolders(FolderList& folders);
70 			status_t			SubscribeFolder(const char* folder);
71 			status_t			UnsubscribeFolder(const char* folder);
72 			status_t			GetQuota(uint64& used, uint64& total);
73 
74 			status_t			ProcessCommand(Command& command,
75 									bigtime_t timeout = kIMAP4ClientTimeout);
76 
77 			ArgumentList&		Capabilities() { return fCapabilities; }
78 			const ArgumentList&	Capabilities() const { return fCapabilities; }
79 
80 			status_t			AddAfterQuakeCommand(Command* command);
81 
82 			const BString&		CommandError() { return fCommandError; }
83 
84 protected:
85 			status_t			HandleResponse(
86 									bigtime_t timeout = kIMAP4ClientTimeout,
87 									bool disconnectOnTimeout = true);
88 			void				ProcessAfterQuacks(bigtime_t timeout);
89 			int32				NextCommandID();
90 
91 private:
92 			/*! Same as ProccessCommand but AfterShockCommands are not send. */
93 			status_t			_ProcessCommandWithoutAfterQuake(
94 									Command& command, bigtime_t timeout);
95 			status_t			_Disconnect();
96 			status_t			_GetAllFolders(StringList& folders);
97 			status_t			_GetSubscribedFolders(StringList& folders);
98 			void				_ParseCapabilities(
99 									const ArgumentList& arguments);
100 
101 protected:
102 			BSocket* 			fSocket;
103 			BBufferedDataIO*	fBufferedSocket;
104 
105 			HandlerList			fHandlerList;
106 			CommandList			fAfterQuackCommands;
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