1 #include "AutoConfig.h"
2 #include "DNSQuery.h"
3
4 #include <Directory.h>
5 #include <Entry.h>
6 #include <File.h>
7 #include <FindDirectory.h>
8 #include <Path.h>
9 #include <stdio.h>
10
11
12 status_t
GetInfoFromMailAddress(const char * email,provider_info * info)13 AutoConfig::GetInfoFromMailAddress(const char* email, provider_info *info)
14 {
15 BString provider = ExtractProvider(email);
16
17 // first check the database
18 if (LoadProviderInfo(provider, info) == B_OK)
19 return B_OK;
20
21 // fallback try to read MX record
22 if (GetMXRecord(provider.String(), info) == B_OK)
23 return B_ENTRY_NOT_FOUND;
24
25 // if no MX record received guess a name
26 GuessServerName(provider.String(), info);
27 return B_ENTRY_NOT_FOUND;
28 }
29
30
31 status_t
GetMXRecord(const char * provider,provider_info * info)32 AutoConfig::GetMXRecord(const char* provider, provider_info *info)
33 {
34 BObjectList<mx_record> mxList(5, true);
35 DNSQuery dnsQuery;
36 if (dnsQuery.GetMXRecords(provider, &mxList) != B_OK)
37 return B_ERROR;
38
39 mx_record *mxRec = mxList.ItemAt(0);
40 if (mxRec == NULL)
41 return B_ERROR;
42
43 info->imap_server = mxRec->serverName;
44 info->pop_server = mxRec->serverName;
45 info->smtp_server = mxRec->serverName;
46
47 info->authentification_pop = 0;
48 info->authentification_smtp = 0;
49 info->username_pattern = 0;
50 return B_OK;
51
52 }
53
54
55 status_t
GuessServerName(const char * provider,provider_info * info)56 AutoConfig::GuessServerName(const char* provider, provider_info* info)
57 {
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;
64
65 info->authentification_pop = 0;
66 info->authentification_smtp = 0;
67 info->username_pattern = 0;
68 return B_OK;
69 }
70
71
72 void
PrintProviderInfo(provider_info * pInfo)73 AutoConfig::PrintProviderInfo(provider_info* pInfo)
74 {
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));
84 }
85
86
87 BString
ExtractProvider(const char * email)88 AutoConfig::ExtractProvider(const char* email)
89 {
90 BString emailS(email);
91 BString provider;
92 int32 at = emailS.FindLast("@");
93 emailS.CopyInto(provider, at + 1, emailS.Length() - at);
94 return provider;
95 }
96
97
98
99 status_t
LoadProviderInfo(const BString & provider,provider_info * info)100 AutoConfig::LoadProviderInfo(const BString &provider, provider_info* info)
101 {
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());
108
109 BFile infoFile(&infoDir, provider.String(), B_READ_ONLY);
110 if (infoFile.InitCheck() != B_OK)
111 return B_ENTRY_NOT_FOUND;
112
113 info->provider = provider;
114 if (ReadProviderInfo(&infoFile, info) == true)
115 return B_OK;
116
117 return B_ERROR;
118 }
119
120
121 bool
ReadProviderInfo(BNode * node,provider_info * info)122 AutoConfig::ReadProviderInfo(BNode *node, provider_info* info)
123 {
124 bool infoFound = false;
125 char buffer[255];
126
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;
133 }
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;
138 }
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;
143 }
144
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;
152 }
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;
158 }
159
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;
167 }
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;
173 }
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;
179 }
180
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;
188 }
189
190 return infoFound;
191 }
192
193