1 #include "AutoConfig.h"
7 #include <FindDirectory.h>
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
)
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
;
32 AutoConfig::GetMXRecord(const char* provider
, provider_info
*info
)
34 BObjectList
<mx_record
> mxList(5, true);
36 if (dnsQuery
.GetMXRecords(provider
, &mxList
) != B_OK
)
39 mx_record
*mxRec
= mxList
.ItemAt(0);
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;
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;
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
));
88 AutoConfig::ExtractProvider(const char* email
)
90 BString
emailS(email
);
92 int32 at
= emailS
.FindLast("@");
93 emailS
.CopyInto(provider
, at
+ 1, emailS
.Length() - at
);
100 AutoConfig::LoadProviderInfo(const BString
&provider
, provider_info
* info
)
103 status_t status
= find_directory(B_USER_SETTINGS_DIRECTORY
, &path
);
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)
122 AutoConfig::ReadProviderInfo(BNode
*node
, provider_info
* info
)
124 bool infoFound
= false;
129 size
= node
->ReadAttr(ATTR_NAME_POPSERVER
, B_STRING_TYPE
, 0, &buffer
, 255);
131 info
->pop_server
= buffer
;
134 size
= node
->ReadAttr(ATTR_NAME_IMAPSERVER
, B_STRING_TYPE
, 0, &buffer
, 255);
136 info
->imap_server
= buffer
;
139 size
= node
->ReadAttr(ATTR_NAME_SMTPSERVER
, B_STRING_TYPE
, 0, &buffer
, 255);
141 info
->smtp_server
= buffer
;
145 // authentication type
147 size
= node
->ReadAttr(ATTR_NAME_AUTHPOP
, B_INT32_TYPE
, 0, &authType
,
149 if (size
== sizeof(int32
)) {
150 info
->authentification_pop
= authType
;
153 size
= node
->ReadAttr(ATTR_NAME_AUTHSMTP
, B_INT32_TYPE
, 0, &authType
,
155 if (size
== sizeof(int32
)) {
156 info
->authentification_smtp
= authType
;
162 size
= node
->ReadAttr(ATTR_NAME_POPSSL
, B_INT32_TYPE
, 0, &ssl
,
164 if (size
== sizeof(int32
)) {
168 size
= node
->ReadAttr(ATTR_NAME_IMAPSSL
, B_INT32_TYPE
, 0, &ssl
,
170 if (size
== sizeof(int32
)) {
171 info
->ssl_imap
= ssl
;
174 size
= node
->ReadAttr(ATTR_NAME_SMTPSSL
, B_INT32_TYPE
, 0, &ssl
,
176 if (size
== sizeof(int32
)) {
177 info
->ssl_smtp
= ssl
;
183 size
= node
->ReadAttr(ATTR_NAME_USERNAME
, B_INT32_TYPE
, 0, &pattern
,
185 if (size
== sizeof(int32
)) {
186 info
->username_pattern
= pattern
;