3 # The Potlatch module provides helper functions for potlatch and its communication with the server
5 # The AMF class is a set of helper functions for encoding and decoding AMF.
7 # Return two-byte integer
9 s.getbyte * 256 + s.getbyte
12 # Return four-byte long
14 ((s.getbyte * 256 + s.getbyte) * 256 + s.getbyte) * 256 + s.getbyte
17 # Return string with two-byte length
19 len = s.getbyte * 256 + s.getbyte
21 str.force_encoding("UTF-8") if str.respond_to?("force_encoding")
25 # Return eight-byte double-precision float
27 a = s.read(8).unpack("G") # G big-endian, E little-endian
31 # Return numeric array
33 Array.new(getlong(s)) { getvalue(s) }
39 while (key = getstring(s))
41 arr[key] = getvalue(s)
43 s.getbyte # skip the 9 'end of object' value
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
58 when 10 then getarray(s) # array
62 # Envelope data into AMF writeable form
63 def self.putdata(index, n)
64 d = encodestring(index + "/onResult")
65 d += encodestring("null")
71 # Pack variables as AMF
72 def self.encodevalue(n)
75 a = 10.chr + encodelong(n.length)
83 a += encodestring(k.to_s) + encodevalue(v)
85 a + 0.chr + 0.chr + 9.chr
87 2.chr + encodestring(n)
88 when Numeric, GeoRecord::Coord
89 0.chr + encodedouble(n)
93 0.chr + encodedouble(1)
95 0.chr + encodedouble(0)
97 raise "Unexpected Ruby type for AMF conversion: #{n.class.name}"
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)
108 # Encode number as eight-byte double precision float
109 def self.encodedouble(n)
113 # Encode number as four-byte long
114 def self.encodelong(n)
119 # The Dispatcher class handles decoding a series of RPC calls
120 # from the request, dispatching them, and encoding the response
122 def initialize(request, &_block)
123 # Get stream for request data
124 @request = StringIO.new(request + 0.chr)
126 # Skip version indicator and client ID
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
136 # Capture the dispatch routine
141 # Read number of message bodies
142 bodies = AMF.getint(@request)
144 # Output response header
145 a, b = bodies.divmod(256)
146 yield 0.chr + 0.chr + 0.chr + 0.chr + a.chr + b.chr
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)
155 result = @dispatch.call(name, *args)
157 yield AMF.putdata(index, result)
162 # The Potlatch class is a helper for Potlatch
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
171 Rails.logger.info(" Message: getpresets")
175 presetmenus = { "point" => [], "way" => [], "POI" => [] }
176 presetnames = { "point" => {}, "way" => {}, "POI" => {} }
179 # StringIO.open(txt) do |file|
180 File.open(Rails.root.join("config", "potlatch", "presets.txt")) do |file|
181 file.each_line do |line|
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)
193 kv.split(",").each do |a|
194 presets[pre][Regexp.last_match(1)] = Regexp.last_match(2) if a =~ /^(.+)=(.*)$/
200 # Read colours/styling
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]+)/
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) != "-"
215 # Read relations colours/styling
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]+)/
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) != "-"
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")
237 icon_tags[icon] = Hash[*tags.scan(/([^;=]+)=([^;=]+)/).flatten]
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+(.+)$}
248 tag = Regexp.last_match(1)
249 type = Regexp.last_match(2)
250 values = Regexp.last_match(3)
251 autotags[type][tag] = if values == "-"
254 values.split(",").sort.reverse
259 [presets, presetmenus, presetnames, colours, casing, areas, autotags, relcolours, relalphas, relwidths, icon_list, {}, icon_tags]