fat: Greatly simplify and clean up dosfs_get_file_map().
[haiku.git] / src / preferences / mail / AutoConfig.cpp
blob45d612b3324b2a41a901c05c3e21f99d454f2757
1 #include "AutoConfig.h"
2 #include "DNSQuery.h"
4 #include <Directory.h>
5 #include <Entry.h>
6 #include <File.h>
7 #include <FindDirectory.h>
8 #include <Path.h>
9 #include <stdio.h>
12 status_t
13 AutoConfig::GetInfoFromMailAddress(const char* email, provider_info *info)
15 BString provider = ExtractProvider(email);
17 // first check the database
18 if (LoadProviderInfo(provider, info) == B_OK)
19 return B_OK;
21 // fallback try to read MX record
22 if (GetMXRecord(provider.String(), info) == B_OK)
23 return B_ENTRY_NOT_FOUND;
25 // if no MX record received guess a name
26 GuessServerName(provider.String(), info);
27 return B_ENTRY_NOT_FOUND;
31 status_t
32 AutoConfig::GetMXRecord(const char* provider, provider_info *info)
34 BObjectList<mx_record> mxList(5, true);
35 DNSQuery dnsQuery;
36 if (dnsQuery.GetMXRecords(provider, &mxList) != B_OK)
37 return B_ERROR;
39 mx_record *mxRec = mxList.ItemAt(0);
40 if (mxRec == NULL)
41 return B_ERROR;
43 info->imap_server = mxRec->serverName;
44 info->pop_server = mxRec->serverName;
45 info->smtp_server = mxRec->serverName;
47 info->authentification_pop = 0;
48 info->authentification_smtp = 0;
49 info->username_pattern = 0;
50 return B_OK;
55 status_t
56 AutoConfig::GuessServerName(const char* provider, provider_info* info)
58 info->imap_server = "mail.";
59 info->imap_server += provider;
60 info->pop_server = "mail.";
61 info->pop_server += provider;
62 info->smtp_server = "mail.";
63 info->smtp_server += provider;
65 info->authentification_pop = 0;
66 info->authentification_smtp = 0;
67 info->username_pattern = 0;
68 return B_OK;
72 void
73 AutoConfig::PrintProviderInfo(provider_info* pInfo)
75 printf("Provider: %s:\n", pInfo->provider.String());
76 printf("pop_mail_host: %s\n", pInfo->pop_server.String());
77 printf("imap_mail_host: %s\n", pInfo->imap_server.String());
78 printf("smtp_host: %s\n", pInfo->smtp_server.String());
79 printf("pop authentication: %i\n", int(pInfo->authentification_pop));
80 printf("smtp authentication: %i\n",
81 int(pInfo->authentification_smtp));
82 printf("username_pattern: %i\n",
83 int(pInfo->username_pattern));
87 BString
88 AutoConfig::ExtractProvider(const char* email)
90 BString emailS(email);
91 BString provider;
92 int32 at = emailS.FindLast("@");
93 emailS.CopyInto(provider, at + 1, emailS.Length() - at);
94 return provider;
99 status_t
100 AutoConfig::LoadProviderInfo(const BString &provider, provider_info* info)
102 BPath path;
103 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
104 if (status != B_OK)
105 return status;
106 path.Append(INFO_DIR);
107 BDirectory infoDir(path.Path());
109 BFile infoFile(&infoDir, provider.String(), B_READ_ONLY);
110 if (infoFile.InitCheck() != B_OK)
111 return B_ENTRY_NOT_FOUND;
113 info->provider = provider;
114 if (ReadProviderInfo(&infoFile, info) == true)
115 return B_OK;
117 return B_ERROR;
121 bool
122 AutoConfig::ReadProviderInfo(BNode *node, provider_info* info)
124 bool infoFound = false;
125 char buffer[255];
127 // server
128 ssize_t size;
129 size = node->ReadAttr(ATTR_NAME_POPSERVER, B_STRING_TYPE, 0, &buffer, 255);
130 if (size > 0) {
131 info->pop_server = buffer;
132 infoFound = true;
134 size = node->ReadAttr(ATTR_NAME_IMAPSERVER, B_STRING_TYPE, 0, &buffer, 255);
135 if (size > 0) {
136 info->imap_server = buffer;
137 infoFound = true;
139 size = node->ReadAttr(ATTR_NAME_SMTPSERVER, B_STRING_TYPE, 0, &buffer, 255);
140 if (size > 0) {
141 info->smtp_server = buffer;
142 infoFound = true;
145 // authentication type
146 int32 authType;
147 size = node->ReadAttr(ATTR_NAME_AUTHPOP, B_INT32_TYPE, 0, &authType,
148 sizeof(int32));
149 if (size == sizeof(int32)) {
150 info->authentification_pop = authType;
151 infoFound = true;
153 size = node->ReadAttr(ATTR_NAME_AUTHSMTP, B_INT32_TYPE, 0, &authType,
154 sizeof(int32));
155 if (size == sizeof(int32)) {
156 info->authentification_smtp = authType;
157 infoFound = true;
160 // ssl
161 int32 ssl;
162 size = node->ReadAttr(ATTR_NAME_POPSSL, B_INT32_TYPE, 0, &ssl,
163 sizeof(int32));
164 if (size == sizeof(int32)) {
165 info->ssl_pop = ssl;
166 infoFound = true;
168 size = node->ReadAttr(ATTR_NAME_IMAPSSL, B_INT32_TYPE, 0, &ssl,
169 sizeof(int32));
170 if (size == sizeof(int32)) {
171 info->ssl_imap = ssl;
172 infoFound = true;
174 size = node->ReadAttr(ATTR_NAME_SMTPSSL, B_INT32_TYPE, 0, &ssl,
175 sizeof(int32));
176 if (size == sizeof(int32)) {
177 info->ssl_smtp = ssl;
178 infoFound = true;
181 // username pattern
182 int32 pattern;
183 size = node->ReadAttr(ATTR_NAME_USERNAME, B_INT32_TYPE, 0, &pattern,
184 sizeof(int32));
185 if (size == sizeof(int32)) {
186 info->username_pattern = pattern;
187 infoFound = true;
190 return infoFound;