vfs: check userland buffers before reading them.
[haiku.git] / src / preferences / mail / AutoConfigWindow.cpp
blob7138de25b94290f0883e678e1aacffa3f9792ba2
1 /*
2 * Copyright 2007-2015, Haiku, Inc. All rights reserved.
3 * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
4 * Distributed under the terms of the MIT License.
5 */
8 #include "AutoConfigWindow.h"
10 #include "AutoConfig.h"
11 #include "AutoConfigView.h"
13 #include <Alert.h>
14 #include <Application.h>
15 #include <Catalog.h>
16 #include <Directory.h>
17 #include <File.h>
18 #include <FindDirectory.h>
19 #include <LayoutBuilder.h>
20 #include <MailSettings.h>
21 #include <Message.h>
22 #include <Path.h>
24 #include <crypt.h>
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "AutoConfigWindow"
31 const float kSpacing = 10;
34 AutoConfigWindow::AutoConfigWindow(BRect rect, ConfigWindow *parent)
36 BWindow(rect, B_TRANSLATE("Create new account"), B_TITLED_WINDOW_LOOK,
37 B_MODAL_APP_WINDOW_FEEL, B_NOT_ZOOMABLE | B_AVOID_FRONT
38 | B_AUTO_UPDATE_SIZE_LIMITS, B_ALL_WORKSPACES),
39 fParentWindow(parent),
40 fAccount(NULL),
41 fMainConfigState(true),
42 fServerConfigState(false),
43 fAutoConfigServer(true)
45 fContainerView = new BGroupView("config container");
47 fBackButton = new BButton("back", B_TRANSLATE("Back"),
48 new BMessage(kBackMsg));
49 fBackButton->SetEnabled(false);
51 fNextButton = new BButton("next", B_TRANSLATE("Next"),
52 new BMessage(kOkMsg));
53 fNextButton->MakeDefault(true);
55 fMainView = new AutoConfigView(fAutoConfig);
56 fMainView->SetLabel(B_TRANSLATE("Account settings"));
57 fContainerView->AddChild(fMainView);
59 BLayoutBuilder::Group<>(this, B_VERTICAL)
60 .SetInsets(B_USE_DEFAULT_SPACING)
61 .Add(fContainerView)
62 .AddGroup(B_HORIZONTAL)
63 .AddGlue()
64 .Add(fBackButton)
65 .Add(fNextButton);
67 // Add a shortcut to close the window using Command-W
68 AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
72 AutoConfigWindow::~AutoConfigWindow()
77 void
78 AutoConfigWindow::MessageReceived(BMessage* msg)
80 status_t status = B_ERROR;
81 BAlert* invalidMailAlert = NULL;
83 switch (msg->what) {
84 case kOkMsg:
85 if (fMainConfigState) {
86 fMainView->GetBasicAccountInfo(fAccountInfo);
87 if (!fMainView->IsValidMailAddress(fAccountInfo.email)) {
88 invalidMailAlert = new BAlert("invalidMailAlert",
89 B_TRANSLATE("Enter a valid e-mail address."),
90 B_TRANSLATE("OK"));
91 invalidMailAlert->SetFlags(invalidMailAlert->Flags()
92 | B_CLOSE_ON_ESCAPE);
93 invalidMailAlert->Go();
94 return;
96 if (fAutoConfigServer) {
97 status = fAutoConfig.GetInfoFromMailAddress(
98 fAccountInfo.email.String(),
99 &fAccountInfo.providerInfo);
101 if (status == B_OK) {
102 fParentWindow->Lock();
103 GenerateBasicAccount();
104 fParentWindow->Unlock();
105 Quit();
107 fMainConfigState = false;
108 fServerConfigState = true;
109 fMainView->Hide();
111 fServerView = new ServerSettingsView(fAccountInfo);
112 fContainerView->AddChild(fServerView);
114 fBackButton->SetEnabled(true);
115 fNextButton->SetLabel(B_TRANSLATE("Finish"));
116 } else {
117 fServerView->GetServerInfo(fAccountInfo);
118 fParentWindow->Lock();
119 GenerateBasicAccount();
120 fParentWindow->Unlock();
121 Quit();
123 break;
125 case kBackMsg:
126 if (fServerConfigState) {
127 fServerView->GetServerInfo(fAccountInfo);
129 fMainConfigState = true;
130 fServerConfigState = false;
132 fContainerView->RemoveChild(fServerView);
133 delete fServerView;
135 fMainView->Show();
136 fBackButton->SetEnabled(false);
138 break;
140 case kServerChangedMsg:
141 fAutoConfigServer = false;
142 break;
144 default:
145 BWindow::MessageReceived(msg);
146 break;
151 bool
152 AutoConfigWindow::QuitRequested()
154 return true;
158 BMailAccountSettings*
159 AutoConfigWindow::GenerateBasicAccount()
161 if (!fAccount) {
162 fParentWindow->Lock();
163 fAccount = fParentWindow->AddAccount();
164 fParentWindow->Unlock();
167 fAccount->SetName(fAccountInfo.accountName.String());
168 fAccount->SetRealName(fAccountInfo.name.String());
169 fAccount->SetReturnAddress(fAccountInfo.email.String());
171 BMessage& inboundArchive = fAccount->InboundSettings();
172 inboundArchive.MakeEmpty();
173 BString inServerName;
174 int32 authType = 0;
175 int32 ssl = 0;
176 if (fAccountInfo.inboundType == IMAP) {
177 inServerName = fAccountInfo.providerInfo.imap_server;
178 ssl = fAccountInfo.providerInfo.ssl_imap;
179 fAccount->SetInboundAddOn("IMAP");
180 } else {
181 inServerName = fAccountInfo.providerInfo.pop_server;
182 authType = fAccountInfo.providerInfo.authentification_pop;
183 ssl = fAccountInfo.providerInfo.ssl_pop;
184 fAccount->SetInboundAddOn("POP3");
186 inboundArchive.AddString("server", inServerName);
187 inboundArchive.AddInt32("auth_method", authType);
188 inboundArchive.AddInt32("flavor", ssl);
189 inboundArchive.AddString("username", fAccountInfo.loginName);
190 set_passwd(&inboundArchive, "cpasswd", fAccountInfo.password);
191 inboundArchive.AddBool("leave_mail_on_server", true);
192 inboundArchive.AddBool("delete_remote_when_local", true);
194 BMessage& outboundArchive = fAccount->OutboundSettings();
195 outboundArchive.MakeEmpty();
196 fAccount->SetOutboundAddOn("SMTP");
197 outboundArchive.AddString("server",
198 fAccountInfo.providerInfo.smtp_server);
199 outboundArchive.AddString("username", fAccountInfo.loginName);
200 set_passwd(&outboundArchive, "cpasswd", fAccountInfo.password);
201 outboundArchive.AddInt32("auth_method",
202 fAccountInfo.providerInfo.authentification_smtp);
203 outboundArchive.AddInt32("flavor",
204 fAccountInfo.providerInfo.ssl_smtp);
206 fParentWindow->Lock();
207 fParentWindow->AccountUpdated(fAccount);
208 fParentWindow->Unlock();
210 return fAccount;