xref: /haiku/headers/os/add-ons/mail_daemon/MailProtocol.h (revision 56430ad8002b8fd1ac69b590e9cc130de6d9e852)
1 /*
2  * Copyright 2004-2016, Haiku, Inc. All Rights Reserved.
3  * Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4  * Copyright 2011 Clemens Zeidler. All rights reserved.
5  *
6  * Distributed under the terms of the MIT License.
7  */
8 #ifndef _MAIL_PROTOCOL_H
9 #define _MAIL_PROTOCOL_H
10 
11 
12 #include <map>
13 
14 #include <Looper.h>
15 #include <OS.h>
16 #include <ObjectList.h>
17 #include <Entry.h>
18 #include <File.h>
19 
20 #include <E-mail.h>
21 #include <MailSettings.h>
22 
23 
24 class BMailFilter;
25 class BMailSettingsView;
26 class BView;
27 
28 
29 class BMailNotifier {
30 public:
31 	virtual						~BMailNotifier() {}
32 
33 	virtual BMailNotifier*		Clone() = 0;
34 
35 	virtual	void				ShowError(const char* error) = 0;
36 	virtual	void				ShowMessage(const char* message) = 0;
37 
38 	virtual void				SetTotalItems(uint32 items) = 0;
39 	virtual void				SetTotalItemsSize(uint64 size) = 0;
40 	virtual	void				ReportProgress(uint32 items, uint64 bytes,
41 									const char* message = NULL) = 0;
42 	virtual void				ResetProgress(const char* message = NULL) = 0;
43 };
44 
45 
46 typedef status_t BMailFilterAction;
47 
48 #define B_NO_MAIL_ACTION		0
49 #define B_MOVE_MAIL_ACTION		1
50 #define B_DELETE_MAIL_ACTION	2
51 
52 
53 class BMailProtocol : public BLooper {
54 public:
55 								BMailProtocol(const char* name,
56 									const BMailAccountSettings& settings);
57 	virtual						~BMailProtocol();
58 
59 			const BMailAccountSettings& AccountSettings() const;
60 
61 			void				SetMailNotifier(BMailNotifier* mailNotifier);
62 			BMailNotifier*		MailNotifier() const;
63 
64 			//! We take ownership of the filters
65 			bool				AddFilter(BMailFilter* filter);
66 			int32				CountFilter() const;
67 			BMailFilter*		FilterAt(int32 index) const;
68 			BMailFilter*		RemoveFilter(int32 index);
69 			bool				RemoveFilter(BMailFilter* filter);
70 
71 	virtual void				MessageReceived(BMessage* message);
72 
73 			// Convenience methods that call the BMailNotifier
74 			void				ShowError(const char* error);
75 			void				ShowMessage(const char* message);
76 
77 #if __GNUC__ > 2
78 			// Unhide virtual base methods
79 			using BHandler::AddFilter;
80 			using BHandler::RemoveFilter;
81 #endif
82 
83 protected:
84 		 	void				SetTotalItems(uint32 items);
85 			void				SetTotalItemsSize(uint64 size);
86 			void				ReportProgress(uint32 items, uint64 bytes,
87 									const char* message = NULL);
88 			void				ResetProgress(const char* message = NULL);
89 			void				NotifyNewMessagesToFetch(int32 nMessages);
90 
91 			// Filter notifications
92 			BMailFilterAction	ProcessHeaderFetched(entry_ref& ref,
93 									BFile& mail, BMessage& attributes);
94 			void				NotifyBodyFetched(const entry_ref& ref,
95 									BFile& mail, BMessage& attributes);
96 			BMailFilterAction	ProcessMessageFetched(entry_ref& ref,
97 									BFile& mail, BMessage& attributes);
98 			void				NotifyMessageReadyToSend(const entry_ref& ref,
99 									BFile& mail);
100 			void				NotifyMessageSent(const entry_ref& ref,
101 									BFile& mail);
102 
103 			void				LoadFilters(
104 									const BMailProtocolSettings& settings);
105 
106 private:
107 	static	BString				_LooperName(const char* name,
108 									const BMailAccountSettings& settings);
109 			BMailFilter*		_LoadFilter(const BMailAddOnSettings& settings);
110 			BMailFilterAction	_ProcessHeaderFetched(entry_ref& ref,
111 									BFile& mail, BMessage& attributes);
112 			void				_NotifyBodyFetched(const entry_ref& ref,
113 									BFile& mail, BMessage& attributes);
114 
115 protected:
116 			const BMailAccountSettings fAccountSettings;
117 			BMailNotifier*		fMailNotifier;
118 
119 private:
120 			BObjectList<BMailFilter> fFilterList;
121 			std::map<entry_ref, image_id> fFilterImages;
122 };
123 
124 
125 class BInboundMailProtocol : public BMailProtocol {
126 public:
127 								BInboundMailProtocol(const char* name,
128 									const BMailAccountSettings& settings);
129 	virtual						~BInboundMailProtocol();
130 
131 	virtual void				MessageReceived(BMessage* message);
132 
133 	virtual	status_t			SyncMessages() = 0;
134 	virtual status_t			FetchBody(const entry_ref& ref,
135 									BMessenger* replyTo);
136 	virtual	status_t			MarkMessageAsRead(const entry_ref& ref,
137 									read_flags flags = B_READ);
138 
139 	static	void				ReplyBodyFetched(const BMessenger& replyTo,
140 									const entry_ref& ref, status_t status);
141 
142 protected:
143 	virtual status_t			HandleFetchBody(const entry_ref& ref,
144 									const BMessenger& replyTo) = 0;
145 
146 			void				NotiyMailboxSynchronized(status_t status);
147 };
148 
149 
150 class BOutboundMailProtocol : public BMailProtocol {
151 public:
152 								BOutboundMailProtocol(const char* name,
153 									const BMailAccountSettings& settings);
154 	virtual						~BOutboundMailProtocol();
155 
156 	virtual	status_t			SendMessages(const BMessage& message,
157 									off_t totalBytes);
158 
159 	virtual void				MessageReceived(BMessage* message);
160 
161 protected:
162 	virtual	status_t			HandleSendMessages(const BMessage& message,
163 									off_t totalBytes) = 0;
164 };
165 
166 
167 // Your protocol needs to export these hooks in order to be picked up
168 extern "C" BInboundMailProtocol* instantiate_inbound_protocol(
169 	const BMailAccountSettings& settings);
170 extern "C" BOutboundMailProtocol* instantiate_outbound_protocol(
171 	const BMailAccountSettings& settings);
172 
173 extern "C" BMailSettingsView* instantiate_protocol_settings_view(
174 	const BMailAccountSettings& accountSettings,
175 	const BMailProtocolSettings& settings);
176 
177 
178 #endif	// _MAIL_PROTOCOL_H
179