gruubY 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -21
  3. data/README.md +11 -6
  4. data/Rakefile +51 -51
  5. data/example/bot.rb +120 -1
  6. data/example/generate_user_session.rb +81 -81
  7. data/example/tdlib.rb +77 -77
  8. data/example/userbot.rb +133 -133
  9. data/lib/gruubY/api.rb +540 -363
  10. data/lib/gruubY/async.rb +13 -13
  11. data/lib/gruubY/bot.rb +104 -90
  12. data/lib/gruubY/bound.rb +14 -14
  13. data/lib/gruubY/client.rb +310 -296
  14. data/lib/gruubY/context.rb +296 -242
  15. data/lib/gruubY/dispatcher.rb +60 -60
  16. data/lib/gruubY/enums.rb +57 -62
  17. data/lib/gruubY/exception.rb +5 -5
  18. data/lib/gruubY/file_stream.rb +17 -17
  19. data/lib/gruubY/filters.rb +292 -292
  20. data/lib/gruubY/group_manager.rb +213 -213
  21. data/lib/gruubY/handlers.rb +225 -170
  22. data/lib/gruubY/keyboard.rb +49 -49
  23. data/lib/gruubY/media.rb +38 -38
  24. data/lib/gruubY/middleware.rb +18 -18
  25. data/lib/gruubY/plugin.rb +26 -26
  26. data/lib/gruubY/raw.rb +25 -25
  27. data/lib/gruubY/retry.rb +14 -14
  28. data/lib/gruubY/server.rb +18 -18
  29. data/lib/gruubY/session.rb +30 -30
  30. data/lib/gruubY/tdlib/client.rb +565 -565
  31. data/lib/gruubY/tdlib/client_manager.rb +37 -37
  32. data/lib/gruubY/tdlib/decorators.rb +17 -17
  33. data/lib/gruubY/tdlib/errors.rb +6 -6
  34. data/lib/gruubY/tdlib/group_manager.rb +58 -58
  35. data/lib/gruubY/tdlib/native.rb +72 -72
  36. data/lib/gruubY/tdlib/schema_builder.rb +237 -237
  37. data/lib/gruubY/tdlib/tdjson.rb +49 -49
  38. data/lib/gruubY/tdlib/user_session.rb +69 -69
  39. data/lib/gruubY/types/base_object.rb +72 -72
  40. data/lib/gruubY/types/bound_entities.rb +289 -245
  41. data/lib/gruubY/types/chat.rb +93 -93
  42. data/lib/gruubY/types/chat_info.rb +32 -31
  43. data/lib/gruubY/types/extra.rb +289 -251
  44. data/lib/gruubY/types/message.rb +301 -295
  45. data/lib/gruubY/types/message_entity.rb +14 -14
  46. data/lib/gruubY/types/registry.rb +86 -67
  47. data/lib/gruubY/types/user.rb +47 -31
  48. data/lib/gruubY/webapp.rb +22 -22
  49. data/lib/gruubY.rb +39 -41
  50. data/plugins/logger.rb +7 -7
  51. metadata +3 -9
  52. data/example/ntgcalls.rb +0 -80
  53. data/lib/gruubY/ntgcalls/native.rb +0 -100
  54. data/lib/gruubY/ntgcalls.rb +0 -478
  55. data/lib/gruubY/raw_types.rb +0 -57
  56. data/plugins/media_tools.rb +0 -13
@@ -1,237 +1,237 @@
1
- require "json"
2
- require "net/http"
3
- require "uri"
4
- require "cgi"
5
- require "stringio"
6
-
7
- module GrubY
8
- module TDLib
9
- class SchemaBuilder
10
- TL_DEF_REGEX = /^(?<name>\w+)(?:#[0-9a-f]+)?\s+(?<params>.*)=\s+(?<type>\w+);$/
11
- PARAM_DETAIL_REGEX = /(?<name>\w+):(?<type>[^\s]+)/
12
- ROW_REGEX = /<tr>\s*<td>(.*?)<\/td>\s*<td>(.*?)<\/td>\s*<td>(.*?)<\/td>\s*<td>(.*?)<\/td>\s*<\/tr>/m
13
- TAG_REGEX = /<[^>]*>/
14
-
15
- def initialize(src:, version: "", commit: "", out: "tdlib.json")
16
- @src = src
17
- @version = version
18
- @commit = commit
19
- @out = out
20
- end
21
-
22
- def call
23
- data = if @src.start_with?("http://", "https://")
24
- fetch_and_parse_tl(@src)
25
- else
26
- parse_tl_from_file(@src)
27
- end
28
-
29
- data["version"] = @version
30
- data["commit"] = @commit
31
-
32
- begin
33
- data["options"] = fetch_options
34
- rescue StandardError => e
35
- warn "Warning: failed to fetch options: #{e.message}"
36
- end
37
-
38
- save_json(data, @out)
39
- end
40
-
41
- private
42
-
43
- def parse_tl_from_file(path)
44
- File.open(path, "r:utf-8") do |file|
45
- parse_tl_from_reader(file)
46
- end
47
- end
48
-
49
- def fetch_and_parse_tl(url)
50
- uri = URI.parse(url)
51
- request = Net::HTTP::Get.new(uri)
52
- request["User-Agent"] = "gruby-tdlib-schema-builder/1.0"
53
-
54
- response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", read_timeout: 30, open_timeout: 30) do |http|
55
- http.request(request)
56
- end
57
- raise "failed to fetch #{url}: status #{response.code}" unless response.is_a?(Net::HTTPSuccess)
58
-
59
- parse_tl_from_reader(StringIO.new(response.body))
60
- end
61
-
62
- def parse_tl_from_reader(reader)
63
- data = {
64
- "name" => "Auto-generated JSON TDLib API",
65
- "version" => "",
66
- "commit" => "",
67
- "classes" => {},
68
- "types" => {},
69
- "updates" => {},
70
- "functions" => {},
71
- "options" => {}
72
- }
73
-
74
- current_description = ""
75
- current_params = {}
76
- is_functions_section = false
77
- start = false
78
-
79
- reader.each_line do |raw_line|
80
- line = raw_line.strip
81
-
82
- if line.include?("---functions---")
83
- is_functions_section = true
84
- next
85
- end
86
-
87
- if line.start_with?("//")
88
- start = true
89
- if line.start_with?("//@")
90
- parse_doc_tags(data, line.sub("//@", ""), current_params) do |description|
91
- current_description = [current_description, description].reject(&:empty?).join(" ").strip
92
- end
93
- end
94
- next
95
- end
96
-
97
- next if line.empty? || !start
98
-
99
- match = TL_DEF_REGEX.match(line)
100
- next unless match
101
-
102
- name = match[:name]
103
- params_str = match[:params]
104
- result_type = match[:type]
105
-
106
- type_def = {
107
- "description" => current_description,
108
- "args" => {},
109
- "type" => result_type
110
- }
111
-
112
- params_str.scan(PARAM_DETAIL_REGEX) do |p_name, p_type|
113
- p_desc = current_params[p_name] || ""
114
- type_def["args"][p_name] = {
115
- "description" => p_desc,
116
- "is_optional" => optional_arg?(p_desc, p_type),
117
- "type" => p_type
118
- }
119
- end
120
-
121
- if is_functions_section
122
- data["functions"][name] = type_def
123
- (data["classes"][result_type] ||= default_class)["functions"] << name
124
- elsif name.start_with?("update")
125
- data["updates"][name] = type_def
126
- (data["classes"][result_type] ||= default_class)["types"] << name
127
- else
128
- data["types"][name] = type_def
129
- (data["classes"][result_type] ||= default_class)["types"] << name
130
- end
131
-
132
- current_description = ""
133
- current_params = {}
134
- end
135
-
136
- data
137
- end
138
-
139
- def parse_doc_tags(data, tag_line, current_params)
140
- parts = (" " + tag_line).split(" @")
141
- current_class = nil
142
-
143
- parts.each do |part|
144
- part = part.strip
145
- next if part.empty?
146
-
147
- tag_name, tag_text = part.split(" ", 2)
148
- tag_text = (tag_text || "").strip
149
-
150
- case tag_name
151
- when "class"
152
- current_class = tag_text
153
- when "description"
154
- if current_class
155
- data["classes"][current_class] = {
156
- "description" => tag_text,
157
- "types" => [],
158
- "functions" => []
159
- }
160
- current_class = nil
161
- else
162
- yield(tag_text)
163
- end
164
- else
165
- clean_name = tag_name.sub(/^param_/, "")
166
- current_params[clean_name] = [current_params[clean_name], tag_text].compact.join(" ").strip
167
- end
168
- end
169
- end
170
-
171
- def default_class
172
- { "description" => "", "types" => [], "functions" => [] }
173
- end
174
-
175
- def optional_arg?(description, type)
176
- text = description.to_s.downcase
177
- text.include?("may be null") ||
178
- text.include?("pass null") ||
179
- text.include?("may be empty") ||
180
- description.to_s.include?("If non-empty,") ||
181
- type.to_s.include?("?")
182
- end
183
-
184
- def fetch_options
185
- url = URI.parse("https://core.telegram.org/tdlib/options")
186
- request = Net::HTTP::Get.new(url)
187
- request["User-Agent"] = "gruby-tdlib-schema-builder/1.0"
188
-
189
- response = Net::HTTP.start(url.host, url.port, use_ssl: true, read_timeout: 30, open_timeout: 30) do |http|
190
- http.request(request)
191
- end
192
- raise "failed to fetch options: status #{response.code}" unless response.is_a?(Net::HTTPSuccess)
193
-
194
- content = response.body
195
- marker_index = content.index("list-of-options-supported-by-tdlib")
196
- raise "could not find options list in HTML" unless marker_index
197
-
198
- options = {}
199
- content[marker_index..].scan(ROW_REGEX) do |name, type_name, writable, description|
200
- clean_name = clean_html(name)
201
- next if clean_name.empty? || clean_name.casecmp("name").zero?
202
-
203
- options[clean_name] = {
204
- "type" => map_type(clean_html(type_name)),
205
- "writable" => clean_html(writable).casecmp("yes").zero?,
206
- "description" => clean_html(description)
207
- }
208
- end
209
-
210
- raise "no options were parsed from HTML" if options.empty?
211
-
212
- options
213
- end
214
-
215
- def clean_html(text)
216
- CGI.unescapeHTML(text.gsub(TAG_REGEX, "").strip)
217
- end
218
-
219
- def map_type(type_name)
220
- case type_name
221
- when "Integer"
222
- "int64"
223
- when "Boolean"
224
- "Bool"
225
- when "String"
226
- "string"
227
- else
228
- type_name
229
- end
230
- end
231
-
232
- def save_json(data, path)
233
- File.write(path, JSON.pretty_generate(data) + "\n")
234
- end
235
- end
236
- end
237
- end
1
+ require "json"
2
+ require "net/http"
3
+ require "uri"
4
+ require "cgi"
5
+ require "stringio"
6
+
7
+ module GrubY
8
+ module TDLib
9
+ class SchemaBuilder
10
+ TL_DEF_REGEX = /^(?<name>\w+)(?:#[0-9a-f]+)?\s+(?<params>.*)=\s+(?<type>\w+);$/
11
+ PARAM_DETAIL_REGEX = /(?<name>\w+):(?<type>[^\s]+)/
12
+ ROW_REGEX = /<tr>\s*<td>(.*?)<\/td>\s*<td>(.*?)<\/td>\s*<td>(.*?)<\/td>\s*<td>(.*?)<\/td>\s*<\/tr>/m
13
+ TAG_REGEX = /<[^>]*>/
14
+
15
+ def initialize(src:, version: "", commit: "", out: "tdlib.json")
16
+ @src = src
17
+ @version = version
18
+ @commit = commit
19
+ @out = out
20
+ end
21
+
22
+ def call
23
+ data = if @src.start_with?("http://", "https://")
24
+ fetch_and_parse_tl(@src)
25
+ else
26
+ parse_tl_from_file(@src)
27
+ end
28
+
29
+ data["version"] = @version
30
+ data["commit"] = @commit
31
+
32
+ begin
33
+ data["options"] = fetch_options
34
+ rescue StandardError => e
35
+ warn "Warning: failed to fetch options: #{e.message}"
36
+ end
37
+
38
+ save_json(data, @out)
39
+ end
40
+
41
+ private
42
+
43
+ def parse_tl_from_file(path)
44
+ File.open(path, "r:utf-8") do |file|
45
+ parse_tl_from_reader(file)
46
+ end
47
+ end
48
+
49
+ def fetch_and_parse_tl(url)
50
+ uri = URI.parse(url)
51
+ request = Net::HTTP::Get.new(uri)
52
+ request["User-Agent"] = "gruby-tdlib-schema-builder/1.0"
53
+
54
+ response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", read_timeout: 30, open_timeout: 30) do |http|
55
+ http.request(request)
56
+ end
57
+ raise "failed to fetch #{url}: status #{response.code}" unless response.is_a?(Net::HTTPSuccess)
58
+
59
+ parse_tl_from_reader(StringIO.new(response.body))
60
+ end
61
+
62
+ def parse_tl_from_reader(reader)
63
+ data = {
64
+ "name" => "Auto-generated JSON TDLib API",
65
+ "version" => "",
66
+ "commit" => "",
67
+ "classes" => {},
68
+ "types" => {},
69
+ "updates" => {},
70
+ "functions" => {},
71
+ "options" => {}
72
+ }
73
+
74
+ current_description = ""
75
+ current_params = {}
76
+ is_functions_section = false
77
+ start = false
78
+
79
+ reader.each_line do |raw_line|
80
+ line = raw_line.strip
81
+
82
+ if line.include?("---functions---")
83
+ is_functions_section = true
84
+ next
85
+ end
86
+
87
+ if line.start_with?("//")
88
+ start = true
89
+ if line.start_with?("//@")
90
+ parse_doc_tags(data, line.sub("//@", ""), current_params) do |description|
91
+ current_description = [current_description, description].reject(&:empty?).join(" ").strip
92
+ end
93
+ end
94
+ next
95
+ end
96
+
97
+ next if line.empty? || !start
98
+
99
+ match = TL_DEF_REGEX.match(line)
100
+ next unless match
101
+
102
+ name = match[:name]
103
+ params_str = match[:params]
104
+ result_type = match[:type]
105
+
106
+ type_def = {
107
+ "description" => current_description,
108
+ "args" => {},
109
+ "type" => result_type
110
+ }
111
+
112
+ params_str.scan(PARAM_DETAIL_REGEX) do |p_name, p_type|
113
+ p_desc = current_params[p_name] || ""
114
+ type_def["args"][p_name] = {
115
+ "description" => p_desc,
116
+ "is_optional" => optional_arg?(p_desc, p_type),
117
+ "type" => p_type
118
+ }
119
+ end
120
+
121
+ if is_functions_section
122
+ data["functions"][name] = type_def
123
+ (data["classes"][result_type] ||= default_class)["functions"] << name
124
+ elsif name.start_with?("update")
125
+ data["updates"][name] = type_def
126
+ (data["classes"][result_type] ||= default_class)["types"] << name
127
+ else
128
+ data["types"][name] = type_def
129
+ (data["classes"][result_type] ||= default_class)["types"] << name
130
+ end
131
+
132
+ current_description = ""
133
+ current_params = {}
134
+ end
135
+
136
+ data
137
+ end
138
+
139
+ def parse_doc_tags(data, tag_line, current_params)
140
+ parts = (" " + tag_line).split(" @")
141
+ current_class = nil
142
+
143
+ parts.each do |part|
144
+ part = part.strip
145
+ next if part.empty?
146
+
147
+ tag_name, tag_text = part.split(" ", 2)
148
+ tag_text = (tag_text || "").strip
149
+
150
+ case tag_name
151
+ when "class"
152
+ current_class = tag_text
153
+ when "description"
154
+ if current_class
155
+ data["classes"][current_class] = {
156
+ "description" => tag_text,
157
+ "types" => [],
158
+ "functions" => []
159
+ }
160
+ current_class = nil
161
+ else
162
+ yield(tag_text)
163
+ end
164
+ else
165
+ clean_name = tag_name.sub(/^param_/, "")
166
+ current_params[clean_name] = [current_params[clean_name], tag_text].compact.join(" ").strip
167
+ end
168
+ end
169
+ end
170
+
171
+ def default_class
172
+ { "description" => "", "types" => [], "functions" => [] }
173
+ end
174
+
175
+ def optional_arg?(description, type)
176
+ text = description.to_s.downcase
177
+ text.include?("may be null") ||
178
+ text.include?("pass null") ||
179
+ text.include?("may be empty") ||
180
+ description.to_s.include?("If non-empty,") ||
181
+ type.to_s.include?("?")
182
+ end
183
+
184
+ def fetch_options
185
+ url = URI.parse("https://core.telegram.org/tdlib/options")
186
+ request = Net::HTTP::Get.new(url)
187
+ request["User-Agent"] = "gruby-tdlib-schema-builder/1.0"
188
+
189
+ response = Net::HTTP.start(url.host, url.port, use_ssl: true, read_timeout: 30, open_timeout: 30) do |http|
190
+ http.request(request)
191
+ end
192
+ raise "failed to fetch options: status #{response.code}" unless response.is_a?(Net::HTTPSuccess)
193
+
194
+ content = response.body
195
+ marker_index = content.index("list-of-options-supported-by-tdlib")
196
+ raise "could not find options list in HTML" unless marker_index
197
+
198
+ options = {}
199
+ content[marker_index..].scan(ROW_REGEX) do |name, type_name, writable, description|
200
+ clean_name = clean_html(name)
201
+ next if clean_name.empty? || clean_name.casecmp("name").zero?
202
+
203
+ options[clean_name] = {
204
+ "type" => map_type(clean_html(type_name)),
205
+ "writable" => clean_html(writable).casecmp("yes").zero?,
206
+ "description" => clean_html(description)
207
+ }
208
+ end
209
+
210
+ raise "no options were parsed from HTML" if options.empty?
211
+
212
+ options
213
+ end
214
+
215
+ def clean_html(text)
216
+ CGI.unescapeHTML(text.gsub(TAG_REGEX, "").strip)
217
+ end
218
+
219
+ def map_type(type_name)
220
+ case type_name
221
+ when "Integer"
222
+ "int64"
223
+ when "Boolean"
224
+ "Bool"
225
+ when "String"
226
+ "string"
227
+ else
228
+ type_name
229
+ end
230
+ end
231
+
232
+ def save_json(data, path)
233
+ File.write(path, JSON.pretty_generate(data) + "\n")
234
+ end
235
+ end
236
+ end
237
+ end
@@ -1,49 +1,49 @@
1
- require "json"
2
- require_relative "native"
3
-
4
- module GrubY
5
- module TDLib
6
- class TdJson
7
- attr_reader :version
8
-
9
- def initialize(lib_path: nil, verbosity: 1)
10
- Native.load!(lib_path)
11
- Native.td_set_log_verbosity_level(verbosity.to_i)
12
- @version = fetch_version
13
- end
14
-
15
- def create_client_id
16
- Native.td_json_client_create
17
- end
18
-
19
- def send(client_id, request)
20
- Native.td_json_client_send(client_id, JSON.generate(request))
21
- end
22
-
23
- def receive(client_id, timeout:)
24
- raw = Native.td_json_client_receive(client_id, timeout.to_f)
25
- raw ? JSON.parse(raw) : nil
26
- end
27
-
28
- def execute(client_id, request)
29
- raw = Native.td_json_client_execute(client_id, JSON.generate(request))
30
- raw ? JSON.parse(raw) : nil
31
- end
32
-
33
- def destroy(client_id)
34
- Native.td_json_client_destroy(client_id)
35
- end
36
-
37
- private
38
-
39
- def fetch_version
40
- temp = create_client_id
41
- response = execute(temp, { "@type" => "getOption", "name" => "version" })
42
- destroy(temp)
43
- response&.dig("value", "value")
44
- rescue StandardError
45
- nil
46
- end
47
- end
48
- end
49
- end
1
+ require "json"
2
+ require_relative "native"
3
+
4
+ module GrubY
5
+ module TDLib
6
+ class TdJson
7
+ attr_reader :version
8
+
9
+ def initialize(lib_path: nil, verbosity: 1)
10
+ Native.load!(lib_path)
11
+ Native.td_set_log_verbosity_level(verbosity.to_i)
12
+ @version = fetch_version
13
+ end
14
+
15
+ def create_client_id
16
+ Native.td_json_client_create
17
+ end
18
+
19
+ def send(client_id, request)
20
+ Native.td_json_client_send(client_id, JSON.generate(request))
21
+ end
22
+
23
+ def receive(client_id, timeout:)
24
+ raw = Native.td_json_client_receive(client_id, timeout.to_f)
25
+ raw ? JSON.parse(raw) : nil
26
+ end
27
+
28
+ def execute(client_id, request)
29
+ raw = Native.td_json_client_execute(client_id, JSON.generate(request))
30
+ raw ? JSON.parse(raw) : nil
31
+ end
32
+
33
+ def destroy(client_id)
34
+ Native.td_json_client_destroy(client_id)
35
+ end
36
+
37
+ private
38
+
39
+ def fetch_version
40
+ temp = create_client_id
41
+ response = execute(temp, { "@type" => "getOption", "name" => "version" })
42
+ destroy(temp)
43
+ response&.dig("value", "value")
44
+ rescue StandardError
45
+ nil
46
+ end
47
+ end
48
+ end
49
+ end