/
whois.cnnic.cn.rb
235 lines (193 loc) · 6.45 KB
/
whois.cnnic.cn.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
#
# = Ruby Whois
#
# An intelligent pure Ruby WHOIS client and parser.
#
#
# Category:: Net
# Package:: Whois
# Author:: Simone Carletti <weppos@weppos.net>
# License:: MIT License
#
#--
#
#++
require 'whois/answer/parser/base'
module Whois
class Answer
class Parser
class WhoisCnnicCn < Base
include Ast
property_not_supported :disclaimer
property_supported :domain do
node('Domain Name') { |raw| raw.downcase }
end
property_not_supported :domain_id
property_supported :status do
content_for_scanner.scan(/Domain Status:\s+(.+)\n/).flatten
end
property_supported :available? do
(content_for_scanner.strip == "no matching record")
end
property_supported :registered? do
(content_for_scanner.strip == "the domain you want to register is reserved") || !available?
end
property_supported :created_on do
if content_for_scanner =~ /Registration Date:\s+(.+)\n/
Time.parse($1)
end
end
property_not_supported :updated_on
property_supported :expires_on do
if content_for_scanner =~ /Expiration Date:\s+(.+)\n/
Time.parse($1)
end
end
property_supported :registrar do
if registered?
sponsor = node("Sponsoring Registrar")
Answer::Registrar.new(
:id => sponsor,
:name => sponsor,
)
end
end
property_supported :registrant_contact do
contact("Registrant", Whois::Answer::Contact::TYPE_REGISTRANT) unless available?
end
property_supported :admin_contact do
contact("Administrative", Whois::Answer::Contact::TYPE_ADMIN) unless available?
end
property_not_supported :technical_contact
property_supported :nameservers do
content_for_scanner.scan(/Name Server:(.+)\n/).flatten.map do |name|
Answer::Nameserver.new(name.downcase)
end
end
protected
def parse
Scanner.new(content_for_scanner).parse
end
def contact(element, type)
Answer::Contact.new(
:type => type,
:name => node("#{element} Name"),
:organization => node("#{element} Organization"),
:email => node("#{element} Email")
)
end
class Scanner
def initialize(content)
@input = StringScanner.new(content)
end
def parse
@ast = {}
while !@input.eos?
trim_newline ||
parse_content
end
@ast
end
private
def parse_content
parse_disclaimer ||
parse_pair ||
parse_section ||
error("Unexpected token")
end
def trim_newline
@input.scan(/\n/)
end
def parse_pair
if @input.scan(/(.*?):(.*?)\n/)
key, value = @input[1].strip, @input[2].strip
@ast[key] = value
else
false
end
end
def parse_disclaimer
if @input.match?(/\*(.*?)\*\n/)
disclaimer = []
while @input.scan(/\*(.*?)\*\n/)
matched = @input[1].strip
disclaimer << matched if matched =~ /\w+/
end
@ast["Disclaimer"] = disclaimer.join(" ")
else
false
end
end
def parse_section
if @input.scan(/([^:]*?)\n/)
section = @input[1].strip
content = parse_section_pairs ||
parse_section_items
@input.match?(/\n+/) || error("Unexpected end of section")
@ast[section] = content
else
false
end
end
def parse_section_items
if @input.match?(/(\s+)([^:]*?)\n/)
items = []
indentation = @input[1].length
while item = parse_section_items_item(indentation)
items << item
end
items
else
false
end
end
def parse_section_items_item(indentation)
if @input.scan(/\s{#{indentation}}(.*)\n/)
@input[1]
else
false
end
end
def parse_section_pairs
contents = {}
while content = parse_section_pair
contents.merge!(content)
end
if !contents.empty?
contents
else
false
end
end
def parse_section_pair
if @input.scan(/(\s+)(.*?):(\s+)(.*?)\n/)
key = @input[2].strip
values = [@input[4].strip]
indentation = @input[1].length + @input[2].length + 1 + @input[3].length
while value = parse_section_pair_newlinevalue(indentation)
values << value
end
{ key => values.join("\n") }
else
false
end
end
def parse_section_pair_newlinevalue(indentation)
if @input.scan(/\s{#{indentation}}(.*)\n/)
@input[1]
else
false
end
end
def error(message)
if @input.eos?
raise "Unexpected end of input."
else
raise "#{message}: #{@input.peek(@input.string.length)}"
end
end
end
end
end
end
end