yandex-pdd 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yandex/pdd.rb +382 -0
- metadata +4 -4
- data/lib/yandex-pdd.rb +0 -5
data/lib/yandex/pdd.rb
ADDED
@@ -0,0 +1,382 @@
|
|
1
|
+
module Yandex
|
2
|
+
class Pdd
|
3
|
+
API_URL = 'https://pddimp.yandex.ru/'
|
4
|
+
TIMEOUT = 10
|
5
|
+
POP3_PORT = 110
|
6
|
+
IMAP_PORT = 143
|
7
|
+
POP3 = 'pop3'
|
8
|
+
IMAP = 'imap'
|
9
|
+
IMPORT_METHOD = POP3
|
10
|
+
HTTP_ERROR = 'HTTP_ERROR'
|
11
|
+
|
12
|
+
NOT_AUTHORIZED = 'NOT_AUTHORIZED'
|
13
|
+
INVALID_RESPONSE = 'INVALID_RESPONSE'
|
14
|
+
REQUEST_FAILED = 'REQUEST_FAILED'
|
15
|
+
|
16
|
+
USER_NOT_FOUND = 'USER_NOT_FOUND'
|
17
|
+
LOGIN_OCCUPIED = 'LOGIN_OCCUPIED'
|
18
|
+
LOGIN_TOO_SHORT = 'LOGIN_TOO_SHORT'
|
19
|
+
LOGIN_TOO_LONG = 'LOGIN_TOO_LONG'
|
20
|
+
INVALID_LOGIN = 'INVALID_LOGIN'
|
21
|
+
|
22
|
+
INVALID_PASSWORD = 'INVALID_PASSWORD'
|
23
|
+
PASSWORD_TOO_SHORT = 'PASSWORD_TOO_SHORT'
|
24
|
+
PASSWORD_TOO_LONG = 'PASSWORD_TOO_LONG'
|
25
|
+
|
26
|
+
CANT_CREATE_ACCOUNT = 'CANT_CREATE_ACCOUNT'
|
27
|
+
USER_LIMIT_EXCEEDED = 'USER_LIMIT_EXCEEDED'
|
28
|
+
|
29
|
+
NO_IMPORT_SETTINGS = 'NO_IMPORT_SETTINGS'
|
30
|
+
|
31
|
+
SERVICE_ERROR = 'SERVICE_ERROR'
|
32
|
+
UNKNOWN_ERROR = 'UNKNOWN_ERROR'
|
33
|
+
|
34
|
+
ERR_R = {
|
35
|
+
'not authenticated' => NOT_AUTHORIZED,
|
36
|
+
'not_authorized' => NOT_AUTHORIZED,
|
37
|
+
'no_login' => INVALID_LOGIN,
|
38
|
+
'bad_login' => INVALID_LOGIN,
|
39
|
+
'no_user' => USER_NOT_FOUND,
|
40
|
+
'not_found' => USER_NOT_FOUND,
|
41
|
+
'user_not_found' => USER_NOT_FOUND,
|
42
|
+
'no such user registered' => USER_NOT_FOUND,
|
43
|
+
'occupied' => LOGIN_OCCUPIED,
|
44
|
+
'login_short' => LOGIN_TOO_SHORT,
|
45
|
+
'badlogin_length' => LOGIN_TOO_LONG,
|
46
|
+
'passwd-badpasswd' => INVALID_PASSWORD,
|
47
|
+
'passwd-tooshort' => PASSWORD_TOO_SHORT,
|
48
|
+
'passwd-toolong' => PASSWORD_TOO_LONG,
|
49
|
+
'hundred_users_limit' => USER_LIMIT_EXCEEDED,
|
50
|
+
|
51
|
+
'no-passwd_cryptpasswd' => INVALID_PASSWORD,
|
52
|
+
'cant_create_account' => CANT_CREATE_ACCOUNT,
|
53
|
+
|
54
|
+
'no_import_settings' => NO_IMPORT_SETTINGS,
|
55
|
+
'no import info on this user' => USER_NOT_FOUND,
|
56
|
+
'unknown' => REQUEST_FAILED,
|
57
|
+
}
|
58
|
+
|
59
|
+
attr_accessor :request, :response
|
60
|
+
attr_reader :error, :http_error
|
61
|
+
|
62
|
+
def initialize(token, cert_file=nil)
|
63
|
+
@token = token.to_s
|
64
|
+
@cert_file = cert_file.to_s
|
65
|
+
@timeout = TIMEOUT
|
66
|
+
end
|
67
|
+
|
68
|
+
def is_user_exists(login)
|
69
|
+
url = API_URL + 'check_user.xml?token=' + @token + '&login=' + quote_encode(login)
|
70
|
+
return nil unless make_request(url)
|
71
|
+
if ( result = get_node_text('/page/result') )
|
72
|
+
return true if ( 'exists' == result )
|
73
|
+
return false if ( 'nouser' == result )
|
74
|
+
end
|
75
|
+
return unknown_error()
|
76
|
+
end
|
77
|
+
|
78
|
+
alias is_user_exists? is_user_exists
|
79
|
+
|
80
|
+
def create_user(login, password, is_encrypted=false)
|
81
|
+
url = API_URL
|
82
|
+
if (is_encrypted)
|
83
|
+
url += 'reg_user_crypto.xml?token=' + @token + '&login=' + quote_encode(login) +
|
84
|
+
'&password=' + quote_encode(password)
|
85
|
+
else
|
86
|
+
url += 'reg_user_token.xml?token=' + @token + '&u_login=' + quote_encode(login) +
|
87
|
+
'&u_password=' + quote_encode(password)
|
88
|
+
end
|
89
|
+
return nil unless make_request(url)
|
90
|
+
if ( uid = get_node_attr('/page/ok/@uid') )
|
91
|
+
return uid
|
92
|
+
end
|
93
|
+
return unknown_error()
|
94
|
+
end
|
95
|
+
|
96
|
+
def create_user_encryped(login, password)
|
97
|
+
return create_user(login, password, true)
|
98
|
+
end
|
99
|
+
|
100
|
+
def update_user(login, data)
|
101
|
+
url = API_URL + 'edit_user.xml?token=' + @token + '&login=' + quote_encode(login) +
|
102
|
+
'&password=' + quote_encode(data[:password]) +
|
103
|
+
'&iname=' + quote_encode(data[:iname] ) +
|
104
|
+
'&fname=' + quote_encode(data[:fname] ) +
|
105
|
+
'&sex=' + quote_encode(data[:sex] ) +
|
106
|
+
'&hintq=' + quote_encode(data[:hintq] ) +
|
107
|
+
'&hinta=' + quote_encode(data[:hinta] )
|
108
|
+
|
109
|
+
return nil unless make_request(url)
|
110
|
+
|
111
|
+
if ( uid = get_node_attr('/page/ok/@uid') )
|
112
|
+
return uid
|
113
|
+
end
|
114
|
+
return unknown_error()
|
115
|
+
end
|
116
|
+
|
117
|
+
def delete_user(login)
|
118
|
+
url = API_URL + 'delete_user.xml?token=' + @token + '&login=' + quote_encode(login)
|
119
|
+
return nil unless make_request(url)
|
120
|
+
return true if is_response_ok
|
121
|
+
return unknown_error()
|
122
|
+
end
|
123
|
+
|
124
|
+
def get_unread_count(login)
|
125
|
+
url = API_URL + 'get_mail_info.xml?token=' + @token + '&login=' + quote_encode(login)
|
126
|
+
return nil unless make_request(url)
|
127
|
+
|
128
|
+
if ( count = get_node_attr('/page/ok/@new_messages') )
|
129
|
+
return count
|
130
|
+
end
|
131
|
+
|
132
|
+
return unknown_error()
|
133
|
+
end
|
134
|
+
|
135
|
+
def set_forward(login, email, save_copy='no')
|
136
|
+
save_copy = (save_copy and not 'no' == save_copy) ? 'yes' : 'no'
|
137
|
+
url = API_URL + 'set_forward.xml?token=' + @token + '&login=' + quote_encode(login) +
|
138
|
+
'&address=' + quote_encode(address) +
|
139
|
+
'©=' + save_copy
|
140
|
+
|
141
|
+
return nil unless make_request(url)
|
142
|
+
|
143
|
+
return true if is_response_ok
|
144
|
+
return unknown_error()
|
145
|
+
end
|
146
|
+
|
147
|
+
def get_user(login)
|
148
|
+
url = API_URL + 'get_user_info.xml?token=' + @token + '&login=' + quote_encode(login)
|
149
|
+
return nil unless make_request(url)
|
150
|
+
user = {
|
151
|
+
:domain => get_node_text('/page/domain/name'),
|
152
|
+
:login => get_node_text('/page/domain/user/login'),
|
153
|
+
:birth_date => get_node_text('/page/domain/user/birth_date'),
|
154
|
+
:fname => get_node_text('/page/domain/user/fname'),
|
155
|
+
:iname => get_node_text('/page/domain/user/iname'),
|
156
|
+
:hinta => get_node_text('/page/domain/user/hinta'),
|
157
|
+
:hintq => get_node_text('/page/domain/user/hintq'),
|
158
|
+
:mail_format => get_node_text('/page/domain/user/mail_format'),
|
159
|
+
:charset => get_node_text('/page/domain/user/charset'),
|
160
|
+
:nickname => get_node_text('/page/domain/user/nickname'),
|
161
|
+
:sex => get_node_text('/page/domain/user/sex'),
|
162
|
+
:enabled => get_node_text('/page/domain/user/enabled'),
|
163
|
+
:signed_eula => get_node_text('/page/domain/user/signed_eula'),
|
164
|
+
}
|
165
|
+
|
166
|
+
return user
|
167
|
+
end
|
168
|
+
|
169
|
+
def get_user_list(page=1, per_page=100)
|
170
|
+
url = API_URL + 'get_domain_users.xml?token=' + @token + '&page=%20' + quote_encode(page) + # HACK XXX
|
171
|
+
'&per_page=' + quote_encode(per_page)
|
172
|
+
return nil unless make_request(url)
|
173
|
+
|
174
|
+
emails = []
|
175
|
+
|
176
|
+
get_node('/page/domains/domain/emails/email/name').each do |n|
|
177
|
+
emails.push(n.inner_xml) unless n.nil?
|
178
|
+
end
|
179
|
+
|
180
|
+
data = {
|
181
|
+
:action_status => get_node_text('/page/domains/domain/emails/action-status'),
|
182
|
+
:found => get_node_text('/page/domains/domain/emails/found'),
|
183
|
+
:total => get_node_text('/page/domains/domain/emails/total'),
|
184
|
+
:domain => get_node_text('/page/domains/domain/name'),
|
185
|
+
:status => get_node_text('/page/domains/domain/status'),
|
186
|
+
:emails_max_count => get_node_text('/page/domains/domain/emails-max-count'),
|
187
|
+
:emails => emails,
|
188
|
+
}
|
189
|
+
|
190
|
+
return data
|
191
|
+
end
|
192
|
+
|
193
|
+
def prepare_import(server, data={})
|
194
|
+
data[:method] = 'pop3' unless ( data[:method] and data[:method] !~ /^pop3|imap$/i )
|
195
|
+
|
196
|
+
url = API_URL + 'set_domain.xml?token=' + @token + '&ext_serv=' + server + '&method=' + data[:method]
|
197
|
+
url += '&ext_port=' + data[:port] if ( data[:port] )
|
198
|
+
url += '&callback=' + data[:callback] if ( data[:callback] )
|
199
|
+
url += '&isssl=no' unless ( data[:use_ssl] )
|
200
|
+
|
201
|
+
return nil unless make_request(url)
|
202
|
+
|
203
|
+
return true if is_response_ok
|
204
|
+
return unknown_error()
|
205
|
+
end
|
206
|
+
|
207
|
+
def start_import(login, data)
|
208
|
+
url = API_URL + 'start_import.xml?token=' + @token + '&login=' + quote_encode(login) +
|
209
|
+
'&ext_login=' + quote_encode(data[:login]) + '&password=' + quote_encode(data[:password])
|
210
|
+
|
211
|
+
return nil unless make_request(url)
|
212
|
+
return true
|
213
|
+
end
|
214
|
+
|
215
|
+
def import_user(login, password, data)
|
216
|
+
data[:login] ||= login
|
217
|
+
data[:save_copy] = ( data[:save_copy] ) ? 'yes' : '0'
|
218
|
+
url = API_URL + 'reg_and_imp.xml?token=' + @token +
|
219
|
+
'&login=' + quote_encode(login) +
|
220
|
+
'&inn_password=' + quote_encode(password) +
|
221
|
+
'&ext_login=' + quote_encode(data[:login]) +
|
222
|
+
'&ext_password=' + quote_encode(data[:password]) +
|
223
|
+
'&fwd_email=' + quote_encode(data[:forward_to]) +
|
224
|
+
'&fwd_copy=' + quote_encode(data[:save_copy])
|
225
|
+
|
226
|
+
return nil unless make_request(url)
|
227
|
+
return true if is_response_ok
|
228
|
+
return unknown_error()
|
229
|
+
end
|
230
|
+
|
231
|
+
def get_import_status(login)
|
232
|
+
url = API_URL + 'check_import.xml?token=' + @token + '&login=' + quote_encode(login)
|
233
|
+
return nil unless make_request(url)
|
234
|
+
data = {
|
235
|
+
:last_check => get_node_attr('/page/ok/@last_check'),
|
236
|
+
:imported => get_node_attr('/page/ok/@imported'),
|
237
|
+
:state => get_node_attr('/page/ok/@state'),
|
238
|
+
}
|
239
|
+
|
240
|
+
return data
|
241
|
+
end
|
242
|
+
|
243
|
+
# # fails for registered users
|
244
|
+
# def import_imap_folder(login, data)
|
245
|
+
#
|
246
|
+
# data[:login] ||= login
|
247
|
+
#
|
248
|
+
# my $url = API_URL + 'import_imap.xml?token=' + @token + '&login=' + quote_encode(login) +
|
249
|
+
# '&ext_login=' + quote_encode(data[:login]) +
|
250
|
+
## '&int_password=' + quote_encode(password) +
|
251
|
+
# '&ext_password=' + quote_encode(data[:password]) +
|
252
|
+
# '©_one_folder=' + quote_encode(data[:folder])
|
253
|
+
# return nil unless make_request(url)
|
254
|
+
#
|
255
|
+
# return true if is_response_ok
|
256
|
+
# return unknown_error()
|
257
|
+
# end
|
258
|
+
|
259
|
+
def stop_import(login)
|
260
|
+
return nil unless (login)
|
261
|
+
url = API_URL + 'stop_import.xml?token=' + @token + '&login=' + quote_encode(login)
|
262
|
+
return nil unless make_request(url)
|
263
|
+
return true if is_response_ok
|
264
|
+
return unknown_error()
|
265
|
+
end
|
266
|
+
|
267
|
+
private
|
268
|
+
def reset_error
|
269
|
+
@error = nil
|
270
|
+
@http_error = nil
|
271
|
+
end
|
272
|
+
|
273
|
+
def get_node(xpath, xml=nil)
|
274
|
+
xml = @xml unless xml
|
275
|
+
return xml.find(xpath)
|
276
|
+
end
|
277
|
+
|
278
|
+
def get_node_text(xpath, xml=nil)
|
279
|
+
xml = @xml unless xml
|
280
|
+
node = get_node(xpath, xml).first
|
281
|
+
return nil if node.nil?
|
282
|
+
return node.inner_xml
|
283
|
+
end
|
284
|
+
|
285
|
+
def get_node_attr(xpath, xml=nil)
|
286
|
+
xml = @xml unless xml
|
287
|
+
a = xml.find(xpath).first
|
288
|
+
return nil if a.nil?
|
289
|
+
return a.value
|
290
|
+
end
|
291
|
+
|
292
|
+
def identify_error(error)
|
293
|
+
return ERR_R[ error.split(',').first ] || REQUEST_FAILED
|
294
|
+
end
|
295
|
+
|
296
|
+
def set_error(code, info, is_http=false)
|
297
|
+
if (is_http)
|
298
|
+
@error = { :code => HTTP_ERROR }
|
299
|
+
@http_error = { :code => code, :info => info }
|
300
|
+
else
|
301
|
+
@error = { :code => code, :info => info }
|
302
|
+
@http_error = nil
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
def unknown_error
|
307
|
+
set_error( UNKNOWN_ERROR, @response ? @response.body : '' )
|
308
|
+
return nil
|
309
|
+
end
|
310
|
+
|
311
|
+
def handle_http_error
|
312
|
+
set_error( @response.code, @response.body, true )
|
313
|
+
end
|
314
|
+
|
315
|
+
def handle_error
|
316
|
+
set_error( identify_error( @_error ), @_error )
|
317
|
+
end
|
318
|
+
|
319
|
+
def parse_response
|
320
|
+
@xml = LibXML::XML::Parser.string( @response.body ).parse
|
321
|
+
if ( @_error = get_node_attr('/page/error/@reason') )
|
322
|
+
handle_error()
|
323
|
+
return nil
|
324
|
+
end
|
325
|
+
|
326
|
+
if ( get_node_attr('/page/xscript_invoke_failed/@error') )
|
327
|
+
info = ''
|
328
|
+
[ 'error', 'block', 'method', 'object', 'exception' ].each do |s|
|
329
|
+
info += s + ': "' + get_node_attr('/page/xscript_invoke_failed/@' + s) + '" '
|
330
|
+
end
|
331
|
+
|
332
|
+
set_error(SERVICE_ERROR, info)
|
333
|
+
return nil
|
334
|
+
end
|
335
|
+
return true
|
336
|
+
rescue
|
337
|
+
@xml = nil
|
338
|
+
set_error(INVALID_RESPONSE, (@response) ? @response.body : '')
|
339
|
+
return nil
|
340
|
+
end
|
341
|
+
|
342
|
+
def is_response_ok
|
343
|
+
return (not get_node('/page/ok').first.nil?)
|
344
|
+
end
|
345
|
+
|
346
|
+
alias is_response_ok? is_response_ok
|
347
|
+
|
348
|
+
def make_request(url)
|
349
|
+
reset_error()
|
350
|
+
@uri = URI::parse(url)
|
351
|
+
@request = Net::HTTP::new(@uri.host, @uri.port)
|
352
|
+
@request.open_timeout = @timeout
|
353
|
+
@request.read_timeout = @timeout
|
354
|
+
@request.use_ssl = true
|
355
|
+
|
356
|
+
if @cert_file
|
357
|
+
@request.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
358
|
+
@request.ca_file = @cert_file
|
359
|
+
end
|
360
|
+
|
361
|
+
@response = @request.get(@uri.request_uri)
|
362
|
+
|
363
|
+
unless ( @response.is_a?(Net::HTTPSuccess) )
|
364
|
+
handle_http_error()
|
365
|
+
return nil
|
366
|
+
end
|
367
|
+
|
368
|
+
return parse_response()
|
369
|
+
|
370
|
+
rescue Exception => e
|
371
|
+
set_error(SERVICE_ERROR, e.to_s)
|
372
|
+
return nil
|
373
|
+
end
|
374
|
+
|
375
|
+
def quote_encode(s)
|
376
|
+
return s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
__END__
|
382
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yandex-pdd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-03-08 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: libxml-ruby
|
16
|
-
requirement: &
|
16
|
+
requirement: &67691090 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,14 +21,14 @@ dependencies:
|
|
21
21
|
version: 1.1.3
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *67691090
|
25
25
|
description: pdd.yandex.ru api gem from https://github.com/dctabuyz/Yandex-API-PDD.ruby
|
26
26
|
email: vick.orel@gmail.com
|
27
27
|
executables: []
|
28
28
|
extensions: []
|
29
29
|
extra_rdoc_files: []
|
30
30
|
files:
|
31
|
-
- lib/yandex
|
31
|
+
- lib/yandex/pdd.rb
|
32
32
|
homepage: http://rubygems.org/gems/yandex-pdd
|
33
33
|
licenses: []
|
34
34
|
post_install_message:
|