MailProtocol.cpp revision 146357b5
10c7f804cSAxel Dörfler/*
2715bf3d1SAxel Dörfler * Copyright 2011-2012, Haiku, Inc. All rights reserved.
30c7f804cSAxel Dörfler * Copyright 2001-2003 Dr. Zoidberg Enterprises. All rights reserved.
40c7f804cSAxel Dörfler */
5f7215ac8SNathan Whitehorn
6f7215ac8SNathan Whitehorn
7715bf3d1SAxel Dörfler//#include <assert.h>
8dfc6cf01SPhilippe Houdoin#include <stdio.h>
9dfc6cf01SPhilippe Houdoin#include <stdlib.h>
10715bf3d1SAxel Dörfler
11715bf3d1SAxel Dörfler#include <fs_attr.h>
12dfc6cf01SPhilippe Houdoin
13f7215ac8SNathan Whitehorn#include <Alert.h>
14dfc6cf01SPhilippe Houdoin#include <Directory.h>
15dfc6cf01SPhilippe Houdoin#include <FindDirectory.h>
16f7215ac8SNathan Whitehorn#include <E-mail.h>
17715bf3d1SAxel Dörfler#include <Locker.h>
18dfc6cf01SPhilippe Houdoin#include <Node.h>
19f7215ac8SNathan Whitehorn#include <NodeInfo.h>
20f7215ac8SNathan Whitehorn#include <NodeMonitor.h>
21dfc6cf01SPhilippe Houdoin#include <Path.h>
22715bf3d1SAxel Dörfler#include <Query.h>
231af4fa4bSClemens Zeidler#include <Roster.h>
24dfc6cf01SPhilippe Houdoin#include <String.h>
25dfc6cf01SPhilippe Houdoin#include <StringList.h>
26dfc6cf01SPhilippe Houdoin#include <VolumeRoster.h>
27f7215ac8SNathan Whitehorn
28715bf3d1SAxel Dörfler#include <MailFilter.h>
29b8d2bbd6SClemens Zeidler#include <MailDaemon.h>
30f7215ac8SNathan Whitehorn#include <MailProtocol.h>
311af4fa4bSClemens Zeidler#include <MailSettings.h>
32dfc6cf01SPhilippe Houdoin
33715bf3d1SAxel Dörfler#include <mail_util.h>
34146357b5SAxel Dörfler#include <MailPrivate.h>
35715bf3d1SAxel Dörfler
361af4fa4bSClemens Zeidler#include "HaikuMailFormatFilter.h"
37f7215ac8SNathan Whitehorn
38dfc6cf01SPhilippe Houdoin
39146357b5SAxel Dörflerusing namespace BPrivate;
40f7215ac8SNathan Whitehorn
41f7215ac8SNathan Whitehorn
420c7f804cSAxel Dörflerconst uint32 kMsgDeleteMessage = '&DeM';
430c7f804cSAxel Dörflerconst uint32 kMsgAppendMessage = '&ApM';
440c7f804cSAxel Dörfler
450c7f804cSAxel Dörflerconst uint32 kMsgMoveFile = '&MoF';
460c7f804cSAxel Dörflerconst uint32 kMsgDeleteFile = '&DeF';
470c7f804cSAxel Dörflerconst uint32 kMsgFileRenamed = '&FiR';
480c7f804cSAxel Dörflerconst uint32 kMsgFileDeleted = '&FDe';
490c7f804cSAxel Dörflerconst uint32 kMsgInit = '&Ini';
500c7f804cSAxel Dörfler
510c7f804cSAxel Dörflerconst uint32 kMsgSendMessage = '&SeM';
520c7f804cSAxel Dörfler
530c7f804cSAxel Dörfler
54715bf3d1SAxel DörflerBMailProtocol::BMailProtocol(const BMailAccountSettings& settings)
551af4fa4bSClemens Zeidler	:
56715bf3d1SAxel Dörfler	fAccountSettings(settings),
57715bf3d1SAxel Dörfler	fMailNotifier(NULL)
58f7215ac8SNathan Whitehorn{
59715bf3d1SAxel Dörfler	AddFilter(new HaikuMailFormatFilter(*this, settings));
60f7215ac8SNathan Whitehorn}
61f7215ac8SNathan Whitehorn
62f7215ac8SNathan Whitehorn
63715bf3d1SAxel DörflerBMailProtocol::~BMailProtocol()
641af4fa4bSClemens Zeidler{
65715bf3d1SAxel Dörfler	delete fMailNotifier;
66eb9b1980SIngo Weinhold
67715bf3d1SAxel Dörfler	for (int i = 0; i < fFilterList.CountItems(); i++)
68715bf3d1SAxel Dörfler		delete fFilterList.ItemAt(i);
69dfc6cf01SPhilippe Houdoin
70146357b5SAxel Dörfler	std::map<entry_ref, image_id>::iterator it = fFilterImages.begin();
71715bf3d1SAxel Dörfler	for (; it != fFilterImages.end(); it++)
72715bf3d1SAxel Dörfler		unload_add_on(it->second);
731af4fa4bSClemens Zeidler}
74f7215ac8SNathan Whitehorn
75f7215ac8SNathan Whitehorn
76715bf3d1SAxel Dörflerconst BMailAccountSettings&
77715bf3d1SAxel DörflerBMailProtocol::AccountSettings() const
781af4fa4bSClemens Zeidler{
79715bf3d1SAxel Dörfler	return fAccountSettings;
801af4fa4bSClemens Zeidler}
81dfc6cf01SPhilippe Houdoin
82f7215ac8SNathan Whitehorn
831af4fa4bSClemens Zeidlervoid
84715bf3d1SAxel DörflerBMailProtocol::SetMailNotifier(BMailNotifier* mailNotifier)
851af4fa4bSClemens Zeidler{
86715bf3d1SAxel Dörfler	delete fMailNotifier;
87715bf3d1SAxel Dörfler	fMailNotifier = mailNotifier;
881af4fa4bSClemens Zeidler}
89f7215ac8SNathan Whitehorn
90dfc6cf01SPhilippe Houdoin
91715bf3d1SAxel DörflerBMailNotifier*
92715bf3d1SAxel DörflerBMailProtocol::MailNotifier() const
931af4fa4bSClemens Zeidler{
94715bf3d1SAxel Dörfler	return fMailNotifier;
951af4fa4bSClemens Zeidler}
96dfc6cf01SPhilippe Houdoin
97eb9b1980SIngo Weinhold
98715bf3d1SAxel Dörflerbool
99715bf3d1SAxel DörflerBMailProtocol::AddFilter(BMailFilter* filter)
100f7215ac8SNathan Whitehorn{
101715bf3d1SAxel Dörfler	BLocker locker(this);
102715bf3d1SAxel Dörfler	return fFilterList.AddItem(filter);
1031af4fa4bSClemens Zeidler}
1041af4fa4bSClemens Zeidler
105dfc6cf01SPhilippe Houdoin
106715bf3d1SAxel Dörflerint32
107715bf3d1SAxel DörflerBMailProtocol::CountFilter() const
1081af4fa4bSClemens Zeidler{
109715bf3d1SAxel Dörfler	BLocker locker(this);
110715bf3d1SAxel Dörfler	return fFilterList.CountItems();
1111af4fa4bSClemens Zeidler}
1121af4fa4bSClemens Zeidler
1131af4fa4bSClemens Zeidler
114715bf3d1SAxel DörflerBMailFilter*
115715bf3d1SAxel DörflerBMailProtocol::FilterAt(int32 index) const
1161af4fa4bSClemens Zeidler{
117715bf3d1SAxel Dörfler	BLocker locker(this);
118715bf3d1SAxel Dörfler	return fFilterList.ItemAt(index);
1191af4fa4bSClemens Zeidler}
1201af4fa4bSClemens Zeidler
1211af4fa4bSClemens Zeidler
122715bf3d1SAxel DörflerBMailFilter*
123715bf3d1SAxel DörflerBMailProtocol::RemoveFilter(int32 index)
1241af4fa4bSClemens Zeidler{
125715bf3d1SAxel Dörfler	BLocker locker(this);
126715bf3d1SAxel Dörfler	return fFilterList.RemoveItemAt(index);
1271af4fa4bSClemens Zeidler}
1281af4fa4bSClemens Zeidler
1291af4fa4bSClemens Zeidler
130715bf3d1SAxel Dörflerbool
131715bf3d1SAxel DörflerBMailProtocol::RemoveFilter(BMailFilter* filter)
1321af4fa4bSClemens Zeidler{
133715bf3d1SAxel Dörfler	BLocker locker(this);
134715bf3d1SAxel Dörfler	return fFilterList.RemoveItem(filter);
1351af4fa4bSClemens Zeidler}
1361af4fa4bSClemens Zeidler
1371af4fa4bSClemens Zeidler
138715bf3d1SAxel Dörflervoid
139715bf3d1SAxel DörflerBMailProtocol::MessageReceived(BMessage* message)
1401af4fa4bSClemens Zeidler{
141715bf3d1SAxel Dörfler	switch (message->what) {
142715bf3d1SAxel Dörfler		case kMsgMoveFile:
143715bf3d1SAxel Dörfler		{
144715bf3d1SAxel Dörfler			entry_ref file;
145715bf3d1SAxel Dörfler			message->FindRef("file", &file);
146715bf3d1SAxel Dörfler			entry_ref dir;
147715bf3d1SAxel Dörfler			message->FindRef("directory", &dir);
148715bf3d1SAxel Dörfler			BDirectory directory(&dir);
149715bf3d1SAxel Dörfler			MoveMessage(file, directory);
150715bf3d1SAxel Dörfler			break;
151715bf3d1SAxel Dörfler		}
1521af4fa4bSClemens Zeidler
153715bf3d1SAxel Dörfler		case kMsgDeleteFile:
154715bf3d1SAxel Dörfler		{
155715bf3d1SAxel Dörfler			entry_ref file;
156715bf3d1SAxel Dörfler			message->FindRef("file", &file);
157715bf3d1SAxel Dörfler			DeleteMessage(file);
158715bf3d1SAxel Dörfler			break;
159715bf3d1SAxel Dörfler		}
1601af4fa4bSClemens Zeidler
161715bf3d1SAxel Dörfler		case kMsgFileRenamed:
162715bf3d1SAxel Dörfler		{
163715bf3d1SAxel Dörfler			entry_ref from;
164715bf3d1SAxel Dörfler			message->FindRef("from", &from);
165715bf3d1SAxel Dörfler			entry_ref to;
166715bf3d1SAxel Dörfler			message->FindRef("to", &to);
167715bf3d1SAxel Dörfler			FileRenamed(from, to);
168715bf3d1SAxel Dörfler			break;
169715bf3d1SAxel Dörfler		}
1701af4fa4bSClemens Zeidler
171715bf3d1SAxel Dörfler		case kMsgFileDeleted:
172715bf3d1SAxel Dörfler		{
173715bf3d1SAxel Dörfler			node_ref node;
174715bf3d1SAxel Dörfler			message->FindInt32("device",&node.device);
175715bf3d1SAxel Dörfler			message->FindInt64("node", &node.node);
176715bf3d1SAxel Dörfler			FileDeleted(node);
177715bf3d1SAxel Dörfler			break;
178715bf3d1SAxel Dörfler		}
1791af4fa4bSClemens Zeidler
180715bf3d1SAxel Dörfler		default:
181715bf3d1SAxel Dörfler			BLooper::MessageReceived(message);
1821af4fa4bSClemens Zeidler	}
1831af4fa4bSClemens Zeidler}
1841af4fa4bSClemens Zeidler
1851af4fa4bSClemens Zeidler
186715bf3d1SAxel Dörflerstatus_t
187715bf3d1SAxel DörflerBMailProtocol::MoveMessage(const entry_ref& ref, BDirectory& dir)
1881af4fa4bSClemens Zeidler{
189715bf3d1SAxel Dörfler	BEntry entry(&ref);
190715bf3d1SAxel Dörfler	return entry.MoveTo(&dir);
1911af4fa4bSClemens Zeidler}
1921af4fa4bSClemens Zeidler
1931af4fa4bSClemens Zeidler
194715bf3d1SAxel Dörflerstatus_t
195715bf3d1SAxel DörflerBMailProtocol::DeleteMessage(const entry_ref& ref)
1961af4fa4bSClemens Zeidler{
197715bf3d1SAxel Dörfler	BEntry entry(&ref);
198715bf3d1SAxel Dörfler	return entry.Remove();
1991af4fa4bSClemens Zeidler}
2001af4fa4bSClemens Zeidler
2011af4fa4bSClemens Zeidler
2021af4fa4bSClemens Zeidlervoid
203715bf3d1SAxel DörflerBMailProtocol::FileRenamed(const entry_ref& from, const entry_ref& to)
2041af4fa4bSClemens Zeidler{
2051af4fa4bSClemens Zeidler}
206dfc6cf01SPhilippe Houdoin
207dfc6cf01SPhilippe Houdoin
2081af4fa4bSClemens Zeidlervoid
209715bf3d1SAxel DörflerBMailProtocol::FileDeleted(const node_ref& node)
2101af4fa4bSClemens Zeidler{
2111af4fa4bSClemens Zeidler}
2121af4fa4bSClemens Zeidler
2131af4fa4bSClemens Zeidler
2141af4fa4bSClemens Zeidlervoid
215715bf3d1SAxel DörflerBMailProtocol::ShowError(const char* error)
2161af4fa4bSClemens Zeidler{
217715bf3d1SAxel Dörfler	if (MailNotifier() != NULL)
218715bf3d1SAxel Dörfler		MailNotifier()->ShowError(error);
2191af4fa4bSClemens Zeidler}
2201af4fa4bSClemens Zeidler
2211af4fa4bSClemens Zeidler
2221af4fa4bSClemens Zeidlervoid
223715bf3d1SAxel DörflerBMailProtocol::ShowMessage(const char* message)
2241af4fa4bSClemens Zeidler{
225715bf3d1SAxel Dörfler	if (MailNotifier() != NULL)
226715bf3d1SAxel Dörfler		MailNotifier()->ShowMessage(message);
2271af4fa4bSClemens Zeidler}
2281af4fa4bSClemens Zeidler
2291af4fa4bSClemens Zeidler
2301af4fa4bSClemens Zeidlervoid
231715bf3d1SAxel DörflerBMailProtocol::SetTotalItems(uint32 items)
2321af4fa4bSClemens Zeidler{
233715bf3d1SAxel Dörfler	if (MailNotifier() != NULL)
234715bf3d1SAxel Dörfler		MailNotifier()->SetTotalItems(items);
2351af4fa4bSClemens Zeidler}
2361af4fa4bSClemens Zeidler
2371af4fa4bSClemens Zeidler
238715bf3d1SAxel Dörflervoid
239715bf3d1SAxel DörflerBMailProtocol::SetTotalItemsSize(uint64 size)
2401af4fa4bSClemens Zeidler{
241715bf3d1SAxel Dörfler	if (MailNotifier() != NULL)
242715bf3d1SAxel Dörfler		MailNotifier()->SetTotalItemsSize(size);
2431af4fa4bSClemens Zeidler}
2441af4fa4bSClemens Zeidler
2451af4fa4bSClemens Zeidler
246715bf3d1SAxel Dörflervoid
247715bf3d1SAxel DörflerBMailProtocol::ReportProgress(uint32 messages, uint64 bytes,
248715bf3d1SAxel Dörfler	const char* message)
2491af4fa4bSClemens Zeidler{
250715bf3d1SAxel Dörfler	if (MailNotifier() != NULL)
251715bf3d1SAxel Dörfler		MailNotifier()->ReportProgress(messages, bytes, message);
2521af4fa4bSClemens Zeidler}
2531af4fa4bSClemens Zeidler
2541af4fa4bSClemens Zeidler
255715bf3d1SAxel Dörflervoid
256715bf3d1SAxel DörflerBMailProtocol::ResetProgress(const char* message)
2571af4fa4bSClemens Zeidler{
258715bf3d1SAxel Dörfler	if (MailNotifier() != NULL)
259715bf3d1SAxel Dörfler		MailNotifier()->ResetProgress(message);
2601af4fa4bSClemens Zeidler}
2611af4fa4bSClemens Zeidler
2621af4fa4bSClemens Zeidler
2631af4fa4bSClemens Zeidlervoid
264715bf3d1SAxel DörflerBMailProtocol::NotifyNewMessagesToFetch(int32 count)
2651af4fa4bSClemens Zeidler{
2661af4fa4bSClemens Zeidler	ResetProgress();
267715bf3d1SAxel Dörfler	SetTotalItems(count);
2681af4fa4bSClemens Zeidler}
2691af4fa4bSClemens Zeidler
2701af4fa4bSClemens Zeidler
2711af4fa4bSClemens Zeidlervoid
272715bf3d1SAxel DörflerBMailProtocol::NotifyHeaderFetched(const entry_ref& ref, BFile* data)
2731af4fa4bSClemens Zeidler{
2741af4fa4bSClemens Zeidler	for (int i = 0; i < fFilterList.CountItems(); i++)
2751af4fa4bSClemens Zeidler		fFilterList.ItemAt(i)->HeaderFetched(ref, data);
2761af4fa4bSClemens Zeidler}
2771af4fa4bSClemens Zeidler
2781af4fa4bSClemens Zeidler
2791af4fa4bSClemens Zeidlervoid
280715bf3d1SAxel DörflerBMailProtocol::NotifyBodyFetched(const entry_ref& ref, BFile* data)
2811af4fa4bSClemens Zeidler{
2821af4fa4bSClemens Zeidler	for (int i = 0; i < fFilterList.CountItems(); i++)
2831af4fa4bSClemens Zeidler		fFilterList.ItemAt(i)->BodyFetched(ref, data);
2841af4fa4bSClemens Zeidler}
2851af4fa4bSClemens Zeidler
2861af4fa4bSClemens Zeidler
2871af4fa4bSClemens Zeidlervoid
288715bf3d1SAxel DörflerBMailProtocol::NotifyMessageReadyToSend(const entry_ref& ref, BFile* data)
2891af4fa4bSClemens Zeidler{
2901af4fa4bSClemens Zeidler	for (int i = 0; i < fFilterList.CountItems(); i++)
2911af4fa4bSClemens Zeidler		fFilterList.ItemAt(i)->MessageReadyToSend(ref, data);
2921af4fa4bSClemens Zeidler}
2931af4fa4bSClemens Zeidler
2941af4fa4bSClemens Zeidler
2951af4fa4bSClemens Zeidlervoid
296715bf3d1SAxel DörflerBMailProtocol::NotifyMessageSent(const entry_ref& ref, BFile* data)
297