forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobe.rb
413 lines (343 loc) · 11.9 KB
/
probe.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
module EmailHelper
class Probe
=begin
get result of probe
result = EmailHelper::Probe.full(
email: '[email protected]',
password: 'somepassword',
folder: 'some_folder', # optional im imap
)
returns on success
{
result: 'ok',
settings: {
inbound: {
adapter: 'imap',
options: {
host: 'imap.gmail.com',
port: 993,
ssl: true,
user: '[email protected]',
password: 'password',
folder: 'some_folder', # optional im imap
},
},
outbound: {
adapter: 'smtp',
options: {
host: 'smtp.gmail.com',
port: 25,
ssl: true,
user: '[email protected]',
password: 'password',
},
},
}
}
returns on fail
result = {
result: 'failed',
}
=end
def self.full(params)
user, domain = EmailHelper.parse_email(params[:email])
if !user || !domain
return {
result: 'invalid',
messages: {
email: 'Invalid email.'
},
}
end
# probe provider based settings
provider_map = EmailHelper.provider(params[:email], params[:password])
domains = [domain]
# get mx records, try to find provider based on mx records
mx_records = EmailHelper.mx_records(domain)
domains = domains.concat(mx_records)
provider_map.each do |_provider, settings|
domains.each do |domain_to_check|
next if domain_to_check !~ /#{settings[:domain]}/i
# add folder to config if needed
if !params[:folder].empty? && settings[:inbound] && settings[:inbound][:options]
settings[:inbound][:options][:folder] = params[:folder]
end
# probe inbound
Rails.logger.debug "INBOUND PROBE PROVIDER: #{settings[:inbound].inspect}"
result_inbound = EmailHelper::Probe.inbound(settings[:inbound])
Rails.logger.debug "INBOUND RESULT PROVIDER: #{result_inbound.inspect}"
next if result_inbound[:result] != 'ok'
# probe outbound
Rails.logger.debug "OUTBOUND PROBE PROVIDER: #{settings[:outbound].inspect}"
result_outbound = EmailHelper::Probe.outbound(settings[:outbound], params[:email])
Rails.logger.debug "OUTBOUND RESULT PROVIDER: #{result_outbound.inspect}"
next if result_outbound[:result] != 'ok'
return {
result: 'ok',
content_messages: result_inbound[:content_messages],
setting: settings,
}
end
end
# probe guess settings
# probe inbound
inbound_mx = EmailHelper.provider_inbound_mx(user, params[:email], params[:password], mx_records)
inbound_guess = EmailHelper.provider_inbound_guess(user, params[:email], params[:password], domain)
inbound_map = inbound_mx + inbound_guess
result = {
result: 'ok',
setting: {}
}
success = false
inbound_map.each do |config|
# add folder to config if needed
if !params[:folder].empty? && config[:options]
config[:options][:folder] = params[:folder]
end
Rails.logger.debug "INBOUND PROBE GUESS: #{config.inspect}"
result_inbound = EmailHelper::Probe.inbound(config)
Rails.logger.debug "INBOUND RESULT GUESS: #{result_inbound.inspect}"
next if result_inbound[:result] != 'ok'
success = true
result[:setting][:inbound] = config
result[:content_messages] = result_inbound[:content_messages]
break
end
# give up, no possible inbound found
if !success
return {
result: 'failed',
reason: 'inbound failed',
}
end
# probe outbound
outbound_mx = EmailHelper.provider_outbound_mx(user, params[:email], params[:password], mx_records)
outbound_guess = EmailHelper.provider_outbound_guess(user, params[:email], params[:password], domain)
outbound_map = outbound_mx + outbound_guess
success = false
outbound_map.each do |config|
Rails.logger.debug "OUTBOUND PROBE GUESS: #{config.inspect}"
result_outbound = EmailHelper::Probe.outbound(config, params[:email])
Rails.logger.debug "OUTBOUND RESULT GUESS: #{result_outbound.inspect}"
next if result_outbound[:result] != 'ok'
success = true
result[:setting][:outbound] = config
break
end
# give up, no possible outbound found
if !success
return {
result: 'failed',
reason: 'outbound failed',
}
end
Rails.logger.info "PROBE FULL SUCCESS: #{result.inspect}"
result
end
=begin
get result of inbound probe
result = EmailHelper::Probe.inbound(
adapter: 'imap',
settings: {
host: 'imap.gmail.com',
port: 993,
ssl: true,
user: '[email protected]',
password: 'password',
folder: 'some_folder', # optional
}
)
returns on success
{
result: 'ok'
}
returns on fail
result = {
result: 'invalid',
settings: {
host: 'imap.gmail.com',
port: 993,
ssl: true,
user: '[email protected]',
password: 'password',
folder: 'some_folder', # optional im imap
},
message: 'error message from used lib',
message_human: 'translated error message, readable for humans',
}
=end
def self.inbound(params)
adapter = params[:adapter].downcase
# validate adapter
if !EmailHelper.available_driver[:inbound][adapter.to_sym]
return {
result: 'failed',
message: "Unknown adapter '#{adapter}'",
}
end
# connection test
result_inbound = {}
begin
require "channel/driver/#{adapter.to_filename}"
driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
driver_instance = driver_class.new
result_inbound = driver_instance.fetch(params[:options], nil, 'check')
rescue => e
return {
result: 'invalid',
settings: params,
message: e.message,
message_human: translation(e.message),
invalid_field: invalid_field(e.message),
}
end
result_inbound
end
=begin
get result of outbound probe
result = EmailHelper::Probe.outbound(
{
adapter: 'smtp',
options: {
host: 'smtp.gmail.com',
port: 25,
ssl: true,
user: '[email protected]',
password: 'password',
}
},
'subject of probe email',
)
returns on success
{
result: 'ok'
}
returns on fail
result = {
result: 'invalid',
settings: {
host: 'stmp.gmail.com',
port: 25,
ssl: true,
user: '[email protected]',
password: 'password',
},
message: 'error message from used lib',
message_human: 'translated error message, readable for humans',
}
=end
def self.outbound(params, email, subject = nil)
adapter = params[:adapter].downcase
# validate adapter
if !EmailHelper.available_driver[:outbound][adapter.to_sym]
return {
result: 'failed',
message: "Unknown adapter '#{adapter}'",
}
end
# prepare test email
mail = if subject
{
from: email,
to: email,
subject: "Zammad Getting started Test Email #{subject}",
body: "This is a Test Email of Zammad to check if sending and receiving is working correctly.\n\nYou can ignore or delete this email.",
}
else
{
from: email,
to: '[email protected]',
subject: 'This is a Test Email',
body: "This is a Test Email of Zammad to verify if Zammad can send emails to an external address.\n\nIf you see this email, you can ignore and delete it.",
}
end
if subject
mail['X-Zammad-Test-Message'] = subject
end
mail['X-Zammad-Ignore'] = 'true'
mail['X-Loop'] = 'yes'
mail['Precedence'] = 'bulk'
mail['Auto-Submitted'] = 'auto-generated'
mail['X-Auto-Response-Suppress'] = 'All'
# test connection
begin
require "channel/driver/#{adapter.to_filename}"
driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
driver_instance = driver_class.new
driver_instance.send(
params[:options],
mail,
)
rescue => e
# check if sending email was ok, but mailserver rejected
if !subject
white_map = {
'Recipient address rejected' => true,
}
white_map.each do |key, _message|
next if e.message !~ /#{Regexp.escape(key)}/i
return {
result: 'ok',
settings: params,
notice: e.message,
}
end
end
return {
result: 'invalid',
settings: params,
message: e.message,
message_human: translation(e.message),
invalid_field: invalid_field(e.message),
}
end
{
result: 'ok',
}
end
def self.invalid_field(message_backend)
invalid_fields.each do |key, fields|
return fields if message_backend =~ /#{Regexp.escape(key)}/i
end
{}
end
def self.invalid_fields
{
'authentication failed' => { user: true, password: true },
'Username and Password not accepted' => { user: true, password: true },
'Incorrect username' => { user: true, password: true },
'Lookup failed' => { user: true },
'Invalid credentials' => { user: true, password: true },
'getaddrinfo: nodename nor servname provided, or not known' => { host: true },
'getaddrinfo: Name or service not known' => { host: true },
'No route to host' => { host: true },
'execution expired' => { host: true },
'Connection refused' => { host: true },
'Mailbox doesn\'t exist' => { folder: true },
'Folder doesn\'t exist' => { folder: true },
'Unknown Mailbox' => { folder: true },
}
end
def self.translation(message_backend)
translations.each do |key, message_human|
return message_human if message_backend =~ /#{Regexp.escape(key)}/i
end
nil
end
def self.translations
{
'authentication failed' => 'Authentication failed!',
'Username and Password not accepted' => 'Authentication failed!',
'Incorrect username' => 'Authentication failed, username incorrect!',
'Lookup failed' => 'Authentication failed, username incorrect!',
'Invalid credentials' => 'Authentication failed, invalid credentials!',
'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
'getaddrinfo: Name or service not known' => 'Hostname not found!',
'No route to host' => 'No route to host!',
'execution expired' => 'Host not reachable!',
'Connection refused' => 'Connection refused!',
}
end
end
end