1/*
2 * Copyright 2004-2012, 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
9
10//!	C-mail API - compatibility function (stubs) for the old mail kit
11
12
13#include <stdlib.h>
14#include <string.h>
15
16#include <Directory.h>
17#include <E-mail.h>
18#include <File.h>
19#include <FindDirectory.h>
20#include <List.h>
21#include <Path.h>
22
23#include <crypt.h>
24#include <MailDaemon.h>
25#include <MailMessage.h>
26#include <MailSettings.h>
27
28
29_EXPORT status_t
30check_for_mail(int32* _incomingCount)
31{
32	status_t status = BMailDaemon().CheckMail();
33	if (status != B_OK)
34		return status;
35
36	if (_incomingCount != NULL)
37		*_incomingCount = BMailDaemon().CountNewMessages(true);
38
39	return B_OK;
40}
41
42
43_EXPORT status_t
44send_queued_mail(void)
45{
46	return BMailDaemon().SendQueuedMail();
47}
48
49
50_EXPORT int32
51count_pop_accounts(void)
52{
53	BMailAccounts accounts;
54	return accounts.CountAccounts();
55}
56
57
58_EXPORT status_t
59get_mail_notification(mail_notification *notification)
60{
61	notification->alert = true;
62	notification->beep = false;
63	return B_OK;
64}
65
66
67_EXPORT status_t
68set_mail_notification(mail_notification *, bool)
69{
70	return B_NO_REPLY;
71}
72
73
74_EXPORT status_t
75get_pop_account(mail_pop_account* account, int32 index)
76{
77	BMailAccounts accounts;
78	BMailAccountSettings* accountSettings = accounts.AccountAt(index);
79	if (accountSettings == NULL)
80		return B_BAD_INDEX;
81
82	const BMessage& settings = accountSettings->InboundSettings();
83	strcpy(account->pop_name, settings.FindString("username"));
84	strcpy(account->pop_host, settings.FindString("server"));
85	strcpy(account->real_name, accountSettings->RealName());
86	strcpy(account->reply_to, accountSettings->ReturnAddress());
87
88	const char* encryptedPassword = get_passwd(&settings, "cpasswd");
89	const char* password = encryptedPassword;
90	if (password == NULL)
91		password = settings.FindString("password");
92	strcpy(account->pop_password, password);
93
94	delete[] encryptedPassword;
95	return B_OK;
96}
97
98
99_EXPORT status_t
100set_pop_account(mail_pop_account *, int32, bool)
101{
102	return B_NO_REPLY;
103}
104
105
106_EXPORT status_t
107get_smtp_host(char* buffer)
108{
109	BMailAccounts accounts;
110	BMailAccountSettings* account = accounts.AccountAt(
111		BMailSettings().DefaultOutboundAccount());
112	if (account == NULL)
113		return B_ERROR;
114
115	const BMessage& settings = account->OutboundSettings();
116
117	if (!settings.HasString("server"))
118		return B_NAME_NOT_FOUND;
119
120	strcpy(buffer, settings.FindString("server"));
121	return B_OK;
122}
123
124
125_EXPORT status_t
126set_smtp_host(char * /* host */, bool /* save */)
127{
128	return B_NO_REPLY;
129}
130
131
132_EXPORT status_t
133forward_mail(entry_ref *ref, const char *recipients, bool now)
134{
135	BFile file(ref, O_RDONLY);
136	status_t status = file.InitCheck();
137	if (status < B_OK)
138		return status;
139
140	BEmailMessage mail(&file);
141	mail.SetTo(recipients);
142
143	return mail.Send(now);
144}
145