]> git.openstreetmap.org Git - rails.git/blob - lib/potlatch.rb
Merge remote-tracking branch 'openstreetmap/pull/1347' into master
[rails.git] / lib / potlatch.rb
1 require "stringio"
2
3 # The Potlatch module provides helper functions for potlatch and its communication with the server
4 module Potlatch
5   # The AMF class is a set of helper functions for encoding and decoding AMF.
6   class AMF
7     # Return two-byte integer
8     def self.getint(s)
9       s.getbyte * 256 + s.getbyte
10     end
11
12     # Return four-byte long
13     def self.getlong(s)
14       ((s.getbyte * 256 + s.getbyte) * 256 + s.getbyte) * 256 + s.getbyte
15     end
16
17     # Return string with two-byte length
18     def self.getstring(s)
19       len = s.getbyte * 256 + s.getbyte
20       str = s.read(len)
21       str.force_encoding("UTF-8") if str.respond_to?("force_encoding")
22       str
23     end
24
25     # Return eight-byte double-precision float
26     def self.getdouble(s)
27       a = s.read(8).unpack("G") # G big-endian, E little-endian
28       a[0]
29     end
30
31     # Return numeric array
32     def self.getarray(s)
33       Array.new(getlong(s)) { getvalue(s) }
34     end
35
36     # Return object/hash
37     def self.getobject(s)
38       arr = {}
39       while (key = getstring(s))
40         break if key == ""
41         arr[key] = getvalue(s)
42       end
43       s.getbyte # skip the 9 'end of object' value
44       arr
45     end
46
47     # Parse and get value
48     def self.getvalue(s)
49       case s.getbyte
50       when 0 then getdouble(s)                  # number
51       when 1 then s.getbyte                     # boolean
52       when 2 then getstring(s)                  # string
53       when 3 then getobject(s)                  # object/hash
54       when 5 then nil                           # null
55       when 6 then nil                           # undefined
56       when 8 then s.read(4) # mixedArray
57                   getobject(s)                  #  |
58       when 10 then getarray(s)                  # array
59       end
60     end
61
62     # Envelope data into AMF writeable form
63     def self.putdata(index, n)
64       d = encodestring(index + "/onResult")
65       d += encodestring("null")
66       d += [-1].pack("N")
67       d += encodevalue(n)
68       d
69     end
70
71     # Pack variables as AMF
72     def self.encodevalue(n)
73       case n.class.to_s
74       when "Array"
75         a = 10.chr + encodelong(n.length)
76         n.each do |b|
77           a += encodevalue(b)
78         end
79         a
80       when "Hash"
81         a = 3.chr
82         n.each do |k, v|
83           a += encodestring(k.to_s) + encodevalue(v)
84         end
85         a + 0.chr + 0.chr + 9.chr
86       when "String"
87         2.chr + encodestring(n)
88       when "Bignum", "Fixnum", "Float"
89         0.chr + encodedouble(n)
90       when "NilClass"
91         5.chr
92       when "TrueClass"
93         0.chr + encodedouble(1)
94       when "FalseClass"
95         0.chr + encodedouble(0)
96       else
97         Rails.logger.error("Unexpected Ruby type for AMF conversion: " + n.class.to_s)
98       end
99     end
100
101     # Encode string with two-byte length
102     def self.encodestring(n)
103       n = n.dup.force_encoding("ASCII-8BIT") if n.respond_to?("force_encoding")
104       a, b = n.size.divmod(256)
105       a.chr + b.chr + n
106     end
107
108     # Encode number as eight-byte double precision float
109     def self.encodedouble(n)
110       [n].pack("G")
111     end
112
113     # Encode number as four-byte long
114     def self.encodelong(n)
115       [n].pack("N")
116     end
117   end
118
119   # The Dispatcher class handles decoding a series of RPC calls
120   # from the request, dispatching them, and encoding the response
121   class Dispatcher
122     def initialize(request, &_block)
123       # Get stream for request data
124       @request = StringIO.new(request + 0.chr)
125
126       # Skip version indicator and client ID
127       @request.read(2)
128
129       # Skip headers
130       AMF.getint(@request).times do     # Read number of headers and loop
131         AMF.getstring(@request)         #  | skip name
132         req.getbyte                     #  | skip boolean
133         AMF.getvalue(@request)          #  | skip value
134       end
135
136       # Capture the dispatch routine
137       @dispatch = Proc.new
138     end
139
140     def each(&_block)
141       # Read number of message bodies
142       bodies = AMF.getint(@request)
143
144       # Output response header
145       a, b = bodies.divmod(256)
146       yield 0.chr + 0.chr + 0.chr + 0.chr + a.chr + b.chr
147
148       # Process the bodies
149       bodies.times do                     # Read each body
150         name = AMF.getstring(@request)    #  | get message name
151         index = AMF.getstring(@request)   #  | get index in response sequence
152         AMF.getlong(@request)             #  | get total size in bytes
153         args = AMF.getvalue(@request)     #  | get response (probably an array)
154
155         result = @dispatch.call(name, *args)
156
157         yield AMF.putdata(index, result)
158       end
159     end
160   end
161
162   # The Potlatch class is a helper for Potlatch
163   class Potlatch
164     # ----- getpresets
165     #             in:   none
166     #             does: reads tag preset menus, colours, and autocomplete config files
167     #         out:  [0] presets, [1] presetmenus, [2] presetnames,
168     #                           [3] colours, [4] casing, [5] areas, [6] autotags
169     #                           (all hashes)
170     def self.get_presets
171       Rails.logger.info("  Message: getpresets")
172
173       # Read preset menus
174       presets = {}
175       presetmenus = { "point" => [], "way" => [], "POI" => [] }
176       presetnames = { "point" => {}, "way" => {}, "POI" => {} }
177       presettype = ""
178       presetcategory = ""
179       # StringIO.open(txt) do |file|
180       File.open(Rails.root.join("config", "potlatch", "presets.txt")) do |file|
181         file.each_line do |line|
182           t = line.chomp
183           if t =~ %r{(\w+)/(\w+)}
184             presettype = Regexp.last_match(1)
185             presetcategory = Regexp.last_match(2)
186             presetmenus[presettype].push(presetcategory)
187             presetnames[presettype][presetcategory] = ["(no preset)"]
188           elsif t =~ /^([\w\s]+):\s?(.+)$/
189             pre = Regexp.last_match(1)
190             kv = Regexp.last_match(2)
191             presetnames[presettype][presetcategory].push(pre)
192             presets[pre] = {}
193             kv.split(",").each do |a|
194               presets[pre][Regexp.last_match(1)] = Regexp.last_match(2) if a =~ /^(.+)=(.*)$/
195             end
196           end
197         end
198       end
199
200       # Read colours/styling
201       colours = {}
202       casing = {}
203       areas = {}
204       File.open(Rails.root.join("config", "potlatch", "colours.txt")) do |file|
205         file.each_line do |line|
206           next unless line.chomp =~ /(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/
207
208           tag = Regexp.last_match(1)
209           colours[tag] = Regexp.last_match(2).hex if Regexp.last_match(2) != "-"
210           casing[tag] = Regexp.last_match(3).hex if Regexp.last_match(3) != "-"
211           areas[tag] = Regexp.last_match(4).hex if Regexp.last_match(4) != "-"
212         end
213       end
214
215       # Read relations colours/styling
216       relcolours = {}
217       relalphas = {}
218       relwidths = {}
219       File.open(Rails.root.join("config", "potlatch", "relation_colours.txt")) do |file|
220         file.each_line do |line|
221           next unless line.chomp =~ /(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/
222
223           tag = Regexp.last_match(1)
224           relcolours[tag] = Regexp.last_match(2).hex if Regexp.last_match(2) != "-"
225           relalphas[tag] = Regexp.last_match(3).to_i if Regexp.last_match(3) != "-"
226           relwidths[tag] = Regexp.last_match(4).to_i if Regexp.last_match(4) != "-"
227         end
228       end
229
230       # Read POI presets
231       icon_list = []
232       icon_tags = {}
233       File.open(Rails.root.join("config", "potlatch", "icon_presets.txt")) do |file|
234         file.each_line do |line|
235           (icon, tags) = line.chomp.split("\t")
236           icon_list.push(icon)
237           icon_tags[icon] = Hash[*tags.scan(/([^;=]+)=([^;=]+)/).flatten]
238         end
239       end
240       icon_list.reverse!
241
242       # Read auto-complete
243       autotags = { "point" => {}, "way" => {}, "POI" => {} }
244       File.open(Rails.root.join("config", "potlatch", "autocomplete.txt")) do |file|
245         file.each_line do |line|
246           next unless line.chomp =~ %r{^([\w:]+)/(\w+)\s+(.+)$}
247
248           tag = Regexp.last_match(1)
249           type = Regexp.last_match(2)
250           values = Regexp.last_match(3)
251           autotags[type][tag] = if values == "-"
252                                   []
253                                 else
254                                   values.split(",").sort.reverse
255                                 end
256         end
257       end
258
259       [presets, presetmenus, presetnames, colours, casing, areas, autotags, relcolours, relalphas, relwidths, icon_list, {}, icon_tags]
260     end
261   end
262 end