xref: /haiku/src/kits/mail/MailProtocol.cpp (revision 109459c8a51e33fe9bbb1d0ad0b7cf36f3e541fb)
10c7f804cSAxel Dörfler /*
281805393SAxel Dörfler  * Copyright 2011-2016, Haiku, Inc. All rights reserved.
30c7f804cSAxel Dörfler  * Copyright 2001-2003 Dr. Zoidberg Enterprises. All rights reserved.
4f7215ac8SNathan Whitehorn  */
5f7215ac8SNathan Whitehorn 
6f7215ac8SNathan Whitehorn 
7f7215ac8SNathan Whitehorn #include <stdio.h>
86e927a5fSIngo Weinhold #include <stdlib.h>
9715bf3d1SAxel Dörfler 
10715bf3d1SAxel Dörfler #include <fs_attr.h>
11f7215ac8SNathan Whitehorn 
12dfc6cf01SPhilippe Houdoin #include <Alert.h>
13*109459c8SMurai Takashi #include <Autolock.h>
14dfc6cf01SPhilippe Houdoin #include <Directory.h>
15dfc6cf01SPhilippe Houdoin #include <E-mail.h>
16*109459c8SMurai Takashi #include <FindDirectory.h>
17dfc6cf01SPhilippe Houdoin #include <Node.h>
18dfc6cf01SPhilippe Houdoin #include <NodeInfo.h>
19dfc6cf01SPhilippe Houdoin #include <NodeMonitor.h>
20dfc6cf01SPhilippe Houdoin #include <Path.h>
21715bf3d1SAxel Dörfler #include <Query.h>
221af4fa4bSClemens Zeidler #include <Roster.h>
23dfc6cf01SPhilippe Houdoin #include <String.h>
24dfc6cf01SPhilippe Houdoin #include <StringList.h>
25dfc6cf01SPhilippe Houdoin #include <VolumeRoster.h>
26dfc6cf01SPhilippe Houdoin 
27715bf3d1SAxel Dörfler #include <MailFilter.h>
28b8d2bbd6SClemens Zeidler #include <MailDaemon.h>
29f7215ac8SNathan Whitehorn #include <MailProtocol.h>
301af4fa4bSClemens Zeidler #include <MailSettings.h>
31f7215ac8SNathan Whitehorn 
32715bf3d1SAxel Dörfler #include <mail_util.h>
33146357b5SAxel Dörfler #include <MailPrivate.h>
34d33e4744SAxel Dörfler #include <NodeMessage.h>
35715bf3d1SAxel Dörfler 
361af4fa4bSClemens Zeidler #include "HaikuMailFormatFilter.h"
37f7215ac8SNathan Whitehorn 
38f7215ac8SNathan Whitehorn 
39146357b5SAxel Dörfler using namespace BPrivate;
40f7215ac8SNathan Whitehorn 
41f7215ac8SNathan Whitehorn 
BMailProtocol(const char * name,const BMailAccountSettings & settings)4262eec600SAxel Dörfler BMailProtocol::BMailProtocol(const char* name,
4362eec600SAxel Dörfler 	const BMailAccountSettings& settings)
44f7215ac8SNathan Whitehorn 	:
4562eec600SAxel Dörfler 	BLooper(_LooperName(name, settings)),
46715bf3d1SAxel Dörfler 	fAccountSettings(settings),
47715bf3d1SAxel Dörfler 	fMailNotifier(NULL)
48f7215ac8SNathan Whitehorn {
491af4fa4bSClemens Zeidler 	AddFilter(new HaikuMailFormatFilter(*this, settings));
501af4fa4bSClemens Zeidler }
511af4fa4bSClemens Zeidler 
521af4fa4bSClemens Zeidler 
~BMailProtocol()53715bf3d1SAxel Dörfler BMailProtocol::~BMailProtocol()
541af4fa4bSClemens Zeidler {
551af4fa4bSClemens Zeidler 	delete fMailNotifier;
561af4fa4bSClemens Zeidler 
571af4fa4bSClemens Zeidler 	for (int i = 0; i < fFilterList.CountItems(); i++)
581af4fa4bSClemens Zeidler 		delete fFilterList.ItemAt(i);
591af4fa4bSClemens Zeidler 
60146357b5SAxel Dörfler 	std::map<entry_ref, image_id>::iterator it = fFilterImages.begin();
611af4fa4bSClemens Zeidler 	for (; it != fFilterImages.end(); it++)
621af4fa4bSClemens Zeidler 		unload_add_on(it->second);
631af4fa4bSClemens Zeidler }
641af4fa4bSClemens Zeidler 
651af4fa4bSClemens Zeidler 
66715bf3d1SAxel Dörfler const BMailAccountSettings&
AccountSettings() const67715bf3d1SAxel Dörfler BMailProtocol::AccountSettings() const
681af4fa4bSClemens Zeidler {
691af4fa4bSClemens Zeidler 	return fAccountSettings;
701af4fa4bSClemens Zeidler }
711af4fa4bSClemens Zeidler 
721af4fa4bSClemens Zeidler 
731af4fa4bSClemens Zeidler void
SetMailNotifier(BMailNotifier * mailNotifier)74715bf3d1SAxel Dörfler BMailProtocol::SetMailNotifier(BMailNotifier* mailNotifier)
751af4fa4bSClemens Zeidler {
761af4fa4bSClemens Zeidler 	delete fMailNotifier;
771af4fa4bSClemens Zeidler 	fMailNotifier = mailNotifier;
781af4fa4bSClemens Zeidler }
791af4fa4bSClemens Zeidler 
801af4fa4bSClemens Zeidler 
81715bf3d1SAxel Dörfler BMailNotifier*
MailNotifier() const82715bf3d1SAxel Dörfler BMailProtocol::MailNotifier() const
831af4fa4bSClemens Zeidler {
84715bf3d1SAxel Dörfler 	return fMailNotifier;
851af4fa4bSClemens Zeidler }
861af4fa4bSClemens Zeidler 
871af4fa4bSClemens Zeidler 
881af4fa4bSClemens Zeidler bool
AddFilter(BMailFilter * filter)89715bf3d1SAxel Dörfler BMailProtocol::AddFilter(BMailFilter* filter)
901af4fa4bSClemens Zeidler {
91*109459c8SMurai Takashi 	BAutolock locker(const_cast< BMailProtocol * >(this));
921af4fa4bSClemens Zeidler 	return fFilterList.AddItem(filter);
931af4fa4bSClemens Zeidler }
941af4fa4bSClemens Zeidler 
951af4fa4bSClemens Zeidler 
961af4fa4bSClemens Zeidler int32
CountFilter() const97715bf3d1SAxel Dörfler BMailProtocol::CountFilter() const
981af4fa4bSClemens Zeidler {
99*109459c8SMurai Takashi 	BAutolock locker(const_cast< BMailProtocol * >(this));
1001af4fa4bSClemens Zeidler 	return fFilterList.CountItems();
1011af4fa4bSClemens Zeidler }
1021af4fa4bSClemens Zeidler 
1031af4fa4bSClemens Zeidler 
104715bf3d1SAxel Dörfler BMailFilter*
FilterAt(int32 index) const105715bf3d1SAxel Dörfler BMailProtocol::FilterAt(int32 index) const
1061af4fa4bSClemens Zeidler {
107*109459c8SMurai Takashi 	BAutolock locker(const_cast< BMailProtocol * >(this));
1081af4fa4bSClemens Zeidler 	return fFilterList.ItemAt(index);
1091af4fa4bSClemens Zeidler }
1101af4fa4bSClemens Zeidler 
1111af4fa4bSClemens Zeidler 
112715bf3d1SAxel Dörfler BMailFilter*
RemoveFilter(int32 index)113715bf3d1SAxel Dörfler BMailProtocol::RemoveFilter(int32 index)
1141af4fa4bSClemens Zeidler {
115*109459c8SMurai Takashi 	BAutolock locker(const_cast< BMailProtocol * >(this));
1161af4fa4bSClemens Zeidler 	return fFilterList.RemoveItemAt(index);
1171af4fa4bSClemens Zeidler }
1181af4fa4bSClemens Zeidler 
1191af4fa4bSClemens Zeidler 
1201af4fa4bSClemens Zeidler bool
RemoveFilter(BMailFilter * filter)121715bf3d1SAxel Dörfler BMailProtocol::RemoveFilter(BMailFilter* filter)
1221af4fa4bSClemens Zeidler {
123*109459c8SMurai Takashi 	BAutolock locker(const_cast< BMailProtocol * >(this));
1241af4fa4bSClemens Zeidler 	return fFilterList.RemoveItem(filter);
1251af4fa4bSClemens Zeidler }
1261af4fa4bSClemens Zeidler 
1271af4fa4bSClemens Zeidler 
1281af4fa4bSClemens Zeidler void
MessageReceived(BMessage * message)129715bf3d1SAxel Dörfler BMailProtocol::MessageReceived(BMessage* message)
1301af4fa4bSClemens Zeidler {
131715bf3d1SAxel Dörfler 	BLooper::MessageReceived(message);
132715bf3d1SAxel Dörfler }
1331af4fa4bSClemens Zeidler 
1341af4fa4bSClemens Zeidler 
1351af4fa4bSClemens Zeidler void
ShowError(const char * error)136715bf3d1SAxel Dörfler BMailProtocol::ShowError(const char* error)
137715bf3d1SAxel Dörfler {
138715bf3d1SAxel Dörfler 	if (MailNotifier() != NULL)
139715bf3d1SAxel Dörfler 		MailNotifier()->ShowError(error);
140715bf3d1SAxel Dörfler }
141715bf3d1SAxel Dörfler 
142715bf3d1SAxel Dörfler 
143715bf3d1SAxel Dörfler void
ShowMessage(const char * message)144715bf3d1SAxel Dörfler BMailProtocol::ShowMessage(const char* message)
145715bf3d1SAxel Dörfler {
146715bf3d1SAxel Dörfler 	if (MailNotifier() != NULL)
147715bf3d1SAxel Dörfler 		MailNotifier()->ShowMessage(message);
148715bf3d1SAxel Dörfler }
149715bf3d1SAxel Dörfler 
150715bf3d1SAxel Dörfler 
151715bf3d1SAxel Dörfler void
SetTotalItems(uint32 items)152715bf3d1SAxel Dörfler BMailProtocol::SetTotalItems(uint32 items)
153715bf3d1SAxel Dörfler {
154715bf3d1SAxel Dörfler 	if (MailNotifier() != NULL)
155715bf3d1SAxel Dörfler 		MailNotifier()->SetTotalItems(items);
156715bf3d1SAxel Dörfler }
157715bf3d1SAxel Dörfler 
158715bf3d1SAxel Dörfler 
159715bf3d1SAxel Dörfler void
SetTotalItemsSize(uint64 size)160715bf3d1SAxel Dörfler BMailProtocol::SetTotalItemsSize(uint64 size)
161715bf3d1SAxel Dörfler {
162715bf3d1SAxel Dörfler 	if (MailNotifier() != NULL)
163715bf3d1SAxel Dörfler 		MailNotifier()->SetTotalItemsSize(size);
164715bf3d1SAxel Dörfler }
165715bf3d1SAxel Dörfler 
166715bf3d1SAxel Dörfler 
167715bf3d1SAxel Dörfler void
ReportProgress(uint32 messages,uint64 bytes,const char * message)168715bf3d1SAxel Dörfler BMailProtocol::ReportProgress(uint32 messages, uint64 bytes,
169715bf3d1SAxel Dörfler 	const char* message)
170715bf3d1SAxel Dörfler {
171715bf3d1SAxel Dörfler 	if (MailNotifier() != NULL)
172715bf3d1SAxel Dörfler 		MailNotifier()->ReportProgress(messages, bytes, message);
173715bf3d1SAxel Dörfler }
174715bf3d1SAxel Dörfler 
175715bf3d1SAxel Dörfler 
176715bf3d1SAxel Dörfler void
ResetProgress(const char * message)177715bf3d1SAxel Dörfler BMailProtocol::ResetProgress(const char* message)
178715bf3d1SAxel Dörfler {
179715bf3d1SAxel Dörfler 	if (MailNotifier() != NULL)
180715bf3d1SAxel Dörfler 		MailNotifier()->ResetProgress(message);
181715bf3d1SAxel Dörfler }
182715bf3d1SAxel Dörfler 
183715bf3d1SAxel Dörfler 
184715bf3d1SAxel Dörfler void
NotifyNewMessagesToFetch(int32 count)185715bf3d1SAxel Dörfler BMailProtocol::NotifyNewMessagesToFetch(int32 count)
186715bf3d1SAxel Dörfler {
187715bf3d1SAxel Dörfler 	ResetProgress();
188715bf3d1SAxel Dörfler 	SetTotalItems(count);
189715bf3d1SAxel Dörfler }
190715bf3d1SAxel Dörfler 
191715bf3d1SAxel Dörfler 
192549949b2SAxel Dörfler BMailFilterAction
ProcessHeaderFetched(entry_ref & ref,BFile & file,BMessage & attributes)193d33e4744SAxel Dörfler BMailProtocol::ProcessHeaderFetched(entry_ref& ref, BFile& file,
194d33e4744SAxel Dörfler 	BMessage& attributes)
195715bf3d1SAxel Dörfler {
196d33e4744SAxel Dörfler 	BMailFilterAction action = _ProcessHeaderFetched(ref, file, attributes);
197d33e4744SAxel Dörfler 	if (action >= B_OK && action != B_DELETE_MAIL_ACTION)
198d33e4744SAxel Dörfler 		file << attributes;
199549949b2SAxel Dörfler 
200d33e4744SAxel Dörfler 	return action;
201715bf3d1SAxel Dörfler }
202715bf3d1SAxel Dörfler 
203715bf3d1SAxel Dörfler 
204715bf3d1SAxel Dörfler void
NotifyBodyFetched(const entry_ref & ref,BFile & file,BMessage & attributes)205d33e4744SAxel Dörfler BMailProtocol::NotifyBodyFetched(const entry_ref& ref, BFile& file,
206d33e4744SAxel Dörfler 	BMessage& attributes)
207715bf3d1SAxel Dörfler {
208d33e4744SAxel Dörfler 	_NotifyBodyFetched(ref, file, attributes);
209d33e4744SAxel Dörfler 	file << attributes;
210d33e4744SAxel Dörfler }
211d33e4744SAxel Dörfler 
212d33e4744SAxel Dörfler 
213d33e4744SAxel Dörfler BMailFilterAction
ProcessMessageFetched(entry_ref & ref,BFile & file,BMessage & attributes)214d33e4744SAxel Dörfler BMailProtocol::ProcessMessageFetched(entry_ref& ref, BFile& file,
215d33e4744SAxel Dörfler 	BMessage& attributes)
216d33e4744SAxel Dörfler {
217d33e4744SAxel Dörfler 	BMailFilterAction action = _ProcessHeaderFetched(ref, file, attributes);
218d33e4744SAxel Dörfler 	if (action >= B_OK && action != B_DELETE_MAIL_ACTION) {
219d33e4744SAxel Dörfler 		_NotifyBodyFetched(ref, file, attributes);
220d33e4744SAxel Dörfler 		file << attributes;
221d33e4744SAxel Dörfler 	}
222d33e4744SAxel Dörfler 
223d33e4744SAxel Dörfler 	return action;
224715bf3d1SAxel Dörfler }
225715bf3d1SAxel Dörfler 
226715bf3d1SAxel Dörfler 
227715bf3d1SAxel Dörfler void
NotifyMessageReadyToSend(const entry_ref & ref,BFile & file)228d33e4744SAxel Dörfler BMailProtocol::NotifyMessageReadyToSend(const entry_ref& ref, BFile& file)
229715bf3d1SAxel Dörfler {
230715bf3d1SAxel Dörfler 	for (int i = 0; i < fFilterList.CountItems(); i++)
231d33e4744SAxel Dörfler 		fFilterList.ItemAt(i)->MessageReadyToSend(ref, file);
232715bf3d1SAxel Dörfler }
233715bf3d1SAxel Dörfler 
234715bf3d1SAxel Dörfler 
235715bf3d1SAxel Dörfler void
NotifyMessageSent(const entry_ref & ref,BFile & file)236d33e4744SAxel Dörfler BMailProtocol::NotifyMessageSent(const entry_ref& ref, BFile& file)
237715bf3d1SAxel Dörfler {
238715bf3d1SAxel Dörfler 	for (int i = 0; i < fFilterList.CountItems(); i++)
239d33e4744SAxel Dörfler 		fFilterList.ItemAt(i)->MessageSent(ref, file);
240715bf3d1SAxel Dörfler }
241715bf3d1SAxel Dörfler 
242715bf3d1SAxel Dörfler 
243715bf3d1SAxel Dörfler void
LoadFilters(const BMailProtocolSettings & settings)244715bf3d1SAxel Dörfler BMailProtocol::LoadFilters(const BMailProtocolSettings& settings)
2451af4fa4bSClemens Zeidler {
2461af4fa4bSClemens Zeidler 	for (int i = 0; i < settings.CountFilterSettings(); i++) {
247715bf3d1SAxel Dörfler 		BMailAddOnSettings* filterSettings = settings.FilterSettingsAt(i);
248ca3341c7SAxel Dörfler 		BMailFilter* filter = _LoadFilter(*filterSettings);
249715bf3d1SAxel Dörfler 		if (filter != NULL)
2501af4fa4bSClemens Zeidler 			AddFilter(filter);
2511af4fa4bSClemens Zeidler 	}
2521af4fa4bSClemens Zeidler }
2531af4fa4bSClemens Zeidler 
2541af4fa4bSClemens Zeidler 
25562eec600SAxel Dörfler /*static*/ BString
_LooperName(const char * addOnName,const BMailAccountSettings & settings)25662eec600SAxel Dörfler BMailProtocol::_LooperName(const char* addOnName,
25762eec600SAxel Dörfler 	const BMailAccountSettings& settings)
25862eec600SAxel Dörfler {
25962eec600SAxel Dörfler 	BString name = addOnName;
26062eec600SAxel Dörfler 
26162eec600SAxel Dörfler 	const char* accountName = settings.Name();
26262eec600SAxel Dörfler 	if (accountName != NULL && accountName[0] != '\0')
26362eec600SAxel Dörfler 		name << " " << accountName;
26462eec600SAxel Dörfler 
26562eec600SAxel Dörfler 	return name;
26662eec600SAxel Dörfler }
26762eec600SAxel Dörfler 
26862eec600SAxel Dörfler 
269715bf3d1SAxel Dörfler BMailFilter*
_LoadFilter(const BMailAddOnSettings & settings)270ca3341c7SAxel Dörfler BMailProtocol::_LoadFilter(const BMailAddOnSettings& settings)
2711af4fa4bSClemens Zeidler {
272ca3341c7SAxel Dörfler 	const entry_ref& ref = settings.AddOnRef();
273146357b5SAxel Dörfler 	std::map<entry_ref, image_id>::iterator it = fFilterImages.find(ref);
2741af4fa4bSClemens Zeidler 	image_id image;
2751af4fa4bSClemens Zeidler 	if (it != fFilterImages.end())
2761af4fa4bSClemens Zeidler 		image = it->second;
2771af4fa4bSClemens Zeidler 	else {
2781af4fa4bSClemens Zeidler 		BEntry entry(&ref);
2791af4fa4bSClemens Zeidler 		BPath path(&entry);
2801af4fa4bSClemens Zeidler 		image = load_add_on(path.Path());
2811af4fa4bSClemens Zeidler 	}
2821af4fa4bSClemens Zeidler 	if (image < 0)
2831af4fa4bSClemens Zeidler 		return NULL;
2841af4fa4bSClemens Zeidler 
285ca3341c7SAxel Dörfler 	BMailFilter* (*instantiateFilter)(BMailProtocol& protocol,
286ca3341c7SAxel Dörfler 		const BMailAddOnSettings& settings);
287715bf3d1SAxel Dörfler 	if (get_image_symbol(image, "instantiate_filter", B_SYMBOL_TYPE_TEXT,
288ca3341c7SAxel Dörfler 			(void**)&instantiateFilter) != B_OK) {
2891af4fa4bSClemens Zeidler 		unload_add_on(image);
2901af4fa4bSClemens Zeidler 		return NULL;
2911af4fa4bSClemens Zeidler 	}
2921af4fa4bSClemens Zeidler 
2931af4fa4bSClemens Zeidler 	fFilterImages[ref] = image;
294ca3341c7SAxel Dörfler 	return instantiateFilter(*this, settings);
2951af4fa4bSClemens Zeidler }
2961af4fa4bSClemens Zeidler 
2971af4fa4bSClemens Zeidler 
298d33e4744SAxel Dörfler BMailFilterAction
_ProcessHeaderFetched(entry_ref & ref,BFile & file,BMessage & attributes)299d33e4744SAxel Dörfler BMailProtocol::_ProcessHeaderFetched(entry_ref& ref, BFile& file,
300d33e4744SAxel Dörfler 	BMessage& attributes)
301d33e4744SAxel Dörfler {
302d33e4744SAxel Dörfler 	entry_ref outRef = ref;
303d33e4744SAxel Dörfler 
304d33e4744SAxel Dörfler 	for (int i = 0; i < fFilterList.CountItems(); i++) {
305d33e4744SAxel Dörfler 		BMailFilterAction action = fFilterList.ItemAt(i)->HeaderFetched(outRef,
306d33e4744SAxel Dörfler 			file, attributes);
307d33e4744SAxel Dörfler 		if (action == B_DELETE_MAIL_ACTION) {
308d33e4744SAxel Dörfler 			// We have to delete the message
309d33e4744SAxel Dörfler 			BEntry entry(&ref);
310d33e4744SAxel Dörfler 			status_t status = entry.Remove();
311d33e4744SAxel Dörfler 			if (status != B_OK) {
312d33e4744SAxel Dörfler 				fprintf(stderr, "BMailProtocol::NotifyHeaderFetched(): could "
313d33e4744SAxel Dörfler 					"not delete mail: %s\n", strerror(status));
314d33e4744SAxel Dörfler 			}
315d33e4744SAxel Dörfler 			return B_DELETE_MAIL_ACTION;
316d33e4744SAxel Dörfler 		}
317d33e4744SAxel Dörfler 	}
318d33e4744SAxel Dörfler 
319d33e4744SAxel Dörfler 	if (ref == outRef)
320d33e4744SAxel Dörfler 		return B_NO_MAIL_ACTION;
321d33e4744SAxel Dörfler 
322d33e4744SAxel Dörfler 	// We have to rename the file
323d33e4744SAxel Dörfler 	node_ref newParentRef;
324d33e4744SAxel Dörfler 	newParentRef.device = outRef.device;
325d33e4744SAxel Dörfler 	newParentRef.node = outRef.directory;
326d33e4744SAxel Dörfler 
327d33e4744SAxel Dörfler 	BDirectory newParent(&newParentRef);
328d33e4744SAxel Dörfler 	status_t status = newParent.InitCheck();
329d33e4744SAxel Dörfler 	BString workerName;
330d33e4744SAxel Dörfler 	if (status == B_OK) {
331d33e4744SAxel Dörfler 		int32 uniqueNumber = 1;
332d33e4744SAxel Dörfler 		do {
333d33e4744SAxel Dörfler 			workerName = outRef.name;
334d33e4744SAxel Dörfler 			if (uniqueNumber > 1)
335d33e4744SAxel Dörfler 				workerName << "_" << uniqueNumber;
336d33e4744SAxel Dörfler 
337d33e4744SAxel Dörfler 			// TODO: support copying to another device!
338d33e4744SAxel Dörfler 			BEntry entry(&ref);
339d33e4744SAxel Dörfler 			status = entry.Rename(workerName);
340d33e4744SAxel Dörfler 
341d33e4744SAxel Dörfler 			uniqueNumber++;
342d33e4744SAxel Dörfler 		} while (status == B_FILE_EXISTS);
343d33e4744SAxel Dörfler 	}
344d33e4744SAxel Dörfler 
345d33e4744SAxel Dörfler 	if (status != B_OK) {
346d33e4744SAxel Dörfler 		fprintf(stderr, "BMailProtocol::NotifyHeaderFetched(): could not "
347d33e4744SAxel Dörfler 			"rename mail (%s)! (should be: %s)\n", strerror(status),
348d33e4744SAxel Dörfler 			workerName.String());
349d33e4744SAxel Dörfler 	}
350d33e4744SAxel Dörfler 
351d33e4744SAxel Dörfler 	ref = outRef;
352d33e4744SAxel Dörfler 	ref.set_name(workerName.String());
353d33e4744SAxel Dörfler 
354d33e4744SAxel Dörfler 	return B_MOVE_MAIL_ACTION;
355d33e4744SAxel Dörfler }
356d33e4744SAxel Dörfler 
357d33e4744SAxel Dörfler 
358d33e4744SAxel Dörfler void
_NotifyBodyFetched(const entry_ref & ref,BFile & file,BMessage & attributes)359d33e4744SAxel Dörfler BMailProtocol::_NotifyBodyFetched(const entry_ref& ref, BFile& file,
360d33e4744SAxel Dörfler 	BMessage& attributes)
361d33e4744SAxel Dörfler {
362d33e4744SAxel Dörfler 	for (int i = 0; i < fFilterList.CountItems(); i++)
363d33e4744SAxel Dörfler 		fFilterList.ItemAt(i)->BodyFetched(ref, file, attributes);
364d33e4744SAxel Dörfler }
365d33e4744SAxel Dörfler 
366d33e4744SAxel Dörfler 
3670c7f804cSAxel Dörfler // #pragma mark -
3680c7f804cSAxel Dörfler 
3690c7f804cSAxel Dörfler 
BInboundMailProtocol(const char * name,const BMailAccountSettings & settings)37062eec600SAxel Dörfler BInboundMailProtocol::BInboundMailProtocol(const char* name,
37162eec600SAxel Dörfler 	const BMailAccountSettings& settings)
3721af4fa4bSClemens Zeidler 	:
37362eec600SAxel Dörfler 	BMailProtocol(name, settings)
3741af4fa4bSClemens Zeidler {
3751af4fa4bSClemens Zeidler 	LoadFilters(fAccountSettings.InboundSettings());
3761af4fa4bSClemens Zeidler }
3771af4fa4bSClemens Zeidler 
3781af4fa4bSClemens Zeidler 
~BInboundMailProtocol()379715bf3d1SAxel Dörfler BInboundMailProtocol::~BInboundMailProtocol()
3801af4fa4bSClemens Zeidler {
3811af4fa4bSClemens Zeidler }
3821af4fa4bSClemens Zeidler 
3831af4fa4bSClemens Zeidler 
3841af4fa4bSClemens Zeidler void
MessageReceived(BMessage * message)385715bf3d1SAxel Dörfler BInboundMailProtocol::MessageReceived(BMessage* message)
3861af4fa4bSClemens Zeidler {
3871af4fa4bSClemens Zeidler 	switch (message->what) {
3881af4fa4bSClemens Zeidler 		case kMsgSyncMessages:
3891af4fa4bSClemens Zeidler 		{
390715bf3d1SAxel Dörfler 			NotiyMailboxSynchronized(SyncMessages());
3911af4fa4bSClemens Zeidler 			break;
3921af4fa4bSClemens Zeidler 		}
3931af4fa4bSClemens Zeidler 
3941af4fa4bSClemens Zeidler 		case kMsgFetchBody:
3951af4fa4bSClemens Zeidler 		{
3961af4fa4bSClemens Zeidler 			entry_ref ref;
39781805393SAxel Dörfler 			if (message->FindRef("ref", &ref) != B_OK)
3981af4fa4bSClemens Zeidler 				break;
3990e657c0bSClemens Zeidler 
40081805393SAxel Dörfler 			BMessenger target;
40181805393SAxel Dörfler 			message->FindMessenger("target", &target);
40281805393SAxel Dörfler 
40381805393SAxel Dörfler 			status_t status = HandleFetchBody(ref, target);
40481805393SAxel Dörfler 			if (status != B_OK)
40581805393SAxel Dörfler 				ReplyBodyFetched(target, ref, status);
4061af4fa4bSClemens Zeidler 			break;
4071af4fa4bSClemens Zeidler 		}
4081af4fa4bSClemens Zeidler 
4091af4fa4bSClemens Zeidler 		case kMsgMarkMessageAsRead:
4101af4fa4bSClemens Zeidler 		{
4111af4fa4bSClemens Zeidler 			entry_ref ref;
4121af4fa4bSClemens Zeidler 			message->FindRef("ref", &ref);
4139967dfd9SClemens Zeidler 			read_flags read = (read_flags)message->FindInt32("read");
414715bf3d1SAxel Dörfler 			MarkMessageAsRead(ref, read);
4151af4fa4bSClemens Zeidler 			break;
4161af4fa4bSClemens Zeidler 		}
4171af4fa4bSClemens Zeidler 
4181af4fa4bSClemens Zeidler 		default:
419715bf3d1SAxel Dörfler 			BMailProtocol::MessageReceived(message);
4200c7f804cSAxel Dörfler 			break;
4211af4fa4bSClemens Zeidler 	}
4221af4fa4bSClemens Zeidler }
4231af4fa4bSClemens Zeidler 
4241af4fa4bSClemens Zeidler 
425715bf3d1SAxel Dörfler status_t
FetchBody(const entry_ref & ref,BMessenger * replyTo)42681805393SAxel Dörfler BInboundMailProtocol::FetchBody(const entry_ref& ref, BMessenger* replyTo)
42781805393SAxel Dörfler {
42881805393SAxel Dörfler 	BMessage message(kMsgFetchBody);
42981805393SAxel Dörfler 	message.AddRef("ref", &ref);
43081805393SAxel Dörfler 	if (replyTo != NULL)
43181805393SAxel Dörfler 		message.AddMessenger("target", *replyTo);
43281805393SAxel Dörfler 
43381805393SAxel Dörfler 	return BMessenger(this).SendMessage(&message);
43481805393SAxel Dörfler }
43581805393SAxel Dörfler 
43681805393SAxel Dörfler 
43781805393SAxel Dörfler status_t
MarkMessageAsRead(const entry_ref & ref,read_flags flag)438715bf3d1SAxel Dörfler BInboundMailProtocol::MarkMessageAsRead(const entry_ref& ref, read_flags flag)
4391af4fa4bSClemens Zeidler {
440715bf3d1SAxel Dörfler 	BNode node(&ref);
441715bf3d1SAxel Dörfler 	return write_read_attr(node, flag);
442715bf3d1SAxel Dörfler }
443715bf3d1SAxel Dörfler 
444715bf3d1SAxel Dörfler 
44581805393SAxel Dörfler /*static*/ void
ReplyBodyFetched(const BMessenger & replyTo,const entry_ref & ref,status_t status)44681805393SAxel Dörfler BInboundMailProtocol::ReplyBodyFetched(const BMessenger& replyTo,
44781805393SAxel Dörfler 	const entry_ref& ref, status_t status)
44881805393SAxel Dörfler {
44981805393SAxel Dörfler 	BMessage message(B_MAIL_BODY_FETCHED);
45081805393SAxel Dörfler 	message.AddInt32("status", status);
45181805393SAxel Dörfler 	message.AddRef("ref", &ref);
45281805393SAxel Dörfler 	replyTo.SendMessage(&message);
45381805393SAxel Dörfler }
45481805393SAxel Dörfler 
45581805393SAxel Dörfler 
4561af4fa4bSClemens Zeidler void
NotiyMailboxSynchronized(status_t status)457715bf3d1SAxel Dörfler BInboundMailProtocol::NotiyMailboxSynchronized(status_t status)
4581af4fa4bSClemens Zeidler {
459715bf3d1SAxel Dörfler 	for (int32 i = 0; i < CountFilter(); i++)
460715bf3d1SAxel Dörfler 		FilterAt(i)->MailboxSynchronized(status);
4611af4fa4bSClemens Zeidler }
4621af4fa4bSClemens Zeidler 
4631af4fa4bSClemens Zeidler 
4640c7f804cSAxel Dörfler // #pragma mark -
4651af4fa4bSClemens Zeidler 
4661af4fa4bSClemens Zeidler 
BOutboundMailProtocol(const char * name,const BMailAccountSettings & settings)46762eec600SAxel Dörfler BOutboundMailProtocol::BOutboundMailProtocol(const char* name,
468715bf3d1SAxel Dörfler 	const BMailAccountSettings& settings)
4691af4fa4bSClemens Zeidler 	:
47062eec600SAxel Dörfler 	BMailProtocol(name, settings)
4711af4fa4bSClemens Zeidler {
472715bf3d1SAxel Dörfler 	LoadFilters(fAccountSettings.OutboundSettings());
4731af4fa4bSClemens Zeidler }
4741af4fa4bSClemens Zeidler 
4751af4fa4bSClemens Zeidler 
~BOutboundMailProtocol()476715bf3d1SAxel Dörfler BOutboundMailProtocol::~BOutboundMailProtocol()
4771af4fa4bSClemens Zeidler {
4781af4fa4bSClemens Zeidler }
4791af4fa4bSClemens Zeidler 
4801af4fa4bSClemens Zeidler 
48162eec600SAxel Dörfler status_t
SendMessages(const BMessage & files,off_t totalBytes)48262eec600SAxel Dörfler BOutboundMailProtocol::SendMessages(const BMessage& files, off_t totalBytes)
48362eec600SAxel Dörfler {
48462eec600SAxel Dörfler 	BMessage message(kMsgSendMessages);
48562eec600SAxel Dörfler 	message.Append(files);
48662eec600SAxel Dörfler 	message.AddInt64("bytes", totalBytes);
48762eec600SAxel Dörfler 
48862eec600SAxel Dörfler 	return BMessenger(this).SendMessage(&message);
48962eec600SAxel Dörfler }
49062eec600SAxel Dörfler 
49162eec600SAxel Dörfler 
4921af4fa4bSClemens Zeidler void
MessageReceived(BMessage * message)493715bf3d1SAxel Dörfler BOutboundMailProtocol::MessageReceived(BMessage* message)
4941af4fa4bSClemens Zeidler {
4951af4fa4bSClemens Zeidler 	switch (message->what) {
49662eec600SAxel Dörfler 		case kMsgSendMessages:
49762eec600SAxel Dörfler 			HandleSendMessages(*message, message->FindInt64("bytes"));
4981af4fa4bSClemens Zeidler 			break;
4991af4fa4bSClemens Zeidler 
5001af4fa4bSClemens Zeidler 		default:
501715bf3d1SAxel Dörfler 			BMailProtocol::MessageReceived(message);
5021af4fa4bSClemens Zeidler 	}
5031af4fa4bSClemens Zeidler }
504